jdk/src/jdk.management/share/classes/com/sun/management/internal/GcInfoBuilder.java
changeset 30355 e37c7eba132f
parent 25859 3317bb8137f4
child 40200 80c101dbb38d
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 package com.sun.management.internal;
       
    26 
       
    27 import java.lang.management.GarbageCollectorMXBean;
       
    28 import java.lang.management.MemoryUsage;
       
    29 import javax.management.openmbean.OpenType;
       
    30 import javax.management.openmbean.SimpleType;
       
    31 import javax.management.openmbean.CompositeType;
       
    32 import javax.management.openmbean.OpenDataException;
       
    33 import com.sun.management.GcInfo;
       
    34 import sun.management.Util;
       
    35 
       
    36 /**
       
    37  * Helper class to build composite data.
       
    38  */
       
    39 public class GcInfoBuilder {
       
    40     private final GarbageCollectorMXBean gc;
       
    41     private final String[] poolNames;
       
    42     private String[] allItemNames;
       
    43 
       
    44     // GC-specific composite type:
       
    45     // Each GarbageCollectorMXBean may have different GC-specific attributes
       
    46     // the CompositeType for the GcInfo could be different.
       
    47     private CompositeType gcInfoCompositeType;
       
    48 
       
    49     // GC-specific items
       
    50     private final int gcExtItemCount;
       
    51     private final String[] gcExtItemNames;
       
    52     private final String[] gcExtItemDescs;
       
    53     private final char[] gcExtItemTypes;
       
    54 
       
    55     GcInfoBuilder(GarbageCollectorMXBean gc, String[] poolNames) {
       
    56         this.gc = gc;
       
    57         this.poolNames = poolNames;
       
    58         this.gcExtItemCount = getNumGcExtAttributes(gc);
       
    59         this.gcExtItemNames = new String[gcExtItemCount];
       
    60         this.gcExtItemDescs = new String[gcExtItemCount];
       
    61         this.gcExtItemTypes = new char[gcExtItemCount];
       
    62 
       
    63         // Fill the information about extension attributes
       
    64         fillGcAttributeInfo(gc, gcExtItemCount, gcExtItemNames,
       
    65                             gcExtItemTypes, gcExtItemDescs);
       
    66 
       
    67         // lazily build the CompositeType for the GcInfo
       
    68         // including the GC-specific extension attributes
       
    69         this.gcInfoCompositeType = null;
       
    70     }
       
    71 
       
    72     GcInfo getLastGcInfo() {
       
    73         MemoryUsage[] usageBeforeGC = new MemoryUsage[poolNames.length];
       
    74         MemoryUsage[] usageAfterGC = new MemoryUsage[poolNames.length];
       
    75         Object[] values = new Object[gcExtItemCount];
       
    76 
       
    77         return getLastGcInfo0(gc, gcExtItemCount, values, gcExtItemTypes,
       
    78                               usageBeforeGC, usageAfterGC);
       
    79     }
       
    80 
       
    81     public String[] getPoolNames() {
       
    82         return poolNames;
       
    83     }
       
    84 
       
    85     int getGcExtItemCount() {
       
    86         return gcExtItemCount;
       
    87     }
       
    88 
       
    89     // Returns the CompositeType for the GcInfo including
       
    90     // the extension attributes
       
    91     synchronized CompositeType getGcInfoCompositeType() {
       
    92         if (gcInfoCompositeType != null)
       
    93             return gcInfoCompositeType;
       
    94 
       
    95         // First, fill with the attributes in the GcInfo
       
    96         String[] gcInfoItemNames = GcInfoCompositeData.getBaseGcInfoItemNames();
       
    97         OpenType<?>[] gcInfoItemTypes = GcInfoCompositeData.getBaseGcInfoItemTypes();
       
    98         int numGcInfoItems = gcInfoItemNames.length;
       
    99 
       
   100         int itemCount = numGcInfoItems + gcExtItemCount;
       
   101         allItemNames = new String[itemCount];
       
   102         String[] allItemDescs = new String[itemCount];
       
   103         OpenType<?>[] allItemTypes = new OpenType<?>[itemCount];
       
   104 
       
   105         System.arraycopy(gcInfoItemNames, 0, allItemNames, 0, numGcInfoItems);
       
   106         System.arraycopy(gcInfoItemNames, 0, allItemDescs, 0, numGcInfoItems);
       
   107         System.arraycopy(gcInfoItemTypes, 0, allItemTypes, 0, numGcInfoItems);
       
   108 
       
   109         // Then fill with the extension GC-specific attributes, if any.
       
   110         if (gcExtItemCount > 0) {
       
   111             fillGcAttributeInfo(gc, gcExtItemCount, gcExtItemNames,
       
   112                                 gcExtItemTypes, gcExtItemDescs);
       
   113             System.arraycopy(gcExtItemNames, 0, allItemNames,
       
   114                              numGcInfoItems, gcExtItemCount);
       
   115             System.arraycopy(gcExtItemDescs, 0, allItemDescs,
       
   116                              numGcInfoItems, gcExtItemCount);
       
   117             for (int i = numGcInfoItems, j = 0; j < gcExtItemCount; i++, j++) {
       
   118                 switch (gcExtItemTypes[j]) {
       
   119                     case 'Z':
       
   120                         allItemTypes[i] = SimpleType.BOOLEAN;
       
   121                         break;
       
   122                     case 'B':
       
   123                         allItemTypes[i] = SimpleType.BYTE;
       
   124                         break;
       
   125                     case 'C':
       
   126                         allItemTypes[i] = SimpleType.CHARACTER;
       
   127                         break;
       
   128                     case 'S':
       
   129                         allItemTypes[i] = SimpleType.SHORT;
       
   130                         break;
       
   131                     case 'I':
       
   132                         allItemTypes[i] = SimpleType.INTEGER;
       
   133                         break;
       
   134                     case 'J':
       
   135                         allItemTypes[i] = SimpleType.LONG;
       
   136                         break;
       
   137                     case 'F':
       
   138                         allItemTypes[i] = SimpleType.FLOAT;
       
   139                         break;
       
   140                     case 'D':
       
   141                         allItemTypes[i] = SimpleType.DOUBLE;
       
   142                         break;
       
   143                     default:
       
   144                         throw new AssertionError(
       
   145                             "Unsupported type [" + gcExtItemTypes[i] + "]");
       
   146                 }
       
   147             }
       
   148         }
       
   149 
       
   150         CompositeType gict = null;
       
   151         try {
       
   152             final String typeName =
       
   153                 "sun.management." + gc.getName() + ".GcInfoCompositeType";
       
   154 
       
   155             gict = new CompositeType(typeName,
       
   156                                      "CompositeType for GC info for " +
       
   157                                          gc.getName(),
       
   158                                      allItemNames,
       
   159                                      allItemDescs,
       
   160                                      allItemTypes);
       
   161         } catch (OpenDataException e) {
       
   162             // shouldn't reach here
       
   163             throw new RuntimeException(e);
       
   164         }
       
   165         gcInfoCompositeType = gict;
       
   166 
       
   167         return gcInfoCompositeType;
       
   168     }
       
   169 
       
   170     synchronized String[] getItemNames() {
       
   171         if (allItemNames == null) {
       
   172             // initialize when forming the composite type
       
   173             getGcInfoCompositeType();
       
   174         }
       
   175         return allItemNames;
       
   176     }
       
   177 
       
   178     // Retrieve information about extension attributes
       
   179     private native int getNumGcExtAttributes(GarbageCollectorMXBean gc);
       
   180     private native void fillGcAttributeInfo(GarbageCollectorMXBean gc,
       
   181                                             int numAttributes,
       
   182                                             String[] attributeNames,
       
   183                                             char[] types,
       
   184                                             String[] descriptions);
       
   185 
       
   186     /**
       
   187      * Returns the last GcInfo
       
   188      *
       
   189      * @param gc GarbageCollectorMXBean that the gc info is associated with.
       
   190      * @param numExtAtts number of extension attributes
       
   191      * @param extAttValues Values of extension attributes to be filled.
       
   192      * @param before Memory usage before GC to be filled.
       
   193      * @param after Memory usage after GC to be filled.
       
   194      */
       
   195     private native GcInfo getLastGcInfo0(GarbageCollectorMXBean gc,
       
   196                                          int numExtAtts,
       
   197                                          Object[] extAttValues,
       
   198                                          char[] extAttTypes,
       
   199                                          MemoryUsage[] before,
       
   200                                          MemoryUsage[] after);
       
   201 }