0N/A/*
2362N/A * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.management.counter.perf;
0N/A
0N/Aimport sun.management.counter.*;
0N/Aimport java.nio.*;
0N/Aimport java.io.UnsupportedEncodingException;
0N/A
0N/Aclass PerfDataEntry {
0N/A private class EntryFieldOffset {
0N/A private final static int SIZEOF_BYTE = 1;
0N/A private final static int SIZEOF_INT = 4;
0N/A private final static int SIZEOF_LONG = 8;
0N/A
0N/A private final static int ENTRY_LENGTH_SIZE = SIZEOF_INT;
0N/A private final static int NAME_OFFSET_SIZE = SIZEOF_INT;
0N/A private final static int VECTOR_LENGTH_SIZE = SIZEOF_INT;
0N/A private final static int DATA_TYPE_SIZE = SIZEOF_BYTE;
0N/A private final static int FLAGS_SIZE = SIZEOF_BYTE;
0N/A private final static int DATA_UNIT_SIZE = SIZEOF_BYTE;
0N/A private final static int DATA_VAR_SIZE = SIZEOF_BYTE;
0N/A private final static int DATA_OFFSET_SIZE = SIZEOF_INT;
0N/A
0N/A final static int ENTRY_LENGTH = 0;
0N/A final static int NAME_OFFSET = ENTRY_LENGTH + ENTRY_LENGTH_SIZE;
0N/A final static int VECTOR_LENGTH = NAME_OFFSET + NAME_OFFSET_SIZE;;
0N/A final static int DATA_TYPE = VECTOR_LENGTH + VECTOR_LENGTH_SIZE;
0N/A final static int FLAGS = DATA_TYPE + DATA_TYPE_SIZE;
0N/A final static int DATA_UNIT = FLAGS + FLAGS_SIZE;
0N/A final static int DATA_VAR = DATA_UNIT + DATA_UNIT_SIZE;
0N/A final static int DATA_OFFSET = DATA_VAR + DATA_VAR_SIZE;
0N/A }
0N/A
0N/A private String name;
0N/A private int entryStart;
0N/A private int entryLength;
0N/A private int vectorLength;
0N/A private PerfDataType dataType;
0N/A private int flags;
0N/A private Units unit;
0N/A private Variability variability;
0N/A private int dataOffset;
0N/A private int dataSize;
0N/A private ByteBuffer data;
0N/A
0N/A PerfDataEntry(ByteBuffer b) {
0N/A entryStart = b.position();
0N/A entryLength = b.getInt();
0N/A
0N/A // check for valid entry length
0N/A if (entryLength <= 0 || entryLength > b.limit()) {
0N/A throw new InstrumentationException("Invalid entry length: " +
0N/A " entryLength = " + entryLength);
0N/A }
0N/A // check if last entry occurs before the eof.
0N/A if ((entryStart + entryLength) > b.limit()) {
0N/A throw new InstrumentationException("Entry extends beyond end of buffer: " +
0N/A " entryStart = " + entryStart +
0N/A " entryLength = " + entryLength +
0N/A " buffer limit = " + b.limit());
0N/A }
0N/A
0N/A b.position(entryStart + EntryFieldOffset.NAME_OFFSET);
0N/A int nameOffset = b.getInt();
0N/A
0N/A if ((entryStart + nameOffset) > b.limit()) {
0N/A throw new InstrumentationException("Invalid name offset: " +
0N/A " entryStart = " + entryStart +
0N/A " nameOffset = " + nameOffset +
0N/A " buffer limit = " + b.limit());
0N/A }
0N/A
0N/A
0N/A b.position(entryStart + EntryFieldOffset.VECTOR_LENGTH);
0N/A vectorLength = b.getInt();
0N/A
0N/A b.position(entryStart + EntryFieldOffset.DATA_TYPE);
0N/A dataType = PerfDataType.toPerfDataType(b.get());
0N/A
0N/A b.position(entryStart + EntryFieldOffset.FLAGS);
0N/A flags = b.get();
0N/A
0N/A b.position(entryStart + EntryFieldOffset.DATA_UNIT);
0N/A unit = Units.toUnits(b.get());
0N/A
0N/A b.position(entryStart + EntryFieldOffset.DATA_VAR);
0N/A variability = Variability.toVariability(b.get());
0N/A
0N/A b.position(entryStart + EntryFieldOffset.DATA_OFFSET);
0N/A dataOffset = b.getInt();
0N/A
0N/A // read in the perfData item name, casting bytes to chars. skip the
0N/A // null terminator
0N/A b.position(entryStart + nameOffset);
0N/A // calculate the length of the name
0N/A int nameLength = 0;
0N/A byte c;
0N/A for (; (c = b.get()) != (byte)0; nameLength++);
0N/A
0N/A byte[] symbolBytes = new byte[nameLength];
0N/A b.position(entryStart + nameOffset);
0N/A for (int i = 0; i < nameLength; i++) {
0N/A symbolBytes[i] = b.get();
0N/A }
0N/A
0N/A // convert name into a String
0N/A try {
0N/A name = new String(symbolBytes, "UTF-8");
0N/A }
0N/A catch (UnsupportedEncodingException e) {
0N/A // should not reach here
0N/A // "UTF-8" is always a known encoding
0N/A throw new InternalError(e.getMessage());
0N/A }
0N/A
0N/A if (variability == Variability.INVALID) {
0N/A throw new InstrumentationException("Invalid variability attribute:" +
0N/A " name = " + name);
0N/A }
0N/A if (unit == Units.INVALID) {
0N/A throw new InstrumentationException("Invalid units attribute: " +
0N/A " name = " + name);
0N/A }
0N/A
0N/A if (vectorLength > 0) {
0N/A dataSize = vectorLength * dataType.size();
0N/A } else {
0N/A dataSize = dataType.size();
0N/A }
0N/A
0N/A // check if data beyond the eof.
0N/A if ((entryStart + dataOffset + dataSize) > b.limit()) {
0N/A throw new InstrumentationException("Data extends beyond end of buffer: " +
0N/A " entryStart = " + entryStart +
0N/A " dataOffset = " + dataOffset+
0N/A " dataSize = " + dataSize +
0N/A " buffer limit = " + b.limit());
0N/A }
0N/A // Construct a ByteBuffer for the data
0N/A b.position(entryStart + dataOffset);
0N/A data = b.slice();
0N/A data.order(b.order());
0N/A data.limit(dataSize);
0N/A }
0N/A
0N/A
0N/A public int size() {
0N/A return entryLength;
0N/A }
0N/A
0N/A public String name() {
0N/A return name;
0N/A }
0N/A
0N/A public PerfDataType type() {
0N/A return dataType;
0N/A }
0N/A
0N/A public Units units() {
0N/A return unit;
0N/A }
0N/A
0N/A public int flags() {
0N/A return flags;
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of elements in the data.
0N/A */
0N/A public int vectorLength() {
0N/A return vectorLength;
0N/A }
0N/A
0N/A public Variability variability() {
0N/A return variability;
0N/A }
0N/A
0N/A public ByteBuffer byteData() {
0N/A data.position(0);
0N/A assert data.remaining() == vectorLength();
0N/A return data.duplicate();
0N/A }
0N/A
0N/A public LongBuffer longData() {
0N/A LongBuffer lb = data.asLongBuffer();
0N/A return lb;
0N/A }
0N/A}