Skip to content

Commit d01bf39

Browse files
authored
Remove 'Unsafe' use from Java prof (#295)
1 parent af11ba0 commit d01bf39

1 file changed

Lines changed: 4 additions & 72 deletions

File tree

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

Lines changed: 4 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,7 @@ private void initializeContextStorage() {
129129
if (this.contextStorage == null) {
130130
int maxPages = getMaxContextPages0();
131131
if (maxPages > 0) {
132-
if (UNSAFE != null) {
133-
contextBaseOffsets = new long[maxPages];
134-
// be sure to choose an illegal address as a sentinel value
135-
Arrays.fill(contextBaseOffsets, Long.MIN_VALUE);
136-
} else {
137-
contextStorage = new ByteBuffer[maxPages];
138-
}
132+
contextStorage = new ByteBuffer[maxPages];
139133
}
140134
}
141135
}
@@ -229,26 +223,7 @@ public void removeThread() {
229223
*/
230224
public void setContext(long spanId, long rootSpanId) {
231225
int tid = TID.get();
232-
if (UNSAFE != null) {
233-
setContextUnsafe(tid, spanId, rootSpanId);
234-
} else {
235-
setContextByteBuffer(tid, spanId, rootSpanId);
236-
}
237-
}
238-
239-
private void setContextUnsafe(int tid, long spanId, long rootSpanId) {
240-
if (contextBaseOffsets == null) {
241-
return;
242-
}
243-
long pageOffset = getPageUnsafe(tid);
244-
if (pageOffset == 0) {
245-
return;
246-
}
247-
int index = (tid % PAGE_SIZE) * CONTEXT_SIZE;
248-
long base = pageOffset + index;
249-
UNSAFE.putLong(base + SPAN_OFFSET, spanId);
250-
UNSAFE.putLong(base + ROOT_SPAN_OFFSET, rootSpanId);
251-
UNSAFE.putLong(base + CHECKSUM_OFFSET, spanId ^ rootSpanId);
226+
setContextByteBuffer(tid, spanId, rootSpanId);
252227
}
253228

254229
private void setContextByteBuffer(int tid, long spanId, long rootSpanId) {
@@ -281,15 +256,6 @@ private ByteBuffer getPage(int tid) {
281256
return page;
282257
}
283258

284-
private long getPageUnsafe(int tid) {
285-
int pageIndex = tid / PAGE_SIZE;
286-
long offset = contextBaseOffsets[pageIndex];
287-
if (offset == Long.MIN_VALUE) {
288-
contextBaseOffsets[pageIndex] = offset = getContextPageOffset0(tid);
289-
}
290-
return offset;
291-
}
292-
293259
/**
294260
* Clears context identifier for current thread.
295261
*/
@@ -304,22 +270,7 @@ public void clearContext() {
304270
*/
305271
public void setContextValue(int offset, int value) {
306272
int tid = TID.get();
307-
if (UNSAFE != null) {
308-
setContextUnsafe(tid, offset, value);
309-
} else {
310-
setContextByteBuffer(tid, offset, value);
311-
}
312-
}
313-
314-
private void setContextUnsafe(int tid, int offset, int value) {
315-
if (contextBaseOffsets == null) {
316-
return;
317-
}
318-
long pageOffset = getPageUnsafe(tid);
319-
if (pageOffset == 0) {
320-
return;
321-
}
322-
UNSAFE.putInt(pageOffset + addressOf(tid, offset), value);
273+
setContextByteBuffer(tid, offset, value);
323274
}
324275

325276
public void setContextByteBuffer(int tid, int offset, int value) {
@@ -335,26 +286,7 @@ public void setContextByteBuffer(int tid, int offset, int value) {
335286

336287
void copyTags(int[] snapshot) {
337288
int tid = TID.get();
338-
if (UNSAFE != null) {
339-
copyTagsUnsafe(tid, snapshot);
340-
} else {
341-
copyTagsByteBuffer(tid, snapshot);
342-
}
343-
}
344-
345-
void copyTagsUnsafe(int tid, int[] snapshot) {
346-
if (contextBaseOffsets == null) {
347-
return;
348-
}
349-
long pageOffset = getPageUnsafe(tid);
350-
if (pageOffset == 0) {
351-
return;
352-
}
353-
long address = pageOffset + addressOf(tid, 0);
354-
for (int i = 0; i < snapshot.length; i++) {
355-
snapshot[i] = UNSAFE.getInt(address);
356-
address += Integer.BYTES;
357-
}
289+
copyTagsByteBuffer(tid, snapshot);
358290
}
359291

360292
void copyTagsByteBuffer(int tid, int[] snapshot) {

0 commit comments

Comments
 (0)