jdk/src/share/classes/sun/management/counter/perf/PerfDataEntry.java
author sherman
Tue, 30 Aug 2011 11:53:11 -0700
changeset 10419 12c063b39232
parent 5506 202f599c92aa
child 14342 8435a30053c1
permissions -rw-r--r--
7084245: Update usages of InternalError to use exception chaining Summary: to use new InternalError constructor with cause chainning Reviewed-by: alanb, ksrini, xuelei, neugens Contributed-by: sebastian.sickelmann@gmx.de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.management.counter.perf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.management.counter.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
class PerfDataEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    private class EntryFieldOffset {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
        private final static int SIZEOF_BYTE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
        private final static int SIZEOF_INT  = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        private final static int SIZEOF_LONG = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        private final static int ENTRY_LENGTH_SIZE    = SIZEOF_INT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        private final static int NAME_OFFSET_SIZE     = SIZEOF_INT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        private final static int VECTOR_LENGTH_SIZE   = SIZEOF_INT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        private final static int DATA_TYPE_SIZE       = SIZEOF_BYTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        private final static int FLAGS_SIZE           = SIZEOF_BYTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        private final static int DATA_UNIT_SIZE       = SIZEOF_BYTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        private final static int DATA_VAR_SIZE        = SIZEOF_BYTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        private final static int DATA_OFFSET_SIZE     = SIZEOF_INT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        final static int ENTRY_LENGTH  = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        final static int NAME_OFFSET   = ENTRY_LENGTH + ENTRY_LENGTH_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        final static int VECTOR_LENGTH = NAME_OFFSET + NAME_OFFSET_SIZE;;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        final static int DATA_TYPE     = VECTOR_LENGTH + VECTOR_LENGTH_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        final static int FLAGS         = DATA_TYPE + DATA_TYPE_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        final static int DATA_UNIT     = FLAGS + FLAGS_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        final static int DATA_VAR      = DATA_UNIT + DATA_UNIT_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        final static int DATA_OFFSET   = DATA_VAR + DATA_VAR_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private String       name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private int          entryStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private int          entryLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private int          vectorLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private PerfDataType dataType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private int          flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private Units        unit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private Variability  variability;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private int          dataOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private int          dataSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private ByteBuffer   data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    PerfDataEntry(ByteBuffer b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        entryStart = b.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        entryLength = b.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // check for valid entry length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (entryLength <= 0 || entryLength > b.limit()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw new InstrumentationException("Invalid entry length: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                               " entryLength = " + entryLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // check if last entry occurs before the eof.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if ((entryStart + entryLength) > b.limit()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throw new InstrumentationException("Entry extends beyond end of buffer: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                                               " entryStart = " + entryStart +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                               " entryLength = " + entryLength +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                               " buffer limit = " + b.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        b.position(entryStart + EntryFieldOffset.NAME_OFFSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        int nameOffset = b.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if ((entryStart + nameOffset) > b.limit()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new InstrumentationException("Invalid name offset: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                               " entryStart = " + entryStart +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                               " nameOffset = " + nameOffset +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                               " buffer limit = " + b.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        b.position(entryStart + EntryFieldOffset.VECTOR_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        vectorLength = b.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        b.position(entryStart + EntryFieldOffset.DATA_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        dataType = PerfDataType.toPerfDataType(b.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        b.position(entryStart + EntryFieldOffset.FLAGS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        flags = b.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        b.position(entryStart + EntryFieldOffset.DATA_UNIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        unit = Units.toUnits(b.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        b.position(entryStart + EntryFieldOffset.DATA_VAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        variability = Variability.toVariability(b.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        b.position(entryStart + EntryFieldOffset.DATA_OFFSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        dataOffset = b.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // read in the perfData item name, casting bytes to chars. skip the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // null terminator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        b.position(entryStart + nameOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // calculate the length of the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        int nameLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        byte c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        for (; (c = b.get()) != (byte)0; nameLength++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        byte[] symbolBytes = new byte[nameLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        b.position(entryStart + nameOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        for (int i = 0; i < nameLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            symbolBytes[i] = b.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // convert name into a String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            name = new String(symbolBytes, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            // should not reach here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            // "UTF-8" is always a known encoding
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   136
            throw new InternalError(e.getMessage(), e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (variability == Variability.INVALID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            throw new InstrumentationException("Invalid variability attribute:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                               " name = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (unit == Units.INVALID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            throw new InstrumentationException("Invalid units attribute: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                               " name = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (vectorLength > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            dataSize = vectorLength * dataType.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            dataSize = dataType.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // check if data beyond the eof.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if ((entryStart + dataOffset + dataSize) > b.limit()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new InstrumentationException("Data extends beyond end of buffer: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                               " entryStart = " + entryStart +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                               " dataOffset = " + dataOffset+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                               " dataSize = " + dataSize +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                               " buffer limit = " + b.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        // Construct a ByteBuffer for the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        b.position(entryStart + dataOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        data = b.slice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        data.order(b.order());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        data.limit(dataSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return entryLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public String name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public PerfDataType type() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return dataType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public Units units() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return unit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public int flags() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        return flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Returns the number of elements in the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public int vectorLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return vectorLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public Variability variability() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return variability;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public ByteBuffer byteData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        data.position(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        assert data.remaining() == vectorLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return data.duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public LongBuffer longData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        LongBuffer lb = data.asLongBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return lb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
}