--- old/src/share/vm/jfr/metadata/metadata.xml 2021-01-22 14:12:43.817191672 +0800 +++ new/src/share/vm/jfr/metadata/metadata.xml 2021-01-22 14:12:43.697186985 +0800 @@ -26,8 +26,9 @@ - - + + + --- old/src/share/vm/jfr/support/jfrThreadLocal.cpp 2021-01-22 14:12:44.519219088 +0800 +++ new/src/share/vm/jfr/support/jfrThreadLocal.cpp 2021-01-22 14:12:44.402214519 +0800 @@ -55,7 +55,11 @@ _stack_trace_hash(0), _stackdepth(0), _entering_suspend_flag(0), - _dead(false) {} + _dead(false) { + + Thread* thread = Thread::current_or_null(); + _parent_trace_id = thread != NULL ? thread->jfr_thread_local()->trace_id() : (traceid)0; +} u8 JfrThreadLocal::add_data_lost(u8 value) { _data_lost += value; @@ -78,6 +82,7 @@ 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(); } @@ -89,6 +94,9 @@ send_java_thread_start_event((JavaThread*)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) { --- old/src/share/vm/jfr/support/jfrThreadLocal.hpp 2021-01-22 14:12:45.182244981 +0800 +++ new/src/share/vm/jfr/support/jfrThreadLocal.hpp 2021-01-22 14:12:45.071240646 +0800 @@ -51,6 +51,7 @@ mutable u4 _stackdepth; volatile jint _entering_suspend_flag; bool _dead; + traceid _parent_trace_id; JfrBuffer* install_native_buffer() const; JfrBuffer* install_java_buffer() const; @@ -127,6 +128,10 @@ _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; --- old/src/share/vm/prims/jni.cpp 2021-01-22 14:12:45.841270718 +0800 +++ new/src/share/vm/prims/jni.cpp 2021-01-22 14:12:45.726266227 +0800 @@ -5025,7 +5025,14 @@ 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(); + } } } --- old/src/share/vm/prims/jvm.cpp 2021-01-22 14:12:46.540298017 +0800 +++ new/src/share/vm/prims/jvm.cpp 2021-01-22 14:12:46.426293565 +0800 @@ -96,6 +96,7 @@ #endif // INCLUDE_ALL_GCS #include +#include #ifndef USDT2 HS_DTRACE_PROBE_DECL1(hotspot, thread__sleep__begin, long long); @@ -3162,6 +3163,15 @@ "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)); + } +#endif + Thread::start(native_thread); JVM_END --- old/src/share/vm/runtime/thread.hpp 2021-01-22 14:12:47.235325160 +0800 +++ new/src/share/vm/runtime/thread.hpp 2021-01-22 14:12:47.122320747 +0800 @@ -331,6 +331,7 @@ // Returns the current thread static inline Thread* current(); + static inline Thread* current_or_null(); // Common thread operations static void set_priority(Thread* thread, ThreadPriority priority); @@ -683,6 +684,13 @@ return thread; } +inline Thread* Thread::current_or_null() { + if (ThreadLocalStorage::is_initialized()) { + return ThreadLocalStorage::thread(); + } + return NULL; +} + // Name support for threads. non-JavaThread subclasses with multiple // uniquely named instances should derive from this. class NamedThread: public Thread {