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.hpp" 27 #include "code/codeBlob.hpp" 28 #include "code/codeCache.hpp" 29 #include "gc_interface/gcCause.hpp" 30 #include "gc_interface/gcName.hpp" 31 #include "gc_implementation/shared/gcTrace.hpp" 32 #include "gc_implementation/shared/gcWhen.hpp" 33 #include "jfr/recorder/access/jfrThreadData.hpp" 34 #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp" 35 #include "jfr/recorder/checkpoint/constant/jfrConstant.hpp" 36 #include "jfr/recorder/jfrRecorder.hpp" 37 #include "jfr/recorder/checkpoint/constant/jfrTagSet.hpp" 38 #include "jfr/recorder/checkpoint/constant/jfrThreadGroup.hpp" 39 #include "jfr/leakprofiler/checkpoint/objectSampleCheckpoint.hpp" 40 #include "jfr/leakprofiler/leakProfiler.hpp" 41 #include "jfr/writers/jfrJavaEventWriter.hpp" 42 #include "memory/metaspaceGCThresholdUpdater.hpp" 43 #include "memory/referenceType.hpp" 44 #include "memory/universe.hpp" 45 #include "runtime/mutexLocker.hpp" 46 #include "runtime/osThread.hpp" 47 #include "runtime/safepoint.hpp" 48 #include "runtime/synchronizer.hpp" 49 #include "runtime/thread.inline.hpp" 50 #include "runtime/vm_operations.hpp" 51 #include "trace/traceVM.hpp" 52 53 #ifdef COMPILER2 54 #include "opto/compile.hpp" 55 #include "opto/node.hpp" 56 #endif 57 #if INCLUDE_ALL_GCS 58 #include "gc_implementation/g1/g1HeapRegionTraceType.hpp" 59 #include "gc_implementation/g1/g1YCTypes.hpp" 60 #endif 61 62 // implementation for the static registration function exposed in the api 63 bool JfrConstantSerializer::register_serializer(JfrConstantTypeId id, bool require_safepoint, bool permit_cache, JfrConstantSerializer* cs) { 64 assert(cs != NULL, "invariant"); 65 return JfrCheckpointManager::register_serializer(id, require_safepoint, permit_cache, cs); 66 } 67 68 class JfrCheckpointThreadCountClosure : public ThreadClosure { 69 private: 70 u4 _total_threads; 71 public: 72 JfrCheckpointThreadCountClosure() : _total_threads(0) {} 73 u4 total_threads() { return _total_threads; } 74 void do_thread(Thread *t) { _total_threads++; } 75 }; 76 77 // Requires a ResourceMark for get_thread_name/as_utf8 78 class JfrCheckpointThreadClosure : public ThreadClosure { 79 private: 80 JfrCheckpointWriter& _writer; 81 Thread* _curthread; 82 public: 83 JfrCheckpointThreadClosure(JfrCheckpointWriter& writer) : _writer(writer), _curthread(Thread::current()) {} 84 void do_thread(Thread* t); 85 }; 86 87 // Requires a ResourceMark for get_thread_name/as_utf8 88 void JfrCheckpointThreadClosure::do_thread(Thread* t) { 89 assert(t != NULL, "invariant"); 90 assert_locked_or_safepoint(Threads_lock); 91 _writer.write_key(t->trace_data()->thread_id()); 92 _writer.write(t->name()); 93 const OSThread* const os_thread = t->osthread(); 94 _writer.write<traceid>(os_thread != NULL ? os_thread->thread_id() : (u8)0); 95 if (t->is_Java_thread()) { 96 JavaThread* const jt = (JavaThread*)t; 97 _writer.write(jt->name()); 98 _writer.write(java_lang_Thread::thread_id(jt->threadObj())); 99 _writer.write(JfrThreadGroup::thread_group_id(jt, _curthread)); 100 // since we are iterating threads during a safepoint, also issue notification 101 JfrJavaEventWriter::notify(jt); 102 return; 103 } 104 _writer.write((const char*)NULL); // java name 105 _writer.write<traceid>((traceid)0); // java thread id 106 _writer.write<traceid>((traceid)0); // java thread group 107 } 108 109 void JfrThreadConstantSet::write_constants(JfrCheckpointWriter& writer) { 110 assert(SafepointSynchronize::is_at_safepoint(), "invariant"); 111 JfrCheckpointThreadCountClosure tcc; 112 Threads::threads_do(&tcc); 113 const u4 total_threads = tcc.total_threads(); 114 // THREADS 115 writer.write_number_of_constants(total_threads); 116 JfrCheckpointThreadClosure tc(writer); 117 Threads::threads_do(&tc); 118 } 119 120 void JfrThreadGroupConstant::write_constants(JfrCheckpointWriter& writer) { 121 assert(SafepointSynchronize::is_at_safepoint(), "invariant"); 122 JfrThreadGroup::write(writer); 123 } 124 125 static const char* flag_value_origin_to_string(Flag::Flags origin) { 126 switch (origin) { 127 case Flag::DEFAULT: return "Default"; 128 case Flag::COMMAND_LINE: return "Command line"; 129 case Flag::ENVIRON_VAR: return "Environment variable"; 130 case Flag::CONFIG_FILE: return "Config file"; 131 case Flag::MANAGEMENT: return "Management"; 132 case Flag::ERGONOMIC: return "Ergonomic"; 133 case Flag::ATTACH_ON_DEMAND: return "Attach on demand"; 134 case Flag::INTERNAL: return "Internal"; 135 default: ShouldNotReachHere(); return ""; 136 } 137 } 138 139 void FlagValueOriginConstant::write_constants(JfrCheckpointWriter& writer) { 140 static const u4 nof_entries = Flag::LAST_VALUE_ORIGIN + 1; 141 writer.write_number_of_constants(nof_entries); 142 for (u4 i = 0; i < nof_entries; ++i) { 143 writer.write_key(i); 144 writer.write(flag_value_origin_to_string((Flag::Flags)i)); 145 } 146 } 147 148 void GCCauseConstant::write_constants(JfrCheckpointWriter& writer) { 149 static const u4 nof_entries = GCCause::_last_gc_cause; 150 writer.write_number_of_constants(nof_entries); 151 for (u4 i = 0; i < nof_entries; ++i) { 152 writer.write_key(i); 153 writer.write(GCCause::to_string((GCCause::Cause)i)); 154 } 155 } 156 157 void GCNameConstant::write_constants(JfrCheckpointWriter& writer) { 158 static const u4 nof_entries = GCNameEndSentinel; 159 writer.write_number_of_constants(nof_entries); 160 for (u4 i = 0; i < nof_entries; ++i) { 161 writer.write_key(i); 162 writer.write(GCNameHelper::to_string((GCName)i)); 163 } 164 } 165 166 void GCWhenConstant::write_constants(JfrCheckpointWriter& writer) { 167 static const u4 nof_entries = GCWhen::GCWhenEndSentinel; 168 writer.write_number_of_constants(nof_entries); 169 for (u4 i = 0; i < nof_entries; ++i) { 170 writer.write_key(i); 171 writer.write(GCWhen::to_string((GCWhen::Type)i)); 172 } 173 } 174 175 void G1HeapRegionTypeConstant::write_constants(JfrCheckpointWriter& writer) { 176 static const u4 nof_entries = G1HeapRegionTraceType::G1HeapRegionTypeEndSentinel; 177 writer.write_number_of_constants(nof_entries); 178 for (u4 i = 0; i < nof_entries; ++i) { 179 writer.write_key(i); 180 writer.write(G1HeapRegionTraceType::to_string((G1HeapRegionTraceType::Type)i)); 181 } 182 } 183 184 void GCThresholdUpdaterConstant::write_constants(JfrCheckpointWriter& writer) { 185 static const u4 nof_entries = MetaspaceGCThresholdUpdater::Last; 186 writer.write_number_of_constants(nof_entries); 187 for (u4 i = 0; i < nof_entries; ++i) { 188 writer.write_key(i); 189 writer.write(MetaspaceGCThresholdUpdater::to_string((MetaspaceGCThresholdUpdater::Type)i)); 190 } 191 } 192 193 void MetadataTypeConstant::write_constants(JfrCheckpointWriter& writer) { 194 static const u4 nof_entries = Metaspace::MetadataTypeCount; 195 writer.write_number_of_constants(nof_entries); 196 for (u4 i = 0; i < nof_entries; ++i) { 197 writer.write_key(i); 198 writer.write(Metaspace::metadata_type_name((Metaspace::MetadataType)i)); 199 } 200 } 201 202 void MetaspaceObjTypeConstant::write_constants(JfrCheckpointWriter& writer) { 203 static const u4 nof_entries = MetaspaceObj::_number_of_types; 204 writer.write_number_of_constants(nof_entries); 205 for (u4 i = 0; i < nof_entries; ++i) { 206 writer.write_key(i); 207 writer.write(MetaspaceObj::type_name((MetaspaceObj::Type)i)); 208 } 209 } 210 211 void G1YCTypeConstant::write_constants(JfrCheckpointWriter& writer) { 212 #if INCLUDE_ALL_GCS 213 static const u4 nof_entries = G1YCTypeEndSentinel; 214 writer.write_number_of_constants(nof_entries); 215 for (u4 i = 0; i < nof_entries; ++i) { 216 writer.write_key(i); 217 writer.write(G1YCTypeHelper::to_string((G1YCType)i)); 218 } 219 #endif 220 } 221 222 static const char* reference_type_to_string(ReferenceType rt) { 223 switch (rt) { 224 case REF_NONE: return "None reference"; 225 case REF_OTHER: return "Other reference"; 226 case REF_SOFT: return "Soft reference"; 227 case REF_WEAK: return "Weak reference"; 228 case REF_FINAL: return "Final reference"; 229 case REF_PHANTOM: return "Phantom reference"; 230 default: 231 ShouldNotReachHere(); 232 return NULL; 233 } 234 } 235 236 void ReferenceTypeConstant::write_constants(JfrCheckpointWriter& writer) { 237 static const u4 nof_entries = REF_PHANTOM + 1; 238 writer.write_number_of_constants(nof_entries); 239 for (u4 i = 0; i < nof_entries; ++i) { 240 writer.write_key(i); 241 writer.write(reference_type_to_string((ReferenceType)i)); 242 } 243 } 244 245 void NarrowOopModeConstant::write_constants(JfrCheckpointWriter& writer) { 246 static const u4 nof_entries = Universe::HeapBasedNarrowOop + 1; 247 writer.write_number_of_constants(nof_entries); 248 for (u4 i = 0; i < nof_entries; ++i) { 249 writer.write_key(i); 250 writer.write(Universe::narrow_oop_mode_to_string((Universe::NARROW_OOP_MODE)i)); 251 } 252 } 253 254 void CompilerPhaseTypeConstant::write_constants(JfrCheckpointWriter& writer) { 255 #ifdef COMPILER2 256 static const u4 nof_entries = PHASE_NUM_TYPES; 257 writer.write_number_of_constants(nof_entries); 258 for (u4 i = 0; i < nof_entries; ++i) { 259 writer.write_key(i); 260 writer.write(CompilerPhaseTypeHelper::to_string((CompilerPhaseType)i)); 261 } 262 #endif 263 } 264 265 void CodeBlobTypeConstant::write_constants(JfrCheckpointWriter& writer) { 266 // only one code blob now in ajdk8 267 static const u4 nof_entries = CodeBlobType::NumTypes; 268 writer.write_number_of_constants(nof_entries); 269 writer.write_key((u4)CodeBlobType::All); 270 writer.write("CodeCache"); 271 }; 272 273 void VMOperationTypeConstant::write_constants(JfrCheckpointWriter& writer) { 274 static const u4 nof_entries = VM_Operation::VMOp_Terminating; 275 writer.write_number_of_constants(nof_entries); 276 for (u4 i = 0; i < nof_entries; ++i) { 277 writer.write_key(i); 278 writer.write(VM_Operation::name(VM_Operation::VMOp_Type(i))); 279 } 280 } 281 282 class ConstantTagSet { 283 private: 284 bool _class_unload; 285 public: 286 explicit ConstantTagSet(bool class_unload) : _class_unload(class_unload) {} 287 void write(JfrCheckpointWriter& writer, JfrCheckpointWriter* leakp_writer) { 288 JfrTagSet::write(&writer, leakp_writer, _class_unload); 289 } 290 }; 291 292 void ClassUnloadConstantSet::write_constants(JfrCheckpointWriter& writer) { 293 ConstantTagSet tag_set(true); 294 if (LeakProfiler::is_running()) { 295 JfrCheckpointWriter leakp_writer(false, true, Thread::current()); 296 tag_set.write(writer, &leakp_writer); 297 ObjectSampleCheckpoint::install(leakp_writer, true, true); 298 return; 299 } 300 tag_set.write(writer, NULL); 301 }; 302 303 void ConstantSet::write_constants(JfrCheckpointWriter& writer) { 304 ConstantTagSet tag_set(false); 305 if (LeakProfiler::is_suspended()) { 306 JfrCheckpointWriter leakp_writer(false, true, Thread::current()); 307 tag_set.write(writer, &leakp_writer); 308 ObjectSampleCheckpoint::install(leakp_writer, false, true); 309 return; 310 } 311 tag_set.write(writer, NULL); 312 }; 313 314 void ThreadStateConstant::write_constants(JfrCheckpointWriter& writer) { 315 // Thread states 316 { 317 enum { 318 #define _dummy_enum(n, v) _dummy_##n, 319 MAKE_JAVATHREAD_STATES(_dummy_enum) 320 NUM_JAVATHREAD_STATES 321 }; 322 323 writer.write_number_of_constants(NUM_JAVATHREAD_STATES); 324 #define _write_ts(n, v) writer.write<u8>(v); writer.write(#n); 325 MAKE_JAVATHREAD_STATES(_write_ts) 326 } 327 } 328 329 void JfrThreadConstant::write_constants(JfrCheckpointWriter& writer) { 330 assert(_thread != NULL, "invariant"); 331 assert(_thread == Thread::current(), "invariant"); 332 assert(_thread->is_Java_thread(), "invariant"); 333 assert(!_thread->trace_data()->has_thread_checkpoint(), "invariant"); 334 ResourceMark rm(_thread); 335 const oop threadObj = _thread->threadObj(); 336 assert(threadObj != NULL, "invariant"); 337 const u8 java_lang_thread_id = java_lang_Thread::thread_id(threadObj); 338 const char* const thread_name = _thread->name(); 339 const traceid thread_group_id = JfrThreadGroup::thread_group_id(_thread); 340 writer.write_number_of_constants(1); 341 writer.write_key(_thread->trace_data()->thread_id()); 342 writer.write(thread_name); 343 writer.write((u8)_thread->osthread()->thread_id()); 344 writer.write(thread_name); 345 writer.write(java_lang_thread_id); 346 writer.write(thread_group_id); 347 JfrThreadGroup::write(&writer, thread_group_id); 348 }