jdk/src/share/classes/sun/management/counter/perf/PerfInstrumentation.java
author mchung
Tue, 17 Jan 2012 15:55:40 -0800
changeset 11530 a9d059c15b80
parent 11125 99b115114fa3
child 14342 8435a30053c1
permissions -rw-r--r--
7117570: Warnings in sun.mangement.* and its subpackages Reviewed-by: mchung, dsamersoff Contributed-by: kurchi.subhra.hazra@oracle.com
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.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.regex.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class PerfInstrumentation {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private ByteBuffer buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private Prologue prologue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private long lastModificationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private long lastUsed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private int  nextEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private SortedMap<String, Counter>  map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public PerfInstrumentation(ByteBuffer b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        prologue = new Prologue(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        buffer = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        buffer.order(prologue.getByteOrder());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        // Check recognized versions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        int major = getMajorVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        int minor = getMinorVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        // Support only 2.0 version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        if (major < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            throw new InstrumentationException("Unsupported version: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                                               major + "." + minor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public int getMajorVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        return prologue.getMajorVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public int getMinorVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        return prologue.getMinorVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public long getModificationTimeStamp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return prologue.getModificationTimeStamp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    void rewind() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // rewind to the first entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        buffer.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        buffer.position(prologue.getEntryOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        nextEntry = buffer.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        // rebuild all the counters
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11125
diff changeset
    76
        map = new TreeMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return (nextEntry < prologue.getUsed());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    Counter getNextCounter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (! hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if ((nextEntry % 4) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            // entries are always 4 byte aligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new InstrumentationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                "Entry index not properly aligned: " + nextEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (nextEntry < 0  || nextEntry > buffer.limit()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            // defensive check to protect against a corrupted shared memory region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new InstrumentationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                "Entry index out of bounds: nextEntry = " + nextEntry +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                ", limit = " + buffer.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        buffer.position(nextEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        PerfDataEntry entry = new PerfDataEntry(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        nextEntry = nextEntry + entry.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        Counter counter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        PerfDataType type = entry.type();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (type == PerfDataType.BYTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (entry.units() == Units.STRING && entry.vectorLength() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                counter = new PerfStringCounter(entry.name(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                                entry.variability(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                                entry.flags(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                                entry.vectorLength(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                                entry.byteData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            } else if (entry.vectorLength() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                counter = new PerfByteArrayCounter(entry.name(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                                   entry.units(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                                   entry.variability(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                                   entry.flags(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                                   entry.vectorLength(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                                   entry.byteData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
           } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                // ByteArrayCounter must have vectorLength > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        else if (type == PerfDataType.LONG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (entry.vectorLength() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                counter = new PerfLongCounter(entry.name(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                              entry.units(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                              entry.variability(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                              entry.flags(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                              entry.longData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                counter = new PerfLongArrayCounter(entry.name(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                                                   entry.units(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                                   entry.variability(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                                   entry.flags(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                                   entry.vectorLength(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                                   entry.longData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            // FIXME: Should we throw an exception for unsupported type?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            // Currently skip such entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public synchronized List<Counter> getAllCounters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        while (hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            Counter c = getNextCounter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                map.put(c.getName(), c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11125
diff changeset
   157
        return new ArrayList<>(map.values());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public synchronized List<Counter> findByPattern(String patternString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        while (hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            Counter c = getNextCounter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                map.put(c.getName(), c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        Pattern pattern = Pattern.compile(patternString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        Matcher matcher = pattern.matcher("");
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11125
diff changeset
   170
        List<Counter> matches = new ArrayList<>();
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11125
diff changeset
   171
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
11125
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 5506
diff changeset
   173
        for (Map.Entry<String,Counter> me: map.entrySet()) {
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 5506
diff changeset
   174
            String name = me.getKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            // apply pattern to counter name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            matcher.reset(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // if the pattern matches, then add Counter to list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (matcher.lookingAt()) {
11125
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 5506
diff changeset
   181
                matches.add(me.getValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return matches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}