Skip to content

Commit dd847f9

Browse files
committed
Thread filter - add a table with linear probing
Remove usage of set to ensure we are signal safe.
1 parent 115a60a commit dd847f9

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

ddprof-lib/src/main/cpp/flightRecorder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,8 +1063,8 @@ void Recording::writeThreads(Buffer *buf) {
10631063
threads.insert(_tid);
10641064

10651065
for (int i = 0; i < CONCURRENCY_LEVEL; ++i) {
1066-
// I can not use merge : cpp 17
1067-
threads.insert(_thread_ids[i][old_index].begin(), _thread_ids[i][old_index].end());
1066+
// Collect thread IDs from the fixed-size table into the main set
1067+
_thread_ids[i][old_index].collect(threads);
10681068
_thread_ids[i][old_index].clear();
10691069
}
10701070

ddprof-lib/src/main/cpp/flightRecorder.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "mutex.h"
3636
#include "objectSampler.h"
3737
#include "threadFilter.h"
38+
#include "threadIdTable.h"
3839
#include "vmEntry.h"
3940

4041
const u64 MAX_JLONG = 0x7fffffffffffffffULL;
@@ -128,9 +129,9 @@ class Recording {
128129
static char *_java_command;
129130

130131
RecordingBuffer _buf[CONCURRENCY_LEVEL];
131-
// we have several sets to avoid lock contention
132-
// we have a second dimension to allow a switch in the active set
133-
std::unordered_set<int> _thread_ids[CONCURRENCY_LEVEL][2];
132+
// we have several tables to avoid lock contention
133+
// we have a second dimension to allow a switch in the active table
134+
ThreadIdTable _thread_ids[CONCURRENCY_LEVEL][2];
134135
std::atomic<int> _active_index{0}; // 0 or 1 globally
135136

136137
int _fd;

ddprof-lib/src/main/cpp/javaApi.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,28 @@ Java_com_datadoghq_profiler_JavaProfiler_filterThread_1remove(JNIEnv *env,
156156
thread_filter->remove(slot_id);
157157
}
158158

159+
// Backward compatibility for existing code
160+
extern "C" DLLEXPORT void JNICALL
161+
Java_com_datadoghq_profiler_JavaProfiler_filterThread0(JNIEnv *env,
162+
jobject unused,
163+
jboolean enable) {
164+
ProfiledThread *current = ProfiledThread::current();
165+
int tid = current->tid();
166+
if (unlikely(tid < 0)) {
167+
return;
168+
}
169+
ThreadFilter *thread_filter = Profiler::instance()->threadFilter();
170+
int slot_id = current->filterSlotId();
171+
if (unlikely(slot_id == -1)) {
172+
return;
173+
}
174+
175+
if (enable) {
176+
thread_filter->add(tid, slot_id);
177+
} else {
178+
thread_filter->remove(slot_id);
179+
}
180+
}
159181

160182
extern "C" DLLEXPORT jobject JNICALL
161183
Java_com_datadoghq_profiler_JavaProfiler_getContextPage0(JNIEnv *env,

ddprof-lib/src/main/cpp/profiler.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ void Profiler::onThreadStart(jvmtiEnv *jvmti, JNIEnv *jni, jthread thread) {
106106
ProfiledThread *current = ProfiledThread::current();
107107
int tid = current->tid();
108108
if (_thread_filter.enabled()) {
109-
current->setFilterSlotId(_thread_filter.registerThread());
110-
_thread_filter.remove(tid);
109+
int slot_id = _thread_filter.registerThread();
110+
current->setFilterSlotId(slot_id);
111+
_thread_filter.remove(slot_id); // Remove from filtering initially
111112
}
112113
updateThreadName(jvmti, jni, thread, true);
113114

ddprof-lib/src/main/java/com/datadoghq/profiler/JavaProfiler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,8 @@ public Map<String, Long> getDebugCounters() {
448448

449449
private native void filterThread_add();
450450
private native void filterThread_remove();
451+
// Backward compatibility for existing code
452+
private native void filterThread0(boolean enable);
451453

452454
private static native int getTid0();
453455
private static native ByteBuffer getContextPage0(int tid);

0 commit comments

Comments
 (0)