AbstractPerfDataBuffer.java revision 16648:0eb0f644345d
1147997Srwatson/*
2147997Srwatson * Copyright (c) 2004, 2014, Oracle and/or its affiliates. All rights reserved.
3147997Srwatson * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4147997Srwatson *
5147997Srwatson * This code is free software; you can redistribute it and/or modify it
6147997Srwatson * under the terms of the GNU General Public License version 2 only, as
7147997Srwatson * published by the Free Software Foundation.  Oracle designates this
8147997Srwatson * particular file as subject to the "Classpath" exception as provided
9147997Srwatson * by Oracle in the LICENSE file that accompanied this code.
10147997Srwatson *
11147997Srwatson * This code is distributed in the hope that it will be useful, but WITHOUT
12147997Srwatson * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13147997Srwatson * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14147997Srwatson * version 2 for more details (a copy is included in the LICENSE file that
15147997Srwatson * accompanied this code).
16147997Srwatson *
17147997Srwatson * You should have received a copy of the GNU General Public License version
18147997Srwatson * 2 along with this work; if not, write to the Free Software Foundation,
19147997Srwatson * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20147997Srwatson *
21147997Srwatson * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22147997Srwatson * or visit www.oracle.com if you need additional information or have any
23147997Srwatson * questions.
24147997Srwatson */
25147997Srwatson
26147997Srwatsonpackage sun.jvmstat.perfdata.monitor;
27147997Srwatson
28147997Srwatsonimport sun.jvmstat.monitor.*;
29147997Srwatsonimport java.util.*;
30147997Srwatsonimport java.io.*;
31147997Srwatsonimport java.lang.reflect.*;
32148693Srwatsonimport java.nio.ByteBuffer;
33148627Srwatson
34148627Srwatson/**
35148627Srwatson * Abstraction for the HotSpot PerfData instrumentation buffer. This class
36147997Srwatson * is responsible for acquiring access to the instrumentation buffer for
37148627Srwatson * a target HotSpot Java Virtual Machine and providing method level access
38147997Srwatson * to its contents.
39147997Srwatson *
40147997Srwatson * @author Brian Doherty
41148627Srwatson * @since 1.5
42148627Srwatson */
43147997Srwatsonpublic abstract class AbstractPerfDataBuffer {
44147997Srwatson
45147997Srwatson    /**
46147997Srwatson     * Reference to the concrete instance created by the
47147997Srwatson     * {@link #createPerfDataBuffer} method.
48147997Srwatson     */
49147997Srwatson    protected PerfDataBufferImpl impl;
50148627Srwatson
51148627Srwatson    /**
52148627Srwatson     * Get the Local Java Virtual Machine Identifier, or <em>lvmid</em>
53148627Srwatson     * for the target JVM associated with this instrumentation buffer.
54148627Srwatson     *
55155547Srwatson     * @return int - the lvmid
56155547Srwatson     */
57148627Srwatson    public int getLocalVmId() {
58148627Srwatson        return impl.getLocalVmId();
59148627Srwatson    }
60147997Srwatson
61147997Srwatson    /**
62147997Srwatson     * Get a copy of the raw instrumentation data.
63147997Srwatson     * This method is used to get a copy of the current bytes in the
64147997Srwatson     * instrumentation buffer. It is generally used for transporting
65147997Srwatson     * those bytes over the network.
66147997Srwatson     *
67147997Srwatson     * @return byte[] - a copy of the bytes in the instrumentation buffer.
68147997Srwatson     */
69147997Srwatson    public byte[] getBytes() {
70147997Srwatson        return impl.getBytes();
71147997Srwatson    }
72147997Srwatson
73147997Srwatson    /**
74147997Srwatson     * Get the capacity of the instrumentation buffer.
75147997Srwatson     *
76147997Srwatson     * @return int - the capacity, or size, of the instrumentation buffer.
77147997Srwatson     */
78147997Srwatson    public int getCapacity() {
79148357Srwatson        return impl.getCapacity();
80147997Srwatson    }
81147997Srwatson
82147997Srwatson    /**
83148357Srwatson     * Find a named Instrumentation object.
84147997Srwatson     *
85147997Srwatson     * This method will look for the named instrumentation object in the
86147997Srwatson     * instrumentation exported by this Java Virtual Machine. If an
87147997Srwatson     * instrumentation object with the given name exists, a Monitor interface
88147997Srwatson     * to that object will be return. Otherwise, the method returns
89147997Srwatson     * {@code null}.
90147997Srwatson     *
91147997Srwatson     * @param name the name of the Instrumentation object to find.
92147997Srwatson     * @return Monitor - the {@link Monitor} object that can be used to
93147997Srwatson     *                   monitor the named instrumentation object, or
94147997Srwatson     *                   {@code null} if the named object doesn't exist.
95148357Srwatson     * @throws MonitorException Thrown if an error occurs while communicating
96148357Srwatson     *                          with the target Java Virtual Machine.
97148357Srwatson     */
98148357Srwatson    public Monitor findByName(String name) throws MonitorException {
99147997Srwatson        return impl.findByName(name);
100147997Srwatson    }
101147997Srwatson
102148357Srwatson    /**
103147997Srwatson     * Find all Instrumentation objects with names matching the given pattern.
104147997Srwatson     *
105147997Srwatson     * This method returns a {@link List} of Monitor objects such that
106147997Srwatson     * the name of each object matches the given pattern.
107148357Srwatson     *
108147997Srwatson     * @param patternString  a string containing a pattern as described in
109147997Srwatson     *                       {@link java.util.regex.Pattern}.
110147997Srwatson     * @return {@code List<Monitor>} - a List of {@link Monitor}
111147997Srwatson     *                objects that can be used to
112147997Srwatson     *                monitor the instrumentation objects whose names match
113148357Srwatson     *                the given pattern. If no instrumentation objects have`
114148357Srwatson     *                names matching the given pattern, then an empty List
115148357Srwatson     *                is returned.
116148357Srwatson     * @throws MonitorException Thrown if an error occurs while communicating
117147997Srwatson     *                          with the target Java Virtual Machine.
118147997Srwatson     * @see java.util.regex.Pattern
119147997Srwatson     */
120148357Srwatson    public List<Monitor> findByPattern(String patternString) throws MonitorException {
121147997Srwatson        return impl.findByPattern(patternString);
122147997Srwatson    }
123147997Srwatson
124147997Srwatson    /**
125147997Srwatson     * Get a list of the inserted and removed monitors since last called.
126147997Srwatson     *
127147997Srwatson     * @return MonitorStatus - the status of available Monitors for the
128147997Srwatson     *                         target Java Virtual Machine.
129148357Srwatson     * @throws MonitorException Thrown if communications errors occur
130147997Srwatson     *                          while communicating with the target.
131147997Srwatson     */
132147997Srwatson    public MonitorStatus getMonitorStatus() throws MonitorException {
133147997Srwatson        return impl.getMonitorStatus();
134147997Srwatson    }
135147997Srwatson
136147997Srwatson    /**
137147997Srwatson     * Get the ByteBuffer containing the instrumentation data.
138147997Srwatson     *
139147997Srwatson     * @return ByteBuffer - a ByteBuffer object that refers to the
140147997Srwatson     *                      instrumentation data.
141147997Srwatson     */
142148357Srwatson    public ByteBuffer getByteBuffer() {
143148357Srwatson        return impl.getByteBuffer();
144148357Srwatson    }
145148357Srwatson
146147997Srwatson    /**
147147997Srwatson     * Create the perfdata instrumentation buffer for the given lvmid
148147997Srwatson     * using the given ByteBuffer object as the source of the instrumentation
149147997Srwatson     * data. This method parses the instrumentation buffer header to determine
150147997Srwatson     * key characteristics of the instrumentation buffer and then dynamically
151147997Srwatson     * loads the appropriate class to handle the particular instrumentation
152147997Srwatson     * version.
153147997Srwatson     *
154147997Srwatson     * @param bb the ByteBuffer that references the instrumentation data.
155147997Srwatson     * @param lvmid the Local Java Virtual Machine identifier for this
156148357Srwatson     *              instrumentation buffer.
157147997Srwatson     *
158147997Srwatson     * @throws MonitorException
159147997Srwatson     */
160147997Srwatson    protected void createPerfDataBuffer(ByteBuffer bb, int lvmid)
161147997Srwatson                   throws MonitorException {
162147997Srwatson        int majorVersion = AbstractPerfDataBufferPrologue.getMajorVersion(bb);
163147997Srwatson        int minorVersion = AbstractPerfDataBufferPrologue.getMinorVersion(bb);
164147997Srwatson
165148357Srwatson        // instantiate the version specific class
166147997Srwatson        String classname = "sun.jvmstat.perfdata.monitor.v"
167147997Srwatson                           + majorVersion + "_" + minorVersion
168147997Srwatson                           + ".PerfDataBuffer";
169147997Srwatson
170147997Srwatson        try {
171148357Srwatson            Class<?> implClass = Class.forName(classname);
172147997Srwatson            Constructor<?> cons = implClass.getConstructor(new Class<?>[] {
173147997Srwatson                    Class.forName("java.nio.ByteBuffer"),
174147997Srwatson                    Integer.TYPE
175147997Srwatson            });
176147997Srwatson
177147997Srwatson            impl = (PerfDataBufferImpl)cons.newInstance(new Object[] {
178147997Srwatson                     bb, lvmid
179147997Srwatson            });
180147997Srwatson
181147997Srwatson        } catch (ClassNotFoundException e) {
182147997Srwatson            // from Class.forName();
183147997Srwatson            throw new IllegalArgumentException(
184147997Srwatson                    "Could not find " + classname + ": " + e.getMessage(), e);
185147997Srwatson
186147997Srwatson        } catch (NoSuchMethodException e) {
187147997Srwatson            // from Class.getConstructor();
188147997Srwatson            throw new IllegalArgumentException(
189147997Srwatson                    "Expected constructor missing in " + classname + ": "
190147997Srwatson                    + e.getMessage(), e);
191147997Srwatson
192147997Srwatson        } catch (IllegalAccessException e) {
193148354Srwatson            // from Constructor.newInstance()
194147997Srwatson            throw new IllegalArgumentException(
195147997Srwatson                   "Unexpected constructor access in " + classname + ": "
196148619Srwatson                   + e.getMessage(), e);
197147997Srwatson
198148357Srwatson        } catch (InstantiationException e) {
199147997Srwatson            throw new IllegalArgumentException(
200147997Srwatson                    classname + "is abstract: " + e.getMessage(), e);
201147997Srwatson
202147997Srwatson        } catch (InvocationTargetException e) {
203147997Srwatson            Throwable cause = e.getCause();
204147997Srwatson            if (cause instanceof MonitorException) {
205148354Srwatson                throw (MonitorException)cause;
206147997Srwatson            }
207148007Srwatson            throw new RuntimeException("Unexpected exception: "
208148007Srwatson                                       + e.getMessage() , e);
209148071Srwatson        }
210148007Srwatson    }
211147997Srwatson}
212147997Srwatson