jdk/src/share/classes/sun/management/GcInfoBuilder.java
author mchung
Tue, 17 Jan 2012 15:55:40 -0800
changeset 11530 a9d059c15b80
parent 5506 202f599c92aa
child 14342 8435a30053c1
permissions -rw-r--r--
7117570: Warnings in sun.mangement.* and its subpackages Reviewed-by: mchung, dsamersoff Contributed-by: kurchi.subhra.hazra@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     2
 * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.lang.management.GarbageCollectorMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.management.MemoryUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.management.openmbean.OpenType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.management.openmbean.SimpleType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.management.openmbean.TabularType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.management.openmbean.TabularData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.management.openmbean.TabularDataSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.management.openmbean.CompositeType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.management.openmbean.CompositeData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.openmbean.CompositeDataSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.openmbean.OpenDataException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.management.GcInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Helper class to build composite data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class GcInfoBuilder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private final GarbageCollectorMXBean gc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private final String[] poolNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private String[] allItemNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // GC-specific composite type:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // Each GarbageCollectorMXBean may have different GC-specific attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // the CompositeType for the GcInfo could be different.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private CompositeType gcInfoCompositeType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // GC-specific items
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private final int gcExtItemCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private final String[] gcExtItemNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private final String[] gcExtItemDescs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final char[] gcExtItemTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    GcInfoBuilder(GarbageCollectorMXBean gc, String[] poolNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.gc = gc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.poolNames = poolNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.gcExtItemCount = getNumGcExtAttributes(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this.gcExtItemNames = new String[gcExtItemCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        this.gcExtItemDescs = new String[gcExtItemCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this.gcExtItemTypes = new char[gcExtItemCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // Fill the information about extension attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        fillGcAttributeInfo(gc, gcExtItemCount, gcExtItemNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                            gcExtItemTypes, gcExtItemDescs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // lazily build the CompositeType for the GcInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // including the GC-specific extension attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        this.gcInfoCompositeType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    GcInfo getLastGcInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        MemoryUsage[] usageBeforeGC = new MemoryUsage[poolNames.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        MemoryUsage[] usageAfterGC = new MemoryUsage[poolNames.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        Object[] values = new Object[gcExtItemCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return getLastGcInfo0(gc, gcExtItemCount, values, gcExtItemTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                              usageBeforeGC, usageAfterGC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public String[] getPoolNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return poolNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    int getGcExtItemCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return gcExtItemCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // Returns the CompositeType for the GcInfo including
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // the extension attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    synchronized CompositeType getGcInfoCompositeType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (gcInfoCompositeType != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return gcInfoCompositeType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // First, fill with the attributes in the GcInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        String[] gcInfoItemNames = GcInfoCompositeData.getBaseGcInfoItemNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        OpenType[] gcInfoItemTypes = GcInfoCompositeData.getBaseGcInfoItemTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        int numGcInfoItems = gcInfoItemNames.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        int itemCount = numGcInfoItems + gcExtItemCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        allItemNames = new String[itemCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        String[] allItemDescs = new String[itemCount];
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   107
        OpenType<?>[] allItemTypes = new OpenType<?>[itemCount];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        System.arraycopy(gcInfoItemNames, 0, allItemNames, 0, numGcInfoItems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        System.arraycopy(gcInfoItemNames, 0, allItemDescs, 0, numGcInfoItems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        System.arraycopy(gcInfoItemTypes, 0, allItemTypes, 0, numGcInfoItems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // Then fill with the extension GC-specific attributes, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (gcExtItemCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            fillGcAttributeInfo(gc, gcExtItemCount, gcExtItemNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                gcExtItemTypes, gcExtItemDescs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            System.arraycopy(gcExtItemNames, 0, allItemNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                             numGcInfoItems, gcExtItemCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            System.arraycopy(gcExtItemDescs, 0, allItemDescs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                             numGcInfoItems, gcExtItemCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            for (int i = numGcInfoItems, j = 0; j < gcExtItemCount; i++, j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                switch (gcExtItemTypes[j]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    case 'Z':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        allItemTypes[i] = SimpleType.BOOLEAN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    case 'B':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                        allItemTypes[i] = SimpleType.BYTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    case 'C':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                        allItemTypes[i] = SimpleType.CHARACTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    case 'S':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        allItemTypes[i] = SimpleType.SHORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    case 'I':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                        allItemTypes[i] = SimpleType.INTEGER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    case 'J':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                        allItemTypes[i] = SimpleType.LONG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                        allItemTypes[i] = SimpleType.FLOAT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    case 'D':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        allItemTypes[i] = SimpleType.DOUBLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    default:
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   148
                        throw new AssertionError(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                            "Unsupported type [" + gcExtItemTypes[i] + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        CompositeType gict = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            final String typeName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                "sun.management." + gc.getName() + ".GcInfoCompositeType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            gict = new CompositeType(typeName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                     "CompositeType for GC info for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                         gc.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                     allItemNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                     allItemDescs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                     allItemTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        } catch (OpenDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            // shouldn't reach here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            throw Util.newException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        gcInfoCompositeType = gict;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return gcInfoCompositeType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    synchronized String[] getItemNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (allItemNames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            // initialize when forming the composite type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            getGcInfoCompositeType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return allItemNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    // Retrieve information about extension attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private native int getNumGcExtAttributes(GarbageCollectorMXBean gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private native void fillGcAttributeInfo(GarbageCollectorMXBean gc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                            int numAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                            String[] attributeNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                            char[] types,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                            String[] descriptions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Returns the last GcInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param gc GarbageCollectorMXBean that the gc info is associated with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param numExtAtts number of extension attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param extAttValues Values of extension attributes to be filled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param before Memory usage before GC to be filled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param after Memory usage after GC to be filled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    private native GcInfo getLastGcInfo0(GarbageCollectorMXBean gc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                         int numExtAtts,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                         Object[] extAttValues,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                         char[] extAttTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                         MemoryUsage[] before,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                         MemoryUsage[] after);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
}