Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 13
Thread filter optim#238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Thread filter optim #238
Changes from all commits
94b255951cb97fcc02c1e50a8d5f30d32c0bf2330990651f528e23ee3d31cc7dfd44de1e6efe42633224d967e7168035f6658097de78a6b2a637ba6ab9411cb5fba80cc170eeFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -16,6 +16,7 @@ | ||
| #include <assert.h> | ||
| #include "arch_dd.h" | ||
r1viollet marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| #include "context.h" | ||
| #include "counters.h" | ||
| #include "engine.h" | ||
| @@ -124,19 +125,103 @@ Java_com_datadoghq_profiler_JavaProfiler_getSamples(JNIEnv *env, | ||
| return (jlong)Profiler::instance()->total_samples(); | ||
| } | ||
| // some duplication between add and remove, though we want to avoid having an extra branch in the hot path | ||
| extern "C" DLLEXPORT void JNICALL | ||
| Java_com_datadoghq_profiler_JavaProfiler_filterThreadAdd0(JNIEnv *env, | ||
| jobject unused) { | ||
| ProfiledThread *current = ProfiledThread::current(); | ||
| if (unlikely(current == nullptr)) { | ||
| assert(false); | ||
| return; | ||
| } | ||
| int tid = current->tid(); | ||
| if (unlikely(tid < 0)) { | ||
| return; | ||
| } | ||
| ThreadFilter *thread_filter = Profiler::instance()->threadFilter(); | ||
| if (unlikely(!thread_filter->enabled())) { | ||
| return; | ||
| } | ||
| int slot_id = current->filterSlotId(); | ||
| if (unlikely(slot_id == -1)) { | ||
| // Thread doesn't have a slot ID yet (e.g., main thread), so register it | ||
| // Happens when we are not enabled before thread start | ||
| slot_id = thread_filter->registerThread(); | ||
| current->setFilterSlotId(slot_id); | ||
| } | ||
| if (unlikely(slot_id == -1)) { | ||
| return; // Failed to register thread | ||
| } | ||
| thread_filter->add(tid, slot_id); | ||
| } | ||
| extern "C" DLLEXPORT void JNICALL | ||
| Java_com_datadoghq_profiler_JavaProfiler_filterThreadRemove0(JNIEnv *env, | ||
| jobject unused) { | ||
| ProfiledThread *current = ProfiledThread::current(); | ||
| if (unlikely(current == nullptr)) { | ||
| ||
| assert(false); | ||
| return; | ||
| } | ||
| int tid = current->tid(); | ||
| if (unlikely(tid < 0)) { | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible? or we should just assert ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question | ||
| return; | ||
| } | ||
| ThreadFilter *thread_filter = Profiler::instance()->threadFilter(); | ||
| if (unlikely(!thread_filter->enabled())) { | ||
| return; | ||
| } | ||
| int slot_id = current->filterSlotId(); | ||
| if (unlikely(slot_id == -1)) { | ||
| // Thread doesn't have a slot ID yet - nothing to remove | ||
| return; | ||
| } | ||
| thread_filter->remove(slot_id); | ||
| } | ||
| // Backward compatibility for existing code | ||
| extern "C" DLLEXPORT void JNICALL | ||
| Java_com_datadoghq_profiler_JavaProfiler_filterThread0(JNIEnv *env, | ||
| jobject unused, | ||
| jboolean enable) { | ||
| int tid = ProfiledThread::currentTid(); | ||
| if (tid < 0) { | ||
| ProfiledThread *current = ProfiledThread::current(); | ||
| if (unlikely(current == nullptr)) { | ||
r1viollet marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| assert(false); | ||
| return; | ||
| } | ||
| int tid = current->tid(); | ||
| if (unlikely(tid < 0)) { | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above | ||
| return; | ||
| } | ||
| ThreadFilter *thread_filter = Profiler::instance()->threadFilter(); | ||
| if (unlikely(!thread_filter->enabled())) { | ||
| return; | ||
| } | ||
| int slot_id = current->filterSlotId(); | ||
| if (unlikely(slot_id == -1)) { | ||
| if (enable) { | ||
| // Thread doesn't have a slot ID yet, so register it | ||
| assert(thread_filter->enabled() && "ThreadFilter should be enabled when trying to register thread"); | ||
| slot_id = thread_filter->registerThread(); | ||
| current->setFilterSlotId(slot_id); | ||
| } else { | ||
| // Thread doesn't have a slot ID yet - nothing to remove | ||
| return; | ||
| } | ||
| } | ||
| if (unlikely(slot_id == -1)) { | ||
| return; // Failed to register thread | ||
| } | ||
| if (enable) { | ||
| thread_filter->add(tid); | ||
| thread_filter->add(tid, slot_id); | ||
| } else { | ||
| thread_filter->remove(tid); | ||
| thread_filter->remove(slot_id); | ||
| } | ||
| } | ||
| @@ -408,27 +493,6 @@ Java_com_datadoghq_profiler_JVMAccess_healthCheck0(JNIEnv *env, | ||
| return true; | ||
| } | ||
| extern "C" DLLEXPORT jlong JNICALL | ||
r1viollet marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Java_com_datadoghq_profiler_ActiveBitmap_bitmapAddressFor0(JNIEnv *env, | ||
| jclass unused, | ||
| jint tid) { | ||
| u64* bitmap = Profiler::instance()->threadFilter()->bitmapAddressFor((int)tid); | ||
| return (jlong)bitmap; | ||
| } | ||
| extern "C" DLLEXPORT jboolean JNICALL | ||
| Java_com_datadoghq_profiler_ActiveBitmap_isActive0(JNIEnv *env, | ||
| jclass unused, | ||
| jint tid) { | ||
| return Profiler::instance()->threadFilter()->accept((int)tid) ? JNI_TRUE : JNI_FALSE; | ||
| } | ||
| extern "C" DLLEXPORT jlong JNICALL | ||
| Java_com_datadoghq_profiler_ActiveBitmap_getActiveCountAddr0(JNIEnv *env, | ||
| jclass unused) { | ||
| return (jlong)Profiler::instance()->threadFilter()->addressOfSize(); | ||
| } | ||
| // Static variable to track the current published context | ||
| static otel_process_ctx_result* current_published_context = nullptr; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -104,10 +104,12 @@ void Profiler::addRuntimeStub(const void *address, int length, | ||
| void Profiler::onThreadStart(jvmtiEnv *jvmti, JNIEnv *jni, jthread thread) { | ||
| ProfiledThread::initCurrentThread(); | ||
| int tid = ProfiledThread::currentTid(); | ||
| ProfiledThread *current = ProfiledThread::current(); | ||
| int tid = current->tid(); | ||
| if (_thread_filter.enabled()) { | ||
| _thread_filter.remove(tid); | ||
| int slot_id = _thread_filter.registerThread(); | ||
| current->setFilterSlotId(slot_id); | ||
r1viollet marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| _thread_filter.remove(slot_id); // Remove from filtering initially | ||
| } | ||
| updateThreadName(jvmti, jni, thread, true); | ||
| @@ -116,16 +118,33 @@ void Profiler::onThreadStart(jvmtiEnv *jvmti, JNIEnv *jni, jthread thread) { | ||
| } | ||
| void Profiler::onThreadEnd(jvmtiEnv *jvmti, JNIEnv *jni, jthread thread) { | ||
| int tid = ProfiledThread::currentTid(); | ||
| if (_thread_filter.enabled()) { | ||
| _thread_filter.remove(tid); | ||
| ProfiledThread *current = ProfiledThread::current(); | ||
| int tid = -1; | ||
| if (current != nullptr) { | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I remember seeing a crash around this.. | ||
| // ProfiledThread is alive - do full cleanup and use efficient tid access | ||
| int slot_id = current->filterSlotId(); | ||
| tid = current->tid(); | ||
| 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; | ||
| } | ||
| } | ||
| updateThreadName(jvmti, jni, thread, true); | ||
| // These can run if we have a valid tid | ||
| updateThreadName(jvmti, jni, thread, false); // false = not self | ||
| _cpu_engine->unregisterThread(tid); | ||
| // unregister here because JNI callers generally don't know about thread exits | ||
| _wall_engine->unregisterThread(tid); | ||
| ProfiledThread::release(); | ||
| } | ||
| int Profiler::registerThread(int tid) { | ||
| @@ -1152,6 +1171,16 @@ Error Profiler::start(Arguments &args, bool reset) { | ||
| } | ||
| _thread_filter.init(args._filter); | ||
| // Minor optim: Register the current thread (start thread won't be called) | ||
| if (_thread_filter.enabled()) { | ||
| ProfiledThread *current = ProfiledThread::current(); | ||
| if (current != nullptr) { | ||
| int slot_id = _thread_filter.registerThread(); | ||
| current->setFilterSlotId(slot_id); | ||
| _thread_filter.remove(slot_id); // Remove from filtering initially (matches onThreadStart behavior) | ||
| } | ||
| } | ||
| _cpu_engine = selectCpuEngine(args); | ||
| _wall_engine = selectWallEngine(args); | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Releasehere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
memory_order_acq_relis good here for the old_index. Though we might be talking about something else.