Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions ddprof-lib/src/main/java/com/datadoghq/profiler/JavaProfiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@
*/
public final class JavaProfiler {
static final Unsafe UNSAFE;
static final boolean isJDK8;
static {
Unsafe unsafe = null;
String version = System.getProperty("java.version");
isJDK8 = version.startsWith("1.8");
try {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
Expand Down Expand Up @@ -129,7 +127,7 @@ private void initializeContextStorage() {
if (this.contextStorage == null) {
int maxPages = getMaxContextPages0();
if (maxPages > 0) {
if (isJDK8) {
if (UNSAFE != null) {
contextBaseOffsets = new long[maxPages];
// be sure to choose an illegal address as a sentinel value
Arrays.fill(contextBaseOffsets, Long.MIN_VALUE);
Expand Down Expand Up @@ -238,14 +236,14 @@ public void removeThread() {
*/
public void setContext(long spanId, long rootSpanId) {
int tid = TID.get();
if (isJDK8) {
setContextJDK8(tid, spanId, rootSpanId);
if (UNSAFE != null) {
setContextUnsafe(tid, spanId, rootSpanId);
} else {
setContextByteBuffer(tid, spanId, rootSpanId);
}
}

private void setContextJDK8(int tid, long spanId, long rootSpanId) {
private void setContextUnsafe(int tid, long spanId, long rootSpanId) {
if (contextBaseOffsets == null) {
return;
}
Expand Down Expand Up @@ -313,14 +311,14 @@ public void clearContext() {
*/
public void setContextValue(int offset, int value) {
int tid = TID.get();
if (isJDK8) {
setContextJDK8(tid, offset, value);
if (UNSAFE != null) {
setContextUnsafe(tid, offset, value);
} else {
setContextByteBuffer(tid, offset, value);
}
}

private void setContextJDK8(int tid, int offset, int value) {
private void setContextUnsafe(int tid, int offset, int value) {
if (contextBaseOffsets == null) {
return;
}
Expand All @@ -344,14 +342,14 @@ public void setContextByteBuffer(int tid, int offset, int value) {

void copyTags(int[] snapshot) {
int tid = TID.get();
if (isJDK8) {
copyTagsJDK8(tid, snapshot);
if (UNSAFE != null) {
copyTagsUnsafe(tid, snapshot);
} else {
copyTagsByteBuffer(tid, snapshot);
}
}

void copyTagsJDK8(int tid, int[] snapshot) {
void copyTagsUnsafe(int tid, int[] snapshot) {
if (contextBaseOffsets == null) {
return;
}
Expand Down
Loading