jdk/src/jdk.management/share/classes/com/sun/management/GcInfo.java
changeset 30355 e37c7eba132f
parent 25859 3317bb8137f4
child 30365 551470085a1d
equal deleted inserted replaced
30354:ca83b4cae363 30355:e37c7eba132f
       
     1 /*
       
     2  * Copyright (c) 2003, 2015, 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 com.sun.management.internal.GcInfoCompositeData;
       
    37 import com.sun.management.internal.GcInfoBuilder;
       
    38 
       
    39 /**
       
    40  * Garbage collection information.  It contains the following
       
    41  * information for one garbage collection as well as GC-specific
       
    42  * attributes:
       
    43  * <blockquote>
       
    44  * <ul>
       
    45  *   <li>Start time</li>
       
    46  *   <li>End time</li>
       
    47  *   <li>Duration</li>
       
    48  *   <li>Memory usage before the collection starts</li>
       
    49  *   <li>Memory usage after the collection ends</li>
       
    50  * </ul>
       
    51  * </blockquote>
       
    52  *
       
    53  * <p>
       
    54  * <tt>GcInfo</tt> is a {@link CompositeData CompositeData}
       
    55  * The GC-specific attributes can be obtained via the CompositeData
       
    56  * interface.  This is a historical relic, and other classes should
       
    57  * not copy this pattern.  Use {@link CompositeDataView} instead.
       
    58  *
       
    59  * <h4>MXBean Mapping</h4>
       
    60  * <tt>GcInfo</tt> is mapped to a {@link CompositeData CompositeData}
       
    61  * with attributes as specified in the {@link #from from} method.
       
    62  *
       
    63  * @author  Mandy Chung
       
    64  * @since   1.5
       
    65  */
       
    66 @jdk.Exported
       
    67 public class GcInfo implements CompositeData, CompositeDataView {
       
    68     private final long index;
       
    69     private final long startTime;
       
    70     private final long endTime;
       
    71     private final Map<String, MemoryUsage> usageBeforeGc;
       
    72     private final Map<String, MemoryUsage> usageAfterGc;
       
    73     private final Object[] extAttributes;
       
    74     private final CompositeData cdata;
       
    75     private final GcInfoBuilder builder;
       
    76 
       
    77     private GcInfo(GcInfoBuilder builder,
       
    78                    long index, long startTime, long endTime,
       
    79                    MemoryUsage[] muBeforeGc,
       
    80                    MemoryUsage[] muAfterGc,
       
    81                    Object[] extAttributes) {
       
    82         this.builder       = builder;
       
    83         this.index         = index;
       
    84         this.startTime     = startTime;
       
    85         this.endTime       = endTime;
       
    86         String[] poolNames = builder.getPoolNames();
       
    87         this.usageBeforeGc = new HashMap<String, MemoryUsage>(poolNames.length);
       
    88         this.usageAfterGc = new HashMap<String, MemoryUsage>(poolNames.length);
       
    89         for (int i = 0; i < poolNames.length; i++) {
       
    90             this.usageBeforeGc.put(poolNames[i],  muBeforeGc[i]);
       
    91             this.usageAfterGc.put(poolNames[i],  muAfterGc[i]);
       
    92         }
       
    93         this.extAttributes = extAttributes;
       
    94         this.cdata = new GcInfoCompositeData(this, builder, extAttributes);
       
    95     }
       
    96 
       
    97     private GcInfo(CompositeData cd) {
       
    98         GcInfoCompositeData.validateCompositeData(cd);
       
    99 
       
   100         this.index         = GcInfoCompositeData.getId(cd);
       
   101         this.startTime     = GcInfoCompositeData.getStartTime(cd);
       
   102         this.endTime       = GcInfoCompositeData.getEndTime(cd);
       
   103         this.usageBeforeGc = GcInfoCompositeData.getMemoryUsageBeforeGc(cd);
       
   104         this.usageAfterGc  = GcInfoCompositeData.getMemoryUsageAfterGc(cd);
       
   105         this.extAttributes = null;
       
   106         this.builder       = null;
       
   107         this.cdata         = cd;
       
   108     }
       
   109 
       
   110     /**
       
   111      * Returns the identifier of this garbage collection which is
       
   112      * the number of collections that this collector has done.
       
   113      *
       
   114      * @return the identifier of this garbage collection which is
       
   115      * the number of collections that this collector has done.
       
   116      */
       
   117     public long getId() {
       
   118         return index;
       
   119     }
       
   120 
       
   121     /**
       
   122      * Returns the start time of this GC in milliseconds
       
   123      * since the Java virtual machine was started.
       
   124      *
       
   125      * @return the start time of this GC.
       
   126      */
       
   127     public long getStartTime() {
       
   128         return startTime;
       
   129     }
       
   130 
       
   131     /**
       
   132      * Returns the end time of this GC in milliseconds
       
   133      * since the Java virtual machine was started.
       
   134      *
       
   135      * @return the end time of this GC.
       
   136      */
       
   137     public long getEndTime() {
       
   138         return endTime;
       
   139     }
       
   140 
       
   141     /**
       
   142      * Returns the elapsed time of this GC in milliseconds.
       
   143      *
       
   144      * @return the elapsed time of this GC in milliseconds.
       
   145      */
       
   146     public long getDuration() {
       
   147         return endTime - startTime;
       
   148     }
       
   149 
       
   150     /**
       
   151      * Returns the memory usage of all memory pools
       
   152      * at the beginning of this GC.
       
   153      * This method returns
       
   154      * a <tt>Map</tt> of the name of a memory pool
       
   155      * to the memory usage of the corresponding
       
   156      * memory pool before GC starts.
       
   157      *
       
   158      * @return a <tt>Map</tt> of memory pool names to the memory
       
   159      * usage of a memory pool before GC starts.
       
   160      */
       
   161     public Map<String, MemoryUsage> getMemoryUsageBeforeGc() {
       
   162         return Collections.unmodifiableMap(usageBeforeGc);
       
   163     }
       
   164 
       
   165     /**
       
   166      * Returns the memory usage of all memory pools
       
   167      * at the end of this GC.
       
   168      * This method returns
       
   169      * a <tt>Map</tt> of the name of a memory pool
       
   170      * to the memory usage of the corresponding
       
   171      * memory pool when GC finishes.
       
   172      *
       
   173      * @return a <tt>Map</tt> of memory pool names to the memory
       
   174      * usage of a memory pool when GC finishes.
       
   175      */
       
   176     public Map<String, MemoryUsage> getMemoryUsageAfterGc() {
       
   177         return Collections.unmodifiableMap(usageAfterGc);
       
   178     }
       
   179 
       
   180    /**
       
   181      * Returns a <tt>GcInfo</tt> object represented by the
       
   182      * given <tt>CompositeData</tt>. The given
       
   183      * <tt>CompositeData</tt> must contain
       
   184      * all the following attributes:
       
   185      *
       
   186      * <p>
       
   187      * <blockquote>
       
   188      * <table border>
       
   189      * <tr>
       
   190      *   <th align=left>Attribute Name</th>
       
   191      *   <th align=left>Type</th>
       
   192      * </tr>
       
   193      * <tr>
       
   194      *   <td>index</td>
       
   195      *   <td><tt>java.lang.Long</tt></td>
       
   196      * </tr>
       
   197      * <tr>
       
   198      *   <td>startTime</td>
       
   199      *   <td><tt>java.lang.Long</tt></td>
       
   200      * </tr>
       
   201      * <tr>
       
   202      *   <td>endTime</td>
       
   203      *   <td><tt>java.lang.Long</tt></td>
       
   204      * </tr>
       
   205      * <tr>
       
   206      *   <td>memoryUsageBeforeGc</td>
       
   207      *   <td><tt>javax.management.openmbean.TabularData</tt></td>
       
   208      * </tr>
       
   209      * <tr>
       
   210      *   <td>memoryUsageAfterGc</td>
       
   211      *   <td><tt>javax.management.openmbean.TabularData</tt></td>
       
   212      * </tr>
       
   213      * </table>
       
   214      * </blockquote>
       
   215      *
       
   216      * @throws IllegalArgumentException if <tt>cd</tt> does not
       
   217      *   represent a <tt>GcInfo</tt> object with the attributes
       
   218      *   described above.
       
   219      *
       
   220      * @return a <tt>GcInfo</tt> object represented by <tt>cd</tt>
       
   221      * if <tt>cd</tt> is not <tt>null</tt>; <tt>null</tt> otherwise.
       
   222      */
       
   223     public static GcInfo from(CompositeData cd) {
       
   224         if (cd == null) {
       
   225             return null;
       
   226         }
       
   227 
       
   228         if (cd instanceof GcInfoCompositeData) {
       
   229             return ((GcInfoCompositeData) cd).getGcInfo();
       
   230         } else {
       
   231             return new GcInfo(cd);
       
   232         }
       
   233 
       
   234     }
       
   235 
       
   236     // Implementation of the CompositeData interface
       
   237     public boolean containsKey(String key) {
       
   238         return cdata.containsKey(key);
       
   239     }
       
   240 
       
   241     public boolean containsValue(Object value) {
       
   242         return cdata.containsValue(value);
       
   243     }
       
   244 
       
   245     public boolean equals(Object obj) {
       
   246         return cdata.equals(obj);
       
   247     }
       
   248 
       
   249     public Object get(String key) {
       
   250         return cdata.get(key);
       
   251     }
       
   252 
       
   253     public Object[] getAll(String[] keys) {
       
   254         return cdata.getAll(keys);
       
   255     }
       
   256 
       
   257     public CompositeType getCompositeType() {
       
   258         return cdata.getCompositeType();
       
   259     }
       
   260 
       
   261     public int hashCode() {
       
   262         return cdata.hashCode();
       
   263     }
       
   264 
       
   265     public String toString() {
       
   266         return cdata.toString();
       
   267     }
       
   268 
       
   269     public Collection<?> values() {
       
   270         return cdata.values();
       
   271     }
       
   272 
       
   273     /**
       
   274      * <p>Return the {@code CompositeData} representation of this
       
   275      * {@code GcInfo}, including any GC-specific attributes.  The
       
   276      * returned value will have at least all the attributes described
       
   277      * in the {@link #from(CompositeData) from} method, plus optionally
       
   278      * other attributes.
       
   279      *
       
   280      * @param ct the {@code CompositeType} that the caller expects.
       
   281      * This parameter is ignored and can be null.
       
   282      *
       
   283      * @return the {@code CompositeData} representation.
       
   284      */
       
   285     public CompositeData toCompositeData(CompositeType ct) {
       
   286         return cdata;
       
   287     }
       
   288 }