Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/management/ThreadMXBean.java
38831 views
/*1* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.management;2627import java.lang.management.ThreadInfo;28import java.util.Map;2930/**31* Platform-specific management interface for the thread system32* of the Java virtual machine.33* <p>34* This platform extension is only available to a thread35* implementation that supports this extension.36*37* @author Paul Hohensee38* @since 6u2539*/4041@jdk.Exported42public interface ThreadMXBean extends java.lang.management.ThreadMXBean {43/**44* Returns the total CPU time for each thread whose ID is45* in the input array {@code ids} in nanoseconds.46* The returned values are of nanoseconds precision but47* not necessarily nanoseconds accuracy.48* <p>49* This method is equivalent to calling the50* {@link ThreadMXBean#getThreadCpuTime(long)}51* method for each thread ID in the input array {@code ids} and setting the52* returned value in the corresponding element of the returned array.53*54* @param ids an array of thread IDs.55* @return an array of long values, each of which is the amount of CPU56* time the thread whose ID is in the corresponding element of the input57* array of IDs has used,58* if the thread of a specified ID exists, the thread is alive,59* and CPU time measurement is enabled;60* {@code -1} otherwise.61*62* @throws NullPointerException if {@code ids} is {@code null}63* @throws IllegalArgumentException if any element in the input array64* {@code ids} is {@code <=} {@code 0}.65* @throws UnsupportedOperationException if the Java66* virtual machine implementation does not support CPU time67* measurement.68*69* @see ThreadMXBean#getThreadCpuTime(long)70* @see #getThreadUserTime71* @see ThreadMXBean#isThreadCpuTimeSupported72* @see ThreadMXBean#isThreadCpuTimeEnabled73* @see ThreadMXBean#setThreadCpuTimeEnabled74*/75public long[] getThreadCpuTime(long[] ids);7677/**78* Returns the CPU time that each thread whose ID is in the input array79* {@code ids} has executed in user mode in nanoseconds.80* The returned values are of nanoseconds precision but81* not necessarily nanoseconds accuracy.82* <p>83* This method is equivalent to calling the84* {@link ThreadMXBean#getThreadUserTime(long)}85* method for each thread ID in the input array {@code ids} and setting the86* returned value in the corresponding element of the returned array.87*88* @param ids an array of thread IDs.89* @return an array of long values, each of which is the amount of user90* mode CPU time the thread whose ID is in the corresponding element of91* the input array of IDs has used,92* if the thread of a specified ID exists, the thread is alive,93* and CPU time measurement is enabled;94* {@code -1} otherwise.95*96* @throws NullPointerException if {@code ids} is {@code null}97* @throws IllegalArgumentException if any element in the input array98* {@code ids} is {@code <=} {@code 0}.99* @throws UnsupportedOperationException if the Java100* virtual machine implementation does not support CPU time101* measurement.102*103* @see ThreadMXBean#getThreadUserTime(long)104* @see #getThreadCpuTime105* @see ThreadMXBean#isThreadCpuTimeSupported106* @see ThreadMXBean#isThreadCpuTimeEnabled107* @see ThreadMXBean#setThreadCpuTimeEnabled108*/109public long[] getThreadUserTime(long[] ids);110111/**112* Returns an approximation of the total amount of memory, in bytes,113* allocated in heap memory for the current thread.114* The returned value is an approximation because some Java virtual machine115* implementations may use object allocation mechanisms that result in a116* delay between the time an object is allocated and the time its size is117* recorded.118*119* <p>120* This is a convenience method for local management use and is121* equivalent to calling:122* <blockquote><pre>123* {@link #getThreadAllocatedBytes getThreadAllocatedBytes}(Thread.currentThread().getId());124* </pre></blockquote>125*126* @return an approximation of the total memory allocated, in bytes, in127* heap memory for the current thread128* if thread memory allocation measurement is enabled;129* {@code -1} otherwise.130*131* @throws UnsupportedOperationException if the Java virtual132* machine implementation does not support thread memory allocation133* measurement.134*135* @see #isThreadAllocatedMemorySupported136* @see #isThreadAllocatedMemoryEnabled137* @see #setThreadAllocatedMemoryEnabled138*139* @since 8u282140*/141public default long getCurrentThreadAllocatedBytes() {142return getThreadAllocatedBytes(Thread.currentThread().getId());143}144145/**146* Returns an approximation of the total amount of memory, in bytes,147* allocated in heap memory for the thread with the specified ID.148* The returned value is an approximation because some Java virtual machine149* implementations may use object allocation mechanisms that result in a150* delay between the time an object is allocated and the time its size is151* recorded.152* <p>153* If the thread with the specified ID is not alive or does not exist,154* this method returns {@code -1}. If thread memory allocation measurement155* is disabled, this method returns {@code -1}.156* A thread is alive if it has been started and has not yet died.157* <p>158* If thread memory allocation measurement is enabled after the thread has159* started, the Java virtual machine implementation may choose any time up160* to and including the time that the capability is enabled as the point161* where thread memory allocation measurement starts.162*163* @param id the thread ID of a thread164* @return an approximation of the total memory allocated, in bytes, in165* heap memory for the thread with the specified ID166* if the thread with the specified ID exists, the thread is alive,167* and thread memory allocation measurement is enabled;168* {@code -1} otherwise.169*170* @throws IllegalArgumentException if {@code id} {@code <=} {@code 0}.171* @throws UnsupportedOperationException if the Java virtual172* machine implementation does not support thread memory allocation173* measurement.174*175* @see #isThreadAllocatedMemorySupported176* @see #isThreadAllocatedMemoryEnabled177* @see #setThreadAllocatedMemoryEnabled178*/179public long getThreadAllocatedBytes(long id);180181/**182* Returns an approximation of the total amount of memory, in bytes,183* allocated in heap memory for each thread whose ID is in the input184* array {@code ids}.185* The returned values are approximations because some Java virtual machine186* implementations may use object allocation mechanisms that result in a187* delay between the time an object is allocated and the time its size is188* recorded.189* <p>190* This method is equivalent to calling the191* {@link #getThreadAllocatedBytes(long)}192* method for each thread ID in the input array {@code ids} and setting the193* returned value in the corresponding element of the returned array.194*195* @param ids an array of thread IDs.196* @return an array of long values, each of which is an approximation of197* the total memory allocated, in bytes, in heap memory for the thread198* whose ID is in the corresponding element of the input array of IDs.199*200* @throws NullPointerException if {@code ids} is {@code null}201* @throws IllegalArgumentException if any element in the input array202* {@code ids} is {@code <=} {@code 0}.203* @throws UnsupportedOperationException if the Java virtual204* machine implementation does not support thread memory allocation205* measurement.206*207* @see #getThreadAllocatedBytes(long)208* @see #isThreadAllocatedMemorySupported209* @see #isThreadAllocatedMemoryEnabled210* @see #setThreadAllocatedMemoryEnabled211*/212public long[] getThreadAllocatedBytes(long[] ids);213214/**215* Tests if the Java virtual machine implementation supports thread memory216* allocation measurement.217*218* @return219* {@code true}220* if the Java virtual machine implementation supports thread memory221* allocation measurement;222* {@code false} otherwise.223*/224public boolean isThreadAllocatedMemorySupported();225226/**227* Tests if thread memory allocation measurement is enabled.228*229* @return {@code true} if thread memory allocation measurement is enabled;230* {@code false} otherwise.231*232* @throws UnsupportedOperationException if the Java virtual233* machine does not support thread memory allocation measurement.234*235* @see #isThreadAllocatedMemorySupported236*/237public boolean isThreadAllocatedMemoryEnabled();238239/**240* Enables or disables thread memory allocation measurement. The default241* is platform dependent.242*243* @param enable {@code true} to enable;244* {@code false} to disable.245*246* @throws UnsupportedOperationException if the Java virtual247* machine does not support thread memory allocation measurement.248*249* @throws SecurityException if a security manager250* exists and the caller does not have251* ManagementPermission("control").252*253* @see #isThreadAllocatedMemorySupported254*/255public void setThreadAllocatedMemoryEnabled(boolean enable);256257/**258* Returns the thread info for each thread whose ID259* is in the input array <tt>ids</tt>,260* with stack trace of the specified maximum number of elements261* and synchronization information.262* If <tt>maxDepth == 0</tt>, no stack trace of the thread263* will be dumped.264*265* <p>266* This method obtains a snapshot of the thread information267* for each thread including:268* <ul>269* <li>stack trace of the specified maximum number of elements,</li>270* <li>the object monitors currently locked by the thread271* if <tt>lockedMonitors</tt> is <tt>true</tt>, and</li>272* <li>the <a href="{@docRoot}/../api/java/lang/management/LockInfo.html#OwnableSynchronizer">273* ownable synchronizers</a> currently locked by the thread274* if <tt>lockedSynchronizers</tt> is <tt>true</tt>.</li>275* </ul>276* <p>277* This method returns an array of the <tt>ThreadInfo</tt> objects,278* each is the thread information about the thread with the same index279* as in the <tt>ids</tt> array.280* If a thread of the given ID is not alive or does not exist,281* <tt>null</tt> will be set in the corresponding element282* in the returned array. A thread is alive if283* it has been started and has not yet died.284* <p>285* If a thread does not lock any object monitor or <tt>lockedMonitors</tt>286* is <tt>false</tt>, the returned <tt>ThreadInfo</tt> object will have an287* empty <tt>MonitorInfo</tt> array. Similarly, if a thread does not288* lock any synchronizer or <tt>lockedSynchronizers</tt> is <tt>false</tt>,289* the returned <tt>ThreadInfo</tt> object290* will have an empty <tt>LockInfo</tt> array.291*292* <p>293* When both <tt>lockedMonitors</tt> and <tt>lockedSynchronizers</tt>294* parameters are <tt>false</tt>, it is equivalent to calling:295* <blockquote><pre>296* {@link #getThreadInfo(long[], int) getThreadInfo(ids, maxDepth)}297* </pre></blockquote>298*299* <p>300* This method is designed for troubleshooting use, but not for301* synchronization control. It might be an expensive operation.302*303* <p>304* <b>MBeanServer access</b>:<br>305* The mapped type of <tt>ThreadInfo</tt> is306* <tt>CompositeData</tt> with attributes as specified in the307* {@link ThreadInfo#from ThreadInfo.from} method.308*309* @implSpec The default implementation throws310* <tt>UnsupportedOperationException</tt>.311*312* @param ids an array of thread IDs.313* @param lockedMonitors if <tt>true</tt>, retrieves all locked monitors.314* @param lockedSynchronizers if <tt>true</tt>, retrieves all locked315* ownable synchronizers.316* @param maxDepth indicates the maximum number of317* {@link StackTraceElement} to be retrieved from the stack trace.318*319* @return an array of the {@link ThreadInfo} objects, each containing320* information about a thread whose ID is in the corresponding321* element of the input array of IDs.322*323* @throws IllegalArgumentException if <tt>maxDepth</tt> is negative.324* @throws java.lang.SecurityException if a security manager325* exists and the caller does not have326* ManagementPermission("monitor").327* @throws java.lang.UnsupportedOperationException328* <ul>329* <li>if <tt>lockedMonitors</tt> is <tt>true</tt> but330* the Java virtual machine does not support monitoring331* of {@linkplain #isObjectMonitorUsageSupported332* object monitor usage}; or</li>333* <li>if <tt>lockedSynchronizers</tt> is <tt>true</tt> but334* the Java virtual machine does not support monitoring335* of {@linkplain #isSynchronizerUsageSupported336* ownable synchronizer usage}.</li>337* </ul>338*339* @see #isObjectMonitorUsageSupported340* @see #isSynchronizerUsageSupported341*342* @since 8u282343*/344345public default ThreadInfo[] getThreadInfo(long[] ids, boolean lockedMonitors,346boolean lockedSynchronizers, int maxDepth) {347throw new UnsupportedOperationException();348}349350/**351* Returns the thread info for all live threads352* with stack trace of the specified maximum number of elements353* and synchronization information.354* if <tt>maxDepth == 0</tt>, no stack trace of the thread355* will be dumped.356* Some threads included in the returned array357* may have been terminated when this method returns.358*359* <p>360* This method returns an array of {@link ThreadInfo} objects361* as specified in the {@link #getThreadInfo(long[], boolean, boolean, int)}362* method.363*364* @implSpec The default implementation throws365* <tt>UnsupportedOperationException</tt>.366*367* @param lockedMonitors if <tt>true</tt>, dump all locked monitors.368* @param lockedSynchronizers if <tt>true</tt>, dump all locked369* ownable synchronizers.370* @param maxDepth indicates the maximum number of371* {@link StackTraceElement} to be retrieved from the stack trace.372*373* @return an array of {@link ThreadInfo} for all live threads.374*375* @throws IllegalArgumentException if <tt>maxDepth</tt> is negative.376* @throws java.lang.SecurityException if a security manager377* exists and the caller does not have378* ManagementPermission("monitor").379* @throws java.lang.UnsupportedOperationException380* <ul>381* <li>if <tt>lockedMonitors</tt> is <tt>true</tt> but382* the Java virtual machine does not support monitoring383* of {@linkplain #isObjectMonitorUsageSupported384* object monitor usage}; or</li>385* <li>if <tt>lockedSynchronizers</tt> is <tt>true</tt> but386* the Java virtual machine does not support monitoring387* of {@linkplain #isSynchronizerUsageSupported388* ownable synchronizer usage}.</li>389* </ul>390*391* @see #isObjectMonitorUsageSupported392* @see #isSynchronizerUsageSupported393*394* @since 8u282395*/396public default ThreadInfo[] dumpAllThreads(boolean lockedMonitors,397boolean lockedSynchronizers, int maxDepth) {398throw new UnsupportedOperationException();399}400}401402403