src/java.management/share/classes/sun/management/ThreadImpl.java
author dcubed
Wed, 18 Sep 2019 20:49:13 -0400
changeset 58223 778fc2dcbdaa
parent 58208 0f3c23c374a4
child 58348 c29e49148be7
permissions -rw-r--r--
8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread Reviewed-by: phh, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
47592
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2017, 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
import java.lang.management.ThreadInfo;
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
    30
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
    31
import javax.management.ObjectName;
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    32
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
    34
 * Implementation for java.lang.management.ThreadMXBean as well as providing the
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
    35
 * supporting method for com.sun.management.ThreadMXBean.
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
    36
 * The supporting method for com.sun.management.ThreadMXBean can be moved to
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
    37
 * jdk.management in the future.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
    40
public class ThreadImpl implements ThreadMXBean {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private final VMManagement jvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    // default for thread contention monitoring is disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private boolean contentionMonitoringEnabled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private boolean cpuTimeEnabled;
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
    46
    private boolean allocatedMemoryEnabled;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Constructor of ThreadImpl class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
    51
    protected ThreadImpl(VMManagement vm) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        this.jvm = vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        this.cpuTimeEnabled = jvm.isThreadCpuTimeEnabled();
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
    54
        this.allocatedMemoryEnabled = jvm.isThreadAllocatedMemoryEnabled();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
    57
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public int getThreadCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        return jvm.getLiveThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
    62
    @Override
2
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
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
    67
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public long getTotalStartedThreadCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return jvm.getTotalThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
    72
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public int getDaemonThreadCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return jvm.getDaemonThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
    77
    @Override
2
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
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
    82
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public synchronized boolean isThreadContentionMonitoringEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
       if (!isThreadContentionMonitoringSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                "Thread contention monitoring is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return contentionMonitoringEnabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
    91
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public boolean isThreadCpuTimeSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return jvm.isOtherThreadCpuTimeSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
    96
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public boolean isCurrentThreadCpuTimeSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return jvm.isCurrentThreadCpuTimeSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   101
    protected boolean isThreadAllocatedMemorySupported() {
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   102
        return jvm.isThreadAllocatedMemorySupported();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   103
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   104
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   105
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public boolean isThreadCpuTimeEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (!isThreadCpuTimeSupported() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            !isCurrentThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                "Thread CPU time measurement is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return cpuTimeEnabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   115
    protected boolean isThreadAllocatedMemoryEnabled() {
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   116
        if (!isThreadAllocatedMemorySupported()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   117
            throw new UnsupportedOperationException(
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   118
                "Thread allocated memory measurement is not supported");
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   119
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   120
        return allocatedMemoryEnabled;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   121
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   122
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   123
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public long[] getAllThreadIds() {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   125
        Util.checkMonitorAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        Thread[] threads = getThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        int length = threads.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        long[] ids = new long[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            Thread t = threads[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            ids[i] = t.getId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return ids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   137
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public ThreadInfo getThreadInfo(long id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        long[] ids = new long[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        ids[0] = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        final ThreadInfo[] infos = getThreadInfo(ids, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return infos[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   145
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public ThreadInfo getThreadInfo(long id, int maxDepth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        long[] ids = new long[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        ids[0] = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        final ThreadInfo[] infos = getThreadInfo(ids, maxDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return infos[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   153
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public ThreadInfo[] getThreadInfo(long[] ids) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return getThreadInfo(ids, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   158
    private void verifyThreadIds(long[] ids) {
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   159
        if (ids == null) {
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   160
            throw new NullPointerException("Null ids parameter.");
58208
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47592
diff changeset
   161
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   163
        for (int i = 0; i < ids.length; i++) {
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   164
            if (ids[i] <= 0) {
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   165
                throw new IllegalArgumentException(
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   166
                    "Invalid thread ID parameter: " + ids[i]);
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   167
            }
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   168
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   169
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   170
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   171
    @Override
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   172
    public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   173
        verifyThreadIds(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   174
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (maxDepth < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                "Invalid maxDepth parameter: " + maxDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
30348
9a9e6c13ab1d 8074368: ThreadMXBean.getThreadInfo() corrupts memory when called with empty array for thread ids
jbachorik
parents: 25859
diff changeset
   180
        // ids has been verified to be non-null
9a9e6c13ab1d 8074368: ThreadMXBean.getThreadInfo() corrupts memory when called with empty array for thread ids
jbachorik
parents: 25859
diff changeset
   181
        // an empty array of ids should return an empty array of ThreadInfos
9a9e6c13ab1d 8074368: ThreadMXBean.getThreadInfo() corrupts memory when called with empty array for thread ids
jbachorik
parents: 25859
diff changeset
   182
        if (ids.length == 0) return new ThreadInfo[0];
9a9e6c13ab1d 8074368: ThreadMXBean.getThreadInfo() corrupts memory when called with empty array for thread ids
jbachorik
parents: 25859
diff changeset
   183
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   184
        Util.checkMonitorAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   186
        ThreadInfo[] infos = new ThreadInfo[ids.length]; // nulls
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (maxDepth == Integer.MAX_VALUE) {
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   188
            getThreadInfo1(ids, -1, infos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } else {
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   190
            getThreadInfo1(ids, maxDepth, infos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return infos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   195
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public void setThreadContentionMonitoringEnabled(boolean enable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (!isThreadContentionMonitoringSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                "Thread contention monitoring is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   202
        Util.checkControlAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (contentionMonitoringEnabled != enable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                if (enable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    // if reeabled, reset contention time statistics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    // for all threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    resetContentionTimes0(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                // update the VM of the state change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                setThreadContentionMonitoringEnabled0(enable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                contentionMonitoringEnabled = enable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   220
    private boolean verifyCurrentThreadCpuTime() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        // check if Thread CPU time measurement is supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (!isCurrentThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                "Current thread CPU time measurement is not supported.");
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
        return isThreadCpuTimeEnabled();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   227
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   229
    @Override
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   230
    public long getCurrentThreadCpuTime() {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   231
        if (verifyCurrentThreadCpuTime()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   232
            return getThreadTotalCpuTime0(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   234
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   237
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public long getThreadCpuTime(long id) {
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   239
        long[] ids = new long[1];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   240
        ids[0] = id;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   241
        final long[] times = getThreadCpuTime(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   242
        return times[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   245
    private boolean verifyThreadCpuTime(long[] ids) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   246
        verifyThreadIds(ids);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
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 (!isThreadCpuTimeSupported() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            !isCurrentThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                "Thread CPU time measurement is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (!isThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            // support current thread only
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   257
            for (int i = 0; i < ids.length; i++) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   258
                if (ids[i] != Thread.currentThread().getId()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   259
                    throw new UnsupportedOperationException(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   260
                        "Thread CPU time measurement is only supported" +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   261
                        " for the current thread.");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   262
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   266
        return isThreadCpuTimeEnabled();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   267
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   268
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   269
    protected long[] getThreadCpuTime(long[] ids) {
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   270
        boolean verified = verifyThreadCpuTime(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   271
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   272
        int length = ids.length;
8003
phh
parents: 8002
diff changeset
   273
        long[] times = new long[length];
phh
parents: 8002
diff changeset
   274
        java.util.Arrays.fill(times, -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   276
        if (verified) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   277
            if (length == 1) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   278
                long id = ids[0];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   279
                if (id == Thread.currentThread().getId()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   280
                    id = 0;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   281
                }
8002
5d022b92fbef 7013682: two test checking cpuTime filed java/lang/management/ThreadMXBean
phh
parents: 8001
diff changeset
   282
                times[0] = getThreadTotalCpuTime0(id);
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   283
            } else {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   284
                getThreadTotalCpuTime1(ids, times);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   285
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   287
        return times;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   290
    @Override
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   291
    public long getCurrentThreadUserTime() {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   292
        if (verifyCurrentThreadCpuTime()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   293
            return getThreadUserCpuTime0(0);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   294
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   295
        return -1;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   296
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   297
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   298
    @Override
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   299
    public long getThreadUserTime(long id) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   300
        long[] ids = new long[1];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   301
        ids[0] = id;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   302
        final long[] times = getThreadUserTime(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   303
        return times[0];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   304
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   305
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   306
    protected long[] getThreadUserTime(long[] ids) {
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   307
        boolean verified = verifyThreadCpuTime(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   308
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   309
        int length = ids.length;
8003
phh
parents: 8002
diff changeset
   310
        long[] times = new long[length];
phh
parents: 8002
diff changeset
   311
        java.util.Arrays.fill(times, -1);
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   312
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   313
        if (verified) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   314
            if (length == 1) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   315
                long id = ids[0];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   316
                if (id == Thread.currentThread().getId()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   317
                    id = 0;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   318
                }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   319
                times[0] = getThreadUserCpuTime0(id);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   320
            } else {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   321
                getThreadUserCpuTime1(ids, times);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   322
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   323
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   324
        return times;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   325
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   327
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public void setThreadCpuTimeEnabled(boolean enable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (!isThreadCpuTimeSupported() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            !isCurrentThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                "Thread CPU time measurement 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.checkControlAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            if (cpuTimeEnabled != enable) {
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   338
                // notify VM of the state change
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                setThreadCpuTimeEnabled0(enable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                cpuTimeEnabled = enable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   345
    protected long getThreadAllocatedBytes(long id) {
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   346
        long[] ids = new long[1];
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   347
        ids[0] = id;
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   348
        final long[] sizes = getThreadAllocatedBytes(ids);
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   349
        return sizes[0];
8001
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
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   352
    private boolean verifyThreadAllocatedMemory(long[] ids) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   353
        verifyThreadIds(ids);
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   354
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   355
        // check if Thread allocated memory measurement is supported.
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   356
        if (!isThreadAllocatedMemorySupported()) {
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   357
            throw new UnsupportedOperationException(
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   358
                "Thread allocated memory measurement is not supported.");
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   359
        }
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   360
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   361
        return isThreadAllocatedMemoryEnabled();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   362
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   363
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   364
    protected long[] getThreadAllocatedBytes(long[] ids) {
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   365
        boolean verified = verifyThreadAllocatedMemory(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   366
8003
phh
parents: 8002
diff changeset
   367
        long[] sizes = new long[ids.length];
phh
parents: 8002
diff changeset
   368
        java.util.Arrays.fill(sizes, -1);
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   369
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   370
        if (verified) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   371
            getThreadAllocatedMemory1(ids, sizes);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   372
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   373
        return sizes;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   374
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   375
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   376
    protected void setThreadAllocatedMemoryEnabled(boolean enable) {
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   377
        if (!isThreadAllocatedMemorySupported()) {
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   378
            throw new UnsupportedOperationException(
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   379
                "Thread allocated memory measurement is not supported.");
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   380
        }
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   381
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   382
        Util.checkControlAccess();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   383
        synchronized (this) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   384
            if (allocatedMemoryEnabled != enable) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   385
                // notify VM of the state change
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   386
                setThreadAllocatedMemoryEnabled0(enable);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   387
                allocatedMemoryEnabled = enable;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   388
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   389
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   390
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   391
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   392
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public long[] findMonitorDeadlockedThreads() {
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        Thread[] threads = findMonitorDeadlockedThreads0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (threads == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        long[] ids = new long[threads.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        for (int i = 0; i < threads.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            Thread t = threads[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            ids[i] = t.getId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        return ids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   409
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    public long[] findDeadlockedThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (!isSynchronizerUsageSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                "Monitoring of Synchronizer Usage is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   416
        Util.checkMonitorAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        Thread[] threads = findDeadlockedThreads0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (threads == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        long[] ids = new long[threads.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        for (int i = 0; i < threads.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            Thread t = threads[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            ids[i] = t.getId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        return ids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   431
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    public void resetPeakThreadCount() {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   433
        Util.checkControlAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        resetPeakThreadCount0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   437
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    public boolean isObjectMonitorUsageSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        return jvm.isObjectMonitorUsageSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   442
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public boolean isSynchronizerUsageSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        return jvm.isSynchronizerUsageSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   447
    private void verifyDumpThreads(boolean lockedMonitors,
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   448
                                   boolean lockedSynchronizers) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (lockedMonitors && !isObjectMonitorUsageSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                "Monitoring of Object Monitor Usage is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   453
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (lockedSynchronizers && !isSynchronizerUsageSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                "Monitoring of Synchronizer Usage is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   459
        Util.checkMonitorAccess();
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   460
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   461
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   462
    @Override
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   463
    public ThreadInfo[] getThreadInfo(long[] ids,
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   464
                                      boolean lockedMonitors,
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   465
                                      boolean lockedSynchronizers) {
47592
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   466
        return dumpThreads0(ids, lockedMonitors, lockedSynchronizers,
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   467
                            Integer.MAX_VALUE);
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   468
    }
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   469
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   470
    public ThreadInfo[] getThreadInfo(long[] ids,
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   471
                                      boolean lockedMonitors,
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   472
                                      boolean lockedSynchronizers,
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   473
                                      int maxDepth) {
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   474
        if (maxDepth < 0) {
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   475
            throw new IllegalArgumentException(
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   476
                    "Invalid maxDepth parameter: " + maxDepth);
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   477
        }
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   478
        verifyThreadIds(ids);
30348
9a9e6c13ab1d 8074368: ThreadMXBean.getThreadInfo() corrupts memory when called with empty array for thread ids
jbachorik
parents: 25859
diff changeset
   479
        // ids has been verified to be non-null
9a9e6c13ab1d 8074368: ThreadMXBean.getThreadInfo() corrupts memory when called with empty array for thread ids
jbachorik
parents: 25859
diff changeset
   480
        // an empty array of ids should return an empty array of ThreadInfos
9a9e6c13ab1d 8074368: ThreadMXBean.getThreadInfo() corrupts memory when called with empty array for thread ids
jbachorik
parents: 25859
diff changeset
   481
        if (ids.length == 0) return new ThreadInfo[0];
9a9e6c13ab1d 8074368: ThreadMXBean.getThreadInfo() corrupts memory when called with empty array for thread ids
jbachorik
parents: 25859
diff changeset
   482
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   483
        verifyDumpThreads(lockedMonitors, lockedSynchronizers);
47592
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   484
        return dumpThreads0(ids, lockedMonitors, lockedSynchronizers, maxDepth);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   487
    @Override
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   488
    public ThreadInfo[] dumpAllThreads(boolean lockedMonitors,
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   489
                                       boolean lockedSynchronizers) {
47592
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   490
        return dumpAllThreads(lockedMonitors, lockedSynchronizers,
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   491
                              Integer.MAX_VALUE);
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   492
    }
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   493
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   494
    public ThreadInfo[] dumpAllThreads(boolean lockedMonitors,
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   495
                                       boolean lockedSynchronizers,
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   496
                                       int maxDepth) {
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   497
        if (maxDepth < 0) {
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   498
            throw new IllegalArgumentException(
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   499
                    "Invalid maxDepth parameter: " + maxDepth);
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   500
        }
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   501
        verifyDumpThreads(lockedMonitors, lockedSynchronizers);
47592
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   502
        return dumpThreads0(null, lockedMonitors, lockedSynchronizers, maxDepth);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    // VM support where maxDepth == -1 to request entire stack dump
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    private static native Thread[] getThreads();
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   507
    private static native void getThreadInfo1(long[] ids,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                                              int maxDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                                              ThreadInfo[] result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    private static native long getThreadTotalCpuTime0(long id);
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   511
    private static native void getThreadTotalCpuTime1(long[] ids, long[] result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    private static native long getThreadUserCpuTime0(long id);
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   513
    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
   514
    private static native void getThreadAllocatedMemory1(long[] ids, long[] result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    private static native void setThreadCpuTimeEnabled0(boolean enable);
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents: 5506
diff changeset
   516
    private static native void setThreadAllocatedMemoryEnabled0(boolean enable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    private static native void setThreadContentionMonitoringEnabled0(boolean enable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    private static native Thread[] findMonitorDeadlockedThreads0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    private static native Thread[] findDeadlockedThreads0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    private static native void resetPeakThreadCount0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    private static native ThreadInfo[] dumpThreads0(long[] ids,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                                                    boolean lockedMonitors,
47592
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   523
                                                    boolean lockedSynchronizers,
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   524
                                                    int maxDepth);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    // tid == 0 to reset contention times for all threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    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
   528
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 30348
diff changeset
   529
    @Override
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   530
    public ObjectName getObjectName() {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 3111
diff changeset
   531
        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
   532
    }
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   533
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
}