Skip to content

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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Down Expand Up @@ -28,20 +28,109 @@
/* 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
}

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; // Not enough data yet
}

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;
}

printf("User diff: %lld\n", user_diff);
printf("Sys diff: %lld\n", sys_diff);
printf("Idle diff: %lld\n", idle_diff);
printf("Wait diff: %lld\n", wait_diff);


double load = (double)(user_diff + sys_diff) / total;
printf("load:%.4f\n",load);

Check failure on line 85 in src/jdk.management/aix/native/libmanagement_ext/UnixOperatingSystem.c

View check run for this annotation

openjdk / jcheck-openjdk/jdk-25332

Whitespace error

Column 31: trailing whitespace Column 32: trailing whitespace Column 33: trailing whitespace Column 34: trailing whitespace
fflush(stdout);
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;

Check failure on line 104 in src/jdk.management/aix/native/libmanagement_ext/UnixOperatingSystem.c

View check run for this annotation

openjdk / jcheck-openjdk/jdk-25332

Whitespace error

Column 20: trailing whitespace Column 21: trailing whitespace
}
if (!initialized) {
prev_stats = curr_stats;
prev_timebase = curr_stats.last_timebase;
initialized = 1;
return -1.0;
}
printf("initialised done");
curr_timebase = curr_stats.last_timebase;
timebase_diff = curr_timebase - prev_timebase;

Check failure on line 115 in src/jdk.management/aix/native/libmanagement_ext/UnixOperatingSystem.c

View check run for this annotation

openjdk / jcheck-openjdk/jdk-25332

Whitespace error

Column 0: trailing whitespace Column 1: trailing whitespace Column 2: trailing whitespace Column 3: trailing whitespace
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
Expand Down
2 changes: 0 additions & 2 deletions test/jdk/ProblemList.txt
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,6 @@ java/io/IO/IO.java 8337935 linux-pp

# jdk_management

com/sun/management/OperatingSystemMXBean/GetProcessCpuLoad.java 8030957 aix-all
com/sun/management/OperatingSystemMXBean/GetSystemCpuLoad.java 8030957 aix-all

java/lang/management/MemoryMXBean/Pending.java 8158837 generic-all
java/lang/management/MemoryMXBean/PendingAllGC.sh 8158837 generic-all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,28 @@

Exception ex = null;
int good = 0;

int maxRetries = 12;
int retries = 0;
for (int i = 0; i < TEST_COUNT; i++) {
double load = mbean.getProcessCpuLoad();
if (load == -1.0 && Platform.isWindows()) {
System.out.printf("Load is %.10f at iteration %d%n", load, i);

Check failure on line 51 in test/jdk/com/sun/management/OperatingSystemMXBean/GetProcessCpuLoad.java

View check run for this annotation

openjdk / jcheck-openjdk/jdk-25332

Whitespace error

Column 0: trailing whitespace Column 1: trailing whitespace Column 2: trailing whitespace Column 3: trailing whitespace
for (int j = 0; j < 10000000; j++) {

Check failure on line 52 in test/jdk/com/sun/management/OperatingSystemMXBean/GetProcessCpuLoad.java

View check run for this annotation

openjdk / jcheck-openjdk/jdk-25332

Whitespace error

Column 0: tab
Math.sin(j); // Force CPU usage
}

Check failure on line 54 in test/jdk/com/sun/management/OperatingSystemMXBean/GetProcessCpuLoad.java

View check run for this annotation

openjdk / jcheck-openjdk/jdk-25332

Whitespace error

Column 5: trailing whitespace Column 6: trailing whitespace
if (load == -1.0 && !Platform.isWindows()) {

Check failure on line 55 in test/jdk/com/sun/management/OperatingSystemMXBean/GetProcessCpuLoad.java

View check run for this annotation

openjdk / jcheck-openjdk/jdk-25332

Whitespace error

Column 0: tab
while (load == -1.0 && retries < maxRetries) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
load = mbean.getProcessCpuLoad();
System.out.println("Load recalculated is "+load);

Check failure on line 63 in test/jdk/com/sun/management/OperatingSystemMXBean/GetProcessCpuLoad.java

View check run for this annotation

openjdk / jcheck-openjdk/jdk-25332

Whitespace error

Column 65: trailing whitespace
retries++;

Check failure on line 64 in test/jdk/com/sun/management/OperatingSystemMXBean/GetProcessCpuLoad.java

View check run for this annotation

openjdk / jcheck-openjdk/jdk-25332

Whitespace error

Column 0: tab Column 1: tab
}

Check failure on line 65 in test/jdk/com/sun/management/OperatingSystemMXBean/GetProcessCpuLoad.java

View check run for this annotation

openjdk / jcheck-openjdk/jdk-25332

Whitespace error

Column 0: tab Column 1: tab
}
else if (load == -1.0 && Platform.isWindows()) {
// Some Windows systems can return -1 occasionally, at any time.
// Will fail if we never see good values.
ex = new RuntimeException("getProcessCpuLoad() returns " + load
Expand All @@ -56,6 +74,7 @@
+ " which is not in the [0.0,1.0] interval");
} else {
// A good reading: forget any previous -1.
System.out.println("load calculated is "+load);
ex = null;
good++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,19 @@

for (int i = 0; i < 10; i++) {
load = mbean.getSystemCpuLoad();
if (load == -1.0 && Platform.isWindows()) {
System.out.printf("System load isi %.15f T IEERATION %d%n",load,i);
System.out.printf("Time: %d - System load is %.25f ITERATION %d%n", System.currentTimeMillis(), load, i);

Check failure on line 48 in test/jdk/com/sun/management/OperatingSystemMXBean/GetSystemCpuLoad.java

View check run for this annotation

openjdk / jcheck-openjdk/jdk-25332

Whitespace error

Column 0: trailing whitespace
if (load == -1.0 && !Platform.isWindows()){

Check failure on line 49 in test/jdk/com/sun/management/OperatingSystemMXBean/GetSystemCpuLoad.java

View check run for this annotation

openjdk / jcheck-openjdk/jdk-25332

Whitespace error

Column 0: tab Column 1: tab
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
load = mbean.getSystemCpuLoad();

Check failure on line 55 in test/jdk/com/sun/management/OperatingSystemMXBean/GetSystemCpuLoad.java

View check run for this annotation

openjdk / jcheck-openjdk/jdk-25332

Whitespace error

Column 48: trailing whitespace

}

Check failure on line 57 in test/jdk/com/sun/management/OperatingSystemMXBean/GetSystemCpuLoad.java

View check run for this annotation

openjdk / jcheck-openjdk/jdk-25332

Whitespace error

Column 13: trailing whitespace Column 14: trailing whitespace Column 15: trailing whitespace Column 16: trailing whitespace
else if (load == -1.0 && Platform.isWindows()) {
// Some Windows 2019 systems can return -1 for the first few reads.
// Remember a -1 in case it never gets better.
ex = new RuntimeException("getSystemCpuLoad() returns " + load
Expand All @@ -57,7 +69,7 @@
ex = null;
}
try {
Thread.sleep(200);
Thread.sleep(1000);
} catch(InterruptedException e) {
e.printStackTrace();
}
Expand Down