jdk/src/jdk.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/PerfDataBufferImpl.java
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 27941 dba160979011
child 31061 fead7d86d75f
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21278
diff changeset
     2
 * Copyright (c) 2004, 2014, 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.jvmstat.perfdata.monitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.jvmstat.monitor.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.regex.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The base classes for the concrete implementations of the HotSpot
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * PerfData instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Brian Doherty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @see AbstractPerfDataBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public abstract class PerfDataBufferImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * The buffer containing the instrumentation data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    protected ByteBuffer buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * A Map of monitor objects found in the instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    protected Map<String, Monitor> monitors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * The Local Java Virtual Machine Identifier for this buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    protected int lvmid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * A Map of monitor object names to aliases as read in from the alias map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    protected Map<String, ArrayList<String>> aliasMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * A cache of resolved monitor aliases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 22584
diff changeset
    69
    protected Map<String, Monitor> aliasCache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param buffer the ByteBuffer containing the instrumentation data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param lvmid the Local Java Virtual Machine Identifier for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *              instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    protected PerfDataBufferImpl(ByteBuffer buffer, int lvmid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        this.buffer = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        this.lvmid = lvmid;
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 22584
diff changeset
    82
        this.monitors = new TreeMap<>();
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 22584
diff changeset
    83
        this.aliasMap = new HashMap<>();
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 22584
diff changeset
    84
        this.aliasCache = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Get the Local Java Virtual Machine Identifier, or <em>lvmid</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * for the target JVM associated with this instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @return int - the lvmid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public int getLocalVmId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return lvmid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Get a copy of the raw instrumentation data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * This method is used to get a copy of the current bytes in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * instrumentation buffer. It is generally used for transporting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * those bytes over the network.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @return byte[] - a copy of the bytes in the instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        ByteBuffer bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
             * this operation is potentially time consuming, and the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
             * is unused when the getBytes() interface is used. However, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
             * call is necessary in order to synchronize this monitoring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
             * client with the target jvm, which assures that the receiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
             * of the byte[] gets an image that is initialized to a usable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
             * state. Otherwise, they might only  get a snapshot of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
             * empty instrumentation buffer immediately after it was created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                if (monitors.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    buildMonitorMap(monitors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            } catch (MonitorException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                /*
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
   123
                 * just ignore this here and let the receiver of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                 * byte[] detect and handle the problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            bb = buffer.duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        bb.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        byte[] bytes = new byte[bb.limit()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        bb.get(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Get the capacity of the instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @return int - the capacity, or size, of the instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public int getCapacity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return buffer.capacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Get the ByteBuffer containing the instrumentation data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return ByteBuffer - a ByteBuffer object that refers to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *                      instrumentation data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    ByteBuffer getByteBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // receiver is responsible for assuring that the buffer's state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // is that of an initialized target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Build the alias mapping. Uses the default alias map file unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * the sun.jvmstat.perfdata.aliasmap file indicates some other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * file as the source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
27941
dba160979011 8066643: (zipfs) Suppress deprecation warnings in jdk.zipfs module
darcy
parents: 25859
diff changeset
   161
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private void buildAliasMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        assert Thread.holdsLock(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        URL aliasURL = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        String filename = System.getProperty("sun.jvmstat.perfdata.aliasmap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (filename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            File f = new File(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                aliasURL = f.toURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                throw new IllegalArgumentException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            aliasURL = getClass().getResource(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                "/sun/jvmstat/perfdata/resources/aliasmap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        assert aliasURL != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        AliasFileParser aliasParser = new AliasFileParser(aliasURL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            aliasParser.parse(aliasMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            System.err.println("Error processing " + filename + ": "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                               + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } catch (SyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            System.err.println("Syntax error parsing " + filename + ": "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                               + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Find the Monitor object for the named counter by using one of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * aliases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    protected Monitor findByAlias(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        assert Thread.holdsLock(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 22584
diff changeset
   204
        Monitor  m = aliasCache.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (m == null) {
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 22584
diff changeset
   206
            ArrayList<String> al = aliasMap.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (al != null) {
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 22584
diff changeset
   208
                for (Iterator<String> i = al.iterator(); i.hasNext() && m == null; ) {
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 22584
diff changeset
   209
                    String alias = i.next();
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21278
diff changeset
   210
                    m = monitors.get(alias);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Find a named Instrumentation object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * This method will look for the named instrumentation object in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * instrumentation exported by this Java Virtual Machine. If an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * instrumentation object with the given name exists, a Monitor interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * to that object will be return. Otherwise, the method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <tt>null</tt>. The method will map requests for instrumention objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * using old names to their current names, if applicable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param name the name of the Instrumentation object to find.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @return Monitor - the {@link Monitor} object that can be used to
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 27941
diff changeset
   232
     *                   monitor the named instrumentation object, or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *                   <tt>null</tt> if the named object doesn't exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @throws MonitorException Thrown if an error occurs while communicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *                          with the target Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public Monitor findByName(String name) throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        Monitor m = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if (monitors.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                buildMonitorMap(monitors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                buildAliasMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            // look for the requested monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            m = monitors.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (m == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                // not found - load any new monitors, and try again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                getNewMonitors(monitors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                m = monitors.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            if (m == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                // still not found, look for aliases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                m = findByAlias(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Find all Instrumentation objects with names matching the given pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * This method returns a {@link List} of Monitor objects such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * the name of each object matches the given pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param patternString a string containing a pattern as described in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *                      {@link java.util.regex.Pattern}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @return List<Monitor> - a List of {@link Monitor} objects that can be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *                monitor the instrumentation objects whose names match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *                the given pattern. If no instrumentation objects have`
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *                names matching the given pattern, then an empty List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *                is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @throws MonitorException Thrown if an error occurs while communicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *                          with the target Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @see java.util.regex.Pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public List<Monitor> findByPattern(String patternString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                throws MonitorException, PatternSyntaxException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            if (monitors.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                buildMonitorMap(monitors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                getNewMonitors(monitors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        Pattern pattern = Pattern.compile(patternString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        Matcher matcher = pattern.matcher("");
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 22584
diff changeset
   291
        List<Monitor> matches = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 22584
diff changeset
   293
        Set<Map.Entry<String,Monitor>> monitorSet = monitors.entrySet();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 22584
diff changeset
   295
        for (Iterator<Map.Entry<String, Monitor>> i = monitorSet.iterator(); i.hasNext(); /* empty */) {
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 22584
diff changeset
   296
            Map.Entry<String, Monitor> me = i.next();
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 22584
diff changeset
   297
            String name = me.getKey();
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 22584
diff changeset
   298
            Monitor m = me.getValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            // apply pattern to monitor item name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            matcher.reset(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            // if the pattern matches, then add monitor to list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            if (matcher.lookingAt()) {
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 22584
diff changeset
   305
                 matches.add(me.getValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return matches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Get a list of the inserted and removed monitors since last called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @return MonitorStatus - the status of available Monitors for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *                         target Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @throws MonitorException Thrown if communications errors occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *                          while communicating with the target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public MonitorStatus getMonitorStatus() throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (monitors.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                buildMonitorMap(monitors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            return getMonitorStatus(monitors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    // PerfDataBuffer implementation specific classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * get the list of inserted and removed monitors since last called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @param m the map of Monitors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @throws MonitorException Thrown if communications errors occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *                          while communicating with the target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    protected abstract MonitorStatus getMonitorStatus(Map<String, Monitor> m)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                     throws MonitorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * build the map of Monitor objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param m the map of Monitors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @throws MonitorException Thrown if communications errors occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *                          while communicating with the target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    protected abstract void buildMonitorMap(Map<String, Monitor> m) throws MonitorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * get the new Monitor objects from the Map of Monitor objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @param m the map of Monitors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @throws MonitorException Thrown if communications errors occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *                          while communicating with the target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    protected abstract void getNewMonitors(Map<String, Monitor> m) throws MonitorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
}