1 /* 2 * Copyright (c) 2004, 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 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 List<Monitor> - a List of {@link Monitor} 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, new Integer(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 }