View Javadoc

1   /***
2    * Copyright 2009 Martin Vysny.
3    *
4    * This file is part of WebVM.
5    *
6    * WebVM is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * WebVM is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with WebVM.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package sk.baka.webvm.analyzer;
20  
21  import sk.baka.webvm.analyzer.hostos.Memory;
22  import java.lang.management.ManagementFactory;
23  import java.lang.management.MemoryUsage;
24  import java.lang.management.ThreadInfo;
25  import java.lang.management.ThreadMXBean;
26  import sk.baka.webvm.analyzer.hostos.Cpu;
27  import sk.baka.webvm.misc.MgmtUtils;
28  
29  /***
30   * Holds history data for a single time unit.
31   * @author Martin Vysny
32   */
33  public final class HistorySample {
34  
35      /***
36       * Returns GC CPU Usage.
37       * @return average CPU usage of GC for this time slice in percent, 0-100.
38       */
39      public final int gcCpuUsage;
40      /***
41       * The time this sample was taken.
42       */
43      public final long sampleTime;
44      /***
45       * The {@link MgmtUtils#getHeapFromRuntime() heap usage}.
46       */
47      public static final int POOL_HEAP = 0;
48      /***
49       * The {@link MgmtUtils#getNonHeapSummary() non-heap usage}, may be null.
50       */
51      public static final int POOL_NON_HEAP = 1;
52      /***
53       * The {@link HostOS.getPhysicalMemory() host OS physical memory}, may be null.
54       */
55      public static final int POOL_PHYS_MEM = 2;
56      /***
57       * The {@link HostOS.getSwap() swap}, may be null.
58       */
59      public static final int POOL_SWAP = 3;
60      /***
61       * The memory usage list, indexed according to the value of the <code>POOL_*</code> constants.
62       */
63      public final MemoryUsage[] memPoolUsage = new MemoryUsage[4];
64      /***
65       * A thread dump. Does not contain any stacktraces. Never null.
66       */
67      public final ThreadInfo[] threads;
68      /***
69       * Count of classes currently loaded in the VM.
70       */
71      public final int classesLoaded;
72      /***
73       * Current count of daemon threads.
74       */
75      public final int daemonThreadCount;
76      /***
77       * Returns current count of all threads.
78       * @return current count of all threads.
79       */
80      public final int threadCount;
81      /***
82       * Shows the host OS CPU usage. A value of 0..100, 0 when not supported.
83       */
84      public final int cpuUsage;
85      /***
86       * Shows the owner java process CPU usage. A value of 0..100, 0 when not supported.
87       */
88      public final int cpuJavaUsage;
89      /***
90       * Shows the host OS CPU IO usage. A value of 0..100, 0 when not supported.
91       */
92      public final int cpuIOUsage;
93  
94      /***
95       * Creates new history sample bean.
96       * @param gcCpuUsage average CPU usage of GC for this time slice in percent.
97       * @param cpuOSUsage Shows the host OS CPU usage. A value of 0..100, 0 when not supported.
98       * @param cpuJavaUsage Shows the owner java process CPU usage. A value of 0..100, 0 when not supported.
99       * @param cpuIOUsage Shows the host OS CPU IO usage. A value of 0..100, 0 when not supported.
100      */
101     public HistorySample(final int gcCpuUsage, final int cpuOSUsage, final int cpuJavaUsage, final int cpuIOUsage) {
102         this.gcCpuUsage = gcCpuUsage;
103         memPoolUsage[POOL_HEAP] = MgmtUtils.getInMB(MgmtUtils.getHeapFromRuntime());
104         memPoolUsage[POOL_NON_HEAP] = MgmtUtils.getInMB(MgmtUtils.getNonHeapSummary());
105         this.sampleTime = System.currentTimeMillis();
106         final ThreadMXBean tbean = ManagementFactory.getThreadMXBean();
107         threads = tbean.getThreadInfo(tbean.getAllThreadIds());
108         threadCount = tbean.getThreadCount();
109         daemonThreadCount = tbean.getDaemonThreadCount();
110         classesLoaded = ManagementFactory.getClassLoadingMXBean().getLoadedClassCount();
111         memPoolUsage[POOL_PHYS_MEM] = MgmtUtils.getInMB(Memory.getPhysicalMemory());
112         memPoolUsage[POOL_SWAP] = MgmtUtils.getInMB(Memory.getSwap());
113         cpuUsage = cpuOSUsage;
114         this.cpuJavaUsage = cpuJavaUsage;
115         this.cpuIOUsage = cpuIOUsage;
116     }
117 }