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