KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > Footprint


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307, USA
18  */

19
20 package edu.umd.cs.findbugs;
21
22 import java.util.List JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.management.ManagementFactory JavaDoc;
25 import java.lang.management.MemoryPoolMXBean JavaDoc;
26 import java.lang.management.GarbageCollectorMXBean JavaDoc;
27
28 /**
29 * Class to maintain a snapshot of a processes's time and memory usage.
30 *
31 * This uses some JDK 1.5 APIs so must be careful that it doesn't cause
32 * any harm when run from 1.4.
33 *
34 * @see FindBugs
35 * @author Brian Cole
36 */

37 public class Footprint {
38
39     private long cpuTime = -1; // in nanoseconds
40
private long clockTime = -1; // in milliseconds
41
private long peakMem = -1; // in bytes
42
private long collectionTime = -1; // in milliseconds
43

44     public Footprint() {
45         pullData();
46     }
47
48     /** uses deltas from base for cpuTime and clockTime (but not peakMemory) */
49     public Footprint(Footprint base) {
50         pullData();
51         if (cpuTime >= 0) {
52             cpuTime = (base.cpuTime >= 0) ? cpuTime-base.cpuTime : base.cpuTime;
53         }
54         if (clockTime >= 0) {
55             clockTime = (base.clockTime >= 0) ? clockTime-base.clockTime : base.clockTime;
56         }
57         // leave peakMem alone
58
if (collectionTime >= 0) {
59             collectionTime = (base.collectionTime >= 0) ? collectionTime-base.collectionTime : base.collectionTime;
60         }
61     }
62
63     private void pullData() {
64
65         try {
66             cpuTime = new OperatingSystemBeanWrapper().getProcessCpuTime();
67         } catch (NoClassDefFoundError JavaDoc ncdfe) { cpuTime = -9; }
68           catch (ClassCastException JavaDoc cce) { cpuTime = -8; }
69           catch (Error JavaDoc error) { cpuTime = -7; } // catch possible Error thrown when complied by the Eclipse compiler
70

71         clockTime = System.currentTimeMillis(); // or new java.util.Date().getTime() ;
72

73         try {
74             peakMem = new MemoryBeanWrapper().getPeakUsage();
75         } catch (NoClassDefFoundError JavaDoc ncdfe) { peakMem = -9; }
76
77         try {
78             collectionTime = new CollectionBeanWrapper().getCollectionTime();
79         } catch (NoClassDefFoundError JavaDoc ncdfe) { collectionTime = -9; }
80     }
81
82     public long getCpuTime() {
83         return cpuTime;
84     }
85     public long getClockTime() {
86         return clockTime;
87     }
88     public long getPeakMemory() {
89         return peakMem;
90     }
91     public long getCollectionTime() {
92         return collectionTime;
93     }
94
95     @Override JavaDoc
96     public String JavaDoc toString() {
97         return "cpuTime="+cpuTime+", clockTime="+clockTime+", peakMemory="+peakMem;
98     }
99     
100     public static void main(String JavaDoc[] argv) {
101         System.out.println(new Footprint());
102     }
103
104     // -------- begin static inner classes --------
105

106     /** Wrapper so that possbile NoClassDefFoundError can be caught. Instantiating
107      * this class will throw a NoClassDefFoundError on JDK 1.4 and earlier. */

108     public static class MemoryBeanWrapper {
109         List JavaDoc<MemoryPoolMXBean JavaDoc> mlist = ManagementFactory.getMemoryPoolMXBeans();
110         //java.lang.management.MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
111
//java.lang.management.MemoryUsage memUsage = memBean.getHeapMemoryUsage();
112

113         public long getPeakUsage() {
114             long sum = 0;
115             // problem: sum of the peaks is not necessarily the peak of the sum.
116
// For example, objects migrate from the 'eden' to the 'survivor' area.
117
for (MemoryPoolMXBean JavaDoc mpBean : mlist) {
118                 java.lang.management.MemoryUsage JavaDoc memUsage = mpBean.getPeakUsage();
119                 if (memUsage != null) sum += memUsage.getUsed(); // or getCommitted()
120
// System.out.println(mpBean.getType()+", "+mpBean.getName()+", "+memUsage.getUsed());
121
//System.out.println("Memory type="+mpBean.getType()+", Pool name="+mpBean.getName()+", Memory usage="+mpBean.getPeakUsage());
122
}
123             // System.out.println();
124
return sum;
125         }
126     }
127
128     /** Wrapper so that possbile NoClassDefFoundError can be caught. Instantiating this
129      * class will throw a NoClassDefFoundError on JDK 1.4 and earlier, or will throw a
130      * ClassCastException on a 1.5-compliant non-sun JRE where the osBean is not a sunBean.
131      * (If compiled by Eclipse, instantiating it will throw an unsubclassed java.lang.Error.) */

132     public static class OperatingSystemBeanWrapper {
133         java.lang.management. OperatingSystemMXBean JavaDoc osBean = ManagementFactory.getOperatingSystemMXBean();
134         // next line compiles fine with sun JDK 1.5 but the eclipse compiler may complain "The type
135
// OperatingSystemMXBean is not accessible due to restriction on required library classes.jar"
136
// depending on the contents of the .classpath file.
137
com.sun.management.OperatingSystemMXBean sunBean = (com.sun.management.OperatingSystemMXBean)osBean;
138       
139         public long getProcessCpuTime() {
140             return sunBean.getProcessCpuTime();
141         }
142     }
143
144     /** Wrapper so that possbile NoClassDefFoundError can be caught. Instantiating
145      * this class will throw a NoClassDefFoundError on JDK 1.4 and earlier. */

146     public static class CollectionBeanWrapper {
147         List JavaDoc<GarbageCollectorMXBean JavaDoc> clist = ManagementFactory.getGarbageCollectorMXBeans();
148       
149         public long getCollectionTime() {
150             long sum = 0;
151             for (GarbageCollectorMXBean JavaDoc gcBean : clist) {
152                 sum += gcBean.getCollectionTime();
153             }
154             return sum;
155         }
156     }
157
158 }
159
Popular Tags