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