-
Notifications
You must be signed in to change notification settings - Fork 9
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
base: main
Are you sure you want to change the base?
Thread filter optim #238
Changes from all commits
94b2559
51cb97f
cc02c1e
50a8d5f
30d32c0
bf23309
90651f5
28e23ee
3d31cc7
dfd44de
1e6efe4
2633224
d967e71
68035f6
658097d
e78a6b2
File filter
Filter by extension
Conversations
Jump to
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" | ||
#include "context.h" | ||
#include "counters.h" | ||
#include "engine.h" | ||
|
@@ -123,19 +124,101 @@ 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 | ||
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)) { | ||
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 think |
||
assert(false); | ||
return; | ||
} | ||
int tid = current->tid(); | ||
if (unlikely(tid < 0)) { | ||
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 |
||
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)) { | ||
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 |
||
assert(false); | ||
return; | ||
} | ||
int tid = current->tid(); | ||
if (unlikely(tid < 0)) { | ||
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 | ||
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); | ||
} | ||
} | ||
|
||
|
@@ -406,24 +489,3 @@ Java_com_datadoghq_profiler_JVMAccess_healthCheck0(JNIEnv *env, | |
jobject unused) { | ||
return true; | ||
} | ||
|
||
extern "C" DLLEXPORT jlong JNICALL | ||
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(); | ||
} |
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.
having to include this for unlikely is not perfect