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 java.lang.management.ManagementFactory;
24  import java.lang.management.OperatingSystemMXBean;
25  
26  /**
27   * @author bregier
28   * 
29   */
30  public class CpuManagement implements CpuManagementInterface {
31      OperatingSystemMXBean osBean;
32  
33      /**
34       * 
35       * @throws UnsupportedOperationException
36       *             if System Load Average is not supported
37       */
38      public CpuManagement() throws UnsupportedOperationException {
39          osBean = ManagementFactory.getOperatingSystemMXBean();
40          if (osBean.getSystemLoadAverage() < 0) {
41              osBean = null;
42              throw new UnsupportedOperationException(
43                      "System Load Average not supported");
44          }
45      }
46  
47      /**
48       * 
49       * @return the load average
50       */
51      public double getLoadAverage() {
52          return osBean.getSystemLoadAverage();
53      }
54  
55      public static void main(String[] args) {
56          long total = 0;
57          CpuManagement cpuManagement = new CpuManagement();
58          System.err.println("LA: " + cpuManagement.getLoadAverage());
59          for (int i = 0; i < 1000 * 1000 * 1000; i ++) {
60              // keep ourselves busy for a while ...
61              // note: we had to add some "work" into the loop or Java 6
62              // optimizes it away. Thanks to Daniel Einspanjer for
63              // pointing that out.
64              total += i;
65              total *= 10;
66          }
67          System.err.println("LA: " + cpuManagement.getLoadAverage());
68  
69          total = 0;
70          for (int i = 0; i < 1000 * 1000 * 1000; i ++) {
71              // keep ourselves busy for a while ...
72              // note: we had to add some "work" into the loop or Java 6
73              // optimizes it away. Thanks to Daniel Einspanjer for
74              // pointing that out.
75              total += i;
76              total *= 10;
77          }
78          System.err.println("LA: " + cpuManagement.getLoadAverage());
79          try {
80              Thread.sleep(10000);
81          } catch (InterruptedException e) {
82          }
83          System.err.println("LA: " + cpuManagement.getLoadAverage());
84  
85          total = 0;
86          for (int i = 0; i < 1000 * 1000 * 1000; i ++) {
87              // keep ourselves busy for a while ...
88              // note: we had to add some "work" into the loop or Java 6
89              // optimizes it away. Thanks to Daniel Einspanjer for
90              // pointing that out.
91              total += i;
92              total *= 10;
93          }
94          System.err.println("LA: " + cpuManagement.getLoadAverage());
95      }
96  }