1 /* 2 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/javaClasses.inline.hpp" 27 #include "code/codeBlob.hpp" 28 #include "code/codeCache.hpp" 29 #include "gc/shared/gcCause.hpp" 30 #include "gc/shared/gcName.hpp" 31 #include "gc/shared/gcTrace.hpp" 32 #include "gc/shared/gcWhen.hpp" 33 #include "jfr/leakprofiler/leakProfiler.hpp" 34 #include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp" 35 #include "jfr/recorder/checkpoint/types/jfrType.hpp" 36 #include "jfr/recorder/jfrRecorder.hpp" 37 #include "jfr/recorder/checkpoint/types/jfrThreadGroup.hpp" 38 #include "jfr/recorder/checkpoint/types/jfrThreadState.hpp" 39 #include "jfr/support/jfrThreadLocal.hpp" 40 #include "jfr/writers/jfrJavaEventWriter.hpp" 41 #include "jfr/utilities/jfrThreadIterator.hpp" 42 #include "memory/metaspaceGCThresholdUpdater.hpp" 43 #include "memory/referenceType.hpp" 44 #include "memory/universe.hpp" 45 #include "oops/compressedOops.hpp" 46 #include "runtime/flags/jvmFlag.hpp" 47 #include "runtime/mutexLocker.hpp" 48 #include "runtime/osThread.hpp" 49 #include "runtime/safepoint.hpp" 50 #include "runtime/synchronizer.hpp" 51 #include "runtime/thread.inline.hpp" 52 #include "runtime/vmOperations.hpp" 53 54 #ifdef COMPILER2 55 #include "opto/compile.hpp" 56 #include "opto/node.hpp" 57 #endif 58 59 // Requires a ResourceMark for get_thread_name/as_utf8 60 class JfrCheckpointThreadClosure : public ThreadClosure { 61 private: 62 JfrCheckpointWriter& _writer; 63 JfrCheckpointContext _ctx; 64 const int64_t _count_position; 65 Thread* const _curthread; 66 u4 _count; 67 68 public: 69 JfrCheckpointThreadClosure(JfrCheckpointWriter& writer) : _writer(writer), 70 _ctx(writer.context()), 71 _count_position(writer.reserve(sizeof(u4))), 72 _curthread(Thread::current()), 73 _count(0) { 74 } 75 76 ~JfrCheckpointThreadClosure() { 77 if (_count == 0) { 78 // restore 79 _writer.set_context(_ctx); 80 return; 81 } 82 _writer.write_count(_count, _count_position); 83 } 84 85 void do_thread(Thread* t); 86 }; 87 88 void JfrCheckpointThreadClosure::do_thread(Thread* t) { 89 assert(t != NULL, "invariant"); 90 ++_count; 91 _writer.write_key(JfrThreadId::jfr_id(t)); 92 const char* const name = JfrThreadName::name(t); 93 assert(name != NULL, "invariant"); 94 _writer.write(name); 95 _writer.write<traceid>(JfrThreadId::os_id(t)); 96 if (t->is_Java_thread()) { 97 _writer.write(name); 98 _writer.write(JfrThreadId::id(t)); 99 _writer.write(JfrThreadGroup::thread_group_id((JavaThread*)t, _curthread)); 100 return; 101 } 102 _writer.write((const char*)NULL); // java name 103 _writer.write((traceid)0); // java thread id 104 _writer.write((traceid)0); // java thread group 105 } 106 107 void JfrThreadConstantSet::serialize(JfrCheckpointWriter& writer) { 108 JfrCheckpointThreadClosure tc(writer); 109 JfrJavaThreadIterator javathreads; 110 while (javathreads.has_next()) { 111 tc.do_thread(javathreads.next()); 112 } 113 JfrNonJavaThreadIterator nonjavathreads; 114 while (nonjavathreads.has_next()) { 115 tc.do_thread(nonjavathreads.next()); 116 } 117 } 118 119 void JfrThreadGroupConstant::serialize(JfrCheckpointWriter& writer) { 120 JfrThreadGroup::serialize(writer); 121 } 122 123 static const char* flag_value_origin_to_string(JVMFlag::Flags origin) { 124 switch (origin) { 125 case JVMFlag::DEFAULT: return "Default"; 126 case JVMFlag::COMMAND_LINE: return "Command line"; 127 case JVMFlag::ENVIRON_VAR: return "Environment variable"; 128 case JVMFlag::CONFIG_FILE: return "Config file"; 129 case JVMFlag::MANAGEMENT: return "Management"; 130 case JVMFlag::ERGONOMIC: return "Ergonomic"; 131 case JVMFlag::ATTACH_ON_DEMAND: return "Attach on demand"; 132 case JVMFlag::INTERNAL: return "Internal"; 133 case JVMFlag::JIMAGE_RESOURCE: return "JImage resource"; 134 default: ShouldNotReachHere(); return ""; 135 } 136 } 137 138 void FlagValueOriginConstant::serialize(JfrCheckpointWriter& writer) { 139 static const u4 nof_entries = JVMFlag::LAST_VALUE_ORIGIN + 1; 140 writer.write_count(nof_entries); 141 for (u4 i = 0; i < nof_entries; ++i) { 142 writer.write_key(i); 143 writer.write(flag_value_origin_to_string((JVMFlag::Flags)i)); 144 } 145 } 146 147 void MonitorInflateCauseConstant::serialize(JfrCheckpointWriter& writer) { 148 static const u4 nof_entries = ObjectSynchronizer::inflate_cause_nof; 149 writer.write_count(nof_entries); 150 for (u4 i = 0; i < nof_entries; ++i) { 151 writer.write_key(i); 152 writer.write(ObjectSynchronizer::inflate_cause_name((ObjectSynchronizer::InflateCause)i)); 153 } 154 } 155 156 void GCCauseConstant::serialize(JfrCheckpointWriter& writer) { 157 static const u4 nof_entries = GCCause::_last_gc_cause; 158 writer.write_count(nof_entries); 159 for (u4 i = 0; i < nof_entries; ++i) { 160 writer.write_key(i); 161 writer.write(GCCause::to_string((GCCause::Cause)i)); 162 } 163 } 164 165 void GCNameConstant::serialize(JfrCheckpointWriter& writer) { 166 static const u4 nof_entries = GCNameEndSentinel; 167 writer.write_count(nof_entries); 168 for (u4 i = 0; i < nof_entries; ++i) { 169 writer.write_key(i); 170 writer.write(GCNameHelper::to_string((GCName)i)); 171 } 172 } 173 174 void GCWhenConstant::serialize(JfrCheckpointWriter& writer) { 175 static const u4 nof_entries = GCWhen::GCWhenEndSentinel; 176 writer.write_count(nof_entries); 177 for (u4 i = 0; i < nof_entries; ++i) { 178 writer.write_key(i); 179 writer.write(GCWhen::to_string((GCWhen::Type)i)); 180 } 181 } 182 183 void GCThresholdUpdaterConstant::serialize(JfrCheckpointWriter& writer) { 184 static const u4 nof_entries = MetaspaceGCThresholdUpdater::Last; 185 writer.write_count(nof_entries); 186 for (u4 i = 0; i < nof_entries; ++i) { 187 writer.write_key(i); 188 writer.write(MetaspaceGCThresholdUpdater::to_string((MetaspaceGCThresholdUpdater::Type)i)); 189 } 190 } 191 192 void MetadataTypeConstant::serialize(JfrCheckpointWriter& writer) { 193 static const u4 nof_entries = Metaspace::MetadataTypeCount; 194 writer.write_count(nof_entries); 195 for (u4 i = 0; i < nof_entries; ++i) { 196 writer.write_key(i); 197 writer.write(Metaspace::metadata_type_name((Metaspace::MetadataType)i)); 198 } 199 } 200 201 void MetaspaceObjectTypeConstant::serialize(JfrCheckpointWriter& writer) { 202 static const u4 nof_entries = MetaspaceObj::_number_of_types; 203 writer.write_count(nof_entries); 204 for (u4 i = 0; i < nof_entries; ++i) { 205 writer.write_key(i); 206 writer.write(MetaspaceObj::type_name((MetaspaceObj::Type)i)); 207 } 208 } 209 210 static const char* reference_type_to_string(ReferenceType rt) { 211 switch (rt) { 212 case REF_NONE: return "None reference"; 213 case REF_OTHER: return "Other reference"; 214 case REF_SOFT: return "Soft reference"; 215 case REF_WEAK: return "Weak reference"; 216 case REF_FINAL: return "Final reference"; 217 case REF_PHANTOM: return "Phantom reference"; 218 default: 219 ShouldNotReachHere(); 220 return NULL; 221 } 222 } 223 224 void ReferenceTypeConstant::serialize(JfrCheckpointWriter& writer) { 225 static const u4 nof_entries = REF_PHANTOM + 1; 226 writer.write_count(nof_entries); 227 for (u4 i = 0; i < nof_entries; ++i) { 228 writer.write_key(i); 229 writer.write(reference_type_to_string((ReferenceType)i)); 230 } 231 } 232 233 void NarrowOopModeConstant::serialize(JfrCheckpointWriter& writer) { 234 static const u4 nof_entries = CompressedOops::HeapBasedNarrowOop + 1; 235 writer.write_count(nof_entries); 236 for (u4 i = 0; i < nof_entries; ++i) { 237 writer.write_key(i); 238 writer.write(CompressedOops::mode_to_string((CompressedOops::Mode)i)); 239 } 240 } 241 242 void CompilerPhaseTypeConstant::serialize(JfrCheckpointWriter& writer) { 243 #ifdef COMPILER2 244 static const u4 nof_entries = PHASE_NUM_TYPES; 245 writer.write_count(nof_entries); 246 for (u4 i = 0; i < nof_entries; ++i) { 247 writer.write_key(i); 248 writer.write(CompilerPhaseTypeHelper::to_string((CompilerPhaseType)i)); 249 } 250 #endif 251 } 252 253 void CodeBlobTypeConstant::serialize(JfrCheckpointWriter& writer) { 254 static const u4 nof_entries = CodeBlobType::NumTypes; 255 writer.write_count(nof_entries); 256 for (u4 i = 0; i < nof_entries; ++i) { 257 writer.write_key(i); 258 writer.write(CodeCache::get_code_heap_name(i)); 259 } 260 }; 261 262 void VMOperationTypeConstant::serialize(JfrCheckpointWriter& writer) { 263 static const u4 nof_entries = VM_Operation::VMOp_Terminating; 264 writer.write_count(nof_entries); 265 for (u4 i = 0; i < nof_entries; ++i) { 266 writer.write_key(i); 267 writer.write(VM_Operation::name(VM_Operation::VMOp_Type(i))); 268 } 269 } 270 271 void ThreadStateConstant::serialize(JfrCheckpointWriter& writer) { 272 JfrThreadState::serialize(writer); 273 } 274 275 void JfrThreadConstant::serialize(JfrCheckpointWriter& writer) { 276 assert(_thread != NULL, "invariant"); 277 assert(_thread == Thread::current(), "invariant"); 278 writer.write_count(1); 279 writer.write_key(JfrThreadId::jfr_id(_thread)); 280 const char* const name = JfrThreadName::name(_thread); 281 writer.write(name); 282 writer.write(JfrThreadId::os_id(_thread)); 283 if (_thread->is_Java_thread()) { 284 writer.write(name); 285 writer.write(JfrThreadId::id(_thread)); 286 JavaThread* const jt = (JavaThread*)_thread; 287 const traceid thread_group_id = JfrThreadGroup::thread_group_id(jt, jt); 288 writer.write(thread_group_id); 289 JfrThreadGroup::serialize(&writer, thread_group_id); 290 return; 291 } 292 writer.write((const char*)NULL); // java name 293 writer.write((traceid)0); // java thread id 294 writer.write((traceid)0); // java thread group 295 }