Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/runtime/PerfDataEntry.java
41161 views
/*1* Copyright (c) 2004, 2021, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot.runtime;2526import java.nio.charset.StandardCharsets;27import java.util.*;28import sun.jvm.hotspot.debugger.*;29import sun.jvm.hotspot.oops.*;30import sun.jvm.hotspot.types.*;31import sun.jvm.hotspot.utilities.*;32import sun.jvm.hotspot.utilities.Observable;33import sun.jvm.hotspot.utilities.Observer;3435public class PerfDataEntry extends VMObject {36private static JIntField entryLengthField;37private static JIntField nameOffsetField;38private static JIntField vectorLengthField;39private static JByteField dataTypeField;40private static JByteField flagsField;41private static JByteField dataUnitsField;42private static JByteField dataVariabilityField;43private static JIntField dataOffsetField;4445static {46VM.registerVMInitializedObserver(new Observer() {47public void update(Observable o, Object data) {48initialize(VM.getVM().getTypeDataBase());49}50});51}5253private static synchronized void initialize(TypeDataBase db) {54Type type = db.lookupType("PerfDataEntry");55entryLengthField = type.getJIntField("entry_length");56nameOffsetField = type.getJIntField("name_offset");57vectorLengthField = type.getJIntField("vector_length");58dataTypeField = type.getJByteField("data_type");59flagsField = type.getJByteField("flags");60dataUnitsField = type.getJByteField("data_units");61dataVariabilityField = type.getJByteField("data_variability");62dataOffsetField = type.getJIntField("data_offset");63}6465public PerfDataEntry(Address addr) {66super(addr);67}6869// Accessors7071public int entryLength() {72return (int) entryLengthField.getValue(addr);73}7475public int nameOffset() {76return (int) nameOffsetField.getValue(addr);77}7879public int vectorLength() {80return (int) vectorLengthField.getValue(addr);81}8283// returns one of the constants in BasicType class84public int dataType() {85char ch = (char) (byte) dataTypeField.getValue(addr);86return BasicType.charToType(ch);87}8889public byte flags() {90return (byte) flagsField.getValue(addr);91}9293public boolean supported() {94return (flags() & 0x1) != 0;95}9697private static class PerfDataUnits {98public static int U_None;99public static int U_Bytes;100public static int U_Ticks;101public static int U_Events;102public static int U_String;103public static int U_Hertz;104105static {106VM.registerVMInitializedObserver(new Observer() {107public void update(Observable o, Object data) {108initialize(VM.getVM().getTypeDataBase());109}110});111}112private static synchronized void initialize(TypeDataBase db) {113U_None = db.lookupIntConstant("PerfData::U_None");114U_Bytes = db.lookupIntConstant("PerfData::U_Bytes");115U_Ticks = db.lookupIntConstant("PerfData::U_Ticks");116U_Events = db.lookupIntConstant("PerfData::U_Events");117U_String = db.lookupIntConstant("PerfData::U_String");118U_Hertz = db.lookupIntConstant("PerfData::U_Hertz");119}120}121122// returns one of the constants in PerfDataUnits123public int dataUnits() {124return (int) dataUnitsField.getValue(addr);125}126127// returns one of the constants in PerfDataVariability128public int dataVariability() {129return (int) dataVariabilityField.getValue(addr);130}131132public int dataOffset() {133return (int) dataOffsetField.getValue(addr);134}135136public String name() {137int off = nameOffset();138return CStringUtilities.getString(addr.addOffsetTo(off));139}140141public boolean booleanValue() {142if (Assert.ASSERTS_ENABLED) {143Assert.that(vectorLength() == 0 &&144dataType() == BasicType.getTBoolean(), "not a boolean");145}146return addr.getJBooleanAt(dataOffset());147}148149public char charValue() {150if (Assert.ASSERTS_ENABLED) {151Assert.that(vectorLength() == 0 &&152dataType() == BasicType.getTChar(), "not a char");153}154return addr.getJCharAt(dataOffset());155}156157public byte byteValue() {158if (Assert.ASSERTS_ENABLED) {159Assert.that(vectorLength() == 0 &&160dataType() == BasicType.getTByte(), "not a byte");161}162return addr.getJByteAt(dataOffset());163164}165166public short shortValue() {167if (Assert.ASSERTS_ENABLED) {168Assert.that(vectorLength() == 0 &&169dataType() == BasicType.getTShort(), "not a short");170}171return addr.getJShortAt(dataOffset());172}173174public int intValue() {175if (Assert.ASSERTS_ENABLED) {176Assert.that(vectorLength() == 0 &&177dataType() == BasicType.getTInt(), "not an int");178}179return addr.getJIntAt(dataOffset());180}181182public long longValue() {183if (Assert.ASSERTS_ENABLED) {184Assert.that(vectorLength() == 0 &&185dataType() == BasicType.getTLong(), "not a long");186}187return addr.getJLongAt(dataOffset());188}189190public float floatValue() {191if (Assert.ASSERTS_ENABLED) {192Assert.that(vectorLength() == 0 &&193dataType() == BasicType.getTFloat(), "not a float");194}195return addr.getJFloatAt(dataOffset());196}197198public double doubleValue() {199if (Assert.ASSERTS_ENABLED) {200Assert.that(vectorLength() == 0 &&201dataType() == BasicType.getTDouble(), "not a double");202}203return addr.getJDoubleAt(dataOffset());204}205206public boolean[] booleanArrayValue() {207int len = vectorLength();208if (Assert.ASSERTS_ENABLED) {209Assert.that(len > 0 &&210dataType() == BasicType.getTBoolean(), "not a boolean vector");211}212boolean[] res = new boolean[len];213final int off = dataOffset();214final long size = getHeap().getBooleanSize();215for (int i = 0; i < len; i++) {216res[i] = addr.getJBooleanAt(off + i * size);217}218return res;219}220221public char[] charArrayValue() {222int len = vectorLength();223if (Assert.ASSERTS_ENABLED) {224Assert.that(len > 0 &&225dataType() == BasicType.getTChar(), "not a char vector");226}227char[] res = new char[len];228final int off = dataOffset();229final long size = getHeap().getCharSize();230for (int i = 0; i < len; i++) {231res[i] = addr.getJCharAt(off + i * size);232}233return res;234}235236public byte[] byteArrayValue() {237int len = vectorLength();238if (Assert.ASSERTS_ENABLED) {239Assert.that(len > 0 &&240dataType() == BasicType.getTByte(), "not a byte vector");241}242byte[] res = new byte[len];243final int off = dataOffset();244final long size = getHeap().getByteSize();245for (int i = 0; i < len; i++) {246res[i] = addr.getJByteAt(off + i * size);247}248return res;249}250251public short[] shortArrayValue() {252int len = vectorLength();253if (Assert.ASSERTS_ENABLED) {254Assert.that(len > 0 &&255dataType() == BasicType.getTShort(), "not a short vector");256}257short[] res = new short[len];258final int off = dataOffset();259final long size = getHeap().getShortSize();260for (int i = 0; i < len; i++) {261res[i] = addr.getJShortAt(off + i * size);262}263return res;264}265266public int[] intArrayValue() {267int len = vectorLength();268if (Assert.ASSERTS_ENABLED) {269Assert.that(len > 0 &&270dataType() == BasicType.getTInt(), "not an int vector");271}272int[] res = new int[len];273final int off = dataOffset();274final long size = getHeap().getIntSize();275for (int i = 0; i < len; i++) {276res[i] = addr.getJIntAt(off + i * size);277}278return res;279}280281public long[] longArrayValue() {282int len = vectorLength();283if (Assert.ASSERTS_ENABLED) {284Assert.that(len > 0 &&285dataType() == BasicType.getTLong(), "not a long vector");286}287long[] res = new long[len];288final int off = dataOffset();289final long size = getHeap().getLongSize();290for (int i = 0; i < len; i++) {291res[i] = addr.getJLongAt(off + i * size);292}293return res;294}295296public float[] floatArrayValue() {297int len = vectorLength();298if (Assert.ASSERTS_ENABLED) {299Assert.that(len > 0 &&300dataType() == BasicType.getTFloat(), "not a float vector");301}302float[] res = new float[len];303final int off = dataOffset();304final long size = getHeap().getFloatSize();305for (int i = 0; i < len; i++) {306res[i] = addr.getJFloatAt(off + i * size);307}308return res;309}310311public double[] doubleArrayValue() {312int len = vectorLength();313if (Assert.ASSERTS_ENABLED) {314Assert.that(len > 0 &&315dataType() == BasicType.getTDouble(), "not a double vector");316}317double[] res = new double[len];318final int off = dataOffset();319final long size = getHeap().getDoubleSize();320for (int i = 0; i < len; i++) {321res[i] = addr.getJDoubleAt(off + i * size);322}323return res;324}325326// value as String327public String valueAsString() {328int dataType = dataType();329int len = vectorLength();330String str = null;331if (len == 0) { // scalar332if (dataType == BasicType.getTBoolean()) {333str = Boolean.toString(booleanValue());334} else if (dataType == BasicType.getTChar()) {335str = "'" + Character.toString(charValue()) + "'";336} else if (dataType == BasicType.getTByte()) {337str = Byte.toString(byteValue());338} else if (dataType == BasicType.getTShort()) {339str = Short.toString(shortValue());340} else if (dataType == BasicType.getTInt()) {341str = Integer.toString(intValue());342} else if (dataType == BasicType.getTLong()) {343str = Long.toString(longValue());344} else if (dataType == BasicType.getTFloat()) {345str = Float.toString(floatValue());346} else if (dataType == BasicType.getTDouble()) {347str = Double.toString(doubleValue());348} else {349str = "<unknown scalar value>";350}351} else { // vector352if (dataType == BasicType.getTBoolean()) {353boolean[] res = booleanArrayValue();354StringBuilder buf = new StringBuilder();355buf.append('[');356for (int i = 0; i < res.length; i++) {357buf.append(res[i]);358buf.append(", ");359}360buf.append(']');361str = buf.toString();362} else if (dataType == BasicType.getTChar()) {363// char[] is returned as a String364str = new String(charArrayValue());365} else if (dataType == BasicType.getTByte()) {366// byte[] is returned as a String367str = CStringUtilities.getString(addr.addOffsetTo(dataOffset()),368StandardCharsets.US_ASCII);369} else if (dataType == BasicType.getTShort()) {370short[] res = shortArrayValue();371StringBuilder buf = new StringBuilder();372buf.append('[');373for (int i = 0; i < res.length; i++) {374buf.append(res[i]);375buf.append(", ");376}377buf.append(']');378str = buf.toString();379} else if (dataType == BasicType.getTInt()) {380int[] res = intArrayValue();381StringBuilder buf = new StringBuilder();382buf.append('[');383for (int i = 0; i < res.length; i++) {384buf.append(res[i]);385buf.append(", ");386}387buf.append(']');388str = buf.toString();389} else if (dataType == BasicType.getTLong()) {390long[] res = longArrayValue();391StringBuilder buf = new StringBuilder();392buf.append('[');393for (int i = 0; i < res.length; i++) {394buf.append(res[i]);395buf.append(", ");396}397buf.append(']');398str = buf.toString();399} else if (dataType == BasicType.getTFloat()) {400float[] res = floatArrayValue();401StringBuilder buf = new StringBuilder();402buf.append('[');403for (int i = 0; i < res.length; i++) {404buf.append(res[i]);405buf.append(", ");406}407buf.append(']');408str = buf.toString();409} else if (dataType == BasicType.getTDouble()) {410double[] res = doubleArrayValue();411StringBuilder buf = new StringBuilder();412buf.append('[');413for (int i = 0; i < res.length; i++) {414buf.append(res[i]);415buf.append(", ");416}417buf.append(']');418str = buf.toString();419} else {420str = "<unknown vector value>";421}422}423424// add units425int dataUnitsValue = dataUnits();426427if (dataUnitsValue == PerfDataUnits.U_Bytes) {428str += " byte(s)";429} else if (dataUnitsValue == PerfDataUnits.U_Ticks) {430str += " tick(s)";431} else if (dataUnitsValue == PerfDataUnits.U_Events) {432str += " event(s)";433} else if (dataUnitsValue == PerfDataUnits.U_Hertz) {434str += " Hz";435}436437return str;438}439440// -- Internals only below this point441private ObjectHeap getHeap() {442return VM.getVM().getObjectHeap();443}444}445446447