Path: blob/master/src/java.management/share/classes/sun/management/counter/perf/PerfDataEntry.java
41161 views
/*1* Copyright (c) 2003, 2011, 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.io.UnsupportedEncodingException;3031class PerfDataEntry {32private class EntryFieldOffset {33private static final int SIZEOF_BYTE = 1;34private static final int SIZEOF_INT = 4;35private static final int SIZEOF_LONG = 8;3637private static final int ENTRY_LENGTH_SIZE = SIZEOF_INT;38private static final int NAME_OFFSET_SIZE = SIZEOF_INT;39private static final int VECTOR_LENGTH_SIZE = SIZEOF_INT;40private static final int DATA_TYPE_SIZE = SIZEOF_BYTE;41private static final int FLAGS_SIZE = SIZEOF_BYTE;42private static final int DATA_UNIT_SIZE = SIZEOF_BYTE;43private static final int DATA_VAR_SIZE = SIZEOF_BYTE;44private static final int DATA_OFFSET_SIZE = SIZEOF_INT;4546static final int ENTRY_LENGTH = 0;47static final int NAME_OFFSET = ENTRY_LENGTH + ENTRY_LENGTH_SIZE;48static final int VECTOR_LENGTH = NAME_OFFSET + NAME_OFFSET_SIZE;;49static final int DATA_TYPE = VECTOR_LENGTH + VECTOR_LENGTH_SIZE;50static final int FLAGS = DATA_TYPE + DATA_TYPE_SIZE;51static final int DATA_UNIT = FLAGS + FLAGS_SIZE;52static final int DATA_VAR = DATA_UNIT + DATA_UNIT_SIZE;53static final int DATA_OFFSET = DATA_VAR + DATA_VAR_SIZE;54}5556private String name;57private int entryStart;58private int entryLength;59private int vectorLength;60private PerfDataType dataType;61private int flags;62private Units unit;63private Variability variability;64private int dataOffset;65private int dataSize;66private ByteBuffer data;6768PerfDataEntry(ByteBuffer b) {69entryStart = b.position();70entryLength = b.getInt();7172// check for valid entry length73if (entryLength <= 0 || entryLength > b.limit()) {74throw new InstrumentationException("Invalid entry length: " +75" entryLength = " + entryLength);76}77// check if last entry occurs before the eof.78if ((entryStart + entryLength) > b.limit()) {79throw new InstrumentationException("Entry extends beyond end of buffer: " +80" entryStart = " + entryStart +81" entryLength = " + entryLength +82" buffer limit = " + b.limit());83}8485b.position(entryStart + EntryFieldOffset.NAME_OFFSET);86int nameOffset = b.getInt();8788if ((entryStart + nameOffset) > b.limit()) {89throw new InstrumentationException("Invalid name offset: " +90" entryStart = " + entryStart +91" nameOffset = " + nameOffset +92" buffer limit = " + b.limit());93}949596b.position(entryStart + EntryFieldOffset.VECTOR_LENGTH);97vectorLength = b.getInt();9899b.position(entryStart + EntryFieldOffset.DATA_TYPE);100dataType = PerfDataType.toPerfDataType(b.get());101102b.position(entryStart + EntryFieldOffset.FLAGS);103flags = b.get();104105b.position(entryStart + EntryFieldOffset.DATA_UNIT);106unit = Units.toUnits(b.get());107108b.position(entryStart + EntryFieldOffset.DATA_VAR);109variability = Variability.toVariability(b.get());110111b.position(entryStart + EntryFieldOffset.DATA_OFFSET);112dataOffset = b.getInt();113114// read in the perfData item name, casting bytes to chars. skip the115// null terminator116b.position(entryStart + nameOffset);117// calculate the length of the name118int nameLength = 0;119byte c;120for (; (c = b.get()) != (byte)0; nameLength++);121122byte[] symbolBytes = new byte[nameLength];123b.position(entryStart + nameOffset);124for (int i = 0; i < nameLength; i++) {125symbolBytes[i] = b.get();126}127128// convert name into a String129try {130name = new String(symbolBytes, "UTF-8");131}132catch (UnsupportedEncodingException e) {133// should not reach here134// "UTF-8" is always a known encoding135throw new InternalError(e.getMessage(), e);136}137138if (variability == Variability.INVALID) {139throw new InstrumentationException("Invalid variability attribute:" +140" name = " + name);141}142if (unit == Units.INVALID) {143throw new InstrumentationException("Invalid units attribute: " +144" name = " + name);145}146147if (vectorLength > 0) {148dataSize = vectorLength * dataType.size();149} else {150dataSize = dataType.size();151}152153// check if data beyond the eof.154if ((entryStart + dataOffset + dataSize) > b.limit()) {155throw new InstrumentationException("Data extends beyond end of buffer: " +156" entryStart = " + entryStart +157" dataOffset = " + dataOffset+158" dataSize = " + dataSize +159" buffer limit = " + b.limit());160}161// Construct a ByteBuffer for the data162b.position(entryStart + dataOffset);163data = b.slice();164data.order(b.order());165data.limit(dataSize);166}167168169public int size() {170return entryLength;171}172173public String name() {174return name;175}176177public PerfDataType type() {178return dataType;179}180181public Units units() {182return unit;183}184185public int flags() {186return flags;187}188189/**190* Returns the number of elements in the data.191*/192public int vectorLength() {193return vectorLength;194}195196public Variability variability() {197return variability;198}199200public ByteBuffer byteData() {201data.position(0);202assert data.remaining() == vectorLength();203return data.duplicate();204}205206public LongBuffer longData() {207LongBuffer lb = data.asLongBuffer();208return lb;209}210}211212213