jdk/src/java.management/share/classes/sun/management/ThreadImpl.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 8003 jdk/src/share/classes/sun/management/ThreadImpl.java@ffba260d6dfa
child 30348 9a9e6c13ab1d
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
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;
8003
phh
parents: 8002
diff changeset
   254
        long[] times = new long[length];
phh
parents: 8002
diff changeset
   255
        java.util.Arrays.fill(times, -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   257
        if (verified) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   258
            if (length == 1) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   259
                long id = ids[0];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   260
                if (id == Thread.currentThread().getId()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   261
                    id = 0;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   262
                }
8002
5d022b92fbef 7013682: two test checking cpuTime filed java/lang/management/ThreadMXBean
phh
parents: 8001
diff changeset
   263
                times[0] = getThreadTotalCpuTime0(id);
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   264
            } else {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   265
                getThreadTotalCpuTime1(ids, times);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   266
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   268
        return times;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   271
    public long getCurrentThreadUserTime() {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   272
        if (verifyCurrentThreadCpuTime()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   273
            return getThreadUserCpuTime0(0);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   274
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   275
        return -1;
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
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   278
    public long getThreadUserTime(long id) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   279
        long[] ids = new long[1];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   280
        ids[0] = id;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   281
        final long[] times = getThreadUserTime(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   282
        return times[0];
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
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   285
    public long[] getThreadUserTime(long[] ids) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   286
        boolean verified = verifyThreadCpuTime(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   287
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   288
        int length = ids.length;
8003
phh
parents: 8002
diff changeset
   289
        long[] times = new long[length];
phh
parents: 8002
diff changeset
   290
        java.util.Arrays.fill(times, -1);
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   291
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   292
        if (verified) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   293
            if (length == 1) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   294
                long id = ids[0];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   295
                if (id == Thread.currentThread().getId()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   296
                    id = 0;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   297
                }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   298
                times[0] = getThreadUserCpuTime0(id);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   299
            } else {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   300
                getThreadUserCpuTime1(ids, times);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   301
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   302
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   303
        return times;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   304
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public void setThreadCpuTimeEnabled(boolean enable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (!isThreadCpuTimeSupported() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            !isCurrentThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                "Thread CPU time measurement is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   313
        Util.checkControlAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (cpuTimeEnabled != enable) {
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   316
                // notify VM of the state change
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                setThreadCpuTimeEnabled0(enable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                cpuTimeEnabled = enable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   323
    public long getThreadAllocatedBytes(long id) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   324
        long[] ids = new long[1];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   325
        ids[0] = id;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   326
        final long[] sizes = getThreadAllocatedBytes(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   327
        return sizes[0];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   328
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   329
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   330
    private boolean verifyThreadAllocatedMemory(long[] ids) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   331
        verifyThreadIds(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   332
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   333
        // check if Thread allocated memory measurement is supported.
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   334
        if (!isThreadAllocatedMemorySupported()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   335
            throw new UnsupportedOperationException(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   336
                "Thread allocated memory measurement is not supported.");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   337
        }
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
        return isThreadAllocatedMemoryEnabled();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   340
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   341
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   342
    public long[] getThreadAllocatedBytes(long[] ids) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   343
        boolean verified = verifyThreadAllocatedMemory(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   344
8003
phh
parents: 8002
diff changeset
   345
        long[] sizes = new long[ids.length];
phh
parents: 8002
diff changeset
   346
        java.util.Arrays.fill(sizes, -1);
8001
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
        if (verified) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   349
            getThreadAllocatedMemory1(ids, sizes);
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
        return sizes;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   352
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   353
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   354
    public void setThreadAllocatedMemoryEnabled(boolean enable) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   355
        if (!isThreadAllocatedMemorySupported()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   356
            throw new UnsupportedOperationException(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   357
                "Thread allocated memory measurement is not supported.");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   358
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   359
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   360
        Util.checkControlAccess();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   361
        synchronized (this) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   362
            if (allocatedMemoryEnabled != enable) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   363
                // notify VM of the state change
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   364
                setThreadAllocatedMemoryEnabled0(enable);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   365
                allocatedMemoryEnabled = enable;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   366
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   367
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   368
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   369
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public long[] findMonitorDeadlockedThreads() {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   371
        Util.checkMonitorAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        Thread[] threads = findMonitorDeadlockedThreads0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (threads == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        long[] ids = new long[threads.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        for (int i = 0; i < threads.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            Thread t = threads[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            ids[i] = t.getId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return ids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    public long[] findDeadlockedThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (!isSynchronizerUsageSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                "Monitoring of Synchronizer Usage is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   392
        Util.checkMonitorAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        Thread[] threads = findDeadlockedThreads0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (threads == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        long[] ids = new long[threads.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        for (int i = 0; i < threads.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            Thread t = threads[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            ids[i] = t.getId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return ids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public void resetPeakThreadCount() {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   408
        Util.checkControlAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        resetPeakThreadCount0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public boolean isObjectMonitorUsageSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        return jvm.isObjectMonitorUsageSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public boolean isSynchronizerUsageSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        return jvm.isSynchronizerUsageSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   420
    private void verifyDumpThreads(boolean lockedMonitors,
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   421
                                   boolean lockedSynchronizers) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (lockedMonitors && !isObjectMonitorUsageSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                "Monitoring of Object Monitor Usage is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   426
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (lockedSynchronizers && !isSynchronizerUsageSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                "Monitoring of Synchronizer Usage is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   432
        Util.checkMonitorAccess();
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   433
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   434
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   435
    public ThreadInfo[] getThreadInfo(long[] ids,
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   436
                                      boolean lockedMonitors,
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   437
                                      boolean lockedSynchronizers) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   438
        verifyThreadIds(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   439
        verifyDumpThreads(lockedMonitors, lockedSynchronizers);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        return dumpThreads0(ids, lockedMonitors, lockedSynchronizers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   443
    public ThreadInfo[] dumpAllThreads(boolean lockedMonitors,
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   444
                                       boolean lockedSynchronizers) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   445
        verifyDumpThreads(lockedMonitors, lockedSynchronizers);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        return dumpThreads0(null, lockedMonitors, lockedSynchronizers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    // VM support where maxDepth == -1 to request entire stack dump
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    private static native Thread[] getThreads();
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   451
    private static native void getThreadInfo1(long[] ids,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                                              int maxDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                                              ThreadInfo[] result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    private static native long getThreadTotalCpuTime0(long id);
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   455
    private static native void getThreadTotalCpuTime1(long[] ids, long[] result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    private static native long getThreadUserCpuTime0(long id);
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   457
    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
   458
    private static native void getThreadAllocatedMemory1(long[] ids, long[] result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    private static native void setThreadCpuTimeEnabled0(boolean enable);
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   460
    private static native void setThreadAllocatedMemoryEnabled0(boolean enable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    private static native void setThreadContentionMonitoringEnabled0(boolean enable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    private static native Thread[] findMonitorDeadlockedThreads0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    private static native Thread[] findDeadlockedThreads0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    private static native void resetPeakThreadCount0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    private static native ThreadInfo[] dumpThreads0(long[] ids,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                                                    boolean lockedMonitors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                                                    boolean lockedSynchronizers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    // tid == 0 to reset contention times for all threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    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
   471
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   472
    public ObjectName getObjectName() {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 3111
diff changeset
   473
        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
   474
    }
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   475
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
}