jdk/src/java.management/share/classes/com/sun/management/GcInfo.java
changeset 30380 aca452591e50
parent 30319 376fd031edd2
parent 30379 42d3c3090d20
child 30381 098f4aed1a53
equal deleted inserted replaced
30319:376fd031edd2 30380:aca452591e50
     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  * {@code GcInfo} 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  * {@code GcInfo} 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 {@code Map} 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 {@code Map} 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 {@code Map} 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 {@code Map} 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 {@code GcInfo} object represented by the
       
   183      * given {@code CompositeData}. The given
       
   184      * {@code CompositeData} must contain
       
   185      * all the following attributes:
       
   186      *
       
   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>{@code java.lang.Long}</td>
       
   196      * </tr>
       
   197      * <tr>
       
   198      *   <td>startTime</td>
       
   199      *   <td>{@code java.lang.Long}</td>
       
   200      * </tr>
       
   201      * <tr>
       
   202      *   <td>endTime</td>
       
   203      *   <td>{@code java.lang.Long}</td>
       
   204      * </tr>
       
   205      * <tr>
       
   206      *   <td>memoryUsageBeforeGc</td>
       
   207      *   <td>{@code javax.management.openmbean.TabularData}</td>
       
   208      * </tr>
       
   209      * <tr>
       
   210      *   <td>memoryUsageAfterGc</td>
       
   211      *   <td>{@code javax.management.openmbean.TabularData}</td>
       
   212      * </tr>
       
   213      * </table>
       
   214      * </blockquote>
       
   215      *
       
   216      * @throws IllegalArgumentException if {@code cd} does not
       
   217      *   represent a {@code GcInfo} object with the attributes
       
   218      *   described above.
       
   219      *
       
   220      * @return a {@code GcInfo} object represented by {@code cd}
       
   221      * if {@code cd} is not {@code null}; {@code null} 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      * 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 }