8030957: AIX: Implement OperatingSystemMXBean.getSystemCpuLoad() and .getProcessCpuLoad() on AIX

Reviewed-by: jkern, mdoerr, mbaesken
This commit is contained in:
Suchismith Roy 2025-10-08 09:15:23 +00:00 committed by Varada M
parent f58e17fd27
commit d45e65bab4
2 changed files with 95 additions and 7 deletions

View File

@ -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,111 @@
/* Implement and update https://bugs.openjdk.org/browse/JDK-8030957 */
#include <jni.h>
#include <libperfstat.h>
#include <pthread.h>
#include <stdlib.h>
#include <time.h>
#include "com_sun_management_internal_OperatingSystemImpl.h"
static struct perfMetrics{
unsigned long long timebase;
perfstat_process_t stats;
perfstat_cpu_total_t cpu_total;
} counters;
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
int perfInit() {
static int initialized = 0;
if (!initialized) {
perfstat_id_t id;
counters.stats = (perfstat_process_t){0};
counters.timebase = 0;
int rc = perfstat_cpu_total(NULL, &counters.cpu_total, sizeof(perfstat_cpu_total_t), 1);
if (rc < 0) {
return -1;
}
rc = perfstat_process(&id, &counters.stats, sizeof(perfstat_process_t), 1);
if (rc < 0) {
return -1;
}
counters.timebase = counters.stats.last_timebase;
initialized = 1;
}
return initialized ? 0 : -1;
}
JNIEXPORT jdouble JNICALL
Java_com_sun_management_internal_OperatingSystemImpl_getCpuLoad0
(JNIEnv *env, jobject dummy)
{
return -1.0;
double load = -1.0;
pthread_mutex_lock(&lock);
if (perfInit() == 0) {
int ret;
perfstat_cpu_total_t cpu_total;
ret = perfstat_cpu_total(NULL, &cpu_total, sizeof(perfstat_cpu_total_t), 1);
if (ret < 0) {
return -1.0;
}
long long user_diff = cpu_total.user - counters.cpu_total.user;
long long sys_diff = cpu_total.sys - counters.cpu_total.sys;
long long idle_diff = cpu_total.idle - counters.cpu_total.idle;
long long wait_diff = cpu_total.wait - counters.cpu_total.wait;
long long total = user_diff + sys_diff + idle_diff + wait_diff;
if (total < (user_diff + sys_diff)) {
total = user_diff + sys_diff;
}
if (total == 0) {
load = 0.0;
} else {
load = (double)(user_diff + sys_diff) / total;
load = MAX(load, 0.0);
load = MIN(load, 1.0);
}
counters.cpu_total = cpu_total;
}
pthread_mutex_unlock(&lock);
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;
double cpu_load = -1.0;
pthread_mutex_lock(&lock);
if (perfInit() == 0) {
int ret;
ret = perfstat_process(&id, &curr_stats, sizeof(perfstat_process_t), 1);
if (ret < 0) {
return -1.0;
}
curr_timebase = curr_stats.last_timebase;
timebase_diff = curr_timebase - counters.timebase;
if ((long long)timebase_diff < 0 || XINTFRAC == 0) {
return -1.0;
}
delta_time = HTIC2NANOSEC(timebase_diff) / 1000000000.0;
user_diff = (double)(curr_stats.ucpu_time - counters.stats.ucpu_time);
sys_diff = (double)(curr_stats.scpu_time - counters.stats.scpu_time);
counters.stats = curr_stats;
counters.timebase = curr_timebase;
if (delta_time == 0) {
cpu_load = 0.0;
} else {
cpu_load = (user_diff + sys_diff) / delta_time;
cpu_load = MAX(cpu_load, 0.0);
cpu_load = MIN(cpu_load, 1.0);
}
}
pthread_mutex_unlock(&lock);
return (jdouble)cpu_load;
}
JNIEXPORT jdouble JNICALL

View File

@ -547,8 +547,6 @@ java/lang/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/ThreadMXBean/ThreadMXBeanStateTest.java 8247426 generic-all
@ -562,7 +560,6 @@ sun/management/jdp/JdpOffTest.java 8308807 aix-ppc6
# jdk_jmx
javax/management/MBeanServer/OldMBeanServerTest.java 8030957 aix-all
javax/management/remote/mandatory/connection/BrokenConnectionTest.java 8262312 linux-all