aboutsummaryrefslogtreecommitdiff
path: root/agent/src/share/classes/sun/jvm/hotspot/runtime/PerfDataEntry.java
blob: 42481ba8ab9e2a3deadf02c0a1e76f5dbc7bd48b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
 * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

package sun.jvm.hotspot.runtime;

import java.util.*;
import sun.jvm.hotspot.debugger.*;
import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.types.*;
import sun.jvm.hotspot.utilities.*;

public class PerfDataEntry extends VMObject {
    private static JIntField  entryLengthField;
    private static JIntField  nameOffsetField;
    private static JIntField  vectorLengthField;
    private static JByteField dataTypeField;
    private static JByteField flagsField;
    private static JByteField dataUnitsField;
    private static JByteField dataVariabilityField;
    private static JIntField  dataOffsetField;

    static {
        VM.registerVMInitializedObserver(new Observer() {
                public void update(Observable o, Object data) {
                    initialize(VM.getVM().getTypeDataBase());
                }
            });
    }

    private static synchronized void initialize(TypeDataBase db) {
        Type type = db.lookupType("PerfDataEntry");
        entryLengthField = type.getJIntField("entry_length");
        nameOffsetField = type.getJIntField("name_offset");
        vectorLengthField = type.getJIntField("vector_length");
        dataTypeField = type.getJByteField("data_type");
        flagsField = type.getJByteField("flags");
        dataUnitsField = type.getJByteField("data_units");
        dataVariabilityField = type.getJByteField("data_variability");
        dataOffsetField = type.getJIntField("data_offset");
    }

    public PerfDataEntry(Address addr) {
        super(addr);
    }

    // Accessors

    public int entryLength() {
        return (int) entryLengthField.getValue(addr);
    }

    public int nameOffset() {
        return (int) nameOffsetField.getValue(addr);
    }

    public int vectorLength() {
        return (int) vectorLengthField.getValue(addr);
    }

    // returns one of the constants in BasicType class
    public int dataType() {
        char ch = (char) (byte) dataTypeField.getValue(addr);
        return BasicType.charToType(ch);
    }

    public byte flags() {
        return (byte) flagsField.getValue(addr);
    }

    public boolean supported() {
        return (flags() & 0x1) != 0;
    }

    // NOTE: Keep this in sync with PerfData::Units enum in VM code
    public interface PerfDataUnits {
        public static final int U_None   = 1;
        public static final int U_Bytes  = 2;
        public static final int U_Ticks  = 3;
        public static final int U_Events = 4;
        public static final int U_String = 5;
        public static final int U_Hertz  = 6;
    }

    // returns one of the constants in PerfDataUnits
    public int dataUnits() {
        return (int) dataUnitsField.getValue(addr);
    }

    // NOTE: Keep this in sync with PerfData::Variability enum in VM code
    public interface PerfDataVariability {
        public static final int V_Constant  = 1;
        public static final int V_Monotonic = 2;
        public static final int V_Variable  = 3;
    }

    // returns one of the constants in PerfDataVariability
    public int dataVariability() {
        return (int) dataVariabilityField.getValue(addr);
    }

    public int dataOffset() {
        return (int) dataOffsetField.getValue(addr);
    }

    public String name() {
        int off = nameOffset();
        return CStringUtilities.getString(addr.addOffsetTo(off));
    }

    public boolean booleanValue() {
        if (Assert.ASSERTS_ENABLED) {
            Assert.that(vectorLength() == 0 &&
                        dataType() == BasicType.tBoolean, "not a boolean");
        }
        return addr.getJBooleanAt(dataOffset());
    }

    public char charValue() {
        if (Assert.ASSERTS_ENABLED) {
            Assert.that(vectorLength() == 0 &&
                        dataType() == BasicType.tChar, "not a char");
        }
        return addr.getJCharAt(dataOffset());
    }

    public byte byteValue() {
        if (Assert.ASSERTS_ENABLED) {
            Assert.that(vectorLength() == 0 &&
                        dataType() == BasicType.tByte, "not a byte");
        }
        return addr.getJByteAt(dataOffset());

    }

    public short shortValue() {
        if (Assert.ASSERTS_ENABLED) {
            Assert.that(vectorLength() == 0 &&
                        dataType() == BasicType.tShort, "not a short");
        }
        return addr.getJShortAt(dataOffset());
    }

