1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package goldengate.common.cpu;
22
23 import com.jezhumble.javasysmon.CpuTimes;
24 import com.jezhumble.javasysmon.JavaSysMon;
25
26
27
28
29
30 public class CpuManagementSysmon implements CpuManagementInterface {
31 public static long delay = 1000;
32
33 JavaSysMon sysMon;
34
35 CpuTimes cpuTimesOld;
36 CpuTimes cpuTimesOldNext;
37 long time;
38
39
40
41
42
43
44 public CpuManagementSysmon() throws UnsupportedOperationException {
45 sysMon = new JavaSysMon();
46 cpuTimesOld = sysMon.cpuTimes();
47 cpuTimesOldNext = cpuTimesOld;
48 time = System.currentTimeMillis();
49 }
50
51
52
53
54
55 public double getLoadAverage() {
56 long newTime = System.currentTimeMillis();
57 CpuTimes cpuTimes = sysMon.cpuTimes();
58 double rate = cpuTimes.getCpuUsage(cpuTimesOld);
59 long delta = newTime - time;
60 if ((delta) > delay) {
61 if ((delta) > 10*delay) {
62 time = newTime;
63 cpuTimesOldNext = cpuTimes;
64 cpuTimesOld = cpuTimes;
65 } else {
66 time = newTime;
67 cpuTimesOldNext = cpuTimes;
68 cpuTimesOld = cpuTimesOldNext;
69 }
70 }
71 return rate;
72 }
73
74 public static void main(String[] args) {
75 long total = 0;
76 CpuManagementSysmon cpuManagement = new CpuManagementSysmon();
77 System.err.println("LA: " + cpuManagement.getLoadAverage());
78 for (int i = 0; i < 1000 * 1000 * 1000; i ++) {
79
80
81
82
83 total += i;
84 total *= 10;
85 }
86 System.err.println("LA: " + cpuManagement.getLoadAverage());
87
88 total = 0;
89 for (int i = 0; i < 1000 * 1000 * 1000; i ++) {
90
91
92
93
94 total += i;
95 total *= 10;
96 }
97 System.err.println("LA: " + cpuManagement.getLoadAverage());
98 try {
99 Thread.sleep(10000);
100 } catch (InterruptedException e) {
101 }
102 System.err.println("LA: " + cpuManagement.getLoadAverage());
103
104 total = 0;
105 for (int i = 0; i < 1000 * 1000 * 1000; i ++) {
106
107
108
109
110 total += i;
111 total *= 10;
112 }
113 System.err.println("LA: " + cpuManagement.getLoadAverage());
114 }
115 }