View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package goldengate.common.cpu;
22  
23  import com.jezhumble.javasysmon.CpuTimes;
24  import com.jezhumble.javasysmon.JavaSysMon;
25  
26  /**
27   * @author bregier
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       * @throws UnsupportedOperationException
42       *             if System Load Average is not supported
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       * @return the load average
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              // keep ourselves busy for a while ...
80              // note: we had to add some "work" into the loop or Java 6
81              // optimizes it away. Thanks to Daniel Einspanjer for
82              // pointing that out.
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              // keep ourselves busy for a while ...
91              // note: we had to add some "work" into the loop or Java 6
92              // optimizes it away. Thanks to Daniel Einspanjer for
93              // pointing that out.
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             // keep ourselves busy for a while ...
107             // note: we had to add some "work" into the loop or Java 6
108             // optimizes it away. Thanks to Daniel Einspanjer for
109             // pointing that out.
110             total += i;
111             total *= 10;
112         }
113         System.err.println("LA: " + cpuManagement.getLoadAverage());
114     }
115 }