Skip to content

Commit 7737a94

Browse files
committed
v2
1 parent e12c3a4 commit 7737a94

5 files changed

Lines changed: 34 additions & 95 deletions

File tree

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ Java_com_datadoghq_profiler_JVMAccess_healthCheck0(JNIEnv *env,
409409

410410
extern "C" DLLEXPORT jlong JNICALL
411411
Java_com_datadoghq_profiler_ActiveBitmap_bitmapAddressFor0(JNIEnv *env,
412-
jclass unused,
413-
jint tid) {
412+
jclass unused,
413+
jint tid) {
414414
u64* bitmap = Profiler::instance()->threadFilter()->bitmapAddressFor((int)tid);
415415
return (jlong)bitmap;
416416
}
@@ -426,4 +426,12 @@ extern "C" DLLEXPORT jlong JNICALL
426426
Java_com_datadoghq_profiler_ActiveBitmap_getActiveCountAddr0(JNIEnv *env,
427427
jclass unused) {
428428
return (jlong)Profiler::instance()->threadFilter()->addressOfSize();
429+
}
430+
431+
extern "C" DLLEXPORT jlong JNICALL
432+
Java_com_datadoghq_profiler_ActiveBitmap_getBitmapValue0(JNIEnv *env,
433+
jclass unused,
434+
jint tid,
435+
jlong v) {
436+
return (jlong)Profiler::instance()->threadFilter()->getBitmapValue((int)tid, (long)v);
429437
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <cassert>
2222
#include <stdlib.h>
2323
#include <string.h>
24+
#include <iostream>
2425

2526
void trackPage() {
2627
Counters::increment(THREAD_FILTER_PAGES, 1);
@@ -117,21 +118,32 @@ u64* ThreadFilter::getBitmapFor(int thread_id) {
117118
}
118119

119120
u64* ThreadFilter::bitmapAddressFor(int thread_id) {
120-
u64* bitmap = getBitmapFor(thread_id);
121+
u64* b = getBitmapFor(thread_id);
121122
thread_id = mapThreadId(thread_id);
122-
return wordAddress(bitmap, thread_id);
123+
assert(b == bitmap(thread_id));
124+
return wordAddress(b, thread_id);
123125
}
124126

127+
u64 ThreadFilter::getBitmapValue(int thread_id, long v) {
128+
thread_id = mapThreadId(thread_id);
129+
u64 *b = bitmap(thread_id);
130+
if (b == NULL) return 0;
131+
assert((long)word(b, thread_id) == v);
132+
return word(b, thread_id);
133+
}
134+
135+
125136
bool ThreadFilter::accept(int thread_id) {
126137
thread_id = mapThreadId(thread_id);
127138
u64 *b = bitmap(thread_id);
128139
return b != NULL && (word(b, thread_id) & (1ULL << (thread_id & 0x3f)));
129140
}
130141

131142
void ThreadFilter::add(int thread_id) {
132-
thread_id = mapThreadId(thread_id);
133143
u64 *b = getBitmapFor(thread_id);
134144
assert (b != NULL);
145+
thread_id = mapThreadId(thread_id);
146+
assert(b == bitmap(thread_id));
135147
u64 bit = 1ULL << (thread_id & 0x3f);
136148
if (!(__sync_fetch_and_or(&word(b, thread_id), bit) & bit)) {
137149
atomicInc(_size);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class ThreadFilter {
7474
void add(int thread_id);
7575
void remove(int thread_id);
7676
u64* bitmapAddressFor(int thread_id);
77+
u64 getBitmapValue(int thread_id, long v);
7778

7879
void collect(std::vector<int> &v);
7980
};

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ public static void initialize() {
2626
static long getBitmask(int tid) {
2727
int tmp = (tid >> 8) & 0xff ;
2828
int bits = 0;
29-
for (int index = 0; index < 7 ; index++) {
29+
for (int index = 0; index <= 7; index++) {
3030
if ((tmp & 0x01) == 0x01) {
31-
bits |= 0x01;
31+
bits |= 1 << (7 - index);
3232
}
33-
tmp >>= 1;
34-
bits <<= 1;
33+
tmp >>>= 1;
3534
}
3635
return 1L << (bits & 0x3f);
3736
}
@@ -44,25 +43,23 @@ static void setActive(int tid, boolean active) {
4443
}
4544
long bitmask = getBitmask(tid);
4645
long value = UNSAFE.getLong(addr);
47-
long newVal;
48-
if (active) {
49-
newVal = value | bitmask;
50-
} else {
51-
newVal = value & ~bitmask;
52-
}
46+
long newVal = active ? (value | bitmask) : (value & ~bitmask);
47+
5348
while (!UNSAFE.compareAndSwapLong(null, addr, value, newVal)) {
5449
value = UNSAFE.getLong(addr);
5550
newVal = active ? (value | bitmask) : (value & ~bitmask);
5651
}
5752
int delta = active ? 1 : -1;
5853
assert activeCountAddr != 0;
5954
UNSAFE.getAndAddInt(null, activeCountAddr, delta);
55+
if (isActive0(tid) != active) {
56+
throw new RuntimeException("SetActive Failed");
57+
}
6058

6159
assert isActive0(tid) == active;
6260
}
6361

6462

65-
6663
// Address of bitmap word that contains the active bit of this thread Id
6764
private static native long bitmapAddressFor0(int tid);
6865

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

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)