Skip to content

Commit 4abd63e

Browse files
authored
Merge pull request #20728 from tajila/jfr2
(0.49) Disable thread buffer writes after thread end
2 parents e082895 + 448c8cf commit 4abd63e

File tree

1 file changed

+11
-37
lines changed

1 file changed

+11
-37
lines changed

runtime/vm/jfr.cpp

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ static bool flushAllThreadBuffers(J9VMThread *currentThread, bool freeBuffers);
4646
static U_8* reserveBuffer(J9VMThread *currentThread, UDATA size);
4747
static J9JFREvent* reserveBufferWithStackTrace(J9VMThread *currentThread, J9VMThread *sampleThread, UDATA eventType, UDATA eventFixedSize);
4848
static void jfrThreadCreated(J9HookInterface **hook, UDATA eventNum, void *eventData, void *userData);
49-
static void jfrThreadDestroy(J9HookInterface **hook, UDATA eventNum, void *eventData, void *userData);
5049
static void jfrClassesUnload(J9HookInterface **hook, UDATA eventNum, void *eventData, void *userData);
5150
static void jfrVMShutdown(J9HookInterface **hook, UDATA eventNum, void *eventData, void *userData);
5251
static void jfrThreadStarting(J9HookInterface **hook, UDATA eventNum, void *eventData, void *userData);
@@ -59,6 +58,7 @@ static int J9THREAD_PROC jfrSamplingThreadProc(void *entryArg);
5958
static void jfrExecutionSampleCallback(J9VMThread *currentThread, IDATA handlerKey, void *userData);
6059
static void jfrThreadCPULoadCallback(J9VMThread *currentThread, IDATA handlerKey, void *userData);
6160
static bool areJFRBuffersReadyForWrite(J9VMThread *currentThread);
61+
6262
/**
6363
* Calculate the size in bytes of a JFR event.
6464
*
@@ -252,6 +252,7 @@ flushAllThreadBuffers(J9VMThread *currentThread, bool freeBuffers)
252252
bool flushedCurrentThread = false;
253253

254254
Assert_VM_mustHaveVMAccess(currentThread);
255+
Assert_VM_true(currentThread->omrVMThread->exclusiveCount > 0);
255256
Assert_VM_true((J9_XACCESS_EXCLUSIVE == vm->exclusiveAccessState) || (J9_XACCESS_EXCLUSIVE == vm->safePointState));
256257

257258
do {
@@ -380,7 +381,6 @@ jfrThreadCreated(J9HookInterface **hook, UDATA eventNum, void *eventData, void *
380381
#endif /* defined(DEBUG) */
381382

382383
/* TODO: allow different buffer sizes on different threads. */
383-
384384
U_8 *buffer = (U_8*)j9mem_allocate_memory(J9JFR_THREAD_BUFFER_SIZE, OMRMEM_CATEGORY_VM);
385385
if (NULL == buffer) {
386386
event->continueInitialization = FALSE;
@@ -395,37 +395,6 @@ jfrThreadCreated(J9HookInterface **hook, UDATA eventNum, void *eventData, void *
395395
}
396396
}
397397

398-
/**
399-
* Hook for thread being destroyed.
400-
*
401-
* @param hook[in] the VM hook interface
402-
* @param eventNum[in] the event number
403-
* @param eventData[in] the event data
404-
* @param userData[in] the registered user data
405-
*/
406-
static void
407-
jfrThreadDestroy(J9HookInterface **hook, UDATA eventNum, void *eventData, void *userData)
408-
{
409-
J9VMThreadDestroyEvent *event = (J9VMThreadDestroyEvent *)eventData;
410-
J9VMThread *currentThread = event->vmThread;
411-
PORT_ACCESS_FROM_VMC(currentThread);
412-
413-
#if defined(DEBUG)
414-
j9tty_printf(PORTLIB, "\n!!! thread destroy %p\n", currentThread);
415-
#endif /* defined(DEBUG) */
416-
417-
418-
/* The current thread pointer (which can appear in other thread buffers) is about to become
419-
* invalid, so write out all of the available data now.
420-
*/
421-
flushAllThreadBuffers(currentThread, false);
422-
writeOutGlobalBuffer(currentThread, false);
423-
424-
/* Free the thread local buffer */
425-
j9mem_free_memory((void*)currentThread->jfrBuffer.bufferStart);
426-
memset(&currentThread->jfrBuffer, 0, sizeof(currentThread->jfrBuffer));
427-
}
428-
429398
/**
430399
* Hook for classes being unloaded.
431400
*
@@ -543,6 +512,15 @@ jfrThreadEnd(J9HookInterface **hook, UDATA eventNum, void *eventData, void *user
543512
if (NULL != jfrEvent) {
544513
initializeEventFields(currentThread, jfrEvent, J9JFR_EVENT_TYPE_THREAD_END);
545514
}
515+
PORT_ACCESS_FROM_VMC(currentThread);
516+
acquireExclusiveVMAccess(currentThread);
517+
flushAllThreadBuffers(currentThread, false);
518+
writeOutGlobalBuffer(currentThread, false);
519+
520+
/* Free the thread local buffer */
521+
j9mem_free_memory((void*)currentThread->jfrBuffer.bufferStart);
522+
memset(&currentThread->jfrBuffer, 0, sizeof(currentThread->jfrBuffer));
523+
releaseExclusiveVMAccess(currentThread);
546524
internalReleaseVMAccess(currentThread);
547525
}
548526

@@ -674,9 +652,6 @@ initializeJFR(J9JavaVM *vm, BOOLEAN lateInit)
674652
if ((*vmHooks)->J9HookRegisterWithCallSite(vmHooks, J9HOOK_VM_THREAD_CREATED, jfrThreadCreated, OMR_GET_CALLSITE(), NULL)) {
675653
goto fail;
676654
}
677-
if ((*vmHooks)->J9HookRegisterWithCallSite(vmHooks, J9HOOK_VM_THREAD_DESTROY, jfrThreadDestroy, OMR_GET_CALLSITE(), NULL)) {
678-
goto fail;
679-
}
680655
if ((*vmHooks)->J9HookRegisterWithCallSite(vmHooks, J9HOOK_VM_CLASSES_UNLOAD, jfrClassesUnload, OMR_GET_CALLSITE(), NULL)) {
681656
goto fail;
682657
}
@@ -806,7 +781,6 @@ tearDownJFR(J9JavaVM *vm)
806781

807782
/* Unhook all the events */
808783
(*vmHooks)->J9HookUnregister(vmHooks, J9HOOK_VM_THREAD_CREATED, jfrThreadCreated, NULL);
809-
(*vmHooks)->J9HookUnregister(vmHooks, J9HOOK_VM_THREAD_DESTROY, jfrThreadDestroy, NULL);
810784
(*vmHooks)->J9HookUnregister(vmHooks, J9HOOK_VM_CLASSES_UNLOAD, jfrClassesUnload, NULL);
811785
(*vmHooks)->J9HookUnregister(vmHooks, J9HOOK_VM_SHUTTING_DOWN, jfrVMShutdown, NULL);
812786
(*vmHooks)->J9HookUnregister(vmHooks, J9HOOK_VM_THREAD_STARTING, jfrThreadStarting, NULL);

0 commit comments

Comments
 (0)