1 /* 2 * Copyright (c) 2016, 2018, 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/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/g1/g1HeapRegionTraceType.hpp" 59 #include "gc/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 MonitorInflateCauseConstant::write_constants(JfrCheckpointWriter& writer) { 149 static const u4 nof_entries = ObjectSynchronizer::inflate_cause_nof; 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(ObjectSynchronizer::inflate_cause_name((ObjectSynchronizer::InflateCause)i)); 154 } 155 } 156 157 void GCCauseConstant::write_constants(JfrCheckpointWriter& writer) { 158 static const u4 nof_entries = GCCause::_last_gc_cause; 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(GCCause::to_string((GCCause::Cause)i)); 163 } 164 } 165 166 void GCNameConstant::write_constants(JfrCheckpointWriter& writer) { 167 static const u4 nof_entries = GCNameEndSentinel; 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(GCNameHelper::to_string((GCName)i)); 172 } 173 } 174 175 void GCWhenConstant::write_constants(JfrCheckpointWriter& writer) { 176 static const u4 nof_entries = GCWhen::GCWhenEndSentinel; 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(GCWhen::to_string((GCWhen::Type)i)); 181 } 182 } 183 184 void G1HeapRegionTypeConstant::write_constants(JfrCheckpointWriter& writer) { 185 static const u4 nof_entries = G1HeapRegionTraceType::G1HeapRegionTypeEndSentinel; 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(G1HeapRegionTraceType::to_string((G1HeapRegionTraceType::Type)i)); 190 } 191 } 192 193 void GCThresholdUpdaterConstant::write_constants(JfrCheckpointWriter& writer) { 194 static const u4 nof_entries = MetaspaceGCThresholdUpdater::Last; 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(MetaspaceGCThresholdUpdater::to_string((MetaspaceGCThresholdUpdater::Type)i)); 199 } 200 } 201 202 void MetadataTypeConstant::write_constants(JfrCheckpointWriter& writer) { 203 static const u4 nof_entries = Metaspace::MetadataTypeCount; 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(Metaspace::metadata_type_name((Metaspace::MetadataType)i)); 208 } 209 } 210 211 void MetaspaceObjTypeConstant::write_constants(JfrCheckpointWriter& writer) { 212 static const u4 nof_entries = MetaspaceObj::_number_of_types; 213 writer.write_number_of_constants(nof_entries); 214 for (u4 i = 0; i < nof_entries; ++i) { 215 writer.write_key(i); 216 writer.write(MetaspaceObj::type_name((MetaspaceObj::Type)i)); 217 } 218 } 219 220 void G1YCTypeConstant::write_constants(JfrCheckpointWriter& writer) { 221 #if INCLUDE_ALL_GCS 222 static const u4 nof_entries = G1YCTypeEndSentinel; 223 writer.write_number_of_constants(nof_entries); 224 for (u4 i = 0; i < nof_entries; ++i) { 225 writer.write_key(i); 226 writer.write(G1YCTypeHelper::to_string((G1YCType)i)); 227 } 228 #endif 229 } 230 231 static const char* reference_type_to_string(ReferenceType rt) { 232 switch (rt) { 233 case REF_NONE: return "None reference"; 234 case REF_OTHER: return "Other reference"; 235 case REF_SOFT: return "Soft reference"; 236 case REF_WEAK: return "Weak reference"; 237 case REF_FINAL: return "Final reference"; 238 case REF_PHANTOM: return "Phantom reference"; 239 default: 240 ShouldNotReachHere(); 241 return NULL; 242 } 243 } 244 245 void ReferenceTypeConstant::write_constants(JfrCheckpointWriter& writer) { 246 static const u4 nof_entries = REF_PHANTOM + 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(reference_type_to_string((ReferenceType)i)); 251 } 252 } 253 254 void NarrowOopModeConstant::write_constants(JfrCheckpointWriter& writer) { 255 static const u4 nof_entries = Universe::HeapBasedNarrowOop + 1; 256 writer.write_number_of_constants(nof_entries); 257 for (u4 i = 0; i < nof_entries; ++i) { 258 writer.write_key(i); 259 writer.write(Universe::narrow_oop_mode_to_string((Universe::NARROW_OOP_MODE)i)); 260 } 261 } 262 263 void CompilerPhaseTypeConstant::write_constants(JfrCheckpointWriter& writer) { 264 #ifdef COMPILER2 265 static const u4 nof_entries = PHASE_NUM_TYPES; 266 writer.write_number_of_constants(nof_entries); 267 for (u4 i = 0; i < nof_entries; ++i) { 268 writer.write_key(i); 269 writer.write(CompilerPhaseTypeHelper::to_string((CompilerPhaseType)i)); 270 } 271 #endif 272 } 273 274 void CodeBlobTypeConstant::write_constants(JfrCheckpointWriter& writer) { 275 static const u4 nof_entries = CodeBlobType::NumTypes; 276 writer.write_number_of_constants(nof_entries); 277 for (u4 i = 0; i < nof_entries; ++i) { 278 writer.write_key(i); 279 writer.write(CodeCache::get_code_heap_name(i)); 280 } 281 }; 282 283 void VMOperationTypeConstant::write_constants(JfrCheckpointWriter& writer) { 284 static const u4 nof_entries = VM_Operation::VMOp_Terminating; 285 writer.write_number_of_constants(nof_entries); 286 for (u4 i = 0; i < nof_entries; ++i) { 287 writer.write_key(i); 288 writer.write(VM_Operation::name(VM_Operation::VMOp_Type(i))); 289 } 290 } 291 292 class ConstantTagSet { 293 private: 294 bool _class_unload; 295 public: 296 explicit ConstantTagSet(bool class_unload) : _class_unload(class_unload) {} 297 void write(JfrCheckpointWriter& writer, JfrCheckpointWriter* leakp_writer) { 298 JfrTagSet::write(&writer, leakp_writer, _class_unload); 299 } 300 }; 301 302 void ClassUnloadConstantSet::write_constants(JfrCheckpointWriter& writer) { 303 ConstantTagSet tag_set(true); 304 if (LeakProfiler::is_running()) { 305 JfrCheckpointWriter leakp_writer(false, true, Thread::current()); 306 tag_set.write(writer, &leakp_writer); 307 ObjectSampleCheckpoint::install(leakp_writer, true, true); 308 return; 309 } 310 tag_set.write(writer, NULL); 311 }; 312 313 void ConstantSet::write_constants(JfrCheckpointWriter& writer) { 314 ConstantTagSet tag_set(false); 315 if (LeakProfiler::is_suspended()) { 316 JfrCheckpointWriter leakp_writer(false, true, Thread::current()); 317 tag_set.write(writer, &leakp_writer); 318 ObjectSampleCheckpoint::install(leakp_writer, false, true); 319 return; 320 } 321 tag_set.write(writer, NULL); 322 }; 323 324 void ThreadStateConstant::write_constants(JfrCheckpointWriter& writer) { 325 // Thread states 326 { 327 enum { 328 #define _dummy_enum(n, v) _dummy_##n, 329 MAKE_JAVATHREAD_STATES(_dummy_enum) 330 NUM_JAVATHREAD_STATES 331 }; 332 333 writer.write_number_of_constants(NUM_JAVATHREAD_STATES); 334 #define _write_ts(n, v) writer.write<u8>(v); writer.write(#n); 335 MAKE_JAVATHREAD_STATES(_write_ts) 336 } 337 } 338 339 void JfrThreadConstant::write_constants(JfrCheckpointWriter& writer) { 340 assert(_thread != NULL, "invariant"); 341 assert(_thread == Thread::current(), "invariant"); 342 assert(_thread->is_Java_thread(), "invariant"); 343 assert(!_thread->trace_data()->has_thread_checkpoint(), "invariant"); 344 ResourceMark rm(_thread); 345 const oop threadObj = _thread->threadObj(); 346 assert(threadObj != NULL, "invariant"); 347 const u8 java_lang_thread_id = java_lang_Thread::thread_id(threadObj); 348 const char* const thread_name = _thread->name(); 349 const traceid thread_group_id = JfrThreadGroup::thread_group_id(_thread); 350 writer.write_number_of_constants(1); 351 writer.write_key(_thread->trace_data()->thread_id()); 352 writer.write(thread_name); 353 writer.write((u8)_thread->osthread()->thread_id()); 354 writer.write(thread_name); 355 writer.write(java_lang_thread_id); 356 writer.write(thread_group_id); 357 JfrThreadGroup::write(&writer, thread_group_id); 358 }