1
1
/*
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.
4
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
5
*
6
6
* This code is free software; you can redistribute it and/or modify it
28
28
/* Implement and update https://bugs.openjdk.org/browse/JDK-8030957 */
29
29
30
30
#include <jni.h>
31
+ #include <time.h>
31
32
#include <stdlib.h>
32
33
#include <libperfstat.h>
33
34
#include "com_sun_management_internal_OperatingSystemImpl.h"
34
35
perfstat_process_t prev_stats = {0 };
35
36
static unsigned long long prev_timebase = 0 ;
36
37
static int initialized = 0 ;
37
-
38
- #define HTIC2SEC (x ) (((double)(x) * XINTFRAC) / 1000000000.0)
39
38
39
+ #define HTIC2SEC (x ) (((double)(x) * XINTFRAC) / 1000000000.0)
40
40
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 ;
41
44
JNIEXPORT jdouble JNICALL
42
45
Java_com_sun_management_internal_OperatingSystemImpl_getCpuLoad0
43
46
(JNIEnv * env , jobject dummy )
44
47
{
45
- return -1.0 ;
46
- }
48
+ perfstat_cpu_total_t cpu_total ;
49
+ int ret ;
47
50
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
+ }
48
55
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
+ }
49
93
50
94
JNIEXPORT jdouble JNICALL
51
95
Java_com_sun_management_internal_OperatingSystemImpl_getProcessCpuLoad0
@@ -55,13 +99,11 @@ Java_com_sun_management_internal_OperatingSystemImpl_getProcessCpuLoad0
55
99
perfstat_id_t id ;
56
100
unsigned long long curr_timebase , timebase_diff ;
57
101
double user_diff , sys_diff , delta_time ;
58
-
59
102
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 ;
62
105
}
63
106
if (!initialized ) {
64
- // First call: just store and return -1.0
65
107
prev_stats = curr_stats ;
66
108
prev_timebase = curr_stats .last_timebase ;
67
109
initialized = 1 ;
0 commit comments