@@ -66,6 +66,7 @@ static final class TSCFrequencyHolder {
66
66
67
67
private ByteBuffer [] contextStorage ;
68
68
private long [] contextBaseOffsets ;
69
+ private static final ByteBuffer SENTINEL = ByteBuffer .allocate (0 );
69
70
70
71
private JavaProfiler () {
71
72
}
@@ -240,6 +241,9 @@ private void setContextJDK8(int tid, long spanId, long rootSpanId) {
240
241
return ;
241
242
}
242
243
long pageOffset = getPageUnsafe (tid );
244
+ if (pageOffset == 0 ) {
245
+ return ;
246
+ }
243
247
int index = (tid % PAGE_SIZE ) * CONTEXT_SIZE ;
244
248
long base = pageOffset + index ;
245
249
UNSAFE .putLong (base + SPAN_OFFSET , spanId );
@@ -252,20 +256,27 @@ private void setContextByteBuffer(int tid, long spanId, long rootSpanId) {
252
256
return ;
253
257
}
254
258
ByteBuffer page = getPage (tid );
259
+ if (page == SENTINEL ) {
260
+ return ;
261
+ }
255
262
int index = (tid % PAGE_SIZE ) * CONTEXT_SIZE ;
256
263
page .putLong (index + SPAN_OFFSET , spanId );
257
264
page .putLong (index + ROOT_SPAN_OFFSET , rootSpanId );
258
265
page .putLong (index + CHECKSUM_OFFSET , spanId ^ rootSpanId );
259
266
}
260
267
261
-
262
-
263
268
private ByteBuffer getPage (int tid ) {
264
269
int pageIndex = tid / PAGE_SIZE ;
265
270
ByteBuffer page = contextStorage [pageIndex ];
266
271
if (page == null ) {
267
272
// the underlying page allocation is atomic so we don't care which view we have over it
268
- contextStorage [pageIndex ] = page = getContextPage0 (tid ).order (ByteOrder .LITTLE_ENDIAN );
273
+ ByteBuffer buffer = getContextPage0 (tid );
274
+ if (buffer == null ) {
275
+ page = SENTINEL ;
276
+ } else {
277
+ page = buffer .order (ByteOrder .LITTLE_ENDIAN );
278
+ }
279
+ contextStorage [pageIndex ] = page ;
269
280
}
270
281
return page ;
271
282
}
@@ -305,6 +316,9 @@ private void setContextJDK8(int tid, int offset, int value) {
305
316
return ;
306
317
}
307
318
long pageOffset = getPageUnsafe (tid );
319
+ if (pageOffset == 0 ) {
320
+ return ;
321
+ }
308
322
UNSAFE .putInt (pageOffset + addressOf (tid , offset ), value );
309
323
}
310
324
@@ -313,6 +327,9 @@ public void setContextByteBuffer(int tid, int offset, int value) {
313
327
return ;
314
328
}
315
329
ByteBuffer page = getPage (tid );
330
+ if (page == SENTINEL ) {
331
+ return ;
332
+ }
316
333
page .putInt (addressOf (tid , offset ), value );
317
334
}
318
335
@@ -330,6 +347,9 @@ void copyTagsJDK8(int tid, int[] snapshot) {
330
347
return ;
331
348
}
332
349
long pageOffset = getPageUnsafe (tid );
350
+ if (pageOffset == 0 ) {
351
+ return ;
352
+ }
333
353
long address = pageOffset + addressOf (tid , 0 );
334
354
for (int i = 0 ; i < snapshot .length ; i ++) {
335
355
snapshot [i ] = UNSAFE .getInt (address );
@@ -342,6 +362,9 @@ void copyTagsByteBuffer(int tid, int[] snapshot) {
342
362
return ;
343
363
}
344
364
ByteBuffer page = getPage (tid );
365
+ if (page == SENTINEL ) {
366
+ return ;
367
+ }
345
368
int address = addressOf (tid , 0 );
346
369
for (int i = 0 ; i < snapshot .length ; i ++) {
347
370
snapshot [i ] = page .getInt (address + i * Integer .BYTES );
0 commit comments