|
16 | 16 |
|
17 | 17 | #include <assert.h> |
18 | 18 |
|
| 19 | +#include "arch_dd.h" |
19 | 20 | #include "context.h" |
20 | 21 | #include "counters.h" |
21 | 22 | #include "engine.h" |
@@ -124,19 +125,103 @@ Java_com_datadoghq_profiler_JavaProfiler_getSamples(JNIEnv *env, |
124 | 125 | return (jlong)Profiler::instance()->total_samples(); |
125 | 126 | } |
126 | 127 |
|
| 128 | +// some duplication between add and remove, though we want to avoid having an extra branch in the hot path |
| 129 | +extern "C" DLLEXPORT void JNICALL |
| 130 | +Java_com_datadoghq_profiler_JavaProfiler_filterThreadAdd0(JNIEnv *env, |
| 131 | + jobject unused) { |
| 132 | + ProfiledThread *current = ProfiledThread::current(); |
| 133 | + if (unlikely(current == nullptr)) { |
| 134 | + assert(false); |
| 135 | + return; |
| 136 | + } |
| 137 | + int tid = current->tid(); |
| 138 | + if (unlikely(tid < 0)) { |
| 139 | + return; |
| 140 | + } |
| 141 | + ThreadFilter *thread_filter = Profiler::instance()->threadFilter(); |
| 142 | + if (unlikely(!thread_filter->enabled())) { |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + int slot_id = current->filterSlotId(); |
| 147 | + if (unlikely(slot_id == -1)) { |
| 148 | + // Thread doesn't have a slot ID yet (e.g., main thread), so register it |
| 149 | + // Happens when we are not enabled before thread start |
| 150 | + slot_id = thread_filter->registerThread(); |
| 151 | + current->setFilterSlotId(slot_id); |
| 152 | + } |
| 153 | + |
| 154 | + if (unlikely(slot_id == -1)) { |
| 155 | + return; // Failed to register thread |
| 156 | + } |
| 157 | + thread_filter->add(tid, slot_id); |
| 158 | +} |
| 159 | + |
| 160 | +extern "C" DLLEXPORT void JNICALL |
| 161 | +Java_com_datadoghq_profiler_JavaProfiler_filterThreadRemove0(JNIEnv *env, |
| 162 | + jobject unused) { |
| 163 | + ProfiledThread *current = ProfiledThread::current(); |
| 164 | + if (unlikely(current == nullptr)) { |
| 165 | + assert(false); |
| 166 | + return; |
| 167 | + } |
| 168 | + int tid = current->tid(); |
| 169 | + if (unlikely(tid < 0)) { |
| 170 | + return; |
| 171 | + } |
| 172 | + ThreadFilter *thread_filter = Profiler::instance()->threadFilter(); |
| 173 | + if (unlikely(!thread_filter->enabled())) { |
| 174 | + return; |
| 175 | + } |
| 176 | + |
| 177 | + int slot_id = current->filterSlotId(); |
| 178 | + if (unlikely(slot_id == -1)) { |
| 179 | + // Thread doesn't have a slot ID yet - nothing to remove |
| 180 | + return; |
| 181 | + } |
| 182 | + thread_filter->remove(slot_id); |
| 183 | +} |
| 184 | + |
| 185 | +// Backward compatibility for existing code |
127 | 186 | extern "C" DLLEXPORT void JNICALL |
128 | 187 | Java_com_datadoghq_profiler_JavaProfiler_filterThread0(JNIEnv *env, |
129 | 188 | jobject unused, |
130 | 189 | jboolean enable) { |
131 | | - int tid = ProfiledThread::currentTid(); |
132 | | - if (tid < 0) { |
| 190 | + ProfiledThread *current = ProfiledThread::current(); |
| 191 | + if (unlikely(current == nullptr)) { |
| 192 | + assert(false); |
| 193 | + return; |
| 194 | + } |
| 195 | + int tid = current->tid(); |
| 196 | + if (unlikely(tid < 0)) { |
133 | 197 | return; |
134 | 198 | } |
135 | 199 | ThreadFilter *thread_filter = Profiler::instance()->threadFilter(); |
| 200 | + if (unlikely(!thread_filter->enabled())) { |
| 201 | + return; |
| 202 | + } |
| 203 | + |
| 204 | + int slot_id = current->filterSlotId(); |
| 205 | + if (unlikely(slot_id == -1)) { |
| 206 | + if (enable) { |
| 207 | + // Thread doesn't have a slot ID yet, so register it |
| 208 | + assert(thread_filter->enabled() && "ThreadFilter should be enabled when trying to register thread"); |
| 209 | + slot_id = thread_filter->registerThread(); |
| 210 | + current->setFilterSlotId(slot_id); |
| 211 | + } else { |
| 212 | + // Thread doesn't have a slot ID yet - nothing to remove |
| 213 | + return; |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + if (unlikely(slot_id == -1)) { |
| 218 | + return; // Failed to register thread |
| 219 | + } |
| 220 | + |
136 | 221 | if (enable) { |
137 | | - thread_filter->add(tid); |
| 222 | + thread_filter->add(tid, slot_id); |
138 | 223 | } else { |
139 | | - thread_filter->remove(tid); |
| 224 | + thread_filter->remove(slot_id); |
140 | 225 | } |
141 | 226 | } |
142 | 227 |
|
@@ -408,27 +493,6 @@ Java_com_datadoghq_profiler_JVMAccess_healthCheck0(JNIEnv *env, |
408 | 493 | return true; |
409 | 494 | } |
410 | 495 |
|
411 | | -extern "C" DLLEXPORT jlong JNICALL |
412 | | -Java_com_datadoghq_profiler_ActiveBitmap_bitmapAddressFor0(JNIEnv *env, |
413 | | - jclass unused, |
414 | | - jint tid) { |
415 | | - u64* bitmap = Profiler::instance()->threadFilter()->bitmapAddressFor((int)tid); |
416 | | - return (jlong)bitmap; |
417 | | -} |
418 | | - |
419 | | -extern "C" DLLEXPORT jboolean JNICALL |
420 | | -Java_com_datadoghq_profiler_ActiveBitmap_isActive0(JNIEnv *env, |
421 | | - jclass unused, |
422 | | - jint tid) { |
423 | | - return Profiler::instance()->threadFilter()->accept((int)tid) ? JNI_TRUE : JNI_FALSE; |
424 | | -} |
425 | | - |
426 | | -extern "C" DLLEXPORT jlong JNICALL |
427 | | -Java_com_datadoghq_profiler_ActiveBitmap_getActiveCountAddr0(JNIEnv *env, |
428 | | - jclass unused) { |
429 | | - return (jlong)Profiler::instance()->threadFilter()->addressOfSize(); |
430 | | -} |
431 | | - |
432 | 496 | // Static variable to track the current published context |
433 | 497 | static otel_process_ctx_result* current_published_context = nullptr; |
434 | 498 |
|
|
0 commit comments