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,93 @@
/* 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;
}

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;
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;
}
if (!initialized) {
prev_stats = curr_stats;
prev_timebase = curr_stats.last_timebase;
initialized = 1;
return -1.0;
}
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
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