jdk/src/java.base/share/classes/sun/misc/Perf.java
author avstepan
Tue, 19 May 2015 16:04:14 +0400
changeset 30655 d83f50188ca9
parent 25859 3317bb8137f4
permissions -rw-r--r--
8080422: some docs cleanup for core libs Summary: some docs cleanup Reviewed-by: rriggs, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2002, 2006, 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
package sun.misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * The Perf class provides the ability to attach to an instrumentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * buffer maintained by a Java virtual machine. The instrumentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * buffer may be for the Java virtual machine running the methods of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * this class or it may be for another Java virtual machine on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * same system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * In addition, this class provides methods to create instrumentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * objects in the instrumentation buffer for the Java virtual machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * that is running these methods. It also contains methods for acquiring
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * the value of a platform specific high resolution clock for time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * stamp and interval measurement purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author   Brian Doherty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @since    1.4.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @see      #getPerf
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @see      sun.misc.Perf$GetPerfAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see      java.nio.ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public final class Perf {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static Perf instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final int PERF_MODE_RO = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static final int PERF_MODE_RW = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private Perf() { }    // prevent instantiation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * The GetPerfAction class is a convenience class for acquiring access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * to the singleton Perf instance using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * <code>AccessController.doPrivileged()</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * An instance of this class can be used as the argument to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * <code>AccessController.doPrivileged(PrivilegedAction)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * <p> Here is a suggested idiom for use of this class:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
    70
     * <blockquote><pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * class MyTrustedClass {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *   private static final Perf perf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *       AccessController.doPrivileged(new Perf.GetPerfAction<Perf>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *   ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * }
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
    76
     * }</pre></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * In the presence of a security manager, the <code>MyTrustedClass</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * class in the above example will need to be granted the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * <em>"sun.misc.Perf.getPerf"</em> <code>RuntimePermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * permission in order to successfully acquire the singleton Perf instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Please note that the <em>"sun.misc.Perf.getPerf"</em> permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * is not a JDK specified permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @see  java.security.AccessController#doPrivileged(PrivilegedAction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @see  java.lang.RuntimePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public static class GetPerfAction implements PrivilegedAction<Perf>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
         * Run the <code>Perf.getPerf()</code> method in a privileged context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
         * @see #getPerf
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        public Perf run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return getPerf();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Return a reference to the singleton Perf instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * The getPerf() method returns the singleton instance of the Perf
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * class. The returned object provides the caller with the capability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * for accessing the instrumentation buffer for this or another local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * If a security manager is installed, its <code>checkPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * method is called with a <code>RuntimePermission</code> with a target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * of <em>"sun.misc.Perf.getPerf"</em>. A security exception will result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * if the caller has not been granted this permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Access to the returned <code>Perf</code> object should be protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * by its caller and not passed on to untrusted code. This object can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * be used to attach to the instrumentation buffer provided by this Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * virtual machine or for those of other Java virtual machines running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * on the same system. The instrumentation buffer may contain senstitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * information. API's built on top of this interface may want to provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * finer grained access control to the contents of individual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * instrumentation objects contained within the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Please note that the <em>"sun.misc.Perf.getPerf"</em> permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * is not a JDK specified permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @return       A reference to the singleton Perf instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @throws AccessControlException  if a security manager exists and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *               its <code>checkPermission</code> method doesn't allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *               access to the <em>"sun.misc.Perf.getPerf"</em> target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @see  java.lang.RuntimePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @see  #attach
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public static Perf getPerf()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            Permission perm = new RuntimePermission("sun.misc.Perf.getPerf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            security.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return instance;
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
     * Attach to the instrumentation buffer for the specified Java virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * This method will attach to the instrumentation buffer for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * specified virtual machine. It returns a <code>ByteBuffer</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * that is initialized to access the instrumentation buffer for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * indicated Java virtual machine. The <code>lvmid</code> parameter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * a integer value that uniquely identifies the target local Java virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * machine. It is typically, but not necessarily, the process id of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * the target Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * If the <code>lvmid</code> identifies a Java virtual machine different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * from the one running this method, then the coherency characteristics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * of the buffer are implementation dependent. Implementations that do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * not support named, coherent, shared memory may return a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * <code>ByteBuffer</code> object that contains only a snap shot of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * data in the instrumentation buffer. Implementations that support named,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * coherent, shared memory, may return a <code>ByteBuffer</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * that will be changing dynamically over time as the target Java virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * machine updates its mapping of this buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * If the <code>lvmid</code> is 0 or equal to the actual <code>lvmid</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * for the Java virtual machine running this method, then the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <code>ByteBuffer</code> object will always be coherent and dynamically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * changing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * The attach mode specifies the access permissions requested for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * instrumentation buffer of the target virtual machine. The permitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * access permissions are:
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   174
     * <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <li>"r"  - Read only access. This Java virtual machine has only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * read access to the instrumentation buffer for the target Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * <li>"rw"  - Read/Write access. This Java virtual machine has read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * write access to the instrumentation buffer for the target Java virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * machine. This mode is currently not supported and is reserved for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * future enhancements.
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   182
     * </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param   lvmid            an integer that uniquely identifies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *                           target local Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param   mode             a string indicating the attach mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return  ByteBuffer       a direct allocated byte buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @throws  IllegalArgumentException  The lvmid or mode was invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @throws  IOException      An I/O error occurred while trying to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *                           the instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @throws  OutOfMemoryError The instrumentation buffer could not be mapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *                           into the virtual machine's address space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @see     java.nio.ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public ByteBuffer attach(int lvmid, String mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
           throws IllegalArgumentException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (mode.compareTo("r") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            return attachImpl(null, lvmid, PERF_MODE_RO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        else if (mode.compareTo("rw") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            return attachImpl(null, lvmid, PERF_MODE_RW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            throw new IllegalArgumentException("unknown mode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Attach to the instrumentation buffer for the specified Java virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * machine owned by the given user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * This method behaves just as the <code>attach(int lvmid, String mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * </code> method, except that it only searches for Java virtual machines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * owned by the specified user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param   user             A <code>String</code> object containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *                           name of the user that owns the target Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *                           virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param   lvmid            an integer that uniquely identifies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *                           target local Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param   mode             a string indicating the attach mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @return  ByteBuffer       a direct allocated byte buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @throws  IllegalArgumentException  The lvmid or mode was invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @throws  IOException      An I/O error occurred while trying to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *                           the instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @throws  OutOfMemoryError The instrumentation buffer could not be mapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *                           into the virtual machine's address space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @see     java.nio.ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public ByteBuffer attach(String user, int lvmid, String mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
           throws IllegalArgumentException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (mode.compareTo("r") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            return attachImpl(user, lvmid, PERF_MODE_RO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        else if (mode.compareTo("rw") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return attachImpl(user, lvmid, PERF_MODE_RW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            throw new IllegalArgumentException("unknown mode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Call the implementation specific attach method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * This method calls into the Java virtual machine to perform the platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * specific attach method. Buffers returned from this method are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * internally managed as <code>PhantomRefereces</code> to provide for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * guaranteed, secure release of the native resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param   user             A <code>String</code> object containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *                           name of the user that owns the target Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *                           virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param   lvmid            an integer that uniquely identifies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *                           target local Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @param   mode             a string indicating the attach mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @return  ByteBuffer       a direct allocated byte buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @throws  IllegalArgumentException  The lvmid or mode was invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @throws  IOException      An I/O error occurred while trying to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *                           the instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @throws  OutOfMemoryError The instrumentation buffer could not be mapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *                           into the virtual machine's address space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private ByteBuffer attachImpl(String user, int lvmid, int mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            throws IllegalArgumentException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        final ByteBuffer b = attach(user, lvmid, mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (lvmid == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            // The native instrumentation buffer for this Java virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            // machine is never unmapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            // This is an instrumentation buffer for another Java virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            // machine with native resources that need to be managed. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            // create a duplicate of the native ByteBuffer and manage it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            // with a Cleaner object (PhantomReference). When the duplicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            // becomes only phantomly reachable, the native resources will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            // be released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            final ByteBuffer dup = b.duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            Cleaner.create(dup, new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                            instance.detach(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        catch (Throwable th) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                            // avoid crashing the reference handler thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                            // but provide for some diagnosability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                            assert false : th.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            return dup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Native method to perform the implementation specific attach mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * The implementation of this method may return distinct or identical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * <code>ByteBuffer</code> objects for two distinct calls requesting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * attachment to the same Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * For the Sun HotSpot JVM, two distinct calls to attach to the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * target Java virtual machine will result in two distinct ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * objects returned by this method. This may change in a future release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @param   user             A <code>String</code> object containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *                           name of the user that owns the target Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *                           virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @param   lvmid            an integer that uniquely identifies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *                           target local Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @param   mode             a string indicating the attach mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @return  ByteBuffer       a direct allocated byte buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @throws  IllegalArgumentException  The lvmid or mode was invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @throws  IOException      An I/O error occurred while trying to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *                           the instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @throws  OutOfMemoryError The instrumentation buffer could not be mapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *                           into the virtual machine's address space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private native ByteBuffer attach(String user, int lvmid, int mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                   throws IllegalArgumentException, IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Native method to perform the implementation specific detach mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * If this method is passed a <code>ByteBuffer</code> object that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * not created by the <code>attach</code> method, then the results of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * this method are undefined, with unpredictable and potentially damaging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * effects to the Java virtual machine. To prevent accidental or malicious
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * use of this method, all native ByteBuffer created by the <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * attach</code> method are managed internally as PhantomReferences
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * and resources are freed by the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * If this method is passed a <code>ByteBuffer</code> object created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * by the <code>attach</code> method with a lvmid for the Java virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * machine running this method (lvmid=0, for example), then the detach
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * request is silently ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @param ByteBuffer  A direct allocated byte buffer created by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *                    <code>attach</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @see   java.nio.ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @see   #attach
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private native void detach(ByteBuffer bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Create a <code>long</code> scalar entry in the instrumentation buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * with the given variability characteristic, units, and initial value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Access to the instrument is provided through the returned <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * ByteBuffer</code> object. Typically, this object should be wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * with <code>LongBuffer</code> view object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param   variability the variability characteristic for this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @param   units       the units for this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @param   name        the name of this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @param   value       the initial value for this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @return  ByteBuffer  a direct allocated ByteBuffer object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *                      allows write access to a native memory location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *                      containing a <code>long</code> value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * see sun.misc.perf.Variability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * see sun.misc.perf.Units
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @see java.nio.ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public native ByteBuffer createLong(String name, int variability,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                                        int units, long value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Create a <code>String</code> entry in the instrumentation buffer with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * the given variability characteristic, units, and initial value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * The maximum length of the <code>String</code> stored in this string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * instrument is given in by <code>maxLength</code> parameter. Updates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * to this instrument with <code>String</code> values with lengths greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * than <code>maxLength</code> will be truncated to <code>maxLength</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * The truncated value will be terminated by a null character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * The underlying implementation may further limit the length of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * value, but will continue to preserve the null terminator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Access to the instrument is provided through the returned <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * ByteBuffer</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @param   variability the variability characteristic for this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @param   units       the units for this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @param   name        the name of this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @param   value       the initial value for this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @param   maxLength   the maximum string length for this string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *                      instrument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @return  ByteBuffer  a direct allocated ByteBuffer that allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *                      write access to a native memory location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *                      containing a <code>long</code> value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * see sun.misc.perf.Variability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * see sun.misc.perf.Units
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @see java.nio.ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    public ByteBuffer createString(String name, int variability,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                                   int units, String value, int maxLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        byte[] v = getBytes(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        byte[] v1 = new byte[v.length+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        System.arraycopy(v, 0, v1, 0, v.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        v1[v.length] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        return createByteArray(name, variability, units, v1, Math.max(v1.length, maxLength));
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
     * Create a <code>String</code> entry in the instrumentation buffer with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * the given variability characteristic, units, and initial value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * The maximum length of the <code>String</code> stored in this string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * instrument is implied by the length of the <code>value</code> parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Subsequent updates to the value of this instrument will be truncated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * to this implied maximum length. The truncated value will be terminated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * by a null character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * The underlying implementation may further limit the length of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * initial or subsequent value, but will continue to preserve the null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * terminator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Access to the instrument is provided through the returned <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * ByteBuffer</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @param   variability the variability characteristic for this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @param   units       the units for this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @param   name        the name of this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @param   value       the initial value for this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @return  ByteBuffer  a direct allocated ByteBuffer that allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *                      write access to a native memory location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *                      containing a <code>long</code> value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * see sun.misc.perf.Variability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * see sun.misc.perf.Units
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @see java.nio.ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public ByteBuffer createString(String name, int variability,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                                   int units, String value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        byte[] v = getBytes(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        byte[] v1 = new byte[v.length+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        System.arraycopy(v, 0, v1, 0, v.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        v1[v.length] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        return createByteArray(name, variability, units, v1, v1.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * Create a <code>byte</code> vector entry in the instrumentation buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * with the given variability characteristic, units, and initial value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * The <code>maxLength</code> parameter limits the size of the byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * array instrument such that the initial or subsequent updates beyond
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * this length are silently ignored. No special handling of truncated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * updates is provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * The underlying implementation may further limit the length of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * length of the initial or subsequent value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * Access to the instrument is provided through the returned <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * ByteBuffer</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @param   variability the variability characteristic for this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @param   units       the units for this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @param   name        the name of this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @param   value       the initial value for this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @param   maxLength   the maximum length of this byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @return  ByteBuffer  a direct allocated byte buffer that allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *                      write access to a native memory location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *                      containing a <code>long</code> value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * see sun.misc.perf.Variability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * see sun.misc.perf.Units
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @see java.nio.ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    public native ByteBuffer createByteArray(String name, int variability,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                                             int units, byte[] value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                                             int maxLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * convert string to an array of UTF-8 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    private static byte[] getBytes(String s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        byte[] bytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            bytes = s.getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            // ignore, UTF-8 encoding is always known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * Return the value of the High Resolution Counter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * The High Resolution Counter returns the number of ticks since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * since the start of the Java virtual machine. The resolution of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * the counter is machine dependent and can be determined from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * value return by the {@link #highResFrequency} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @return  the number of ticks of machine dependent resolution since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *          the start of the Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @see #highResFrequency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @see java.lang.System#currentTimeMillis()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    public native long highResCounter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * Returns the frequency of the High Resolution Counter, in ticks per
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * This value can be used to convert the value of the High Resolution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Counter, as returned from a call to the {@link #highResCounter} method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * into the number of seconds since the start of the Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @return  the frequency of the High Resolution Counter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @see #highResCounter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    public native long highResFrequency();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    private static native void registerNatives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        registerNatives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        instance = new Perf();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
}