jdk/src/share/classes/sun/management/ThreadImpl.java
author mchung
Thu, 10 Apr 2008 10:47:13 -0700
changeset 401 ef01e0dccd63
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031) Summary: Add new methods in ManagementFactory class to obtain platform MXBeans Reviewed-by: alanb, dfuchs, emcmanus
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.management.ThreadMXBean;
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    29
import java.lang.management.ManagementFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.management.ThreadInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.management.LockInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.management.MonitorInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    37
import javax.management.ObjectName;
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    38
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Implementation class for the thread subsystem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Standard and committed hotspot-specific metrics if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * ManagementFactory.getThreadMXBean() returns an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
class ThreadImpl implements ThreadMXBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private final VMManagement jvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // default for thread contention monitoring is disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private boolean contentionMonitoringEnabled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private boolean cpuTimeEnabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Constructor of ThreadImpl class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    ThreadImpl(VMManagement vm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.jvm = vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.cpuTimeEnabled = jvm.isThreadCpuTimeEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public int getThreadCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        return jvm.getLiveThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public int getPeakThreadCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return jvm.getPeakThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public long getTotalStartedThreadCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return jvm.getTotalThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public int getDaemonThreadCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return jvm.getDaemonThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public boolean isThreadContentionMonitoringSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return jvm.isThreadContentionMonitoringSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public synchronized boolean isThreadContentionMonitoringEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
       if (!isThreadContentionMonitoringSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                "Thread contention monitoring is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return contentionMonitoringEnabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public boolean isThreadCpuTimeSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return jvm.isOtherThreadCpuTimeSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public boolean isCurrentThreadCpuTimeSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return jvm.isCurrentThreadCpuTimeSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public boolean isThreadCpuTimeEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (!isThreadCpuTimeSupported() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            !isCurrentThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                "Thread CPU time measurement is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return cpuTimeEnabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public long[] getAllThreadIds() {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   108
        Util.checkMonitorAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        Thread[] threads = getThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        int length = threads.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        long[] ids = new long[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            Thread t = threads[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            ids[i] = t.getId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return ids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public ThreadInfo getThreadInfo(long id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (id <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                "Invalid thread ID parameter: " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        long[] ids = new long[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        ids[0] = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        final ThreadInfo[] infos = getThreadInfo(ids, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return infos[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public ThreadInfo getThreadInfo(long id, int maxDepth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (id <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                "Invalid thread ID parameter: " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (maxDepth < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                "Invalid maxDepth parameter: " + maxDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        long[] ids = new long[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        ids[0] = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        final ThreadInfo[] infos = getThreadInfo(ids, maxDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return infos[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public ThreadInfo[] getThreadInfo(long[] ids) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return getThreadInfo(ids, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (ids == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new NullPointerException("Null ids parameter.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (maxDepth < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                "Invalid maxDepth parameter: " + maxDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   162
        Util.checkMonitorAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        ThreadInfo[] infos = new ThreadInfo[ids.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (maxDepth == Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            getThreadInfo0(ids, -1, infos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            getThreadInfo0(ids, maxDepth, infos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return infos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public void setThreadContentionMonitoringEnabled(boolean enable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (!isThreadContentionMonitoringSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                "Thread contention monitoring is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   181
        Util.checkControlAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (contentionMonitoringEnabled != enable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                if (enable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    // if reeabled, reset contention time statistics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    // for all threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    resetContentionTimes0(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                // update the VM of the state change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                setThreadContentionMonitoringEnabled0(enable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                contentionMonitoringEnabled = enable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public long getCurrentThreadCpuTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // check if Thread CPU time measurement is supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (!isCurrentThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                "Current thread CPU time measurement is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (!isThreadCpuTimeEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return getThreadTotalCpuTime0(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public long getThreadCpuTime(long id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        // check if Thread CPU time measurement is supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (!isThreadCpuTimeSupported() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            !isCurrentThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                "Thread CPU Time Measurement is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (!isThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            // support current thread only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            if (id != Thread.currentThread().getId()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    "Thread CPU Time Measurement is only supported" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    " for the current thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (id <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                "Invalid thread ID parameter: " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (!isThreadCpuTimeEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (id == Thread.currentThread().getId()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            // current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            return getThreadTotalCpuTime0(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            return getThreadTotalCpuTime0(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public long getCurrentThreadUserTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        // check if Thread CPU time measurement is supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (!isCurrentThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                "Current thread CPU time measurement is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (!isThreadCpuTimeEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return getThreadUserCpuTime0(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public long getThreadUserTime(long id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // check if Thread CPU time measurement is supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (!isThreadCpuTimeSupported() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            !isCurrentThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                "Thread CPU time measurement is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (!isThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            // support current thread only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            if (id != Thread.currentThread().getId()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    "Thread CPU time measurement is only supported" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    " for the current thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (id <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                "Invalid thread ID parameter: " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (!isThreadCpuTimeEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (id == Thread.currentThread().getId()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            // current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
           return getThreadUserCpuTime0(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
           return getThreadUserCpuTime0(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public void setThreadCpuTimeEnabled(boolean enable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (!isThreadCpuTimeSupported() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            !isCurrentThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                "Thread CPU time measurement is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   303
        Util.checkControlAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if (cpuTimeEnabled != enable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                // update VM of the state change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                setThreadCpuTimeEnabled0(enable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                cpuTimeEnabled = enable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public long[] findMonitorDeadlockedThreads() {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   314
        Util.checkMonitorAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        Thread[] threads = findMonitorDeadlockedThreads0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (threads == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        long[] ids = new long[threads.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        for (int i = 0; i < threads.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            Thread t = threads[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            ids[i] = t.getId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return ids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public long[] findDeadlockedThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if (!isSynchronizerUsageSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                "Monitoring of Synchronizer Usage is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   335
        Util.checkMonitorAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        Thread[] threads = findDeadlockedThreads0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (threads == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        long[] ids = new long[threads.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        for (int i = 0; i < threads.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            Thread t = threads[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            ids[i] = t.getId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        return ids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public void resetPeakThreadCount() {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   351
        Util.checkControlAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        resetPeakThreadCount0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public boolean isObjectMonitorUsageSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        return jvm.isObjectMonitorUsageSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public boolean isSynchronizerUsageSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return jvm.isSynchronizerUsageSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    public ThreadInfo[] getThreadInfo(long[] ids,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                                      boolean lockedMonitors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                                      boolean lockedSynchronizers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (ids == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            throw new NullPointerException("Null ids parameter.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (lockedMonitors && !isObjectMonitorUsageSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                "Monitoring of Object Monitor Usage is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (lockedSynchronizers && !isSynchronizerUsageSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                "Monitoring of Synchronizer Usage is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   379
        Util.checkMonitorAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        return dumpThreads0(ids, lockedMonitors, lockedSynchronizers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public ThreadInfo[] dumpAllThreads(boolean lockedMonitors, boolean lockedSynchronizers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (lockedMonitors && !isObjectMonitorUsageSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                "Monitoring of Object Monitor Usage is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (lockedSynchronizers && !isSynchronizerUsageSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                "Monitoring of Synchronizer Usage is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   394
        Util.checkMonitorAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        return dumpThreads0(null, lockedMonitors, lockedSynchronizers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    // VM support where maxDepth == -1 to request entire stack dump
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    private static native Thread[] getThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    private static native void getThreadInfo0(long[] ids,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                                              int maxDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                                              ThreadInfo[] result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    private static native long getThreadTotalCpuTime0(long id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    private static native long getThreadUserCpuTime0(long id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    private static native void setThreadCpuTimeEnabled0(boolean enable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    private static native void setThreadContentionMonitoringEnabled0(boolean enable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    private static native Thread[] findMonitorDeadlockedThreads0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    private static native Thread[] findDeadlockedThreads0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    private static native void resetPeakThreadCount0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    private static native ThreadInfo[] dumpThreads0(long[] ids,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                                                    boolean lockedMonitors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                                                    boolean lockedSynchronizers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    // tid == 0 to reset contention times for all threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    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
   416
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   417
    public ObjectName getObjectName() {
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   418
        return Util.newObjectName(ManagementFactory.THREAD_MXBEAN_NAME);
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   419
    }
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   420
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
}
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   422