From d504f84316e831bb27babf84d9478dace58212af Mon Sep 17 00:00:00 2001 From: Denghui Dong Date: Thu, 10 Sep 2020 15:49:20 +0800 Subject: [PATCH] [Backport] 8237499: JFR: Include stack trace in the ThreadStart event Summary: Test Plan: jdk/test/jdk/jfr/event/runtime/TestThreadStartEndEvents.java Reviewed-by: zhengxiaolinX, yuleil Issue: https://github.com/alibaba/dragonwell8/issues/127 --- src/share/vm/jfr/metadata/metadata.xml | 5 +++-- src/share/vm/jfr/support/jfrThreadLocal.cpp | 10 +++++++++- src/share/vm/jfr/support/jfrThreadLocal.hpp | 5 +++++ src/share/vm/prims/jni.cpp | 9 ++++++++- src/share/vm/prims/jvm.cpp | 12 +++++++++++- 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/share/vm/jfr/metadata/metadata.xml b/src/share/vm/jfr/metadata/metadata.xml index 359b095b8..7ae07951b 100644 --- a/src/share/vm/jfr/metadata/metadata.xml +++ b/src/share/vm/jfr/metadata/metadata.xml @@ -26,8 +26,9 @@ - - + + + diff --git a/src/share/vm/jfr/support/jfrThreadLocal.cpp b/src/share/vm/jfr/support/jfrThreadLocal.cpp index 8d6ea9557..bc26529c6 100644 --- a/src/share/vm/jfr/support/jfrThreadLocal.cpp +++ b/src/share/vm/jfr/support/jfrThreadLocal.cpp @@ -60,7 +60,11 @@ JfrThreadLocal::JfrThreadLocal() : _cached_top_frame_bci(max_jint), _alloc_count(0), _alloc_count_until_sample(1), - _cached_event_id(MaxJfrEventId) {} + _cached_event_id(MaxJfrEventId) { + + Thread* thread = ThreadLocalStorage::thread(); + _parent_trace_id = thread != NULL ? thread->jfr_thread_local()->trace_id() : (traceid)0; +} u8 JfrThreadLocal::add_data_lost(u8 value) { _data_lost += value; @@ -83,6 +87,7 @@ const JfrBlobHandle& JfrThreadLocal::thread_blob() const { static void send_java_thread_start_event(JavaThread* jt) { EventThreadStart event; event.set_thread(jt->jfr_thread_local()->thread_id()); + event.set_parentThread(jt->jfr_thread_local()->parent_thread_id()); event.commit(); } @@ -98,6 +103,9 @@ void JfrThreadLocal::on_start(Thread* t) { } } } + if (t->jfr_thread_local()->has_cached_stack_trace()) { + t->jfr_thread_local()->clear_cached_stack_trace(); + } } static void send_java_thread_end_events(traceid id, JavaThread* jt) { diff --git a/src/share/vm/jfr/support/jfrThreadLocal.hpp b/src/share/vm/jfr/support/jfrThreadLocal.hpp index 3c5292805..3662a30f9 100644 --- a/src/share/vm/jfr/support/jfrThreadLocal.hpp +++ b/src/share/vm/jfr/support/jfrThreadLocal.hpp @@ -52,6 +52,7 @@ class JfrThreadLocal { volatile jint _entering_suspend_flag; bool _excluded; bool _dead; + traceid _parent_trace_id; // Jfr callstack collection relies on vframeStream. // But the bci of top frame can not be determined by vframeStream in some scenarios. // For example, in the opto CallLeafNode runtime call of @@ -149,6 +150,10 @@ class JfrThreadLocal { _trace_id = thread_id; } + traceid parent_thread_id() const { + return _parent_trace_id; + } + void set_cached_stack_trace_id(traceid id, unsigned int hash = 0) { _stack_trace_id = id; _stack_trace_hash = hash; diff --git a/src/share/vm/prims/jni.cpp b/src/share/vm/prims/jni.cpp index a4a387dcd..221fe4a4c 100644 --- a/src/share/vm/prims/jni.cpp +++ b/src/share/vm/prims/jni.cpp @@ -5025,7 +5025,14 @@ static void post_thread_start_event(const JavaThread* jt) { EventThreadStart event; if (event.should_commit()) { event.set_thread(JFR_THREAD_ID(jt)); - event.commit(); + event.set_parentThread((traceid)0); + if (EventThreadStart::is_stacktrace_enabled()) { + jt->jfr_thread_local()->set_cached_stack_trace_id((traceid)0); + event.commit(); + jt->jfr_thread_local()->clear_cached_stack_trace(); + } else { + event.commit(); + } } } diff --git a/src/share/vm/prims/jvm.cpp b/src/share/vm/prims/jvm.cpp index ffdaf4be5..8e539df31 100644 --- a/src/share/vm/prims/jvm.cpp +++ b/src/share/vm/prims/jvm.cpp @@ -101,6 +101,7 @@ #endif // INCLUDE_ALL_GCS #include +#include #ifndef USDT2 HS_DTRACE_PROBE_DECL1(hotspot, thread__sleep__begin, long long); @@ -3230,6 +3231,15 @@ JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread)) "unable to create new native thread"); } +#if INCLUDE_JFR + if (JfrRecorder::is_recording() && EventThreadStart::is_enabled() && + EventThreadStart::is_stacktrace_enabled()) { + JfrThreadLocal* tl = native_thread->jfr_thread_local(); + // skip Thread.start() and Thread.start0() + tl->set_cached_stack_trace_id(JfrStackTraceRepository::record(thread, 2, WALK_BY_DEFAULT)); + } +#endif + Thread::start(native_thread); JVM_END @@ -5054,4 +5064,4 @@ JVM_ENTRY(void, JVM_MarkPreempted(JNIEnv* env, jclass klass, jobject threadObj, VM_ForceSafepoint force_safepoint_op; VMThread::execute(&force_safepoint_op); } -JVM_END \ No newline at end of file +JVM_END -- GitLab