Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 9 additions & 24 deletions ddprof-lib/src/main/cpp/profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "vmStructs_dd.h"
#include "wallClock.h"
#include <algorithm>
#include <cassert>
#include <dlfcn.h>
#include <fstream>
#include <memory>
Expand Down Expand Up @@ -106,48 +107,32 @@ 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();
current->setFilterSlotId(slot_id);
_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) {
Expand Down
Loading