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/iterator.hpp" 43 #include "memory/metaspaceGCThresholdUpdater.hpp" 44 #include "memory/referenceType.hpp" 45 #include "memory/universe.hpp" 46 #include "oops/compressedOops.hpp" 47 #include "runtime/flags/jvmFlag.hpp" 48 #include "runtime/mutexLocker.hpp" 49 #include "runtime/osThread.hpp" 50 #include "runtime/safepoint.hpp" 51 #include "runtime/synchronizer.hpp" 52 #include "runtime/thread.inline.hpp" 53 #include "runtime/vmOperations.hpp" 54 55 #ifdef COMPILER2 56 #include "opto/compile.hpp" 57 #include "opto/node.hpp" 58 #endif 59 60 // Requires a ResourceMark for get_thread_name/as_utf8 61 class JfrCheckpointThreadClosure : public ThreadClosure { 62 private: 63 JfrCheckpointWriter& _writer; 64 JfrCheckpointContext _ctx; 65 const int64_t _count_position; 66 Thread* const _curthread; 67 u4 _count; 68 69 public: 70 JfrCheckpointThreadClosure(JfrCheckpointWriter& writer) : _writer(writer), 71 _ctx(writer.context()), 72 _count_position(writer.reserve(sizeof(u4))), 73 _curthread(Thread::current()), 74 _count(0) { 75 } 76 77 ~JfrCheckpointThreadClosure() { 78 if (_count == 0) { 79 // restore 80 _writer.set_context(_ctx); 81 return; 82 } 83 _writer.write_count(_count, _count_position); 84 } 85 86 void do_thread(Thread* t); 87 }; 88 89 void JfrCheckpointThreadClosure::do_thread(Thread* t) { 90 assert(t != NULL, "invariant"); 91 ++_count; 92 _writer.write_key(JfrThreadId::jfr_id(t)); 93 const char* const name = JfrThreadName::name(t); 94 assert(name != NULL, "invariant"); 95 _writer.write(name); 96 _writer.write<traceid>(JfrThreadId::os_id(t)); 97 if (t->is_Java_thread()) { 98 _writer.write(name); 99 _writer.write(JfrThreadId::id(t)); 100 _writer.write(JfrThreadGroup::thread_group_id((JavaThread*)t, _curthread)); 101 return; 102 } 103 _writer.write((const char*)NULL); // java name 104 _writer.write((traceid)0); // java thread id 105 _writer.write((traceid)0); // java thread group 106 } 107 108 void JfrThreadConstantSet::serialize(JfrCheckpointWriter& writer) { 109 JfrCheckpointThreadClosure tc(writer); 110 JfrJavaThreadIterator javathreads; 111 while (javathreads.has_next()) { 112 tc.do_thread(javathreads.next()); 113 } 114 JfrNonJavaThreadIterator nonjavathreads; 115 while (nonjavathreads.has_next()) { 116 tc.do_thread(nonjavathreads.next()); 117 } 118 } 119 120 void JfrThreadGroupConstant::serialize(JfrCheckpointWriter& writer) { 121 JfrThreadGroup::serialize(writer); 122 } 123 124 static const char* flag_value_origin_to_string(JVMFlag::Flags origin) { 125 switch (origin) { 126 case JVMFlag::DEFAULT: return "Default"; 127 case JVMFlag::COMMAND_LINE: return "Command line"; 128 case JVMFlag::ENVIRON_VAR: return "Environment variable"; 129 case JVMFlag::CONFIG_FILE: return "Config file"; 130 case JVMFlag::MANAGEMENT: return "Management"; 131 case JVMFlag::ERGONOMIC: return "Ergonomic"; 132 case JVMFlag::ATTACH_ON_DEMAND: return "Attach on demand"; 133 case JVMFlag::INTERNAL: return "Internal"; 134 case JVMFlag::JIMAGE_RESOURCE: return "JImage resource"; 135 default: ShouldNotReachHere(); return ""; 136 } 137 } 138 139 void FlagValueOriginConstant::serialize(JfrCheckpointWriter& writer) { 140 static const u4 nof_entries = JVMFlag::LAST_VALUE_ORIGIN + 1; 141 writer.write_count(nof_entries); 142 for (u4 i = 0; i < nof_entries; ++i) { 143 writer.write_key(i); 144 writer.write(flag_value_origin_to_string((JVMFlag::Flags)i)); 145 } 146 } 147 148 void MonitorInflateCauseConstant::serialize(JfrCheckpointWriter& writer) { 149 static const u4 nof_entries = ObjectSynchronizer::inflate_cause_nof; 150 writer.write_count(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::serialize(JfrCheckpointWriter& writer) { 158 static const u4 nof_entries = GCCause::_last_gc_cause; 159 writer.write_count(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::serialize(JfrCheckpointWriter& writer) { 167 static const u4 nof_entries = GCNameEndSentinel; 168 writer.write_count(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::serialize(JfrCheckpointWriter& writer) { 176 static const u4 nof_entries = GCWhen::GCWhenEndSentinel; 177 writer.write_count(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 GCThresholdUpdaterConstant::serialize(JfrCheckpointWriter& writer) { 185 static const u4 nof_entries = MetaspaceGCThresholdUpdater::Last; 186 writer.write_count(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::serialize(JfrCheckpointWriter& writer) { 194 static const u4 nof_entries = Metaspace::MetadataTypeCount; 195 writer.write_count(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 MetaspaceObjectTypeConstant::serialize(JfrCheckpointWriter& writer) { 203 static const u4 nof_entries = MetaspaceObj::_number_of_types; 204 writer.write_count(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 static const char* reference_type_to_string(ReferenceType rt) { 212 switch (rt) { 213 case REF_NONE: return "None reference"; 214 case REF_OTHER: return "Other reference"; 215 case REF_SOFT: return "Soft reference"; 216 case REF_WEAK: return "Weak reference"; 217 case REF_FINAL: return "Final reference"; 218 case REF_PHANTOM: return "Phantom reference"; 219 default: 220 ShouldNotReachHere(); 221 return NULL; 222 } 223 } 224 225 void ReferenceTypeConstant::serialize(JfrCheckpointWriter& writer) { 226 static const u4 nof_entries = REF_PHANTOM + 1; 227 writer.write_count(nof_entries); 228 for (u4 i = 0; i < nof_entries; ++i) { 229 writer.write_key(i); 230 writer.write(reference_type_to_string((ReferenceType)i)); 231 } 232 } 233 234 void NarrowOopModeConstant::serialize(JfrCheckpointWriter& writer) { 235 static const u4 nof_entries = CompressedOops::HeapBasedNarrowOop + 1; 236 writer.write_count(nof_entries); 237 for (u4 i = 0; i < nof_entries; ++i) { 238 writer.write_key(i); 239 writer.write(CompressedOops::mode_to_string((CompressedOops::Mode)i)); 240 } 241 } 242 243 void CompilerPhaseTypeConstant::serialize(JfrCheckpointWriter& writer) { 244 #ifdef COMPILER2 245 static const u4 nof_entries = PHASE_NUM_TYPES; 246 writer.write_count(nof_entries); 247 for (u4 i = 0; i < nof_entries; ++i) { 248 writer.write_key(i); 249 writer.write(CompilerPhaseTypeHelper::to_string((CompilerPhaseType)i)); 250 } 251 #endif 252 } 253 254 void CodeBlobTypeConstant::serialize(JfrCheckpointWriter& writer) { 255 static const u4 nof_entries = CodeBlobType::NumTypes; 256 writer.write_count(nof_entries); 257 for (u4 i = 0; i < nof_entries; ++i) { 258 writer.write_key(i); 259 writer.write(CodeCache::get_code_heap_name(i)); 260 } 261 }; 262 263 void VMOperationTypeConstant::serialize(JfrCheckpointWriter& writer) { 264 static const u4 nof_entries = VM_Operation::VMOp_Terminating; 265 writer.write_count(nof_entries); 266 for (u4 i = 0; i < nof_entries; ++i) { 267 writer.write_key(i); 268 writer.write(VM_Operation::name(VM_Operation::VMOp_Type(i))); 269 } 270 } 271 272 void ThreadStateConstant::serialize(JfrCheckpointWriter& writer) { 273 JfrThreadState::serialize(writer); 274 } 275 276 void JfrThreadConstant::serialize(JfrCheckpointWriter& writer) { 277 assert(_thread != NULL, "invariant"); 278 assert(_thread == Thread::current(), "invariant"); 279 writer.write_count(1); 280 writer.write_key(JfrThreadId::jfr_id(_thread)); 281 const char* const name = JfrThreadName::name(_thread); 282 writer.write(name); 283 writer.write(JfrThreadId::os_id(_thread)); 284 if (_thread->is_Java_thread()) { 285 writer.write(name); 286 writer.write(JfrThreadId::id(_thread)); 287 JavaThread* const jt = (JavaThread*)_thread; 288 const traceid thread_group_id = JfrThreadGroup::thread_group_id(jt, jt); 289 writer.write(thread_group_id); 290 JfrThreadGroup::serialize(&writer, thread_group_id); 291 return; 292 } 293 writer.write((const char*)NULL); // java name 294 writer.write((traceid)0); // java thread id 295 writer.write((traceid)0); // java thread group 296 }