    public int intValue() {
        if (Assert.ASSERTS_ENABLED) {
            Assert.that(vectorLength() == 0 &&
                        dataType() == BasicType.tInt, "not an int");
        }
        return addr.getJIntAt(dataOffset());
    }

    public long longValue() {
        if (Assert.ASSERTS_ENABLED) {
            Assert.that(vectorLength() == 0 &&
                        dataType() == BasicType.tLong, "not a long");
        }
        return addr.getJLongAt(dataOffset());
    }

    public float floatValue() {
        if (Assert.ASSERTS_ENABLED) {
            Assert.that(vectorLength() == 0 &&
                        dataType() == BasicType.tFloat, "not a float");
        }
        return addr.getJFloatAt(dataOffset());
    }

    public double doubleValue() {
        if (Assert.ASSERTS_ENABLED) {
            Assert.that(vectorLength() == 0 &&
                        dataType() == BasicType.tDouble, "not a double");
        }
        return addr.getJDoubleAt(dataOffset());
    }

    public boolean[] booleanArrayValue() {
        int len = vectorLength();
        if (Assert.ASSERTS_ENABLED) {
            Assert.that(len > 0 &&
                        dataType() == BasicType.tBoolean, "not a boolean vector");
        }
        boolean[] res = new boolean[len];
        final int off = dataOffset();
        final long size =  getHeap().getBooleanSize();
        for (int i = 0; i < len; i++) {
            res[i] = addr.getJBooleanAt(off + i * size);
        }
        return res;
    }

    public char[] charArrayValue() {
        int len = vectorLength();
        if (Assert.ASSERTS_ENABLED) {
            Assert.that(len > 0 &&
                        dataType() == BasicType.tChar, "not a char vector");
        }
        char[] res = new char[len];
        final int off = dataOffset();
        final long size = getHeap().getCharSize();
        for (int i = 0; i < len; i++) {
            res[i] = addr.getJCharAt(off + i * size);
        }
        return res;
    }

    public byte[] byteArrayValue() {
        int len = vectorLength();
        if (Assert.ASSERTS_ENABLED) {
            Assert.that(len > 0 &&
                        dataType() == BasicType.tByte, "not a byte vector");
        }
        byte[] res = new byte[len];
        final int off = dataOffset();
        final long size = getHeap().getByteSize();
        for (int i = 0; i < len; i++) {
            res[i] = addr.getJByteAt(off + i * size);
        }
        return res;
    }

    public short[] shortArrayValue() {
        int len = vectorLength();
        if (Assert.ASSERTS_ENABLED) {
            Assert.that(len > 0 &&
                        dataType() == BasicType.tShort, "not a short vector");
        }
        short[] res = new short[len];
        final int off = dataOffset();
        final long size = getHeap().getShortSize();
        for (int i = 0; i < len; i++) {
            res[i] = addr.getJShortAt(off + i * size);
        }
        return res;
    }

    public int[] intArrayValue() {
        int len = vectorLength();
        if (Assert.ASSERTS_ENABLED) {
            Assert.that(len > 0 &&
                        dataType() == BasicType.tInt, "not an int vector");
        }
        int[] res = new int[len];
        final int off = dataOffset();
        final long size = getHeap().getIntSize();
        for (int i = 0; i < len; i++) {
            res[i] = addr.getJIntAt(off + i * size);
        }
        return res;
    }

    public long[] longArrayValue() {
        int len = vectorLength();
        if (Assert.ASSERTS_ENABLED) {
            Assert.that(len > 0 &&
                        dataType() == BasicType.tLong, "not a long vector");
        }
        long[] res = new long[len];
        final int off = dataOffset();
        final long size = getHeap().getLongSize();
        for (int i = 0; i < len; i++) {
            res[i] = addr.getJLongAt(off + i * size);
        }
        return res;
    }

    public float[] floatArrayValue() {
        int len = vectorLength();
        if (Assert.ASSERTS_ENABLED) {
            Assert.that(len > 0 &&
                        dataType() == BasicType.tFloat, "not a float vector");
        }
        float[] res = new float[len];
        final int off = dataOffset();
        final long size = getHeap().getFloatSize();
        for (int i = 0; i < len; i++) {
            res[i] = addr.getJFloatAt(off + i * size);
        }
        return res;
    }

