jdk/src/java.management/share/classes/com/sun/management/GcInfo.java
changeset 30355 e37c7eba132f
parent 30354 ca83b4cae363
child 30356 a56e57aad51f
equal deleted inserted replaced
30354:ca83b4cae363 30355:e37c7eba132f
     1 /*
       
     2  * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package com.sun.management;
       
    27 
       
    28 import java.lang.management.MemoryUsage;
       
    29 import javax.management.openmbean.CompositeData;
       
    30 import javax.management.openmbean.CompositeDataView;
       
    31 import javax.management.openmbean.CompositeType;
       
    32 import java.util.Collection;
       
    33 import java.util.Collections;
       
    34 import java.util.HashMap;
       
    35 import java.util.Map;
       
    36 import java.util.List;
       
    37 import sun.management.GcInfoCompositeData;
       
    38 import sun.management.GcInfoBuilder;
       
    39 
       
    40 /**
       
    41  * Garbage collection information.  It contains the following
       
    42  * information for one garbage collection as well as GC-specific
       
    43  * attributes:
       
    44  * <blockquote>
       
    45  * <ul>
       
    46  *   <li>Start time</li>
       
    47  *   <li>End time</li>
       
    48  *   <li>Duration</li>
       
    49  *   <li>Memory usage before the collection starts</li>
       
    50  *   <li>Memory usage after the collection ends</li>
       
    51  * </ul>
       
    52  * </blockquote>
       
    53  *
       
    54  * <p>
       
    55  * <tt>GcInfo</tt> is a {@link CompositeData CompositeData}
       
    56  * The GC-specific attributes can be obtained via the CompositeData
       
    57  * interface.  This is a historical relic, and other classes should
       
    58  * not copy this pattern.  Use {@link CompositeDataView} instead.
       
    59  *
       
    60  * <h4>MXBean Mapping</h4>
       
    61  * <tt>GcInfo</tt> is mapped to a {@link CompositeData CompositeData}
       
    62  * with attributes as specified in the {@link #from from} method.
       
    63  *
       
    64  * @author  Mandy Chung
       
    65  * @since   1.5
       
    66  */
       
    67 @jdk.Exported
       
    68 public class GcInfo implements CompositeData, CompositeDataView {
       
    69     private final long index;
       
    70     private final long startTime;
       
    71     private final long endTime;
       
    72     private final Map<String, MemoryUsage> usageBeforeGc;
       
    73     private final Map<String, MemoryUsage> usageAfterGc;
       
    74     private final Object[] extAttributes;
       
    75     private final CompositeData cdata;
       
    76     private final GcInfoBuilder builder;
       
    77 
       
    78     private GcInfo(GcInfoBuilder builder,
       
    79                    long index, long startTime, long endTime,
       
    80                    MemoryUsage[] muBeforeGc,
       
    81                    MemoryUsage[] muAfterGc,
       
    82                    Object[] extAttributes) {
       
    83         this.builder       = builder;
       
    84         this.index         = index;
       
    85         this.startTime     = startTime;
       
    86         this.endTime       = endTime;
       
    87         String[] poolNames = builder.getPoolNames();
       
    88         this.usageBeforeGc = new HashMap<String, MemoryUsage>(poolNames.length);
       
    89         this.usageAfterGc = new HashMap<String, MemoryUsage>(poolNames.length);
       
    90         for (int i = 0; i < poolNames.length; i++) {
       
    91             this.usageBeforeGc.put(poolNames[i],  muBeforeGc[i]);
       
    92             this.usageAfterGc.put(poolNames[i],  muAfterGc[i]);
       
    93         }
       
    94         this.extAttributes = extAttributes;
       
    95         this.cdata = new GcInfoCompositeData(this, builder, extAttributes);
       
    96     }
       
    97 
       
    98     private GcInfo(CompositeData cd) {
       
    99         GcInfoCompositeData.validateCompositeData(cd);
       
   100 
       
   101         this.index         = GcInfoCompositeData.getId(cd);
       
   102         this.startTime     = GcInfoCompositeData.getStartTime(cd);
       
   103         this.endTime       = GcInfoCompositeData.getEndTime(cd);
       
   104         this.usageBeforeGc = GcInfoCompositeData.getMemoryUsageBeforeGc(cd);
       
   105         this.usageAfterGc  = GcInfoCompositeData.getMemoryUsageAfterGc(cd);
       
   106         this.extAttributes = null;
       
   107         this.builder       = null;
       
   108         this.cdata         = cd;
       
   109     }
       
   110 
       
   111     /**
       
   112      * Returns the identifier of this garbage collection which is
       
   113      * the number of collections that this collector has done.
       
   114      *
       
   115      * @return the identifier of this garbage collection which is
       
   116      * the number of collections that this collector has done.
       
   117      */
       
   118     public long getId() {
       
   119         return index;
       
   120     }
       
   121 
       
   122     /**
       
   123      * Returns the start time of this GC in milliseconds
       
   124      * since the Java virtual machine was started.
       
   125      *
       
   126      * @return the start time of this GC.
       
   127      */
       
   128     public long getStartTime() {
       
   129         return startTime;
       
   130     }
       
   131 
       
   132     /**
       
   133      * Returns the end time of this GC in milliseconds
       
   134      * since the Java virtual machine was started.
       
   135      *
       
   136      * @return the end time of this GC.
       
   137      */
       
   138     public long getEndTime() {
       
   139         return endTime;
       
   140     }
       
   141 
       
   142     /**
       
   143      * Returns the elapsed time of this GC in milliseconds.
       
   144      *
       
   145      * @return the elapsed time of this GC in milliseconds.
       
   146      */
       
   147     public long getDuration() {
       
   148         return endTime - startTime;
       
   149     }
       
   150 
       
   151     /**
       
   152      * Returns the memory usage of all memory pools
       
   153      * at the beginning of this GC.
       
   154      * This method returns
       
   155      * a <tt>Map</tt> of the name of a memory pool
       
   156      * to the memory usage of the corresponding
       
   157      * memory pool before GC starts.
       
   158      *
       
   159      * @return a <tt>Map</tt> of memory pool names to the memory
       
   160      * usage of a memory pool before GC starts.
       
   161      */
       
   162     public Map<String, MemoryUsage> getMemoryUsageBeforeGc() {
       
   163         return Collections.unmodifiableMap(usageBeforeGc);
       
   164     }
       
   165 
       
   166     /**
       
   167      * Returns the memory usage of all memory pools
       
   168      * at the end of this GC.
       
   169      * This method returns
       
   170      * a <tt>Map</tt> of the name of a memory pool
       
   171      * to the memory usage of the corresponding
       
   172      * memory pool when GC finishes.
       
   173      *
       
   174      * @return a <tt>Map</tt> of memory pool names to the memory
       
   175      * usage of a memory pool when GC finishes.
       
   176      */
       
   177     public Map<String, MemoryUsage> getMemoryUsageAfterGc() {
       
   178         return Collections.unmodifiableMap(usageAfterGc);
       
   179     }
       
   180 
       
   181    /**
       
   182      * Returns a <tt>GcInfo</tt> object represented by the
       
   183      * given <tt>CompositeData</tt>. The given
       
   184      * <tt>CompositeData</tt> must contain
       
   185      * all the following attributes:
       
   186      *
       
   187      * <p>
       
   188      * <blockquote>
       
   189      * <table border>
       
   190      * <tr>
       
   191      *   <th align=left>Attribute Name</th>
       
   192      *   <th align=left>Type</th>
       
   193      * </tr>
       
   194      * <tr>
       
   195      *   <td>index</td>
       
   196      *   <td><tt>java.lang.Long</tt></td>
       
   197      * </tr>
       
   198      * <tr>
       
   199      *   <td>startTime</td>
       
   200      *   <td><tt>java.lang.Long</tt></td>
       
   201      * </tr>
       
   202      * <tr>
       
   203      *   <td>endTime</td>
       
   204      *   <td><tt>java.lang.Long</tt></td>
       
   205      * </tr>
       
   206      * <tr>
       
   207      *   <td>memoryUsageBeforeGc</td>
       
   208      *   <td><tt>javax.management.openmbean.TabularData</tt></td>
       
   209      * </tr>
       
   210      * <tr>
       
   211      *   <td>memoryUsageAfterGc</td>
       
   212      *   <td><tt>javax.management.openmbean.TabularData</tt></td>
       
   213      * </tr>
       
   214      * </table>
       
   215      * </blockquote>
       
   216      *
       
   217      * @throws IllegalArgumentException if <tt>cd</tt> does not
       
   218      *   represent a <tt>GcInfo</tt> object with the attributes
       
   219      *   described above.
       
   220      *
       
   221      * @return a <tt>GcInfo</tt> object represented by <tt>cd</tt>
       
   222      * if <tt>cd</tt> is not <tt>null</tt>; <tt>null</tt> otherwise.
       
   223      */
       
   224     public static GcInfo from(CompositeData cd) {
       
   225         if (cd == null) {
       
   226             return null;
       
   227         }
       
   228 
       
   229         if (cd instanceof GcInfoCompositeData) {
       
   230             return ((GcInfoCompositeData) cd).getGcInfo();
       
   231         } else {
       
   232             return new GcInfo(cd);
       
   233         }
       
   234 
       
   235     }
       
   236 
       
   237     // Implementation of the CompositeData interface
       
   238     public boolean containsKey(String key) {
       
   239         return cdata.containsKey(key);
       
   240     }
       
   241 
       
   242     public boolean containsValue(Object value) {
       
   243         return cdata.containsValue(value);
       
   244     }
       
   245 
       
   246     public boolean equals(Object obj) {
       
   247         return cdata.equals(obj);
       
   248     }
       
   249 
       
   250     public Object get(String key) {
       
   251         return cdata.get(key);
       
   252     }
       
   253 
       
   254     public Object[] getAll(String[] keys) {
       
   255         return cdata.getAll(keys);
       
   256     }
       
   257 
       
   258     public CompositeType getCompositeType() {
       
   259         return cdata.getCompositeType();
       
   260     }
       
   261 
       
   262     public int hashCode() {
       
   263         return cdata.hashCode();
       
   264     }
       
   265 
       
   266     public String toString() {
       
   267         return cdata.toString();
       
   268     }
       
   269 
       
   270     public Collection<?> values() {
       
   271         return cdata.values();
       
   272     }
       
   273 
       
   274     /**
       
   275      * <p>Return the {@code CompositeData} representation of this
       
   276      * {@code GcInfo}, including any GC-specific attributes.  The
       
   277      * returned value will have at least all the attributes described
       
   278      * in the {@link #from(CompositeData) from} method, plus optionally
       
   279      * other attributes.
       
   280      *
       
   281      * @param ct the {@code CompositeType} that the caller expects.
       
   282      * This parameter is ignored and can be null.
       
   283      *
       
   284      * @return the {@code CompositeData} representation.
       
   285      */
       
   286     public CompositeData toCompositeData(CompositeType ct) {
       
   287         return cdata;
       
   288     }
       
   289 }