Path: blob/master/src/java.management/share/classes/sun/management/counter/perf/PerfInstrumentation.java
41161 views
/*1* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.management.counter.perf;2627import sun.management.counter.*;28import java.nio.*;29import java.util.*;30import java.util.regex.*;3132public class PerfInstrumentation {33private ByteBuffer buffer;34private Prologue prologue;35private long lastModificationTime;36private long lastUsed;37private int nextEntry;38private SortedMap<String, Counter> map;3940public PerfInstrumentation(ByteBuffer b) {41prologue = new Prologue(b);42buffer = b;43buffer.order(prologue.getByteOrder());4445// Check recognized versions46int major = getMajorVersion();47int minor = getMinorVersion();4849// Support only 2.0 version50if (major < 2) {51throw new InstrumentationException("Unsupported version: " +52major + "." + minor);53}54rewind();55}5657public int getMajorVersion() {58return prologue.getMajorVersion();59}6061public int getMinorVersion() {62return prologue.getMinorVersion();63}6465public long getModificationTimeStamp() {66return prologue.getModificationTimeStamp();67}6869void rewind() {70// rewind to the first entry71buffer.rewind();72buffer.position(prologue.getEntryOffset());73nextEntry = buffer.position();74// rebuild all the counters75map = new TreeMap<>();76}7778boolean hasNext() {79return (nextEntry < prologue.getUsed());80}8182Counter getNextCounter() {83if (! hasNext()) {84return null;85}8687if ((nextEntry % 4) != 0) {88// entries are always 4 byte aligned.89throw new InstrumentationException(90"Entry index not properly aligned: " + nextEntry);91}9293if (nextEntry < 0 || nextEntry > buffer.limit()) {94// defensive check to protect against a corrupted shared memory region.95throw new InstrumentationException(96"Entry index out of bounds: nextEntry = " + nextEntry +97", limit = " + buffer.limit());98}99100buffer.position(nextEntry);101PerfDataEntry entry = new PerfDataEntry(buffer);102nextEntry = nextEntry + entry.size();103104Counter counter = null;105PerfDataType type = entry.type();106if (type == PerfDataType.BYTE) {107if (entry.units() == Units.STRING && entry.vectorLength() > 0) {108counter = new PerfStringCounter(entry.name(),109entry.variability(),110entry.flags(),111entry.vectorLength(),112entry.byteData());113} else if (entry.vectorLength() > 0) {114counter = new PerfByteArrayCounter(entry.name(),115entry.units(),116entry.variability(),117entry.flags(),118entry.vectorLength(),119entry.byteData());120} else {121// ByteArrayCounter must have vectorLength > 0122assert false;123}124}125else if (type == PerfDataType.LONG) {126if (entry.vectorLength() == 0) {127counter = new PerfLongCounter(entry.name(),128entry.units(),129entry.variability(),130entry.flags(),131entry.longData());132} else {133counter = new PerfLongArrayCounter(entry.name(),134entry.units(),135entry.variability(),136entry.flags(),137entry.vectorLength(),138entry.longData());139}140}141else {142// FIXME: Should we throw an exception for unsupported type?143// Currently skip such entry144assert false;145}146return counter;147}148149public synchronized List<Counter> getAllCounters() {150while (hasNext()) {151Counter c = getNextCounter();152if (c != null) {153map.put(c.getName(), c);154}155}156return new ArrayList<>(map.values());157}158159public synchronized List<Counter> findByPattern(String patternString) {160while (hasNext()) {161Counter c = getNextCounter();162if (c != null) {163map.put(c.getName(), c);164}165}166167Pattern pattern = Pattern.compile(patternString);168Matcher matcher = pattern.matcher("");169List<Counter> matches = new ArrayList<>();170171172for (Map.Entry<String,Counter> me: map.entrySet()) {173String name = me.getKey();174175// apply pattern to counter name176matcher.reset(name);177178// if the pattern matches, then add Counter to list179if (matcher.lookingAt()) {180matches.add(me.getValue());181}182}183return matches;184}185}186187188