Skip to content

Commit 52852f7

Browse files
committed
Use async events for JFR execution samples
Signed-off-by: Graham Chapman <graham_chapman@ca.ibm.com>
1 parent c5a42b3 commit 52852f7

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

runtime/oti/j9nonbuilder.h

+1
Original file line numberDiff line numberDiff line change
@@ -6153,6 +6153,7 @@ typedef struct J9JavaVM {
61536153
omrthread_monitor_t jfrSamplerMutex;
61546154
omrthread_t jfrSamplerThread;
61556155
UDATA jfrSamplerState;
6156+
IDATA jfrAsyncKey;
61566157
#endif /* defined(J9VM_OPT_JFR) */
61576158
#if JAVA_SPEC_VERSION >= 22
61586159
omrthread_monitor_t closeScopeMutex;

runtime/vm/jfr.cpp

+20-14
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ extern "C" {
3636
// TODO: allow configureable values
3737
#define J9JFR_THREAD_BUFFER_SIZE (1024*1024)
3838
#define J9JFR_GLOBAL_BUFFER_SIZE (10 * J9JFR_THREAD_BUFFER_SIZE)
39-
#define J9JFR_SAMPLING_RATE 1000
39+
#define J9JFR_SAMPLING_RATE 10
4040

4141
static UDATA jfrEventSize(J9JFREvent *jfrEvent);
4242
static void tearDownJFR(J9JavaVM *vm);
@@ -53,6 +53,7 @@ static void jfrThreadEnd(J9HookInterface **hook, UDATA eventNum, void *eventData
5353
static void jfrVMInitialized(J9HookInterface **hook, UDATA eventNum, void *eventData, void *userData);
5454
static void initializeEventFields(J9VMThread *currentThread, J9JFREvent *jfrEvent, UDATA eventType);
5555
static int J9THREAD_PROC jfrSamplingThreadProc(void *entryArg);
56+
static void jfrExecutionSampleCallback(J9VMThread *currentThread, IDATA handlerKey, void *userData);
5657
static void initializeJFRConstantEvents(J9JavaVM *vm);
5758
static void freeJFRConstantEvents(J9JavaVM *vm);
5859

@@ -556,6 +557,12 @@ initializeJFR(J9JavaVM *vm)
556557
J9HookInterface **vmHooks = getVMHookInterface(vm);
557558
U_8 *buffer = NULL;
558559

560+
/* Register async handler for execution samples */
561+
vm->jfrAsyncKey = J9RegisterAsyncEvent(vm, jfrExecutionSampleCallback, NULL);
562+
if (vm->jfrAsyncKey < 0) {
563+
goto fail;
564+
}
565+
559566
if ((*vmHooks)->J9HookRegisterWithCallSite(vmHooks, J9HOOK_VM_THREAD_CREATED, jfrThreadCreated, OMR_GET_CALLSITE(), NULL)) {
560567
goto fail;
561568
}
@@ -668,6 +675,11 @@ tearDownJFR(J9JavaVM *vm)
668675
j9mem_free_memory(vm->jfrState.metaDataBlobFile);
669676
vm->jfrState.metaDataBlobFile = NULL;
670677
vm->jfrState.metaDataBlobFileSize = 0;
678+
if (vm->jfrAsyncKey >= 0) {
679+
J9UnregisterAsyncEvent(vm, vm->jfrAsyncKey);
680+
vm->jfrAsyncKey = -1;
681+
}
682+
671683
}
672684

673685
/**
@@ -732,6 +744,12 @@ jfrExecutionSample(J9VMThread *currentThread, J9VMThread *sampleThread)
732744
}
733745
}
734746

747+
static void
748+
jfrExecutionSampleCallback(J9VMThread *currentThread, IDATA handlerKey, void *userData)
749+
{
750+
jfrExecutionSample(currentThread, currentThread);
751+
}
752+
735753
static int J9THREAD_PROC
736754
jfrSamplingThreadProc(void *entryArg)
737755
{
@@ -743,19 +761,7 @@ jfrSamplingThreadProc(void *entryArg)
743761
vm->jfrSamplerState = J9JFR_SAMPLER_STATE_RUNNING;
744762
omrthread_monitor_notify_all(vm->jfrSamplerMutex);
745763
while (J9JFR_SAMPLER_STATE_STOP != vm->jfrSamplerState) {
746-
omrthread_monitor_exit(vm->jfrSamplerMutex);
747-
internalEnterVMFromJNI(currentThread);
748-
acquireExclusiveVMAccess(currentThread);
749-
J9VMThread *walkThread = J9_LINKED_LIST_START_DO(vm->mainThread);
750-
while (NULL != walkThread) {
751-
if (VM_VMHelpers::threadCanRunJavaCode(walkThread)) {
752-
jfrExecutionSample(currentThread, walkThread);
753-
}
754-
walkThread = J9_LINKED_LIST_NEXT_DO(vm->mainThread, walkThread);
755-
}
756-
releaseExclusiveVMAccess(currentThread);
757-
internalExitVMToJNI(currentThread);
758-
omrthread_monitor_enter(vm->jfrSamplerMutex);
764+
J9SignalAsyncEvent(vm, NULL, vm->jfrAsyncKey);
759765
omrthread_monitor_wait_timed(vm->jfrSamplerMutex, J9JFR_SAMPLING_RATE, 0);
760766
}
761767
omrthread_monitor_exit(vm->jfrSamplerMutex);

0 commit comments

Comments
 (0)