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