jdk/src/share/classes/sun/management/ThreadImpl.java
author phh
Thu, 20 Jan 2011 19:34:40 -0500
changeset 8001 192adf3627b7
parent 5506 202f599c92aa
child 8002 5d022b92fbef
permissions -rw-r--r--
6173675: M&M: approximate memory allocation rate/amount per thread Summary: Subclass com.sun.management.ThreadMXBean from java.lang.management.ThreadMXBean, add getAllocatedBytes() and friends to c.s.m.ThreadMXBean and have sun.management.ThreadImpl implement c.s.m.ThreadMXBean rather than j.l.m.ThreadMXBean. Reviewed-by: mchung, alanb, dholmes, emcmanus
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2011, 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: 4156
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: 4156
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: 4156
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    28
import java.lang.management.ManagementFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.management.ThreadInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    32
import javax.management.ObjectName;
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    33
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Implementation class for the thread subsystem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Standard and committed hotspot-specific metrics if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * ManagementFactory.getThreadMXBean() returns an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
    41
class ThreadImpl implements com.sun.management.ThreadMXBean {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private final VMManagement jvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // default for thread contention monitoring is disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private boolean contentionMonitoringEnabled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private boolean cpuTimeEnabled;
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
    48
    private boolean allocatedMemoryEnabled;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Constructor of ThreadImpl class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    ThreadImpl(VMManagement vm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        this.jvm = vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        this.cpuTimeEnabled = jvm.isThreadCpuTimeEnabled();
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
    56
        this.allocatedMemoryEnabled = jvm.isThreadAllocatedMemoryEnabled();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public int getThreadCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        return jvm.getLiveThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public int getPeakThreadCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return jvm.getPeakThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public long getTotalStartedThreadCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return jvm.getTotalThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public int getDaemonThreadCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return jvm.getDaemonThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public boolean isThreadContentionMonitoringSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        return jvm.isThreadContentionMonitoringSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public synchronized boolean isThreadContentionMonitoringEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
       if (!isThreadContentionMonitoringSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                "Thread contention monitoring is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return contentionMonitoringEnabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public boolean isThreadCpuTimeSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return jvm.isOtherThreadCpuTimeSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public boolean isCurrentThreadCpuTimeSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return jvm.isCurrentThreadCpuTimeSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
    95
    public boolean isThreadAllocatedMemorySupported() {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
    96
        return jvm.isThreadAllocatedMemorySupported();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
    97
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
    98
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public boolean isThreadCpuTimeEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (!isThreadCpuTimeSupported() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            !isCurrentThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                "Thread CPU time measurement is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return cpuTimeEnabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   108
    public boolean isThreadAllocatedMemoryEnabled() {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   109
        if (!isThreadAllocatedMemorySupported()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   110
            throw new UnsupportedOperationException(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   111
                "Thread allocated memory measurement is not supported");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   112
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   113
        return allocatedMemoryEnabled;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   114
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   115
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public long[] getAllThreadIds() {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   117
        Util.checkMonitorAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        Thread[] threads = getThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        int length = threads.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        long[] ids = new long[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            Thread t = threads[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            ids[i] = t.getId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return ids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public ThreadInfo getThreadInfo(long id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        long[] ids = new long[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        ids[0] = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        final ThreadInfo[] infos = getThreadInfo(ids, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return infos[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public ThreadInfo getThreadInfo(long id, int maxDepth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        long[] ids = new long[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        ids[0] = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        final ThreadInfo[] infos = getThreadInfo(ids, maxDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return infos[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public ThreadInfo[] getThreadInfo(long[] ids) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return getThreadInfo(ids, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   147
    private void verifyThreadIds(long[] ids) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (ids == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new NullPointerException("Null ids parameter.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   152
        for (int i = 0; i < ids.length; i++) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   153
            if (ids[i] <= 0) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   154
                throw new IllegalArgumentException(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   155
                    "Invalid thread ID parameter: " + ids[i]);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   156
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   157
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   158
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   159
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   160
    public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   161
        verifyThreadIds(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   162
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (maxDepth < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                "Invalid maxDepth parameter: " + maxDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   168
        Util.checkMonitorAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   170
        ThreadInfo[] infos = new ThreadInfo[ids.length]; // nulls
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (maxDepth == Integer.MAX_VALUE) {
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   172
            getThreadInfo1(ids, -1, infos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        } else {
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   174
            getThreadInfo1(ids, maxDepth, infos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return infos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public void setThreadContentionMonitoringEnabled(boolean enable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (!isThreadContentionMonitoringSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                "Thread contention monitoring is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   185
        Util.checkControlAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (contentionMonitoringEnabled != enable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                if (enable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    // if reeabled, reset contention time statistics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    // for all threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    resetContentionTimes0(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                // update the VM of the state change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                setThreadContentionMonitoringEnabled0(enable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                contentionMonitoringEnabled = enable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   203
    private boolean verifyCurrentThreadCpuTime() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        // check if Thread CPU time measurement is supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (!isCurrentThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                "Current thread CPU time measurement is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   209
        return isThreadCpuTimeEnabled();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   210
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   212
    public long getCurrentThreadCpuTime() {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   213
        if (verifyCurrentThreadCpuTime()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   214
            return getThreadTotalCpuTime0(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   216
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public long getThreadCpuTime(long id) {
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   220
        long[] ids = new long[1];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   221
        ids[0] = id;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   222
        final long[] times = getThreadCpuTime(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   223
        return times[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   226
    private boolean verifyThreadCpuTime(long[] ids) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   227
        verifyThreadIds(ids);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // check if Thread CPU time measurement is supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (!isThreadCpuTimeSupported() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            !isCurrentThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                "Thread CPU time measurement is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (!isThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            // support current thread only
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   238
            for (int i = 0; i < ids.length; i++) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   239
                if (ids[i] != Thread.currentThread().getId()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   240
                    throw new UnsupportedOperationException(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   241
                        "Thread CPU time measurement is only supported" +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   242
                        " for the current thread.");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   243
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   247
        return isThreadCpuTimeEnabled();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   248
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   249
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   250
    public long[] getThreadCpuTime(long[] ids) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   251
        boolean verified = verifyThreadCpuTime(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   252
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   253
        int length = ids.length;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   254
        long[] times = java.util.Arrays.fill(new long[length], -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   256
        if (verified) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   257
            if (length == 1) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   258
                long id = ids[0];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   259
                if (id == Thread.currentThread().getId()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   260
                    id = 0;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   261
                }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   262
                getThreadTotalCpuTime0(id);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   263
            } else {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   264
                getThreadTotalCpuTime1(ids, times);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   265
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   267
        return times;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   270
    public long getCurrentThreadUserTime() {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   271
        if (verifyCurrentThreadCpuTime()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   272
            return getThreadUserCpuTime0(0);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   273
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   274
        return -1;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   275
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   276
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   277
    public long getThreadUserTime(long id) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   278
        long[] ids = new long[1];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   279
        ids[0] = id;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   280
        final long[] times = getThreadUserTime(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   281
        return times[0];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   282
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   283
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   284
    public long[] getThreadUserTime(long[] ids) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   285
        boolean verified = verifyThreadCpuTime(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   286
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   287
        int length = ids.length;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   288
        long[] times = java.util.Arrays.fill(new long[length], -1);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   289
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   290
        if (verified) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   291
            if (length == 1) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   292
                long id = ids[0];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   293
                if (id == Thread.currentThread().getId()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   294
                    id = 0;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   295
                }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   296
                times[0] = getThreadUserCpuTime0(id);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   297
            } else {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   298
                getThreadUserCpuTime1(ids, times);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   299
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   300
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   301
        return times;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   302
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public void setThreadCpuTimeEnabled(boolean enable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (!isThreadCpuTimeSupported() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            !isCurrentThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                "Thread CPU time measurement is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   311
        Util.checkControlAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (cpuTimeEnabled != enable) {
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   314
                // notify VM of the state change
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                setThreadCpuTimeEnabled0(enable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                cpuTimeEnabled = enable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   321
    public long getThreadAllocatedBytes(long id) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   322
        long[] ids = new long[1];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   323
        ids[0] = id;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   324
        final long[] sizes = getThreadAllocatedBytes(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   325
        return sizes[0];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   326
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   327
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   328
    private boolean verifyThreadAllocatedMemory(long[] ids) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   329
        verifyThreadIds(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   330
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   331
        // check if Thread allocated memory measurement is supported.
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   332
        if (!isThreadAllocatedMemorySupported()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   333
            throw new UnsupportedOperationException(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   334
                "Thread allocated memory measurement is not supported.");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   335
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   336
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   337
        return isThreadAllocatedMemoryEnabled();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   338
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   339
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   340
    public long[] getThreadAllocatedBytes(long[] ids) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   341
        boolean verified = verifyThreadAllocatedMemory(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   342
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   343
        long[] times = java.util.Arrays.fill(new long[length], -1);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   344
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   345
        if (verified) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   346
            getThreadAllocatedMemory1(ids, sizes);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   347
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   348
        return sizes;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   349
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   350
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   351
    public void setThreadAllocatedMemoryEnabled(boolean enable) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   352
        if (!isThreadAllocatedMemorySupported()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   353
            throw new UnsupportedOperationException(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   354
                "Thread allocated memory measurement is not supported.");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   355
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   356
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   357
        Util.checkControlAccess();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   358
        synchronized (this) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   359
            if (allocatedMemoryEnabled != enable) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   360
                // notify VM of the state change
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   361
                setThreadAllocatedMemoryEnabled0(enable);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   362
                allocatedMemoryEnabled = enable;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   363
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   364
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   365
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   366
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public long[] findMonitorDeadlockedThreads() {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   368
        Util.checkMonitorAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        Thread[] threads = findMonitorDeadlockedThreads0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (threads == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        long[] ids = new long[threads.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        for (int i = 0; i < threads.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            Thread t = threads[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            ids[i] = t.getId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        return ids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public long[] findDeadlockedThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (!isSynchronizerUsageSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                "Monitoring of Synchronizer Usage is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   389
        Util.checkMonitorAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        Thread[] threads = findDeadlockedThreads0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (threads == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        long[] ids = new long[threads.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        for (int i = 0; i < threads.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            Thread t = threads[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            ids[i] = t.getId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return ids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    public void resetPeakThreadCount() {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   405
        Util.checkControlAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        resetPeakThreadCount0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    public boolean isObjectMonitorUsageSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        return jvm.isObjectMonitorUsageSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    public boolean isSynchronizerUsageSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        return jvm.isSynchronizerUsageSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   417
    private void verifyDumpThreads(boolean lockedMonitors,
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   418
                                   boolean lockedSynchronizers) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (lockedMonitors && !isObjectMonitorUsageSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                "Monitoring of Object Monitor Usage is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   423
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (lockedSynchronizers && !isSynchronizerUsageSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                "Monitoring of Synchronizer Usage is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   429
        Util.checkMonitorAccess();
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   430
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   431
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   432
    public ThreadInfo[] getThreadInfo(long[] ids,
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   433
                                      boolean lockedMonitors,
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   434
                                      boolean lockedSynchronizers) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   435
        verifyThreadIds(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   436
        verifyDumpThreads(lockedMonitors, lockedSynchronizers);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        return dumpThreads0(ids, lockedMonitors, lockedSynchronizers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   440
    public ThreadInfo[] dumpAllThreads(boolean lockedMonitors,
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   441
                                       boolean lockedSynchronizers) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   442
        verifyDumpThreads(lockedMonitors, lockedSynchronizers);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        return dumpThreads0(null, lockedMonitors, lockedSynchronizers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    // VM support where maxDepth == -1 to request entire stack dump
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    private static native Thread[] getThreads();
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   448
    private static native void getThreadInfo1(long[] ids,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                                              int maxDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                                              ThreadInfo[] result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    private static native long getThreadTotalCpuTime0(long id);
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   452
    private static native void getThreadTotalCpuTime1(long[] ids, long[] result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    private static native long getThreadUserCpuTime0(long id);
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   454
    private static native void getThreadUserCpuTime1(long[] ids, long[] result);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   455
    private static native void getThreadAllocatedMemory1(long[] ids, long[] result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    private static native void setThreadCpuTimeEnabled0(boolean enable);
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   457
    private static native void setThreadAllocatedMemoryEnabled0(boolean enable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    private static native void setThreadContentionMonitoringEnabled0(boolean enable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    private static native Thread[] findMonitorDeadlockedThreads0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    private static native Thread[] findDeadlockedThreads0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    private static native void resetPeakThreadCount0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    private static native ThreadInfo[] dumpThreads0(long[] ids,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                                                    boolean lockedMonitors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                                                    boolean lockedSynchronizers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    // tid == 0 to reset contention times for all threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    private static native void resetContentionTimes0(long tid);
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   468
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   469
    public ObjectName getObjectName() {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 3111
diff changeset
   470
        return Util.newObjectName(ManagementFactory.THREAD_MXBEAN_NAME);
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   471
    }
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   472
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
}