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