Skip to content

Commit b10ba9d

Browse files
authored
Merge pull request #20881 from thallium/jfr-context-switch
JFR ThreadContextSwitchRate fix-ups
2 parents 7fb956b + ef1e555 commit b10ba9d

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

runtime/vm/jfr.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ jint
642642
initializeJFR(J9JavaVM *vm, BOOLEAN lateInit)
643643
{
644644
PORT_ACCESS_FROM_JAVAVM(vm);
645+
OMRPORT_ACCESS_FROM_J9PORT(PORTLIB);
645646
jint rc = JNI_ERR;
646647
J9HookInterface **vmHooks = getVMHookInterface(vm);
647648
U_8 *buffer = NULL;
@@ -723,7 +724,14 @@ initializeJFR(J9JavaVM *vm, BOOLEAN lateInit)
723724

724725
vm->jfrState.prevSysCPUTime.timestamp = -1;
725726
vm->jfrState.prevProcTimestamp = -1;
726-
vm->jfrState.prevContextSwitchTimestamp = -1;
727+
728+
if (0 == omrsysinfo_get_number_context_switches(&vm->jfrState.prevContextSwitches)) {
729+
vm->jfrState.prevContextSwitchTimestamp = j9time_nano_time();
730+
} else {
731+
vm->jfrState.prevContextSwitchTimestamp = -1;
732+
vm->jfrState.prevContextSwitches = 0;
733+
}
734+
727735
if (omrthread_monitor_init_with_name(&vm->jfrBufferMutex, 0, "JFR global buffer mutex")) {
728736
goto fail;
729737
}
@@ -1017,11 +1025,13 @@ jfrThreadContextSwitchRate(J9VMThread *currentThread)
10171025

10181026
initializeEventFields(currentThread, (J9JFREvent *)jfrEvent, J9JFR_EVENT_TYPE_THREAD_CONTEXT_SWITCH_RATE);
10191027

1020-
if (-1 == jfrState->prevContextSwitchTimestamp) {
1021-
jfrEvent->switchRate = 0;
1028+
if ((-1 == jfrState->prevContextSwitchTimestamp)
1029+
|| (currentTime == jfrState->prevContextSwitchTimestamp)
1030+
) {
1031+
jfrEvent->switchRate = 0.0f;
10221032
} else {
10231033
int64_t timeDelta = currentTime - jfrState->prevContextSwitchTimestamp;
1024-
jfrEvent->switchRate = ((double)(switches - jfrState->prevContextSwitches) / timeDelta) * 1e9;
1034+
jfrEvent->switchRate = (switches - jfrState->prevContextSwitches) * 1e9f / timeDelta;
10251035
}
10261036
jfrState->prevContextSwitches = switches;
10271037
jfrState->prevContextSwitchTimestamp = currentTime;

0 commit comments

Comments
 (0)