Skip to content

Commit

Permalink
refactor cpuload
Browse files Browse the repository at this point in the history
Signed-off-by: Gengchen Tuo <[email protected]>
  • Loading branch information
thallium committed Oct 10, 2024
1 parent 94218be commit fa1cdf5
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions runtime/vm/jfr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,20 +753,19 @@ jfrCPULoad(J9VMThread *currentThread)
PORT_ACCESS_FROM_VMC(currentThread);
OMRPORT_ACCESS_FROM_J9PORT(PORTLIB);

J9JFRCPULoad *jfrEvent = (J9JFRCPULoad *)reserveBuffer(currentThread, sizeof(J9JFRCPULoad));
if (NULL != jfrEvent) {
initializeEventFields(currentThread, (J9JFREvent *)jfrEvent, J9JFR_EVENT_TYPE_CPU_LOAD);
UDATA numberOfCpus = j9sysinfo_get_number_CPUs_by_type(J9PORT_CPU_ONLINE);

omrthread_process_time_t currentProcCPUTime = {0};
intptr_t rc = omrthread_get_process_times(&currentProcCPUTime);

UDATA success = 0;
U_64 currentTime = (U_64)j9time_current_time_nanos(&success);
if (rc < 0 || 0 == success) {
jfrEvent->jvmUser = -1;
jfrEvent->jvmSystem = -1;
} else {
omrthread_process_time_t currentProcCPUTime = {0};
intptr_t processTimeRC = omrthread_get_process_times(&currentProcCPUTime);

J9SysinfoCPUTime currentSysCPUTime = {0};
intptr_t sysTimeRC = omrsysinfo_get_CPU_utilization(&currentSysCPUTime);

if (0 == processTimeRC && 0 == sysTimeRC) {
J9JFRCPULoad *jfrEvent = (J9JFRCPULoad *)reserveBuffer(currentThread, sizeof(J9JFRCPULoad));
if (NULL != jfrEvent) {
initializeEventFields(currentThread, (J9JFREvent *)jfrEvent, J9JFR_EVENT_TYPE_CPU_LOAD);
UDATA numberOfCpus = j9sysinfo_get_number_CPUs_by_type(J9PORT_CPU_ONLINE);
U_64 currentTime = (U_64)j9time_nano_time();

omrthread_process_time_t *prevProcCPUTime = &currentThread->javaVM->jfrState.prevProcCPUTime;
I_64 *prevProcTimestamp = &currentThread->javaVM->jfrState.prevProcTimestamp;
if (-1 == *prevProcTimestamp) {
Expand All @@ -779,13 +778,7 @@ jfrCPULoad(J9VMThread *currentThread)
}
*prevProcCPUTime = currentProcCPUTime;
*prevProcTimestamp = currentTime;
}

J9SysinfoCPUTime currentSysCPUTime = {0};
rc = omrsysinfo_get_CPU_utilization(&currentSysCPUTime);
if (rc < 0) {
jfrEvent->machineTotal = rc;
} else {
J9SysinfoCPUTime *prevSysCPUTime = &currentThread->javaVM->jfrState.prevSysCPUTime;
if (-1 == prevSysCPUTime->timestamp) {
jfrEvent->machineTotal = 0;
Expand All @@ -795,6 +788,7 @@ jfrCPULoad(J9VMThread *currentThread)
*prevSysCPUTime = currentSysCPUTime;
}
}

}

void
Expand All @@ -807,12 +801,10 @@ jfrThreadCPULoad(J9VMThread *currentThread, J9VMThread *sampleThread)

int32_t rc = omrthread_get_self_thread_time(&threadTime);

UDATA success = 0;
U_64 currentTime = (U_64)j9time_current_time_nanos(&success);

if (success && -1 != rc) {
if (-1 != rc) {
I_64 sysTime = (I_64)threadTime.sysTime;
I_64 userTime = (I_64)threadTime.userTime;
U_64 currentTime = (U_64)j9time_nano_time();

J9JFRThreadCPULoad *jfrEvent = (J9JFRThreadCPULoad*)reserveBuffer(currentThread, sizeof(*jfrEvent));
if (NULL != jfrEvent) {
Expand Down

0 comments on commit fa1cdf5

Please sign in to comment.