From fa98b52ae1333fe4a1e565daedb1966c25dffa6f Mon Sep 17 00:00:00 2001 From: Zhengyu Gu Date: Thu, 15 Jan 2026 13:24:17 -0500 Subject: [PATCH] v0 --- ddprof-lib/src/main/cpp/profiler.cpp | 33 ++++++++-------------------- 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/ddprof-lib/src/main/cpp/profiler.cpp b/ddprof-lib/src/main/cpp/profiler.cpp index 5f83a14d..4885c085 100644 --- a/ddprof-lib/src/main/cpp/profiler.cpp +++ b/ddprof-lib/src/main/cpp/profiler.cpp @@ -29,6 +29,7 @@ #include "vmStructs_dd.h" #include "wallClock.h" #include +#include #include #include #include @@ -106,8 +107,9 @@ void Profiler::addRuntimeStub(const void *address, int length, } void Profiler::onThreadStart(jvmtiEnv *jvmti, JNIEnv *jni, jthread thread) { - ProfiledThread::initCurrentThread(); ProfiledThread *current = ProfiledThread::current(); + assert(current != nullptr); + int tid = current->tid(); if (_thread_filter.enabled()) { int slot_id = _thread_filter.registerThread(); @@ -115,39 +117,22 @@ void Profiler::onThreadStart(jvmtiEnv *jvmti, JNIEnv *jni, jthread thread) { _thread_filter.remove(slot_id); // Remove from filtering initially } updateThreadName(jvmti, jni, thread, true); - - _cpu_engine->registerThread(tid); - _wall_engine->registerThread(tid); } void Profiler::onThreadEnd(jvmtiEnv *jvmti, JNIEnv *jni, jthread thread) { ProfiledThread *current = ProfiledThread::current(); - int tid = -1; - - if (current != nullptr) { - // ProfiledThread is alive - do full cleanup and use efficient tid access - int slot_id = current->filterSlotId(); - tid = current->tid(); + assert(current != nullptr); + // ProfiledThread is alive - do full cleanup and use efficient tid access + int slot_id = current->filterSlotId(); + int tid = current->tid(); - if (_thread_filter.enabled()) { + if (_thread_filter.enabled()) { _thread_filter.unregisterThread(slot_id); current->setFilterSlotId(-1); - } - - ProfiledThread::release(); - } else { - // ProfiledThread already cleaned up - try to get tid from JVMTI as fallback - tid = VMThread::nativeThreadId(jni, thread); - if (tid < 0) { - // No ProfiledThread AND can't get tid from JVMTI - nothing we can do - return; - } } - + // These can run if we have a valid tid updateThreadName(jvmti, jni, thread, false); // false = not self - _cpu_engine->unregisterThread(tid); - _wall_engine->unregisterThread(tid); } int Profiler::registerThread(int tid) {