/* * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ #include "precompiled.hpp" #include "classfile/javaClasses.hpp" #include "code/codeBlob.hpp" #include "code/codeCache.hpp" #include "gc_interface/gcCause.hpp" #include "gc_interface/gcName.hpp" #include "gc_implementation/shared/gcTrace.hpp" #include "gc_implementation/shared/gcWhen.hpp" #include "jfr/recorder/access/jfrThreadData.hpp" #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp" #include "jfr/recorder/checkpoint/constant/jfrConstant.hpp" #include "jfr/recorder/jfrRecorder.hpp" #include "jfr/recorder/checkpoint/constant/jfrTagSet.hpp" #include "jfr/recorder/checkpoint/constant/jfrThreadGroup.hpp" #include "jfr/leakprofiler/checkpoint/objectSampleCheckpoint.hpp" #include "jfr/leakprofiler/leakProfiler.hpp" #include "jfr/writers/jfrJavaEventWriter.hpp" #include "memory/metaspaceGCThresholdUpdater.hpp" #include "memory/referenceType.hpp" #include "memory/universe.hpp" #include "runtime/mutexLocker.hpp" #include "runtime/osThread.hpp" #include "runtime/safepoint.hpp" #include "runtime/synchronizer.hpp" #include "runtime/thread.inline.hpp" #include "runtime/vm_operations.hpp" #include "trace/traceVM.hpp" #ifdef COMPILER2 #include "opto/compile.hpp" #include "opto/node.hpp" #endif #if INCLUDE_ALL_GCS #include "gc_implementation/g1/g1HeapRegionTraceType.hpp" #include "gc_implementation/g1/g1YCTypes.hpp" #endif // implementation for the static registration function exposed in the api bool JfrConstantSerializer::register_serializer(JfrConstantTypeId id, bool require_safepoint, bool permit_cache, JfrConstantSerializer* cs) { assert(cs != NULL, "invariant"); return JfrCheckpointManager::register_serializer(id, require_safepoint, permit_cache, cs); } class JfrCheckpointThreadCountClosure : public ThreadClosure { private: u4 _total_threads; public: JfrCheckpointThreadCountClosure() : _total_threads(0) {} u4 total_threads() { return _total_threads; } void do_thread(Thread *t) { _total_threads++; } }; // Requires a ResourceMark for get_thread_name/as_utf8 class JfrCheckpointThreadClosure : public ThreadClosure { private: JfrCheckpointWriter& _writer; Thread* _curthread; public: JfrCheckpointThreadClosure(JfrCheckpointWriter& writer) : _writer(writer), _curthread(Thread::current()) {} void do_thread(Thread* t); }; // Requires a ResourceMark for get_thread_name/as_utf8 void JfrCheckpointThreadClosure::do_thread(Thread* t) { assert(t != NULL, "invariant"); assert_locked_or_safepoint(Threads_lock); _writer.write_key(t->trace_data()->thread_id()); _writer.write(t->name()); const OSThread* const os_thread = t->osthread(); _writer.write(os_thread != NULL ? os_thread->thread_id() : (u8)0); if (t->is_Java_thread()) { JavaThread* const jt = (JavaThread*)t; _writer.write(jt->name()); _writer.write(java_lang_Thread::thread_id(jt->threadObj())); _writer.write(JfrThreadGroup::thread_group_id(jt, _curthread)); // since we are iterating threads during a safepoint, also issue notification JfrJavaEventWriter::notify(jt); return; } _writer.write((const char*)NULL); // java name _writer.write((traceid)0); // java thread id _writer.write((traceid)0); // java thread group } void JfrThreadConstantSet::write_constants(JfrCheckpointWriter& writer) { assert(SafepointSynchronize::is_at_safepoint(), "invariant"); JfrCheckpointThreadCountClosure tcc; Threads::threads_do(&tcc); const u4 total_threads = tcc.total_threads(); // THREADS writer.write_number_of_constants(total_threads); JfrCheckpointThreadClosure tc(writer); Threads::threads_do(&tc); } void JfrThreadGroupConstant::write_constants(JfrCheckpointWriter& writer) { assert(SafepointSynchronize::is_at_safepoint(), "invariant"); JfrThreadGroup::write(writer); } static const char* flag_value_origin_to_string(Flag::Flags origin) { switch (origin) { case Flag::DEFAULT: return "Default"; case Flag::COMMAND_LINE: return "Command line"; case Flag::ENVIRON_VAR: return "Environment variable"; case Flag::CONFIG_FILE: return "Config file"; case Flag::MANAGEMENT: return "Management"; case Flag::ERGONOMIC: return "Ergonomic"; case Flag::ATTACH_ON_DEMAND: return "Attach on demand"; case Flag::INTERNAL: return "Internal"; default: ShouldNotReachHere(); return ""; } } void FlagValueOriginConstant::write_constants(JfrCheckpointWriter& writer) { static const u4 nof_entries = Flag::LAST_VALUE_ORIGIN + 1; writer.write_number_of_constants(nof_entries); for (u4 i = 0; i < nof_entries; ++i) { writer.write_key(i); writer.write(flag_value_origin_to_string((Flag::Flags)i)); } } void GCCauseConstant::write_constants(JfrCheckpointWriter& writer) { static const u4 nof_entries = GCCause::_last_gc_cause; writer.write_number_of_constants(nof_entries); for (u4 i = 0; i < nof_entries; ++i) { writer.write_key(i); writer.write(GCCause::to_string((GCCause::Cause)i)); } } void GCNameConstant::write_constants(JfrCheckpointWriter& writer) { static const u4 nof_entries = GCNameEndSentinel; writer.write_number_of_constants(nof_entries); for (u4 i = 0; i < nof_entries; ++i) { writer.write_key(i); writer.write(GCNameHelper::to_string((GCName)i)); } } void GCWhenConstant::write_constants(JfrCheckpointWriter& writer) { static const u4 nof_entries = GCWhen::GCWhenEndSentinel; writer.write_number_of_constants(nof_entries); for (u4 i = 0; i < nof_entries; ++i) { writer.write_key(i); writer.write(GCWhen::to_string((GCWhen::Type)i)); } } void G1HeapRegionTypeConstant::write_constants(JfrCheckpointWriter& writer) { static const u4 nof_entries = G1HeapRegionTraceType::G1HeapRegionTypeEndSentinel; writer.write_number_of_constants(nof_entries); for (u4 i = 0; i < nof_entries; ++i) { writer.write_key(i); writer.write(G1HeapRegionTraceType::to_string((G1HeapRegionTraceType::Type)i)); } } void GCThresholdUpdaterConstant::write_constants(JfrCheckpointWriter& writer) { static const u4 nof_entries = MetaspaceGCThresholdUpdater::Last; writer.write_number_of_constants(nof_entries); for (u4 i = 0; i < nof_entries; ++i) { writer.write_key(i); writer.write(MetaspaceGCThresholdUpdater::to_string((MetaspaceGCThresholdUpdater::Type)i)); } } void MetadataTypeConstant::write_constants(JfrCheckpointWriter& writer) { static const u4 nof_entries = Metaspace::MetadataTypeCount; writer.write_number_of_constants(nof_entries); for (u4 i = 0; i < nof_entries; ++i) { writer.write_key(i); writer.write(Metaspace::metadata_type_name((Metaspace::MetadataType)i)); } } void MetaspaceObjTypeConstant::write_constants(JfrCheckpointWriter& writer) { static const u4 nof_entries = MetaspaceObj::_number_of_types; writer.write_number_of_constants(nof_entries); for (u4 i = 0; i < nof_entries; ++i) { writer.write_key(i); writer.write(MetaspaceObj::type_name((MetaspaceObj::Type)i)); } } void G1YCTypeConstant::write_constants(JfrCheckpointWriter& writer) { #if INCLUDE_ALL_GCS static const u4 nof_entries = G1YCTypeEndSentinel; writer.write_number_of_constants(nof_entries); for (u4 i = 0; i < nof_entries; ++i) { writer.write_key(i); writer.write(G1YCTypeHelper::to_string((G1YCType)i)); } #endif } static const char* reference_type_to_string(ReferenceType rt) { switch (rt) { case REF_NONE: return "None reference"; case REF_OTHER: return "Other reference"; case REF_SOFT: return "Soft reference"; case REF_WEAK: return "Weak reference"; case REF_FINAL: return "Final reference"; case REF_PHANTOM: return "Phantom reference"; default: ShouldNotReachHere(); return NULL; } } void ReferenceTypeConstant::write_constants(JfrCheckpointWriter& writer) { static const u4 nof_entries = REF_PHANTOM + 1; writer.write_number_of_constants(nof_entries); for (u4 i = 0; i < nof_entries; ++i) { writer.write_key(i); writer.write(reference_type_to_string((ReferenceType)i)); } } void NarrowOopModeConstant::write_constants(JfrCheckpointWriter& writer) { static const u4 nof_entries = Universe::HeapBasedNarrowOop + 1; writer.write_number_of_constants(nof_entries); for (u4 i = 0; i < nof_entries; ++i) { writer.write_key(i); writer.write(Universe::narrow_oop_mode_to_string((Universe::NARROW_OOP_MODE)i)); } } void CompilerPhaseTypeConstant::write_constants(JfrCheckpointWriter& writer) { #ifdef COMPILER2 static const u4 nof_entries = PHASE_NUM_TYPES; writer.write_number_of_constants(nof_entries); for (u4 i = 0; i < nof_entries; ++i) { writer.write_key(i); writer.write(CompilerPhaseTypeHelper::to_string((CompilerPhaseType)i)); } #endif } void CodeBlobTypeConstant::write_constants(JfrCheckpointWriter& writer) { // only one code blob now in ajdk8 static const u4 nof_entries = CodeBlobType::NumTypes; writer.write_number_of_constants(nof_entries); writer.write_key((u4)CodeBlobType::All); writer.write("CodeCache"); }; void VMOperationTypeConstant::write_constants(JfrCheckpointWriter& writer) { static const u4 nof_entries = VM_Operation::VMOp_Terminating; writer.write_number_of_constants(nof_entries); for (u4 i = 0; i < nof_entries; ++i) { writer.write_key(i); writer.write(VM_Operation::name(VM_Operation::VMOp_Type(i))); } } class ConstantTagSet { private: bool _class_unload; public: explicit ConstantTagSet(bool class_unload) : _class_unload(class_unload) {} void write(JfrCheckpointWriter& writer, JfrCheckpointWriter* leakp_writer) { JfrTagSet::write(&writer, leakp_writer, _class_unload); } }; void ClassUnloadConstantSet::write_constants(JfrCheckpointWriter& writer) { ConstantTagSet tag_set(true); if (LeakProfiler::is_running()) { JfrCheckpointWriter leakp_writer(false, true, Thread::current()); tag_set.write(writer, &leakp_writer); ObjectSampleCheckpoint::install(leakp_writer, true, true); return; } tag_set.write(writer, NULL); }; void ConstantSet::write_constants(JfrCheckpointWriter& writer) { ConstantTagSet tag_set(false); if (LeakProfiler::is_suspended()) { JfrCheckpointWriter leakp_writer(false, true, Thread::current()); tag_set.write(writer, &leakp_writer); ObjectSampleCheckpoint::install(leakp_writer, false, true); return; } tag_set.write(writer, NULL); }; void ThreadStateConstant::write_constants(JfrCheckpointWriter& writer) { // Thread states { enum { #define _dummy_enum(n, v) _dummy_##n, MAKE_JAVATHREAD_STATES(_dummy_enum) NUM_JAVATHREAD_STATES }; writer.write_number_of_constants(NUM_JAVATHREAD_STATES); #define _write_ts(n, v) writer.write(v); writer.write(#n); MAKE_JAVATHREAD_STATES(_write_ts) } } void JfrThreadConstant::write_constants(JfrCheckpointWriter& writer) { assert(_thread != NULL, "invariant"); assert(_thread == Thread::current(), "invariant"); assert(_thread->is_Java_thread(), "invariant"); assert(!_thread->trace_data()->has_thread_checkpoint(), "invariant"); ResourceMark rm(_thread); const oop threadObj = _thread->threadObj(); assert(threadObj != NULL, "invariant"); const u8 java_lang_thread_id = java_lang_Thread::thread_id(threadObj); const char* const thread_name = _thread->name(); const traceid thread_group_id = JfrThreadGroup::thread_group_id(_thread); writer.write_number_of_constants(1); writer.write_key(_thread->trace_data()->thread_id()); writer.write(thread_name); writer.write((u8)_thread->osthread()->thread_id()); writer.write(thread_name); writer.write(java_lang_thread_id); writer.write(thread_group_id); JfrThreadGroup::write(&writer, thread_group_id); }