13
13
14
14
package com .baidu .bifromq .basekv .store .util ;
15
15
16
- import io .micrometer .common .lang .Nullable ;
17
16
import java .lang .management .ManagementFactory ;
18
17
import java .lang .management .OperatingSystemMXBean ;
19
18
import java .lang .reflect .InvocationTargetException ;
22
21
import java .util .List ;
23
22
24
23
public class ProcessUtil {
25
- private static final ProcessCpuLoad CPU_LOAD = new ProcessCpuLoad ();
24
+ private interface IProcessCpuLoad {
25
+ double get ();
26
+ }
27
+
28
+ private static final List <String > OPERATING_SYSTEM_BEAN_CLASS_NAMES = Arrays .asList (
29
+ "com.ibm.lang.management.OperatingSystemMXBean" , // J9
30
+ "com.sun.management.OperatingSystemMXBean" // HotSpot
31
+ );
32
+
33
+ private static final List <String > POSSIBLE_METHODS =
34
+ Arrays .asList ("getProcessCpuLoad" , "getSystemCpuLoad" , "getCpuLoad" );
35
+
36
+ private static final IProcessCpuLoad CPU_LOAD ;
37
+
38
+ static {
39
+ OperatingSystemMXBean operatingSystemBean = ManagementFactory .getOperatingSystemMXBean ();
40
+ Method method = null ;
41
+ for (String className : OPERATING_SYSTEM_BEAN_CLASS_NAMES ) {
42
+ try {
43
+ Class <?> osBeanClass = Class .forName (className );
44
+ // ensure the Bean we have is actually an instance of the interface
45
+ osBeanClass .cast (operatingSystemBean );
46
+ method = findMethod (osBeanClass );
47
+ break ;
48
+ } catch (Throwable ignore ) {
49
+ }
50
+ }
51
+ if (method != null ) {
52
+ IProcessCpuLoad finalMethod = null ;
53
+ try {
54
+ double load = (double ) method .invoke (operatingSystemBean );
55
+ finalMethod = new ProcessCpuLoad (operatingSystemBean , method );
56
+ } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e ) {
57
+ finalMethod = () -> Double .NaN ;
58
+ } finally {
59
+ CPU_LOAD = finalMethod ;
60
+ }
61
+ } else {
62
+ CPU_LOAD = () -> Double .NaN ;
63
+ }
64
+ }
26
65
27
66
public static double cpuLoad () {
28
67
return CPU_LOAD .get ();
@@ -32,51 +71,34 @@ public static String processId() {
32
71
return ManagementFactory .getRuntimeMXBean ().getName ();
33
72
}
34
73
35
- private static class ProcessCpuLoad {
36
- private static final List <String > OPERATING_SYSTEM_BEAN_CLASS_NAMES = Arrays .asList (
37
- "com.ibm.lang.management.OperatingSystemMXBean" , // J9
38
- "com.sun.management.OperatingSystemMXBean" // HotSpot
39
- );
40
-
41
- private static final List <String > POSSIBLE_METHODS =
42
- Arrays .asList ("getProcessCpuLoad" , "getSystemCpuLoad" , "getCpuLoad" );
74
+ private static class ProcessCpuLoad implements IProcessCpuLoad {
43
75
private final OperatingSystemMXBean operatingSystemBean ;
44
76
45
- @ Nullable
46
77
private final Method cpuUsageMethod ;
47
78
48
- ProcessCpuLoad () {
49
- this .operatingSystemBean = ManagementFactory .getOperatingSystemMXBean ();
50
- Method method = null ;
51
- for (String className : OPERATING_SYSTEM_BEAN_CLASS_NAMES ) {
52
- try {
53
- Class <?> osBeanClass = Class .forName (className );
54
- // ensure the Bean we have is actually an instance of the interface
55
- osBeanClass .cast (operatingSystemBean );
56
- method = findMethod (osBeanClass );
57
- break ;
58
- } catch (Throwable ignore ) {
59
- }
60
- }
61
- this .cpuUsageMethod = method ;
79
+ private ProcessCpuLoad (OperatingSystemMXBean operatingSystemBean , Method cpuUsageMethod ) {
80
+ this .operatingSystemBean = operatingSystemBean ;
81
+ this .cpuUsageMethod = cpuUsageMethod ;
62
82
}
63
83
64
- private double get () {
84
+
85
+ public double get () {
65
86
try {
66
- return cpuUsageMethod != null ? (double ) cpuUsageMethod .invoke (operatingSystemBean ) : Double .NaN ;
87
+ double load = (double ) cpuUsageMethod .invoke (operatingSystemBean );
88
+ return load > 1.0 ? load / 100 : load ;
67
89
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e ) {
68
90
return Double .NaN ;
69
91
}
70
92
}
93
+ }
71
94
72
- private Method findMethod (Class <?> clazz ) {
73
- for (String methodName : POSSIBLE_METHODS ) {
74
- try {
75
- return clazz .getMethod (methodName );
76
- } catch (ClassCastException | NoSuchMethodException | SecurityException e ) {
77
- }
95
+ private static Method findMethod (Class <?> clazz ) {
96
+ for (String methodName : POSSIBLE_METHODS ) {
97
+ try {
98
+ return clazz .getMethod (methodName );
99
+ } catch (ClassCastException | NoSuchMethodException | SecurityException e ) {
78
100
}
79
- return null ;
80
101
}
102
+ return null ;
81
103
}
82
104
}
0 commit comments