-
Notifications
You must be signed in to change notification settings - Fork 6.1k
JDK-8030957 - AIX: Implement OperatingSystemMXBean.getSystemCpuLoad() and .getProcessCpuLoad() on AIX #25332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
d7834c6
d5d9f02
19513b7
10e9471
4651787
2207f10
f2ab9d5
80a6dba
e4c076f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved. | ||
* Copyright (c) 2015, 2020 SAP SE. All rights reserved. | ||
* Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved. | ||
* Copyright (c) 2015, 2025 SAP SE. 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 | ||
|
@@ -28,20 +28,92 @@ | |
/* Implement and update https://bugs.openjdk.org/browse/JDK-8030957 */ | ||
|
||
#include <jni.h> | ||
#include <time.h> | ||
#include <stdlib.h> | ||
#include <libperfstat.h> | ||
#include "com_sun_management_internal_OperatingSystemImpl.h" | ||
perfstat_process_t prev_stats = {0}; | ||
static unsigned long long prev_timebase = 0; | ||
static int initialized = 0; | ||
|
||
#define HTIC2SEC(x) (((double)(x) * XINTFRAC) / 1000000000.0) | ||
|
||
static perfstat_cpu_total_t cpu_total_old; | ||
static time_t last_sample_time = 0; | ||
static double last_cpu_load = -1.0; | ||
JNIEXPORT jdouble JNICALL | ||
Java_com_sun_management_internal_OperatingSystemImpl_getCpuLoad0 | ||
(JNIEnv *env, jobject dummy) | ||
{ | ||
return -1.0; | ||
perfstat_cpu_total_t cpu_total; | ||
int ret; | ||
|
||
time_t now = time(NULL); | ||
if (initialized && (now - last_sample_time < 5)) { | ||
return last_cpu_load; // Return cached value if less than 5s | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure this is needed? How slow is perfstat_cpu_total? |
||
|
||
ret = perfstat_cpu_total(NULL, &cpu_total, sizeof(perfstat_cpu_total_t), 1); | ||
if (ret < 0) { | ||
return -1.0; | ||
} | ||
|
||
if (!initialized) { | ||
cpu_total_old = cpu_total; | ||
initialized = 1; | ||
last_sample_time = now; | ||
return -1.0; | ||
} | ||
|
||
long long user_diff = cpu_total.user - cpu_total_old.user; | ||
long long sys_diff = cpu_total.sys - cpu_total_old.sys; | ||
long long idle_diff = cpu_total.idle - cpu_total_old.idle; | ||
long long wait_diff = cpu_total.wait - cpu_total_old.wait; | ||
long long total = user_diff + sys_diff + idle_diff + wait_diff; | ||
|
||
if (total == 0) { | ||
return -1.0; | ||
} | ||
|
||
double load = (double)(user_diff + sys_diff) / total; | ||
last_cpu_load = load; | ||
last_sample_time = now; | ||
cpu_total_old = cpu_total; | ||
|
||
return load; | ||
} | ||
|
||
JNIEXPORT jdouble JNICALL | ||
Java_com_sun_management_internal_OperatingSystemImpl_getProcessCpuLoad0 | ||
(JNIEnv *env, jobject dummy) | ||
{ | ||
return -1.0; | ||
perfstat_process_t curr_stats; | ||
perfstat_id_t id; | ||
unsigned long long curr_timebase, timebase_diff; | ||
double user_diff, sys_diff, delta_time; | ||
|
||
if (perfstat_process(&id, &curr_stats, sizeof(perfstat_process_t), 1) < 0) { | ||
return -1.0; | ||
} | ||
if (!initialized) { | ||
prev_stats = curr_stats; | ||
prev_timebase = curr_stats.last_timebase; | ||
initialized = 1; | ||
return -1.0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, and above in the other function: this seems wrong and would result in the first measurement to be off (or very large if displayed as unsigned). See how Linux does it in perfInit(). |
||
} | ||
curr_timebase = curr_stats.last_timebase; | ||
timebase_diff = curr_timebase - prev_timebase; | ||
if ((long long)timebase_diff <= 0 || XINTFRAC == 0) { | ||
return -1.0; | ||
} | ||
|
||
delta_time = HTIC2SEC(timebase_diff); | ||
user_diff = (double)(curr_stats.ucpu_time - prev_stats.ucpu_time); | ||
sys_diff = (double)(curr_stats.scpu_time - prev_stats.scpu_time); | ||
prev_stats = curr_stats; | ||
prev_timebase = curr_timebase; | ||
double cpu_load = (user_diff + sys_diff) / delta_time; | ||
return (jdouble)cpu_load; | ||
} | ||
|
||
JNIEXPORT jdouble JNICALL | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not threadsafe. What happens if you call this concurrently from multiple threads? Visibility of these three values can be in any order of updates. You'd get skewed values.
Check out how Linux does it. They use mutexes to synchronize access to this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nit, you may want to group these together into a nice structure, too.