Skip to content

Commit 793d42c

Browse files
authored
Unify context setting code (#231)
1 parent ee3b167 commit 793d42c

1 file changed

Lines changed: 10 additions & 12 deletions

File tree

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@
3535
*/
3636
public final class JavaProfiler {
3737
static final Unsafe UNSAFE;
38-
static final boolean isJDK8;
3938
static {
4039
Unsafe unsafe = null;
4140
String version = System.getProperty("java.version");
42-
isJDK8 = version.startsWith("1.8");
4341
try {
4442
Field f = Unsafe.class.getDeclaredField("theUnsafe");
4543
f.setAccessible(true);
@@ -129,7 +127,7 @@ private void initializeContextStorage() {
129127
if (this.contextStorage == null) {
130128
int maxPages = getMaxContextPages0();
131129
if (maxPages > 0) {
132-
if (isJDK8) {
130+
if (UNSAFE != null) {
133131
contextBaseOffsets = new long[maxPages];
134132
// be sure to choose an illegal address as a sentinel value
135133
Arrays.fill(contextBaseOffsets, Long.MIN_VALUE);
@@ -238,14 +236,14 @@ public void removeThread() {
238236
*/
239237
public void setContext(long spanId, long rootSpanId) {
240238
int tid = TID.get();
241-
if (isJDK8) {
242-
setContextJDK8(tid, spanId, rootSpanId);
239+
if (UNSAFE != null) {
240+
setContextUnsafe(tid, spanId, rootSpanId);
243241
} else {
244242
setContextByteBuffer(tid, spanId, rootSpanId);
245243
}
246244
}
247245

248-
private void setContextJDK8(int tid, long spanId, long rootSpanId) {
246+
private void setContextUnsafe(int tid, long spanId, long rootSpanId) {
249247
if (contextBaseOffsets == null) {
250248
return;
251249
}
@@ -313,14 +311,14 @@ public void clearContext() {
313311
*/
314312
public void setContextValue(int offset, int value) {
315313
int tid = TID.get();
316-
if (isJDK8) {
317-
setContextJDK8(tid, offset, value);
314+
if (UNSAFE != null) {
315+
setContextUnsafe(tid, offset, value);
318316
} else {
319317
setContextByteBuffer(tid, offset, value);
320318
}
321319
}
322320

323-
private void setContextJDK8(int tid, int offset, int value) {
321+
private void setContextUnsafe(int tid, int offset, int value) {
324322
if (contextBaseOffsets == null) {
325323
return;
326324
}
@@ -344,14 +342,14 @@ public void setContextByteBuffer(int tid, int offset, int value) {
344342

345343
void copyTags(int[] snapshot) {
346344
int tid = TID.get();
347-
if (isJDK8) {
348-
copyTagsJDK8(tid, snapshot);
345+
if (UNSAFE != null) {
346+
copyTagsUnsafe(tid, snapshot);
349347
} else {
350348
copyTagsByteBuffer(tid, snapshot);
351349
}
352350
}
353351

354-
void copyTagsJDK8(int tid, int[] snapshot) {
352+
void copyTagsUnsafe(int tid, int[] snapshot) {
355353
if (contextBaseOffsets == null) {
356354
return;
357355
}

0 commit comments

Comments
 (0)