001: /*
002: * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.management;
027:
028: import java.lang.management.MemoryUsage;
029: import javax.management.openmbean.CompositeData;
030: import javax.management.openmbean.CompositeDataView;
031: import javax.management.openmbean.CompositeType;
032: import java.util.Collection;
033: import java.util.Collections;
034: import java.util.HashMap;
035: import java.util.Map;
036: import java.util.List;
037: import sun.management.GcInfoCompositeData;
038: import sun.management.GcInfoBuilder;
039:
040: /**
041: * Garbage collection information. It contains the following
042: * information for one garbage collection as well as GC-specific
043: * attributes:
044: * <blockquote>
045: * <ul>
046: * <li>Start time</li>
047: * <li>End time</li>
048: * <li>Duration</li>
049: * <li>Memory usage before the collection starts</li>
050: * <li>Memory usage after the collection ends</li>
051: * </ul>
052: * </blockquote>
053: *
054: * <p>
055: * <tt>GcInfo</tt> is a {@link CompositeData CompositeData}
056: * The GC-specific attributes can be obtained via the CompositeData
057: * interface. This is a historical relic, and other classes should
058: * not copy this pattern. Use {@link CompositeDataView} instead.
059: *
060: * <h4>MXBean Mapping</h4>
061: * <tt>GcInfo</tt> is mapped to a {@link CompositeData CompositeData}
062: * with attributes as specified in the {@link #from from} method.
063: *
064: * @author Mandy Chung
065: * @version 1.15, 05/05/07
066: * @since 1.5
067: */
068: public class GcInfo implements CompositeData, CompositeDataView {
069: private final long index;
070: private final long startTime;
071: private final long endTime;
072: private final Map<String, MemoryUsage> usageBeforeGc;
073: private final Map<String, MemoryUsage> usageAfterGc;
074: private final Object[] extAttributes;
075: private final CompositeData cdata;
076: private final GcInfoBuilder builder;
077:
078: private GcInfo(GcInfoBuilder builder, long index, long startTime,
079: long endTime, MemoryUsage[] muBeforeGc,
080: MemoryUsage[] muAfterGc, Object[] extAttributes) {
081: this .builder = builder;
082: this .index = index;
083: this .startTime = startTime;
084: this .endTime = endTime;
085: String[] poolNames = builder.getPoolNames();
086: this .usageBeforeGc = new HashMap<String, MemoryUsage>(
087: poolNames.length);
088: this .usageAfterGc = new HashMap<String, MemoryUsage>(
089: poolNames.length);
090: for (int i = 0; i < poolNames.length; i++) {
091: this .usageBeforeGc.put(poolNames[i], muBeforeGc[i]);
092: this .usageAfterGc.put(poolNames[i], muAfterGc[i]);
093: }
094: this .extAttributes = extAttributes;
095: this .cdata = new GcInfoCompositeData(this , builder,
096: extAttributes);
097: }
098:
099: private GcInfo(CompositeData cd) {
100: GcInfoCompositeData.validateCompositeData(cd);
101:
102: this .index = GcInfoCompositeData.getId(cd);
103: this .startTime = GcInfoCompositeData.getStartTime(cd);
104: this .endTime = GcInfoCompositeData.getEndTime(cd);
105: this .usageBeforeGc = GcInfoCompositeData
106: .getMemoryUsageBeforeGc(cd);
107: this .usageAfterGc = GcInfoCompositeData
108: .getMemoryUsageAfterGc(cd);
109: this .extAttributes = null;
110: this .builder = null;
111: this .cdata = cd;
112: }
113:
114: /**
115: * Returns the identifier of this garbage collection which is
116: * the number of collections that this collector has done.
117: *
118: * @return the identifier of this garbage collection which is
119: * the number of collections that this collector has done.
120: */
121: public long getId() {
122: return index;
123: }
124:
125: /**
126: * Returns the start time of this GC in milliseconds
127: * since the Java virtual machine was started.
128: *
129: * @return the start time of this GC.
130: */
131: public long getStartTime() {
132: return startTime;
133: }
134:
135: /**
136: * Returns the end time of this GC in milliseconds
137: * since the Java virtual machine was started.
138: *
139: * @return the end time of this GC.
140: */
141: public long getEndTime() {
142: return endTime;
143: }
144:
145: /**
146: * Returns the elapsed time of this GC in milliseconds.
147: *
148: * @return the elapsed time of this GC in milliseconds.
149: */
150: public long getDuration() {
151: return endTime - startTime;
152: }
153:
154: /**
155: * Returns the memory usage of all memory pools
156: * at the beginning of this GC.
157: * This method returns
158: * a <tt>Map</tt> of the name of a memory pool
159: * to the memory usage of the corresponding
160: * memory pool before GC starts.
161: *
162: * @return a <tt>Map</tt> of memory pool names to the memory
163: * usage of a memory pool before GC starts.
164: */
165: public Map<String, MemoryUsage> getMemoryUsageBeforeGc() {
166: return Collections.unmodifiableMap(usageBeforeGc);
167: }
168:
169: /**
170: * Returns the memory usage of all memory pools
171: * at the end of this GC.
172: * This method returns
173: * a <tt>Map</tt> of the name of a memory pool
174: * to the memory usage of the corresponding
175: * memory pool when GC finishes.
176: *
177: * @return a <tt>Map</tt> of memory pool names to the memory
178: * usage of a memory pool when GC finishes.
179: */
180: public Map<String, MemoryUsage> getMemoryUsageAfterGc() {
181: return Collections.unmodifiableMap(usageAfterGc);
182: }
183:
184: /**
185: * Returns a <tt>GcInfo</tt> object represented by the
186: * given <tt>CompositeData</tt>. The given
187: * <tt>CompositeData</tt> must contain
188: * all the following attributes:
189: *
190: * <p>
191: * <blockquote>
192: * <table border>
193: * <tr>
194: * <th align=left>Attribute Name</th>
195: * <th align=left>Type</th>
196: * </tr>
197: * <tr>
198: * <td>index</td>
199: * <td><tt>java.lang.Long</tt></td>
200: * </tr>
201: * <tr>
202: * <td>startTime</td>
203: * <td><tt>java.lang.Long</tt></td>
204: * </tr>
205: * <tr>
206: * <td>endTime</td>
207: * <td><tt>java.lang.Long</tt></td>
208: * </tr>
209: * <tr>
210: * <td>memoryUsageBeforeGc</td>
211: * <td><tt>javax.management.openmbean.TabularData</tt></td>
212: * </tr>
213: * <tr>
214: * <td>memoryUsageAfterGc</td>
215: * <td><tt>javax.management.openmbean.TabularData</tt></td>
216: * </tr>
217: * </table>
218: * </blockquote>
219: *
220: * @throws IllegalArgumentException if <tt>cd</tt> does not
221: * represent a <tt>GcInfo</tt> object with the attributes
222: * described above.
223: *
224: * @return a <tt>GcInfo</tt> object represented by <tt>cd</tt>
225: * if <tt>cd</tt> is not <tt>null</tt>; <tt>null</tt> otherwise.
226: */
227: public static GcInfo from(CompositeData cd) {
228: if (cd == null) {
229: return null;
230: }
231:
232: if (cd instanceof GcInfoCompositeData) {
233: return ((GcInfoCompositeData) cd).getGcInfo();
234: } else {
235: return new GcInfo(cd);
236: }
237:
238: }
239:
240: // Implementation of the CompositeData interface
241: public boolean containsKey(String key) {
242: return cdata.containsKey(key);
243: }
244:
245: public boolean containsValue(Object value) {
246: return cdata.containsValue(value);
247: }
248:
249: public boolean equals(Object obj) {
250: return cdata.equals(obj);
251: }
252:
253: public Object get(String key) {
254: return cdata.get(key);
255: }
256:
257: public Object[] getAll(String[] keys) {
258: return cdata.getAll(keys);
259: }
260:
261: public CompositeType getCompositeType() {
262: return cdata.getCompositeType();
263: }
264:
265: public int hashCode() {
266: return cdata.hashCode();
267: }
268:
269: public String toString() {
270: return cdata.toString();
271: }
272:
273: public Collection values() {
274: return cdata.values();
275: }
276:
277: /**
278: * <p>Return the {@code CompositeData} representation of this
279: * {@code GcInfo}, including any GC-specific attributes. The
280: * returned value will have at least all the attributes described
281: * in the {@link #from(CompositeData) from} method, plus optionally
282: * other attributes.
283: *
284: * @param ct the {@code CompositeType} that the caller expects.
285: * This parameter is ignored and can be null.
286: *
287: * @return the {@code CompositeData} representation.
288: */
289: public CompositeData toCompositeData(CompositeType ct) {
290: return cdata;
291: }
292: }
|