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