Skip to content

Commit 459727c

Browse files
system cpu load
1 parent d5d9f02 commit 459727c

File tree

2 files changed

+52
-80
lines changed

2 files changed

+52
-80
lines changed

src/jdk.management/aix/native/libmanagement_ext/UnixOperatingSystem.c

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
3-
* Copyright (c) 2015, 2020 SAP SE. All rights reserved.
2+
* Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2015, 2025 SAP SE. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
66
* This code is free software; you can redistribute it and/or modify it
@@ -28,24 +28,68 @@
2828
/* Implement and update https://bugs.openjdk.org/browse/JDK-8030957 */
2929

3030
#include <jni.h>
31+
#include <time.h>
3132
#include <stdlib.h>
3233
#include <libperfstat.h>
3334
#include "com_sun_management_internal_OperatingSystemImpl.h"
3435
perfstat_process_t prev_stats = {0};
3536
static unsigned long long prev_timebase = 0;
3637
static int initialized = 0;
37-
38-
#define HTIC2SEC(x) (((double)(x) * XINTFRAC) / 1000000000.0)
3938

39+
#define HTIC2SEC(x) (((double)(x) * XINTFRAC) / 1000000000.0)
4040

41+
static perfstat_cpu_total_t cpu_total_old;
42+
static time_t last_sample_time = 0;
43+
static double last_cpu_load = -1.0;
4144
JNIEXPORT jdouble JNICALL
4245
Java_com_sun_management_internal_OperatingSystemImpl_getCpuLoad0
4346
(JNIEnv *env, jobject dummy)
4447
{
45-
return -1.0;
46-
}
48+
perfstat_cpu_total_t cpu_total;
49+
int ret;
4750

51+
time_t now = time(NULL);
52+
if (initialized && (now - last_sample_time < 5)) {
53+
return last_cpu_load; // Return cached value if less than 5s
54+
}
4855

56+
ret = perfstat_cpu_total(NULL, &cpu_total, sizeof(perfstat_cpu_total_t), 1);
57+
if (ret < 0) {
58+
return -1.0;
59+
}
60+
61+
if (!initialized) {
62+
cpu_total_old = cpu_total;
63+
initialized = 1;
64+
last_sample_time = now;
65+
return -1.0; // Not enough data yet
66+
}
67+
68+
long long user_diff = cpu_total.user - cpu_total_old.user;
69+
long long sys_diff = cpu_total.sys - cpu_total_old.sys;
70+
long long idle_diff = cpu_total.idle - cpu_total_old.idle;
71+
long long wait_diff = cpu_total.wait - cpu_total_old.wait;
72+
long long total = user_diff + sys_diff + idle_diff + wait_diff;
73+
74+
if (total == 0) {
75+
return -1.0;
76+
}
77+
78+
printf("User diff: %lld\n", user_diff);
79+
printf("Sys diff: %lld\n", sys_diff);
80+
printf("Idle diff: %lld\n", idle_diff);
81+
printf("Wait diff: %lld\n", wait_diff);
82+
83+
84+
double load = (double)(user_diff + sys_diff) / total;
85+
printf("load:%.4f\n",load);
86+
fflush(stdout);
87+
last_cpu_load = load;
88+
last_sample_time = now;
89+
cpu_total_old = cpu_total;
90+
91+
return load;
92+
}
4993

5094
JNIEXPORT jdouble JNICALL
5195
Java_com_sun_management_internal_OperatingSystemImpl_getProcessCpuLoad0
@@ -55,13 +99,11 @@ Java_com_sun_management_internal_OperatingSystemImpl_getProcessCpuLoad0
5599
perfstat_id_t id;
56100
unsigned long long curr_timebase, timebase_diff;
57101
double user_diff, sys_diff, delta_time;
58-
59102

60-
if (perfstat_process(&id, &curr_stats, sizeof(perfstat_process_t), 1) == -1) {
61-
return -1.0; // Unable to get stats
103+
if (perfstat_process(&id, &curr_stats, sizeof(perfstat_process_t), 1) < 0) {
104+
return -1.0;
62105
}
63106
if (!initialized) {
64-
// First call: just store and return -1.0
65107
prev_stats = curr_stats;
66108
prev_timebase = curr_stats.last_timebase;
67109
initialized = 1;

test/jdk/com/sun/management/OperatingSystemMXBean/GetSystemCpuLoad.java

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)