jdk/src/share/classes/sun/jvmstat/perfdata/monitor/v1_0/PerfDataBuffer.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5627 e636ac7a63a4
child 25401 2e2b5a66a787
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5627
diff changeset
     2
 * Copyright (c) 2004, 2010, 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.v1_0;
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 sun.jvmstat.perfdata.monitor.*;
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
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The concrete implementation of version 1.0 of the HotSpot PerfData
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Instrumentation buffer. This class is responsible for parsing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * instrumentation memory and constructing the necessary objects to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * represent and access the instrumentation objects contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * memory buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Brian Doherty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @see AbstractPerfDataBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class PerfDataBuffer extends PerfDataBufferImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final boolean DEBUG = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final int syncWaitMs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            Integer.getInteger("sun.jvmstat.perdata.syncWaitMs", 5000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final ArrayList EMPTY_LIST = new ArrayList(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * the following constants must be kept in sync with struct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * PerfDataEntry in perfMemory.hpp
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private final static int PERFDATA_ENTRYLENGTH_OFFSET=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final static int PERFDATA_ENTRYLENGTH_SIZE=4;   // sizeof(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private final static int PERFDATA_NAMELENGTH_OFFSET=4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private final static int PERFDATA_NAMELENGTH_SIZE=4;    // sizeof(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private final static int PERFDATA_VECTORLENGTH_OFFSET=8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private final static int PERFDATA_VECTORLENGTH_SIZE=4;  // sizeof(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private final static int PERFDATA_DATATYPE_OFFSET=12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private final static int PERFDATA_DATATYPE_SIZE=1;      // sizeof(byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final static int PERFDATA_FLAGS_OFFSET=13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private final static int PERFDATA_FLAGS_SIZE=1;        // sizeof(byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final static int PERFDATA_DATAUNITS_OFFSET=14;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private final static int PERFDATA_DATAUNITS_SIZE=1;     // sizeof(byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private final static int PERFDATA_DATAATTR_OFFSET=15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private final static int PERFDATA_DATAATTR_SIZE=1;      // sizeof(byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private final static int PERFDATA_NAME_OFFSET=16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    PerfDataBufferPrologue prologue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    int nextEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    int pollForEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    int perfDataItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    long lastModificationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    int lastUsed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    IntegerMonitor overflow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    ArrayList<Monitor> insertedMonitors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Construct a PerfDataBufferImpl instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * This class is dynamically loaded by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * {@link AbstractPerfDataBuffer#createPerfDataBuffer}, and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * constructor is called to instantiate the instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param buffer the buffer containing the instrumentation data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param lvmid the Local Java Virtual Machine Identifier for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *              instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public PerfDataBuffer(ByteBuffer buffer, int lvmid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
           throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        super(buffer, lvmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        prologue = new PerfDataBufferPrologue(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        this.buffer.order(prologue.getByteOrder());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    protected void buildMonitorMap(Map<String, Monitor> map) throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        assert Thread.holdsLock(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        // start at the beginning of the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        buffer.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // create pseudo monitors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        buildPseudoMonitors(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // position buffer to start of the data section
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        buffer.position(prologue.getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        nextEntry = buffer.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        perfDataItem = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        int used = prologue.getUsed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        long modificationTime = prologue.getModificationTimeStamp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        Monitor m = getNextMonitorEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        while (m != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            map.put(m.getName(), m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            m = getNextMonitorEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
         * set the last modification data. These are set to the values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         * recorded before parsing the data structure. This allows the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
         * the data structure to be modified while the Map is being built.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
         * The Map may contain more entries than indicated based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
         * time stamp, but this is handled by ignoring duplicate entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
         * when the Map is updated in getNewMonitors().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        lastUsed = used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        lastModificationTime = modificationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        // synchronize with the target jvm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        synchWithTarget(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // work around 1.4.2 counter inititization bugs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        kludge(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        insertedMonitors = new ArrayList<Monitor>(map.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    protected void getNewMonitors(Map<String, Monitor> map) throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        assert Thread.holdsLock(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int used = prologue.getUsed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        long modificationTime = prologue.getModificationTimeStamp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if ((used > lastUsed) || (lastModificationTime > modificationTime)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            lastUsed = used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            lastModificationTime = modificationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            Monitor monitor = getNextMonitorEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            while (monitor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                String name = monitor.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                // guard against duplicate entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                if (!map.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    map.put(name, monitor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                     * insertedMonitors is null when called from pollFor()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                     * via buildMonitorMap(). Since we update insertedMonitors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                     * at the end of buildMonitorMap(), it's ok to skip the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                     * add here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    if (insertedMonitors != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        insertedMonitors.add(monitor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                monitor = getNextMonitorEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    protected MonitorStatus getMonitorStatus(Map<String, Monitor> map) throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        assert Thread.holdsLock(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        assert insertedMonitors != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // load any new monitors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        getNewMonitors(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // current implementation doesn't support deletion or reuse of entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        ArrayList removed = EMPTY_LIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        ArrayList inserted = insertedMonitors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        insertedMonitors = new ArrayList<Monitor>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return new MonitorStatus(inserted, removed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Build the pseudo monitors used to map the prolog data into counters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    protected void buildPseudoMonitors(Map<String, Monitor> map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        Monitor monitor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        String name = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        IntBuffer ib = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        name = PerfDataBufferPrologue.PERFDATA_MAJOR_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        ib = prologue.majorVersionBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        monitor = new PerfIntegerMonitor(name, Units.NONE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                         Variability.CONSTANT, false, ib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        map.put(name, monitor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        name = PerfDataBufferPrologue.PERFDATA_MINOR_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        ib = prologue.minorVersionBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        monitor = new PerfIntegerMonitor(name, Units.NONE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                         Variability.CONSTANT, false, ib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        map.put(name, monitor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        name = PerfDataBufferPrologue.PERFDATA_BUFFER_SIZE_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        ib = prologue.sizeBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        monitor = new PerfIntegerMonitor(name, Units.BYTES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                         Variability.MONOTONIC, false, ib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        map.put(name, monitor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        name = PerfDataBufferPrologue.PERFDATA_BUFFER_USED_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        ib = prologue.usedBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        monitor = new PerfIntegerMonitor(name, Units.BYTES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                         Variability.MONOTONIC, false, ib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        map.put(name, monitor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        name = PerfDataBufferPrologue.PERFDATA_OVERFLOW_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        ib = prologue.overflowBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        monitor = new PerfIntegerMonitor(name, Units.BYTES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                         Variability.MONOTONIC, false, ib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        map.put(name, monitor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        this.overflow = (IntegerMonitor)monitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        name = PerfDataBufferPrologue.PERFDATA_MODTIMESTAMP_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        LongBuffer lb = prologue.modificationTimeStampBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        monitor = new PerfLongMonitor(name, Units.TICKS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                      Variability.MONOTONIC, false, lb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        map.put(name, monitor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Method to provide a gross level of synchronization with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * target monitored jvm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * gross synchronization works by polling for the hotspot.rt.hrt.ticks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * counter, which is the last counter created by the StatSampler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * initialization code. The counter is updated when the watcher thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * starts scheduling tasks, which is the last thing done in vm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * initialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    protected void synchWithTarget(Map<String, Monitor> map) throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
         * synch must happen with syncWaitMs from now. Default is 5 seconds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
         * which is reasonabally generous and should provide for extreme
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
         * situations like startup delays due to allocation of large ISM heaps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        long timeLimit = System.currentTimeMillis() + syncWaitMs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        String name = "hotspot.rt.hrt.ticks";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        LongMonitor ticks = (LongMonitor)pollFor(map, name, timeLimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
         * loop waiting for the ticks counter to be non zero. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         * an indication that the jvm is initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        log("synchWithTarget: " + lvmid + " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        while (ticks.longValue() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            log(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            try { Thread.sleep(20); } catch (InterruptedException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (System.currentTimeMillis() > timeLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                lognl("failed: " + lvmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                throw new MonitorException("Could Not Synchronize with target");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        lognl("success: " + lvmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Method to poll the instrumentation memory for a counter with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * the given name. The polling period is bounded by the timeLimit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    protected Monitor pollFor(Map<String, Monitor> map, String name, long timeLimit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                      throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        Monitor monitor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        log("polling for: " + lvmid + "," + name + " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        pollForEntry = nextEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        while ((monitor = map.get(name)) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            log(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            try { Thread.sleep(20); } catch (InterruptedException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            long t = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if ((t > timeLimit) || (overflow.intValue() > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                lognl("failed: " + lvmid + "," + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                dumpAll(map, lvmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                throw new MonitorException("Could not find expected counter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            getNewMonitors(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        lognl("success: " + lvmid + "," + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return monitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * method to make adjustments for known counter problems. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * method depends on the availability of certain counters, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * is generally guaranteed by the synchWithTarget() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    protected void kludge(Map<String, Monitor> map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (Boolean.getBoolean("sun.jvmstat.perfdata.disableKludge")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            // bypass all kludges
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        String name = "java.vm.version";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        StringMonitor jvm_version = (StringMonitor)map.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (jvm_version == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            jvm_version = (StringMonitor)findByAlias(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        name = "java.vm.name";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        StringMonitor jvm_name = (StringMonitor)map.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if (jvm_name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            jvm_name = (StringMonitor)findByAlias(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        name = "hotspot.vm.args";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        StringMonitor args = (StringMonitor)map.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (args == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            args = (StringMonitor)findByAlias(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        assert ((jvm_name != null) && (jvm_version != null) && (args != null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (jvm_name.stringValue().indexOf("HotSpot") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (jvm_version.stringValue().startsWith("1.4.2")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                kludgeMantis(map, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * method to repair the 1.4.2 parallel scavenge counters that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * incorrectly initialized by the JVM when UseAdaptiveSizePolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * is set. This bug couldn't be fixed for 1.4.2 FCS due to putback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * restrictions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    private void kludgeMantis(Map<String, Monitor> map, StringMonitor args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
         * the HotSpot 1.4.2 JVM with the +UseParallelGC option along
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
         * with its default +UseAdaptiveSizePolicy option has a bug with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
         * the initialization of the sizes of the eden and survivor spaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
         * See bugid 4890736.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
         * note - use explicit 1.4.2 counter names here - don't update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
         * to latest counter names or attempt to find aliases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        String cname = "hotspot.gc.collector.0.name";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        StringMonitor collector = (StringMonitor)map.get(cname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (collector.stringValue().compareTo("PSScavenge") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            boolean adaptiveSizePolicy = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
             * HotSpot processes the -XX:Flags/.hotspotrc arguments prior to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
             * processing the command line arguments. This allows the command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
             * line arguments to override any defaults set in .hotspotrc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            cname = "hotspot.vm.flags";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            StringMonitor flags = (StringMonitor)map.get(cname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            String allArgs = flags.stringValue() + " " + args.stringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
             * ignore the -XX: prefix as it only applies to the arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
             * passed from the command line (i.e. the invocation api).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
             * arguments passed through .hotspotrc omit the -XX: prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            int ahi = allArgs.lastIndexOf("+AggressiveHeap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            int aspi = allArgs.lastIndexOf("-UseAdaptiveSizePolicy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if (ahi != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                 * +AggressiveHeap was set, check if -UseAdaptiveSizePolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                 * is set after +AggressiveHeap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                if ((aspi != -1) && (aspi > ahi)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    adaptiveSizePolicy = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                 * +AggressiveHeap not set, must be +UseParallelGC. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                 * relative position of -UseAdaptiveSizePolicy is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                 * important in this case, as it will override the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                 * UseParallelGC default (+UseAdaptiveSizePolicy) if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                 * appears anywhere in the JVM arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                if (aspi != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    adaptiveSizePolicy = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (adaptiveSizePolicy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                // adjust the buggy AdaptiveSizePolicy size counters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                // first remove the real counters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                String eden_size = "hotspot.gc.generation.0.space.0.size";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                String s0_size = "hotspot.gc.generation.0.space.1.size";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                String s1_size = "hotspot.gc.generation.0.space.2.size";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                map.remove(eden_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                map.remove(s0_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                map.remove(s1_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                // get the maximum new generation size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                String new_max_name = "hotspot.gc.generation.0.capacity.max";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                LongMonitor new_max = (LongMonitor)map.get(new_max_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                 * replace the real counters with pseudo counters that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                 * initialized to to the correct values. The maximum size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                 * the eden and survivor spaces are supposed to be:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                 *    max_eden_size = new_size - (2*alignment).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                 *    max_survivor_size = new_size - (2*alignment).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                 * since we don't know the alignment value used, and because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                 * of other parallel scavenge bugs that result in oversized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                 * spaces, we just set the maximum size of each space to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                 * full new gen size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                Monitor monitor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                LongBuffer lb = LongBuffer.allocate(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                lb.put(new_max.longValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                monitor = new PerfLongMonitor(eden_size, Units.BYTES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                                              Variability.CONSTANT, false, lb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                map.put(eden_size, monitor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                monitor = new PerfLongMonitor(s0_size, Units.BYTES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                                              Variability.CONSTANT, false, lb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                map.put(s0_size, monitor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                monitor = new PerfLongMonitor(s1_size, Units.BYTES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                                              Variability.CONSTANT, false, lb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                map.put(s1_size, monitor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * method to extract the next monitor entry from the instrumentation memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * assumes that nextEntry is the offset into the byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * at which to start the search for the next entry. method leaves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * next entry pointing to the next entry or to the end of data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    protected Monitor getNextMonitorEntry() throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        Monitor monitor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        // entries are always 4 byte aligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        if ((nextEntry % 4) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            throw new MonitorStructureException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                   "Entry index not properly aligned: " + nextEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        // protect against a corrupted shared memory region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        if ((nextEntry < 0) || (nextEntry > buffer.limit())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            throw new MonitorStructureException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                   "Entry index out of bounds: nextEntry = " + nextEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                   + ", limit = " + buffer.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        // check for the end of the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        if (nextEntry == buffer.limit()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            lognl("getNextMonitorEntry():"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                  + " nextEntry == buffer.limit(): returning");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        buffer.position(nextEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        int entryStart = buffer.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        int entryLength = buffer.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        // check for valid entry length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        if ((entryLength < 0) || (entryLength > buffer.limit())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            throw new MonitorStructureException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                   "Invalid entry length: entryLength = " + entryLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        // check if last entry occurs before the eof.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        if ((entryStart + entryLength) > buffer.limit()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            throw new MonitorStructureException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                   "Entry extends beyond end of buffer: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                   + " entryStart = " + entryStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                   + " entryLength = " + entryLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                   + " buffer limit = " + buffer.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        if (entryLength == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            // end of data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        int nameLength = buffer.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        int vectorLength = buffer.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        byte dataType = buffer.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        byte flags = buffer.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        Units u = Units.toUnits(buffer.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        Variability v = Variability.toVariability(buffer.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        boolean supported = (flags & 0x01) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        // defend against corrupt entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        if ((nameLength <= 0) || (nameLength > entryLength)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            throw new MonitorStructureException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                   "Invalid Monitor name length: " + nameLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        if ((vectorLength < 0) || (vectorLength > entryLength)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            throw new MonitorStructureException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                   "Invalid Monitor vector length: " + vectorLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        // read in the perfData item name, casting bytes to chars. skip the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        // null terminator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        byte[] nameBytes = new byte[nameLength-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        for (int i = 0; i < nameLength-1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            nameBytes[i] = buffer.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        // convert name into a String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        String name = new String(nameBytes, 0, nameLength-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        if (v == Variability.INVALID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            throw new MonitorDataException("Invalid variability attribute:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                                           + " entry index = " + perfDataItem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                                           + " name = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        if (u == Units.INVALID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            throw new MonitorDataException("Invalid units attribute: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                                           + " entry index = " + perfDataItem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                                           + " name = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if (vectorLength == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            // scalar Types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            if (dataType == BasicType.LONG.intValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                offset = entryStart + entryLength - 8;  /* 8 = sizeof(long) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                buffer.position(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                LongBuffer lb = buffer.asLongBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                lb.limit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                monitor = new PerfLongMonitor(name, u, v, supported, lb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                perfDataItem++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                // bad data types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                throw new MonitorTypeException("Invalid Monitor type:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                                    + " entry index = " + perfDataItem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                                    + " name = " + name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                                    + " type = " + dataType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            // vector types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            if (dataType == BasicType.BYTE.intValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                if (u != Units.STRING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    // only byte arrays of type STRING are currently supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    throw new MonitorTypeException("Invalid Monitor type:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                                      + " entry index = " + perfDataItem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                                      + " name = " + name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                                      + " type = " + dataType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                offset = entryStart + PERFDATA_NAME_OFFSET + nameLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                buffer.position(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                ByteBuffer bb = buffer.slice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                bb.limit(vectorLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                bb.position(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                if (v == Variability.CONSTANT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    monitor = new PerfStringConstantMonitor(name, supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                                                            bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                } else if (v == Variability.VARIABLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                    monitor = new PerfStringVariableMonitor(name, supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                                                            bb, vectorLength-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    // Monotonically increasing byte arrays are not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    throw new MonitorDataException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                            "Invalid variability attribute:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                            + " entry index = " + perfDataItem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                            + " name = " + name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                            + " variability = " + v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                perfDataItem++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                // bad data types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                throw new MonitorTypeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                        "Invalid Monitor type:" + " entry index = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                        + perfDataItem + " name = " + name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                        + " type = " + dataType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        // setup index to next entry for next iteration of the loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        nextEntry = entryStart + entryLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        return monitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * Method to dump debugging information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    private void dumpAll(Map map, int lvmid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            Set keys = map.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            System.err.println("Dump for " + lvmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            for (Iterator i = keys.iterator(); i.hasNext(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                Monitor monitor = (Monitor)map.get(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                System.err.println(j + "\t" + monitor.getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                                   + "=" + monitor.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            System.err.println("nextEntry = " + nextEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                               + " pollForEntry = " + pollForEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            System.err.println("Buffer info:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            System.err.println("buffer = " + buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    private void lognl(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            System.err.println(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    private void log(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            System.err.print(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
}