Path: blob/master/src/java.management/share/classes/sun/management/counter/perf/Prologue.java
41161 views
/*1* Copyright (c) 2003, 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.*;2930class Prologue {31// these constants should match their #define counterparts in vmdata.hpp32private static final byte PERFDATA_BIG_ENDIAN = 0;33private static final byte PERFDATA_LITTLE_ENDIAN = 1;34private static final int PERFDATA_MAGIC = 0xcafec0c0;3536private class PrologueFieldOffset {37private static final int SIZEOF_BYTE = 1;38private static final int SIZEOF_INT = 4;39private static final int SIZEOF_LONG = 8;4041private static final int MAGIC_SIZE = SIZEOF_INT;42private static final int BYTE_ORDER_SIZE = SIZEOF_BYTE;43private static final int MAJOR_SIZE = SIZEOF_BYTE;44private static final int MINOR_SIZE = SIZEOF_BYTE;45private static final int ACCESSIBLE_SIZE = SIZEOF_BYTE;46private static final int USED_SIZE = SIZEOF_INT;47private static final int OVERFLOW_SIZE = SIZEOF_INT;48private static final int MOD_TIMESTAMP_SIZE = SIZEOF_LONG;49private static final int ENTRY_OFFSET_SIZE = SIZEOF_INT;50private static final int NUM_ENTRIES_SIZE = SIZEOF_INT;5152// these constants must match the field offsets and sizes53// in the PerfDataPrologue structure in perfMemory.hpp54static final int MAGIC = 0;55static final int BYTE_ORDER = MAGIC + MAGIC_SIZE;56static final int MAJOR_VERSION = BYTE_ORDER + BYTE_ORDER_SIZE;57static final int MINOR_VERSION = MAJOR_VERSION + MAJOR_SIZE;58static final int ACCESSIBLE = MINOR_VERSION + MINOR_SIZE;59static final int USED = ACCESSIBLE + ACCESSIBLE_SIZE;60static final int OVERFLOW = USED + USED_SIZE;61static final int MOD_TIMESTAMP = OVERFLOW + OVERFLOW_SIZE;62static final int ENTRY_OFFSET = MOD_TIMESTAMP + MOD_TIMESTAMP_SIZE;63static final int NUM_ENTRIES = ENTRY_OFFSET + ENTRY_OFFSET_SIZE;64static final int PROLOGUE_2_0_SIZE = NUM_ENTRIES + NUM_ENTRIES_SIZE;65}666768private ByteBuffer header;69private int magic;7071Prologue(ByteBuffer b) {72this.header = b.duplicate();7374// the magic number is always stored in big-endian format75// save and restore the buffer's initial byte order around76// the fetch of the data.77header.order(ByteOrder.BIG_ENDIAN);78header.position(PrologueFieldOffset.MAGIC);79magic = header.getInt();8081// the magic number is always stored in big-endian format82if (magic != PERFDATA_MAGIC) {83throw new InstrumentationException("Bad Magic: " +84Integer.toHexString(getMagic()));85}868788// set the buffer's byte order according to the value of its89// byte order field.90header.order(getByteOrder());9192// Check version93int major = getMajorVersion();94int minor = getMinorVersion();9596if (major < 2) {97throw new InstrumentationException("Unsupported version: " +98major + "." + minor);99}100101// Currently, only support 2.0 version.102header.limit(PrologueFieldOffset.PROLOGUE_2_0_SIZE);103}104105public int getMagic() {106return magic;107}108109public int getMajorVersion() {110header.position(PrologueFieldOffset.MAJOR_VERSION);111return (int)header.get();112}113114public int getMinorVersion() {115header.position(PrologueFieldOffset.MINOR_VERSION);116return (int)header.get();117}118119public ByteOrder getByteOrder() {120header.position(PrologueFieldOffset.BYTE_ORDER);121122byte byte_order = header.get();123if (byte_order == PERFDATA_BIG_ENDIAN) {124return ByteOrder.BIG_ENDIAN;125}126else {127return ByteOrder.LITTLE_ENDIAN;128}129}130131public int getEntryOffset() {132header.position(PrologueFieldOffset.ENTRY_OFFSET);133return header.getInt();134}135136// The following fields are updated asynchronously137// while they are accessed by these methods.138public int getUsed() {139header.position(PrologueFieldOffset.USED);140return header.getInt();141}142143public int getOverflow() {144header.position(PrologueFieldOffset.OVERFLOW);145return header.getInt();146}147148public long getModificationTimeStamp() {149header.position(PrologueFieldOffset.MOD_TIMESTAMP);150return header.getLong();151}152153public int getNumEntries() {154header.position(PrologueFieldOffset.NUM_ENTRIES);155return header.getInt();156}157158public boolean isAccessible() {159header.position(PrologueFieldOffset.ACCESSIBLE);160byte b = header.get();161return (b == 0 ? false : true);162}163}164165166