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