apetushkov@9858: /* ddong@9885: * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. apetushkov@9858: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. apetushkov@9858: * apetushkov@9858: * This code is free software; you can redistribute it and/or modify it apetushkov@9858: * under the terms of the GNU General Public License version 2 only, as apetushkov@9858: * published by the Free Software Foundation. apetushkov@9858: * apetushkov@9858: * This code is distributed in the hope that it will be useful, but WITHOUT apetushkov@9858: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or apetushkov@9858: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License apetushkov@9858: * version 2 for more details (a copy is included in the LICENSE file that apetushkov@9858: * accompanied this code). apetushkov@9858: * apetushkov@9858: * You should have received a copy of the GNU General Public License version apetushkov@9858: * 2 along with this work; if not, write to the Free Software Foundation, apetushkov@9858: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. apetushkov@9858: * apetushkov@9858: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA apetushkov@9858: * or visit www.oracle.com if you need additional information or have any apetushkov@9858: * questions. apetushkov@9858: * apetushkov@9858: */ apetushkov@9858: apetushkov@9858: #include "precompiled.hpp" apetushkov@9858: #include "classfile/javaClasses.hpp" apetushkov@9858: #include "code/codeBlob.hpp" apetushkov@9858: #include "code/codeCache.hpp" apetushkov@9858: #include "gc_interface/gcCause.hpp" apetushkov@9858: #include "gc_interface/gcName.hpp" apetushkov@9858: #include "gc_implementation/shared/gcTrace.hpp" apetushkov@9858: #include "gc_implementation/shared/gcWhen.hpp" apetushkov@9858: #include "jfr/leakprofiler/checkpoint/objectSampleCheckpoint.hpp" apetushkov@9858: #include "jfr/leakprofiler/leakProfiler.hpp" apetushkov@9858: #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp" apetushkov@9858: #include "jfr/recorder/checkpoint/types/jfrType.hpp" apetushkov@9858: #include "jfr/recorder/jfrRecorder.hpp" apetushkov@9858: #include "jfr/recorder/checkpoint/types/jfrThreadGroup.hpp" apetushkov@9858: #include "jfr/recorder/checkpoint/types/jfrThreadState.hpp" apetushkov@9858: #include "jfr/recorder/checkpoint/types/jfrTypeSet.hpp" apetushkov@9858: #include "jfr/support/jfrThreadLocal.hpp" apetushkov@9858: #include "jfr/writers/jfrJavaEventWriter.hpp" apetushkov@9858: #include "memory/metaspaceGCThresholdUpdater.hpp" apetushkov@9858: #include "memory/referenceType.hpp" apetushkov@9858: #include "memory/universe.hpp" apetushkov@9858: #include "runtime/mutexLocker.hpp" apetushkov@9858: #include "runtime/osThread.hpp" apetushkov@9858: #include "runtime/safepoint.hpp" apetushkov@9858: #include "runtime/synchronizer.hpp" apetushkov@9858: #include "runtime/thread.inline.hpp" apetushkov@9858: #include "runtime/vm_operations.hpp" apetushkov@9858: apetushkov@9858: #ifdef COMPILER2 apetushkov@9858: #include "opto/compile.hpp" apetushkov@9858: #include "opto/node.hpp" apetushkov@9858: #endif apetushkov@9858: #if INCLUDE_ALL_GCS shade@9862: #include "gc_implementation/g1/g1HeapRegionTraceType.hpp" apetushkov@9858: #include "gc_implementation/g1/g1YCTypes.hpp" apetushkov@9858: #endif apetushkov@9858: apetushkov@9858: // Requires a ResourceMark for get_thread_name/as_utf8 apetushkov@9858: class JfrCheckpointThreadClosure : public ThreadClosure { apetushkov@9858: private: apetushkov@9858: JfrCheckpointWriter& _writer; apetushkov@9858: JfrCheckpointContext _ctx; bulasevich@9947: const int64_t _count_position; apetushkov@9858: Thread* const _curthread; apetushkov@9858: u4 _count; apetushkov@9858: apetushkov@9858: public: apetushkov@9858: JfrCheckpointThreadClosure(JfrCheckpointWriter& writer) : _writer(writer), apetushkov@9858: _ctx(writer.context()), apetushkov@9858: _count_position(writer.reserve(sizeof(u4))), apetushkov@9858: _curthread(Thread::current()), apetushkov@9858: _count(0) { apetushkov@9858: } apetushkov@9858: apetushkov@9858: ~JfrCheckpointThreadClosure() { apetushkov@9858: if (_count == 0) { apetushkov@9858: // restore apetushkov@9858: _writer.set_context(_ctx); apetushkov@9858: return; apetushkov@9858: } apetushkov@9858: _writer.write_count(_count, _count_position); apetushkov@9858: } apetushkov@9858: apetushkov@9858: void do_thread(Thread* t); apetushkov@9858: }; apetushkov@9858: apetushkov@9858: // Requires a ResourceMark for get_thread_name/as_utf8 apetushkov@9858: void JfrCheckpointThreadClosure::do_thread(Thread* t) { apetushkov@9858: assert(t != NULL, "invariant"); apetushkov@9858: assert_locked_or_safepoint(Threads_lock); apetushkov@9858: const JfrThreadLocal* const tl = t->jfr_thread_local(); apetushkov@9858: assert(tl != NULL, "invariant"); apetushkov@9858: if (tl->is_dead()) { apetushkov@9858: return; apetushkov@9858: } apetushkov@9858: ++_count; apetushkov@9858: _writer.write_key(tl->thread_id()); apetushkov@9858: _writer.write(t->name()); apetushkov@9858: const OSThread* const os_thread = t->osthread(); apetushkov@9858: _writer.write(os_thread != NULL ? os_thread->thread_id() : 0); apetushkov@9858: if (t->is_Java_thread()) { apetushkov@9858: JavaThread* const jt = (JavaThread*)t; apetushkov@9858: _writer.write(jt->name()); apetushkov@9858: _writer.write(java_lang_Thread::thread_id(jt->threadObj())); apetushkov@9858: _writer.write(JfrThreadGroup::thread_group_id(jt, _curthread)); apetushkov@9858: // since we are iterating threads during a safepoint, also issue notification apetushkov@9858: JfrJavaEventWriter::notify(jt); apetushkov@9858: return; apetushkov@9858: } apetushkov@9858: _writer.write((const char*)NULL); // java name apetushkov@9858: _writer.write((traceid)0); // java thread id apetushkov@9858: _writer.write((traceid)0); // java thread group apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrThreadConstantSet::serialize(JfrCheckpointWriter& writer) { apetushkov@9858: assert(SafepointSynchronize::is_at_safepoint(), "invariant"); apetushkov@9858: JfrCheckpointThreadClosure tc(writer); apetushkov@9858: Threads::threads_do(&tc); apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrThreadGroupConstant::serialize(JfrCheckpointWriter& writer) { apetushkov@9858: assert(SafepointSynchronize::is_at_safepoint(), "invariant"); apetushkov@9858: JfrThreadGroup::serialize(writer); apetushkov@9858: } apetushkov@9858: apetushkov@9858: static const char* flag_value_origin_to_string(Flag::Flags origin) { apetushkov@9858: switch (origin) { apetushkov@9858: case Flag::DEFAULT: return "Default"; apetushkov@9858: case Flag::COMMAND_LINE: return "Command line"; apetushkov@9858: case Flag::ENVIRON_VAR: return "Environment variable"; apetushkov@9858: case Flag::CONFIG_FILE: return "Config file"; apetushkov@9858: case Flag::MANAGEMENT: return "Management"; apetushkov@9858: case Flag::ERGONOMIC: return "Ergonomic"; apetushkov@9858: case Flag::ATTACH_ON_DEMAND: return "Attach on demand"; apetushkov@9858: case Flag::INTERNAL: return "Internal"; apetushkov@9858: default: ShouldNotReachHere(); return ""; apetushkov@9858: } apetushkov@9858: } apetushkov@9858: apetushkov@9858: void FlagValueOriginConstant::serialize(JfrCheckpointWriter& writer) { apetushkov@9858: static const u4 nof_entries = Flag::LAST_VALUE_ORIGIN + 1; apetushkov@9858: writer.write_count(nof_entries); apetushkov@9858: for (u4 i = 0; i < nof_entries; ++i) { apetushkov@9858: writer.write_key(i); apetushkov@9858: writer.write(flag_value_origin_to_string((Flag::Flags)i)); apetushkov@9858: } apetushkov@9858: } apetushkov@9858: apetushkov@9858: void MonitorInflateCauseConstant::serialize(JfrCheckpointWriter& writer) { apetushkov@9858: // XXX no such counters. implement? apetushkov@9858: // static const u4 nof_entries = ObjectSynchronizer::inflate_cause_nof; apetushkov@9858: // writer.write_count(nof_entries); apetushkov@9858: // for (u4 i = 0; i < nof_entries; ++i) { apetushkov@9858: // writer.write_key(i); apetushkov@9858: // writer.write(ObjectSynchronizer::inflate_cause_name((ObjectSynchronizer::InflateCause)i)); apetushkov@9858: // } apetushkov@9858: } apetushkov@9858: apetushkov@9858: void GCCauseConstant::serialize(JfrCheckpointWriter& writer) { apetushkov@9858: static const u4 nof_entries = GCCause::_last_gc_cause; apetushkov@9858: writer.write_count(nof_entries); apetushkov@9858: for (u4 i = 0; i < nof_entries; ++i) { apetushkov@9858: writer.write_key(i); apetushkov@9858: writer.write(GCCause::to_string((GCCause::Cause)i)); apetushkov@9858: } apetushkov@9858: } apetushkov@9858: apetushkov@9858: void GCNameConstant::serialize(JfrCheckpointWriter& writer) { apetushkov@9858: static const u4 nof_entries = GCNameEndSentinel; apetushkov@9858: writer.write_count(nof_entries); apetushkov@9858: for (u4 i = 0; i < nof_entries; ++i) { apetushkov@9858: writer.write_key(i); apetushkov@9858: writer.write(GCNameHelper::to_string((GCName)i)); apetushkov@9858: } apetushkov@9858: } apetushkov@9858: apetushkov@9858: void GCWhenConstant::serialize(JfrCheckpointWriter& writer) { apetushkov@9858: static const u4 nof_entries = GCWhen::GCWhenEndSentinel; apetushkov@9858: writer.write_count(nof_entries); apetushkov@9858: for (u4 i = 0; i < nof_entries; ++i) { apetushkov@9858: writer.write_key(i); apetushkov@9858: writer.write(GCWhen::to_string((GCWhen::Type)i)); apetushkov@9858: } apetushkov@9858: } apetushkov@9858: apetushkov@9858: void G1HeapRegionTypeConstant::serialize(JfrCheckpointWriter& writer) { neugens@9861: static const u4 nof_entries = G1HeapRegionTraceType::G1HeapRegionTypeEndSentinel; neugens@9861: writer.write_count(nof_entries); neugens@9861: for (u4 i = 0; i < nof_entries; ++i) { neugens@9861: writer.write_key(i); neugens@9861: writer.write(G1HeapRegionTraceType::to_string((G1HeapRegionTraceType::Type)i)); neugens@9861: } apetushkov@9858: } apetushkov@9858: apetushkov@9858: void GCThresholdUpdaterConstant::serialize(JfrCheckpointWriter& writer) { apetushkov@9858: static const u4 nof_entries = MetaspaceGCThresholdUpdater::Last; apetushkov@9858: writer.write_count(nof_entries); apetushkov@9858: for (u4 i = 0; i < nof_entries; ++i) { apetushkov@9858: writer.write_key(i); apetushkov@9858: writer.write(MetaspaceGCThresholdUpdater::to_string((MetaspaceGCThresholdUpdater::Type)i)); apetushkov@9858: } apetushkov@9858: } apetushkov@9858: apetushkov@9858: void MetadataTypeConstant::serialize(JfrCheckpointWriter& writer) { apetushkov@9858: static const u4 nof_entries = Metaspace::MetadataTypeCount; apetushkov@9858: writer.write_count(nof_entries); apetushkov@9858: for (u4 i = 0; i < nof_entries; ++i) { apetushkov@9858: writer.write_key(i); apetushkov@9858: writer.write(Metaspace::metadata_type_name((Metaspace::MetadataType)i)); apetushkov@9858: } apetushkov@9858: } apetushkov@9858: apetushkov@9858: void MetaspaceObjectTypeConstant::serialize(JfrCheckpointWriter& writer) { apetushkov@9858: static const u4 nof_entries = MetaspaceObj::_number_of_types; apetushkov@9858: writer.write_count(nof_entries); apetushkov@9858: for (u4 i = 0; i < nof_entries; ++i) { apetushkov@9858: writer.write_key(i); apetushkov@9858: writer.write(MetaspaceObj::type_name((MetaspaceObj::Type)i)); apetushkov@9858: } apetushkov@9858: } apetushkov@9858: apetushkov@9858: void G1YCTypeConstant::serialize(JfrCheckpointWriter& writer) { apetushkov@9858: #if INCLUDE_ALL_GCS apetushkov@9858: static const u4 nof_entries = G1YCTypeEndSentinel; apetushkov@9858: writer.write_count(nof_entries); apetushkov@9858: for (u4 i = 0; i < nof_entries; ++i) { apetushkov@9858: writer.write_key(i); apetushkov@9858: writer.write(G1YCTypeHelper::to_string((G1YCType)i)); apetushkov@9858: } apetushkov@9858: #endif apetushkov@9858: } apetushkov@9858: apetushkov@9858: static const char* reference_type_to_string(ReferenceType rt) { apetushkov@9858: switch (rt) { apetushkov@9858: case REF_NONE: return "None reference"; apetushkov@9858: case REF_OTHER: return "Other reference"; apetushkov@9858: case REF_SOFT: return "Soft reference"; apetushkov@9858: case REF_WEAK: return "Weak reference"; apetushkov@9858: case REF_FINAL: return "Final reference"; apetushkov@9858: case REF_PHANTOM: return "Phantom reference"; apetushkov@9858: default: apetushkov@9858: ShouldNotReachHere(); apetushkov@9858: return NULL; apetushkov@9858: } apetushkov@9858: } apetushkov@9858: apetushkov@9858: void ReferenceTypeConstant::serialize(JfrCheckpointWriter& writer) { apetushkov@9858: static const u4 nof_entries = REF_PHANTOM + 1; apetushkov@9858: writer.write_count(nof_entries); apetushkov@9858: for (u4 i = 0; i < nof_entries; ++i) { apetushkov@9858: writer.write_key(i); apetushkov@9858: writer.write(reference_type_to_string((ReferenceType)i)); apetushkov@9858: } apetushkov@9858: } apetushkov@9858: apetushkov@9858: void NarrowOopModeConstant::serialize(JfrCheckpointWriter& writer) { apetushkov@9858: static const u4 nof_entries = Universe::HeapBasedNarrowOop + 1; apetushkov@9858: writer.write_count(nof_entries); apetushkov@9858: for (u4 i = 0; i < nof_entries; ++i) { apetushkov@9858: writer.write_key(i); apetushkov@9858: writer.write(Universe::narrow_oop_mode_to_string((Universe::NARROW_OOP_MODE)i)); apetushkov@9858: } apetushkov@9858: } apetushkov@9858: apetushkov@9858: void CompilerPhaseTypeConstant::serialize(JfrCheckpointWriter& writer) { apetushkov@9858: #ifdef COMPILER2 apetushkov@9858: static const u4 nof_entries = PHASE_NUM_TYPES; apetushkov@9858: writer.write_count(nof_entries); apetushkov@9858: for (u4 i = 0; i < nof_entries; ++i) { apetushkov@9858: writer.write_key(i); apetushkov@9858: writer.write(CompilerPhaseTypeHelper::to_string((CompilerPhaseType)i)); apetushkov@9858: } apetushkov@9858: #endif apetushkov@9858: } apetushkov@9858: apetushkov@9858: void CodeBlobTypeConstant::serialize(JfrCheckpointWriter& writer) { neugens@9861: static const u4 nof_entries = CodeBlobType::NumTypes; neugens@9861: writer.write_count(nof_entries); neugens@9861: writer.write_key((u4)CodeBlobType::All); neugens@9861: writer.write("CodeCache"); apetushkov@9858: }; apetushkov@9858: apetushkov@9858: void VMOperationTypeConstant::serialize(JfrCheckpointWriter& writer) { apetushkov@9858: static const u4 nof_entries = VM_Operation::VMOp_Terminating; apetushkov@9858: writer.write_count(nof_entries); apetushkov@9858: for (u4 i = 0; i < nof_entries; ++i) { apetushkov@9858: writer.write_key(i); apetushkov@9858: writer.write(VM_Operation::name(VM_Operation::VMOp_Type(i))); apetushkov@9858: } apetushkov@9858: } apetushkov@9858: apetushkov@9858: class TypeSetSerialization { apetushkov@9858: private: apetushkov@9858: bool _class_unload; apetushkov@9858: public: apetushkov@9858: explicit TypeSetSerialization(bool class_unload) : _class_unload(class_unload) {} apetushkov@9858: void write(JfrCheckpointWriter& writer, JfrCheckpointWriter* leakp_writer) { apetushkov@9858: JfrTypeSet::serialize(&writer, leakp_writer, _class_unload); apetushkov@9858: } apetushkov@9858: }; apetushkov@9858: apetushkov@9858: void ClassUnloadTypeSet::serialize(JfrCheckpointWriter& writer) { apetushkov@9858: TypeSetSerialization type_set(true); apetushkov@9858: if (LeakProfiler::is_running()) { apetushkov@9858: JfrCheckpointWriter leakp_writer(false, true, Thread::current()); apetushkov@9858: type_set.write(writer, &leakp_writer); apetushkov@9858: ObjectSampleCheckpoint::install(leakp_writer, true, true); apetushkov@9858: return; apetushkov@9858: } apetushkov@9858: type_set.write(writer, NULL); apetushkov@9858: }; apetushkov@9858: apetushkov@9858: void TypeSet::serialize(JfrCheckpointWriter& writer) { apetushkov@9858: TypeSetSerialization type_set(false); ddong@9885: if (LeakProfiler::is_running()) { apetushkov@9858: JfrCheckpointWriter leakp_writer(false, true, Thread::current()); apetushkov@9858: type_set.write(writer, &leakp_writer); apetushkov@9858: ObjectSampleCheckpoint::install(leakp_writer, false, true); apetushkov@9858: return; apetushkov@9858: } apetushkov@9858: type_set.write(writer, NULL); apetushkov@9858: }; apetushkov@9858: apetushkov@9858: void ThreadStateConstant::serialize(JfrCheckpointWriter& writer) { apetushkov@9858: JfrThreadState::serialize(writer); apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrThreadConstant::serialize(JfrCheckpointWriter& writer) { apetushkov@9858: assert(_thread != NULL, "invariant"); apetushkov@9858: assert(_thread == Thread::current(), "invariant"); apetushkov@9858: assert(_thread->is_Java_thread(), "invariant"); apetushkov@9858: assert(!_thread->jfr_thread_local()->has_thread_checkpoint(), "invariant"); apetushkov@9858: ResourceMark rm(_thread); apetushkov@9858: const oop threadObj = _thread->threadObj(); apetushkov@9858: assert(threadObj != NULL, "invariant"); apetushkov@9858: const u8 java_lang_thread_id = java_lang_Thread::thread_id(threadObj); apetushkov@9858: const char* const thread_name = _thread->name(); apetushkov@9858: const traceid thread_group_id = JfrThreadGroup::thread_group_id(_thread); apetushkov@9858: writer.write_count(1); apetushkov@9858: writer.write_key(_thread->jfr_thread_local()->thread_id()); apetushkov@9858: writer.write(thread_name); apetushkov@9858: writer.write((traceid)_thread->osthread()->thread_id()); apetushkov@9858: writer.write(thread_name); apetushkov@9858: writer.write(java_lang_thread_id); apetushkov@9858: writer.write(thread_group_id); apetushkov@9858: JfrThreadGroup::serialize(&writer, thread_group_id); apetushkov@9858: }