jdk/src/jdk.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/AbstractPerfDataBuffer.java
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 25859 3317bb8137f4
child 31061 fead7d86d75f
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 2004, 2014, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.jvmstat.perfdata.monitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.misc.Perf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.jvmstat.monitor.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Abstraction for the HotSpot PerfData instrumentation buffer. This class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * is responsible for acquiring access to the instrumentation buffer for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * a target HotSpot Java Virtual Machine and providing method level access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * to its contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Brian Doherty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public abstract class AbstractPerfDataBuffer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * Reference to the concrete instance created by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * {@link #createPerfDataBuffer} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    protected PerfDataBufferImpl impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Get the Local Java Virtual Machine Identifier, or <em>lvmid</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * for the target JVM associated with this instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @return int - the lvmid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public int getLocalVmId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        return impl.getLocalVmId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Get a copy of the raw instrumentation data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * This method is used to get a copy of the current bytes in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * instrumentation buffer. It is generally used for transporting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * those bytes over the network.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @return byte[] - a copy of the bytes in the instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return impl.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Get the capacity of the instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @return int - the capacity, or size, of the instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public int getCapacity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return impl.getCapacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Find a named Instrumentation object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * This method will look for the named instrumentation object in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * instrumentation exported by this Java Virtual Machine. If an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * instrumentation object with the given name exists, a Monitor interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * to that object will be return. Otherwise, the method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param name the name of the Instrumentation object to find.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @return Monitor - the {@link Monitor} object that can be used to
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
    94
     *                   monitor the named instrumentation object, or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *                   <tt>null</tt> if the named object doesn't exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @throws MonitorException Thrown if an error occurs while communicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *                          with the target Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public Monitor findByName(String name) throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return impl.findByName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Find all Instrumentation objects with names matching the given pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * This method returns a {@link List} of Monitor objects such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * the name of each object matches the given pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param patternString  a string containing a pattern as described in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *                       {@link java.util.regex.Pattern}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @return List<Monitor> - a List of {@link Monitor} objects that can be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *                monitor the instrumentation objects whose names match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *                the given pattern. If no instrumentation objects have`
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *                names matching the given pattern, then an empty List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *                is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @throws MonitorException Thrown if an error occurs while communicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *                          with the target Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @see java.util.regex.Pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public List<Monitor> findByPattern(String patternString) throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return impl.findByPattern(patternString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Get a list of the inserted and removed monitors since last called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @return MonitorStatus - the status of available Monitors for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *                         target Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @throws MonitorException Thrown if communications errors occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *                          while communicating with the target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public MonitorStatus getMonitorStatus() throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return impl.getMonitorStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Get the ByteBuffer containing the instrumentation data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @return ByteBuffer - a ByteBuffer object that refers to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *                      instrumentation data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public ByteBuffer getByteBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return impl.getByteBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Create the perfdata instrumentation buffer for the given lvmid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * using the given ByteBuffer object as the source of the instrumentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * data. This method parses the instrumentation buffer header to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * key characteristics of the instrumentation buffer and then dynamically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * loads the appropriate class to handle the particular instrumentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param bb the ByteBuffer that references the instrumentation data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param lvmid the Local Java Virtual Machine identifier for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *              instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @throws MonitorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    protected void createPerfDataBuffer(ByteBuffer bb, int lvmid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                   throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        int majorVersion = AbstractPerfDataBufferPrologue.getMajorVersion(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        int minorVersion = AbstractPerfDataBufferPrologue.getMinorVersion(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // instantiate the version specific class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        String classname = "sun.jvmstat.perfdata.monitor.v"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                           + majorVersion + "_" + minorVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                           + ".PerfDataBuffer";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            Class<?> implClass = Class.forName(classname);
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   172
            Constructor<?> cons = implClass.getConstructor(new Class<?>[] {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    Class.forName("java.nio.ByteBuffer"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    Integer.TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            impl = (PerfDataBufferImpl)cons.newInstance(new Object[] {
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25401
diff changeset
   178
                     bb, lvmid
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            // from Class.forName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    "Could not find " + classname + ": " + e.getMessage(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            // from Class.getConstructor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    "Expected constructor missing in " + classname + ": "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    + e.getMessage(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            // from Constructor.newInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                   "Unexpected constructor access in " + classname + ": "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                   + e.getMessage(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    classname + "is abstract: " + e.getMessage(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            Throwable cause = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (cause instanceof MonitorException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                throw (MonitorException)cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            throw new RuntimeException("Unexpected exception: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                       + e.getMessage() , e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
}