KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > jvm > MemoryReporter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * MemoryReporter.java
26  *
27  * Created on July 25, 2005, 3:38 PM
28  */

29
30 package com.sun.enterprise.admin.mbeans.jvm;
31
32 import com.sun.enterprise.util.i18n.StringManager;
33 import java.lang.management.GarbageCollectorMXBean JavaDoc;
34 import java.lang.management.ManagementFactory JavaDoc;
35 import java.lang.management.MemoryMXBean JavaDoc;
36 import java.lang.management.MemoryPoolMXBean JavaDoc;
37 import java.lang.management.MemoryUsage JavaDoc;
38 import java.lang.management.RuntimeMXBean JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Set JavaDoc;
43 import javax.management.MBeanServerConnection JavaDoc;
44 import javax.management.ObjectName JavaDoc;
45
46 /**
47  */

48 class MemoryReporter {
49     private final MBeanServerConnection JavaDoc mbsc;
50     private RuntimeMXBean JavaDoc rmbean;
51     private MemoryMXBean JavaDoc mmbean;
52     private List JavaDoc<MemoryPoolMXBean JavaDoc> pools;
53     private List JavaDoc<GarbageCollectorMXBean JavaDoc> gcmbeans;
54     private final static StringManager sm = StringManager.getManager(MemoryReporter.class);
55     
56     public MemoryReporter(final MBeanServerConnection JavaDoc mbsc) {
57         this.mbsc = mbsc;
58     }
59     public String JavaDoc getMemoryReport() {
60         init();
61         final StringBuilderNewLineAppender sb = new StringBuilderNewLineAppender(new StringBuilder JavaDoc());
62         sb.append(getMemoryPoolReport());
63         sb.append(getGarbageCollectionReport());
64         sb.append(getMemoryMXBeanReport());
65         return ( sb.toString() );
66     }
67     
68     private void init() throws RuntimeException JavaDoc {
69         try {
70             this.rmbean = ManagementFactory.newPlatformMXBeanProxy(mbsc,
71                                                  ManagementFactory.RUNTIME_MXBEAN_NAME,
72                                                  RuntimeMXBean JavaDoc.class);
73             this.mmbean = ManagementFactory.newPlatformMXBeanProxy(mbsc,
74                                                  ManagementFactory.MEMORY_MXBEAN_NAME,
75                                                  MemoryMXBean JavaDoc.class);
76             ObjectName JavaDoc poolName = new ObjectName JavaDoc(ManagementFactory.MEMORY_POOL_MXBEAN_DOMAIN_TYPE+",*");;
77             ObjectName JavaDoc gcName = new ObjectName JavaDoc(ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE+",*");
78             Set JavaDoc mbeans = mbsc.queryNames(poolName, null);
79             if (mbeans != null) {
80                 pools = new ArrayList JavaDoc<MemoryPoolMXBean JavaDoc>();
81                 Iterator JavaDoc iterator = mbeans.iterator();
82                 MemoryPoolMXBean JavaDoc p = null;
83                     while (iterator.hasNext()) {
84                         ObjectName JavaDoc objName = (ObjectName JavaDoc) iterator.next();
85                         p = ManagementFactory.newPlatformMXBeanProxy(mbsc,
86                                                    objName.getCanonicalName(),
87                                                    MemoryPoolMXBean JavaDoc.class);
88                         pools.add(p);
89                 }
90             }
91             mbeans = mbsc.queryNames(gcName, null);
92             if (mbeans != null) {
93                 gcmbeans = new ArrayList JavaDoc<GarbageCollectorMXBean JavaDoc>();
94                 Iterator JavaDoc iterator = mbeans.iterator();
95                 GarbageCollectorMXBean JavaDoc gc = null;
96                 while (iterator.hasNext()) {
97                     ObjectName JavaDoc objName = (ObjectName JavaDoc) iterator.next();
98                     gc = ManagementFactory.newPlatformMXBeanProxy(mbsc,
99                                                objName.getCanonicalName(),
100                                                GarbageCollectorMXBean JavaDoc.class);
101                     gcmbeans.add(gc);
102                 }
103             }
104         } catch (final Exception JavaDoc e) {
105             throw new RuntimeException JavaDoc(e);
106         }
107     }
108     
109     private String JavaDoc getMemoryPoolReport() {
110         final StringBuilderNewLineAppender sb = new StringBuilderNewLineAppender(new StringBuilder JavaDoc());
111         final long millis = rmbean.getUptime();
112         final String JavaDoc uptime = sm.getString("uptime", JVMInformationCollector.millis2HoursMinutesSeconds(millis));
113         sb.append(uptime);
114         for (final MemoryPoolMXBean JavaDoc m : pools) {
115             final String JavaDoc n = m.getName();
116             sb.append(sm.getString("memory.pool.name", n));
117             MemoryUsage JavaDoc mu = m.getUsage();
118             sb.append(mu2String(mu));
119         }
120         return ( sb.toString() );
121     }
122     private String JavaDoc mu2String(final MemoryUsage JavaDoc mu) {
123         final StringBuilderNewLineAppender sb = new StringBuilderNewLineAppender(new StringBuilder JavaDoc());
124         final String JavaDoc init = JVMInformationCollector.formatLong(mu.getInit());
125         sb.append(sm.getString("memory.usage.init", init));
126         final String JavaDoc comm = JVMInformationCollector.formatLong(mu.getCommitted());
127         sb.append(sm.getString("memory.usage.comm", comm));
128         final String JavaDoc max = JVMInformationCollector.formatLong(mu.getMax());
129         sb.append(sm.getString("memory.usage.max", max));
130         final String JavaDoc used = JVMInformationCollector.formatLong(mu.getUsed());
131         sb.append(sm.getString("memory.usage.used", used));
132         return ( sb.toString() );
133     }
134     private String JavaDoc getGarbageCollectionReport() {
135         final StringBuilderNewLineAppender sb = new StringBuilderNewLineAppender(new StringBuilder JavaDoc());
136         for (final GarbageCollectorMXBean JavaDoc m : gcmbeans) {
137             final String JavaDoc name = sm.getString("gc.name", m.getName());
138             sb.append(name);
139             final String JavaDoc cc = sm.getString("gc.numcol", JVMInformationCollector.formatLong(m.getCollectionCount()));
140             sb.append(cc);
141             final String JavaDoc gct = sm.getString("gc.coltime", JVMInformationCollector.millis2SecondsMillis(m.getCollectionTime()));
142             sb.append(gct);
143         }
144         return ( sb.toString() );
145     }
146     private String JavaDoc getMemoryMXBeanReport() {
147         final StringBuilderNewLineAppender sb = new StringBuilderNewLineAppender(new StringBuilder JavaDoc());
148         sb.append(sm.getString("heap.mem.usage"));
149         sb.append(mu2String(mmbean.getHeapMemoryUsage()));
150         sb.append(sm.getString("nonheap.mem.usage"));
151         sb.append(mu2String(mmbean.getNonHeapMemoryUsage()));
152         sb.append(sm.getString("obj.fin.pending", mmbean.getObjectPendingFinalizationCount()));
153         return ( sb.toString() );
154     }
155 }
Popular Tags