Skip to content

Commit

Permalink
8326446: The User and System of jdk.CPULoad on Apple M1 are inaccurate
Browse files Browse the repository at this point in the history
Reviewed-by: phh
Backport-of: 8dbd4b391f7deb1a46a5f07f0bc46f23d6715ddb
  • Loading branch information
Long Yang authored and TheRealMDoerr committed May 29, 2024
1 parent 787cf27 commit 2d834b3
Showing 1 changed file with 28 additions and 34 deletions.
62 changes: 28 additions & 34 deletions src/hotspot/os/bsd/os_perf_bsd.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -39,17 +39,18 @@
#include <net/if.h>
#include <net/if_dl.h>
#include <net/route.h>
#include <sys/times.h>
#endif

static const double NANOS_PER_SEC = 1000000000.0;

class CPUPerformanceInterface::CPUPerformance : public CHeapObj<mtInternal> {
friend class CPUPerformanceInterface;
private:
long _total_cpu_nanos;
uint64_t _jvm_real;
long _total_csr_nanos;
long _jvm_user_nanos;
long _jvm_system_nanos;
uint64_t _jvm_user;
uint64_t _jvm_system;
long _jvm_context_switches;
long _used_ticks;
long _total_ticks;
Expand Down Expand Up @@ -83,11 +84,11 @@ class CPUPerformanceInterface::CPUPerformance : public CHeapObj<mtInternal> {
};

CPUPerformanceInterface::CPUPerformance::CPUPerformance() {
_total_cpu_nanos= 0;
_total_csr_nanos= 0;
_jvm_real = 0;
_total_csr_nanos = 0;
_jvm_context_switches = 0;
_jvm_user_nanos = 0;
_jvm_system_nanos = 0;
_jvm_user = 0;
_jvm_system = 0;
_used_ticks = 0;
_total_ticks = 0;
_active_processor_count = 0;
Expand Down Expand Up @@ -148,42 +149,35 @@ int CPUPerformanceInterface::CPUPerformance::cpu_load_total_process(double* cpu_
int CPUPerformanceInterface::CPUPerformance::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) {
#ifdef __APPLE__
int result = cpu_load_total_process(psystemTotalLoad);
mach_port_t task = mach_task_self();
mach_msg_type_number_t task_info_count = TASK_INFO_MAX;
task_info_data_t task_info_data;
kern_return_t kr = task_info(task, TASK_ABSOLUTETIME_INFO, (task_info_t)task_info_data, &task_info_count);
if (kr != KERN_SUCCESS) {

struct tms buf;
clock_t jvm_real = times(&buf);
if (jvm_real == (clock_t) (-1)) {
return OS_ERR;
}
task_absolutetime_info_t absolutetime_info = (task_absolutetime_info_t)task_info_data;

int active_processor_count = os::active_processor_count();
long jvm_user_nanos = absolutetime_info->total_user;
long jvm_system_nanos = absolutetime_info->total_system;

long total_cpu_nanos;
if(!now_in_nanos(&total_cpu_nanos)) {
return OS_ERR;
}
uint64_t jvm_user = buf.tms_utime;
uint64_t jvm_system = buf.tms_stime;

if (_total_cpu_nanos == 0 || active_processor_count != _active_processor_count) {
// First call or change in active processor count
if (active_processor_count != _active_processor_count) {
// Change in active processor count
result = OS_ERR;
}
} else {
uint64_t delta = active_processor_count * (jvm_real - _jvm_real);
if (delta == 0) {
// Avoid division by zero
return OS_ERR;
}

long delta_nanos = active_processor_count * (total_cpu_nanos - _total_cpu_nanos);
if (delta_nanos == 0) {
// Avoid division by zero
return OS_ERR;
*pjvmUserLoad = normalize((double)(jvm_user - _jvm_user) / delta);
*pjvmKernelLoad = normalize((double)(jvm_system - _jvm_system) / delta);
}

*pjvmUserLoad = normalize((double)(jvm_user_nanos - _jvm_user_nanos)/delta_nanos);
*pjvmKernelLoad = normalize((double)(jvm_system_nanos - _jvm_system_nanos)/delta_nanos);

_active_processor_count = active_processor_count;
_total_cpu_nanos = total_cpu_nanos;
_jvm_user_nanos = jvm_user_nanos;
_jvm_system_nanos = jvm_system_nanos;
_jvm_real = jvm_real;
_jvm_user = jvm_user;
_jvm_system = jvm_system;

return result;
#else
Expand Down

1 comment on commit 2d834b3

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.