    public double[] doubleArrayValue() {
        int len = vectorLength();
        if (Assert.ASSERTS_ENABLED) {
            Assert.that(len > 0 &&
                        dataType() == BasicType.tDouble, "not a double vector");
        }
        double[] res = new double[len];
        final int off = dataOffset();
        final long size = getHeap().getDoubleSize();
        for (int i = 0; i < len; i++) {
            res[i] = addr.getJDoubleAt(off + i * size);
        }
        return res;
    }

    // value as String
    public String valueAsString() {
        int dataType = dataType();
        int len = vectorLength();
        String str = null;
        if (len == 0) { // scalar
            switch (dataType) {
            case BasicType.tBoolean:
                str = Boolean.toString(booleanValue());
                break;
            case BasicType.tChar:
                str = "'" + Character.toString(charValue()) + "'";
                break;
            case BasicType.tByte:
                str = Byte.toString(byteValue());
                break;
            case BasicType.tShort:
                str = Short.toString(shortValue());
                break;
            case BasicType.tInt:
                str = Integer.toString(intValue());
                break;
            case BasicType.tLong:
                str = Long.toString(longValue());
                break;
            case BasicType.tFloat:
                str = Float.toString(floatValue());
                break;
            case BasicType.tDouble:
                str = Double.toString(doubleValue());
                break;
            default:
                str = "<unknown scalar value>";
                break;
            }
        } else { // vector
            switch (dataType) {
            case BasicType.tBoolean: {
                boolean[] res = booleanArrayValue();
                StringBuffer buf = new StringBuffer();
                buf.append('[');
                for (int i = 0; i < res.length; i++) {
                    buf.append(Boolean.toString(res[i]));
                    buf.append(", ");
                }
                buf.append(']');
                str = buf.toString();
                break;
            }

            case BasicType.tChar: {
                // char[] is returned as a String
                str = new String(charArrayValue());
                break;
            }

            case BasicType.tByte: {
                // byte[] is returned as a String
                try {
                    str = new String(byteArrayValue(), "US-ASCII");
                } catch (java.io.UnsupportedEncodingException e) {
                    str = "can't decode string : " + e.getMessage();
                }
                break;
            }

            case BasicType.tShort: {
                short[] res = shortArrayValue();
                StringBuffer buf = new StringBuffer();
                buf.append('[');
                for (int i = 0; i < res.length; i++) {
                    buf.append(Short.toString(res[i]));
                    buf.append(", ");
                }
                buf.append(']');
                str = buf.toString();
                break;
            }

            case BasicType.tInt: {
                int[] res = intArrayValue();
                StringBuffer buf = new StringBuffer();
                buf.append('[');
                for (int i = 0; i < res.length; i++) {
                    buf.append(Integer.toString(res[i]));
                    buf.append(", ");
                }
                buf.append(']');
                str = buf.toString();
                break;
            }

            case BasicType.tLong: {
                long[] res = longArrayValue();
                StringBuffer buf = new StringBuffer();
                buf.append('[');
                for (int i = 0; i < res.length; i++) {
                    buf.append(Long.toString(res[i]));
                    buf.append(", ");
                }
                buf.append(']');
                str = buf.toString();
                break;
            }

            case BasicType.tFloat: {
                float[] res = floatArrayValue();
                StringBuffer buf = new StringBuffer();
                buf.append('[');
                for (int i = 0; i < res.length; i++) {
                    buf.append(Float.toString(res[i]));
                    buf.append(", ");
                }
                buf.append(']');
                str = buf.toString();
                break;
            }

            case BasicType.tDouble: {
                double[] res = doubleArrayValue();
                StringBuffer buf = new StringBuffer();
                buf.append('[');
                for (int i = 0; i < res.length; i++) {
                    buf.append(Double.toString(res[i]));
                    buf.append(", ");
                }
                buf.append(']');
                str = buf.toString();
                break;
            }

            default:
                str = "<unknown vector value>";
                break;
            }
        }

        // add units
        switch (dataUnits()) {
        case PerfDataUnits.U_None:
            break;
        case PerfDataUnits.U_Bytes:
            str += " byte(s)";
            break;
        case PerfDataUnits.U_Ticks:
            str += " tick(s)";
            break;
        case PerfDataUnits.U_Events:
            str += " event(s)";
            break;
        case PerfDataUnits.U_String:
            break;
        case PerfDataUnits.U_Hertz:
            str += " Hz";
            break;
        }

        return str;
    }

    // -- Internals only below this point
    private ObjectHeap getHeap() {
        return VM.getVM().getObjectHeap();
    }
}