mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-22 20:14:43 +02:00
8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
Add com.sun.management.getCurrentThreadAllocatedBytes, implement getThreadAllocatedBytes(long) independent of getThreadAllocatedBytes(long[]) Reviewed-by: mchung, dholmes, sspitsyn
This commit is contained in:
parent
916bbc23b7
commit
1bce27d402
11 changed files with 335 additions and 133 deletions
|
@ -160,7 +160,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
*
|
||||
* @return an array of {@code long}, each is a thread ID.
|
||||
*
|
||||
* @throws java.lang.SecurityException if a security manager
|
||||
* @throws SecurityException if a security manager
|
||||
* exists and the caller does not have
|
||||
* ManagementPermission("monitor").
|
||||
*/
|
||||
|
@ -199,7 +199,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
* it does not exist.
|
||||
*
|
||||
* @throws IllegalArgumentException if {@code id <= 0}.
|
||||
* @throws java.lang.SecurityException if a security manager
|
||||
* @throws SecurityException if a security manager
|
||||
* exists and the caller does not have
|
||||
* ManagementPermission("monitor").
|
||||
*/
|
||||
|
@ -237,7 +237,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
*
|
||||
* @throws IllegalArgumentException if any element in the input array
|
||||
* {@code ids} is {@code <= 0}.
|
||||
* @throws java.lang.SecurityException if a security manager
|
||||
* @throws SecurityException if a security manager
|
||||
* exists and the caller does not have
|
||||
* ManagementPermission("monitor").
|
||||
*/
|
||||
|
@ -284,7 +284,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
*
|
||||
* @throws IllegalArgumentException if {@code id <= 0}.
|
||||
* @throws IllegalArgumentException if {@code maxDepth is negative}.
|
||||
* @throws java.lang.SecurityException if a security manager
|
||||
* @throws SecurityException if a security manager
|
||||
* exists and the caller does not have
|
||||
* ManagementPermission("monitor").
|
||||
*
|
||||
|
@ -337,7 +337,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
* @throws IllegalArgumentException if {@code maxDepth is negative}.
|
||||
* @throws IllegalArgumentException if any element in the input array
|
||||
* {@code ids} is {@code <= 0}.
|
||||
* @throws java.lang.SecurityException if a security manager
|
||||
* @throws SecurityException if a security manager
|
||||
* exists and the caller does not have
|
||||
* ManagementPermission("monitor").
|
||||
*
|
||||
|
@ -360,7 +360,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
* @return {@code true} if thread contention monitoring is enabled;
|
||||
* {@code false} otherwise.
|
||||
*
|
||||
* @throws java.lang.UnsupportedOperationException if the Java virtual
|
||||
* @throws UnsupportedOperationException if the Java virtual
|
||||
* machine does not support thread contention monitoring.
|
||||
*
|
||||
* @see #isThreadContentionMonitoringSupported
|
||||
|
@ -374,10 +374,10 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
* @param enable {@code true} to enable;
|
||||
* {@code false} to disable.
|
||||
*
|
||||
* @throws java.lang.UnsupportedOperationException if the Java
|
||||
* @throws UnsupportedOperationException if the Java
|
||||
* virtual machine does not support thread contention monitoring.
|
||||
*
|
||||
* @throws java.lang.SecurityException if a security manager
|
||||
* @throws SecurityException if a security manager
|
||||
* exists and the caller does not have
|
||||
* ManagementPermission("control").
|
||||
*
|
||||
|
@ -394,7 +394,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
* the current thread has executed in user mode or system mode.
|
||||
*
|
||||
* <p>
|
||||
* This is a convenient method for local management use and is
|
||||
* This is a convenience method for local management use and is
|
||||
* equivalent to calling:
|
||||
* <blockquote><pre>
|
||||
* {@link #getThreadCpuTime getThreadCpuTime}(Thread.currentThread().getId());
|
||||
|
@ -403,7 +403,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
* @return the total CPU time for the current thread if CPU time
|
||||
* measurement is enabled; {@code -1} otherwise.
|
||||
*
|
||||
* @throws java.lang.UnsupportedOperationException if the Java
|
||||
* @throws UnsupportedOperationException if the Java
|
||||
* virtual machine does not support CPU time measurement for
|
||||
* the current thread.
|
||||
*
|
||||
|
@ -421,7 +421,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
* not necessarily nanoseconds accuracy.
|
||||
*
|
||||
* <p>
|
||||
* This is a convenient method for local management use and is
|
||||
* This is a convenience method for local management use and is
|
||||
* equivalent to calling:
|
||||
* <blockquote><pre>
|
||||
* {@link #getThreadUserTime getThreadUserTime}(Thread.currentThread().getId());
|
||||
|
@ -430,7 +430,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
* @return the user-level CPU time for the current thread if CPU time
|
||||
* measurement is enabled; {@code -1} otherwise.
|
||||
*
|
||||
* @throws java.lang.UnsupportedOperationException if the Java
|
||||
* @throws UnsupportedOperationException if the Java
|
||||
* virtual machine does not support CPU time measurement for
|
||||
* the current thread.
|
||||
*
|
||||
|
@ -467,7 +467,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
* {@code -1} otherwise.
|
||||
*
|
||||
* @throws IllegalArgumentException if {@code id <= 0}.
|
||||
* @throws java.lang.UnsupportedOperationException if the Java
|
||||
* @throws UnsupportedOperationException if the Java
|
||||
* virtual machine does not support CPU time measurement for
|
||||
* other threads.
|
||||
*
|
||||
|
@ -502,7 +502,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
* {@code -1} otherwise.
|
||||
*
|
||||
* @throws IllegalArgumentException if {@code id <= 0}.
|
||||
* @throws java.lang.UnsupportedOperationException if the Java
|
||||
* @throws UnsupportedOperationException if the Java
|
||||
* virtual machine does not support CPU time measurement for
|
||||
* other threads.
|
||||
*
|
||||
|
@ -548,7 +548,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
* @return {@code true} if thread CPU time measurement is enabled;
|
||||
* {@code false} otherwise.
|
||||
*
|
||||
* @throws java.lang.UnsupportedOperationException if the Java virtual
|
||||
* @throws UnsupportedOperationException if the Java virtual
|
||||
* machine does not support CPU time measurement for other threads
|
||||
* nor for the current thread.
|
||||
*
|
||||
|
@ -564,11 +564,11 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
* @param enable {@code true} to enable;
|
||||
* {@code false} to disable.
|
||||
*
|
||||
* @throws java.lang.UnsupportedOperationException if the Java
|
||||
* @throws UnsupportedOperationException if the Java
|
||||
* virtual machine does not support CPU time measurement for
|
||||
* any threads nor for the current thread.
|
||||
*
|
||||
* @throws java.lang.SecurityException if a security manager
|
||||
* @throws SecurityException if a security manager
|
||||
* exists and the caller does not have
|
||||
* ManagementPermission("control").
|
||||
*
|
||||
|
@ -604,7 +604,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
* @return an array of IDs of the threads that are monitor
|
||||
* deadlocked, if any; {@code null} otherwise.
|
||||
*
|
||||
* @throws java.lang.SecurityException if a security manager
|
||||
* @throws SecurityException if a security manager
|
||||
* exists and the caller does not have
|
||||
* ManagementPermission("monitor").
|
||||
*
|
||||
|
@ -616,7 +616,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
* Resets the peak thread count to the current number of
|
||||
* live threads.
|
||||
*
|
||||
* @throws java.lang.SecurityException if a security manager
|
||||
* @throws SecurityException if a security manager
|
||||
* exists and the caller does not have
|
||||
* ManagementPermission("control").
|
||||
*
|
||||
|
@ -642,10 +642,10 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
* deadlocked waiting for object monitors or ownable synchronizers, if any;
|
||||
* {@code null} otherwise.
|
||||
*
|
||||
* @throws java.lang.SecurityException if a security manager
|
||||
* @throws SecurityException if a security manager
|
||||
* exists and the caller does not have
|
||||
* ManagementPermission("monitor").
|
||||
* @throws java.lang.UnsupportedOperationException if the Java virtual
|
||||
* @throws UnsupportedOperationException if the Java virtual
|
||||
* machine does not support monitoring of ownable synchronizer usage.
|
||||
*
|
||||
* @see #isSynchronizerUsageSupported
|
||||
|
@ -704,10 +704,10 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
* information about a thread whose ID is in the corresponding
|
||||
* element of the input array of IDs.
|
||||
*
|
||||
* @throws java.lang.SecurityException if a security manager
|
||||
* @throws SecurityException if a security manager
|
||||
* exists and the caller does not have
|
||||
* ManagementPermission("monitor").
|
||||
* @throws java.lang.UnsupportedOperationException
|
||||
* @throws UnsupportedOperationException
|
||||
* <ul>
|
||||
* <li>if {@code lockedMonitors} is {@code true} but
|
||||
* the Java virtual machine does not support monitoring
|
||||
|
@ -794,10 +794,10 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
* element of the input array of IDs.
|
||||
*
|
||||
* @throws IllegalArgumentException if {@code maxDepth} is negative.
|
||||
* @throws java.lang.SecurityException if a security manager
|
||||
* @throws SecurityException if a security manager
|
||||
* exists and the caller does not have
|
||||
* ManagementPermission("monitor").
|
||||
* @throws java.lang.UnsupportedOperationException
|
||||
* @throws UnsupportedOperationException
|
||||
* <ul>
|
||||
* <li>if {@code lockedMonitors} is {@code true} but
|
||||
* the Java virtual machine does not support monitoring
|
||||
|
@ -835,10 +835,10 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
*
|
||||
* @return an array of {@link ThreadInfo} for all live threads.
|
||||
*
|
||||
* @throws java.lang.SecurityException if a security manager
|
||||
* @throws SecurityException if a security manager
|
||||
* exists and the caller does not have
|
||||
* ManagementPermission("monitor").
|
||||
* @throws java.lang.UnsupportedOperationException
|
||||
* @throws UnsupportedOperationException
|
||||
* <ul>
|
||||
* <li>if {@code lockedMonitors} is {@code true} but
|
||||
* the Java virtual machine does not support monitoring
|
||||
|
@ -884,10 +884,10 @@ public interface ThreadMXBean extends PlatformManagedObject {
|
|||
* @return an array of {@link ThreadInfo} for all live threads.
|
||||
*
|
||||
* @throws IllegalArgumentException if {@code maxDepth} is negative.
|
||||
* @throws java.lang.SecurityException if a security manager
|
||||
* @throws SecurityException if a security manager
|
||||
* exists and the caller does not have
|
||||
* ManagementPermission("monitor").
|
||||
* @throws java.lang.UnsupportedOperationException
|
||||
* @throws UnsupportedOperationException
|
||||
* <ul>
|
||||
* <li>if {@code lockedMonitors} is {@code true} but
|
||||
* the Java virtual machine does not support monitoring
|
||||
|
|
|
@ -29,6 +29,7 @@ import java.lang.management.ManagementFactory;
|
|||
import java.lang.management.ThreadInfo;
|
||||
import java.lang.management.ThreadMXBean;
|
||||
import javax.management.ObjectName;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Implementation for java.lang.management.ThreadMXBean as well as providing the
|
||||
|
@ -112,11 +113,15 @@ public class ThreadImpl implements ThreadMXBean {
|
|||
return cpuTimeEnabled;
|
||||
}
|
||||
|
||||
protected boolean isThreadAllocatedMemoryEnabled() {
|
||||
private void ensureThreadAllocatedMemorySupported() {
|
||||
if (!isThreadAllocatedMemorySupported()) {
|
||||
throw new UnsupportedOperationException(
|
||||
"Thread allocated memory measurement is not supported");
|
||||
"Thread allocated memory measurement is not supported.");
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean isThreadAllocatedMemoryEnabled() {
|
||||
ensureThreadAllocatedMemorySupported();
|
||||
return allocatedMemoryEnabled;
|
||||
}
|
||||
|
||||
|
@ -155,16 +160,18 @@ public class ThreadImpl implements ThreadMXBean {
|
|||
return getThreadInfo(ids, 0);
|
||||
}
|
||||
|
||||
private void verifyThreadIds(long[] ids) {
|
||||
if (ids == null) {
|
||||
throw new NullPointerException("Null ids parameter.");
|
||||
private void verifyThreadId(long id) {
|
||||
if (id <= 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid thread ID parameter: " + id);
|
||||
}
|
||||
}
|
||||
|
||||
private void verifyThreadIds(long[] ids) {
|
||||
Objects.requireNonNull(ids);
|
||||
|
||||
for (int i = 0; i < ids.length; i++) {
|
||||
if (ids[i] <= 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid thread ID parameter: " + ids[i]);
|
||||
}
|
||||
verifyThreadId(ids[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -342,26 +349,41 @@ public class ThreadImpl implements ThreadMXBean {
|
|||
}
|
||||
}
|
||||
|
||||
protected long getCurrentThreadAllocatedBytes() {
|
||||
if (isThreadAllocatedMemoryEnabled()) {
|
||||
return getThreadAllocatedMemory0(0);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private boolean verifyThreadAllocatedMemory(long id) {
|
||||
verifyThreadId(id);
|
||||
return isThreadAllocatedMemoryEnabled();
|
||||
}
|
||||
|
||||
protected long getThreadAllocatedBytes(long id) {
|
||||
long[] ids = new long[1];
|
||||
ids[0] = id;
|
||||
final long[] sizes = getThreadAllocatedBytes(ids);
|
||||
return sizes[0];
|
||||
boolean verified = verifyThreadAllocatedMemory(id);
|
||||
|
||||
if (verified) {
|
||||
return getThreadAllocatedMemory0(
|
||||
Thread.currentThread().getId() == id ? 0 : id);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private boolean verifyThreadAllocatedMemory(long[] ids) {
|
||||
verifyThreadIds(ids);
|
||||
|
||||
// check if Thread allocated memory measurement is supported.
|
||||
if (!isThreadAllocatedMemorySupported()) {
|
||||
throw new UnsupportedOperationException(
|
||||
"Thread allocated memory measurement is not supported.");
|
||||
}
|
||||
|
||||
return isThreadAllocatedMemoryEnabled();
|
||||
}
|
||||
|
||||
protected long[] getThreadAllocatedBytes(long[] ids) {
|
||||
Objects.requireNonNull(ids);
|
||||
|
||||
if (ids.length == 1) {
|
||||
long size = getThreadAllocatedBytes(ids[0]);
|
||||
return new long[] { size };
|
||||
}
|
||||
|
||||
boolean verified = verifyThreadAllocatedMemory(ids);
|
||||
|
||||
long[] sizes = new long[ids.length];
|
||||
|
@ -374,10 +396,7 @@ public class ThreadImpl implements ThreadMXBean {
|
|||
}
|
||||
|
||||
protected void setThreadAllocatedMemoryEnabled(boolean enable) {
|
||||
if (!isThreadAllocatedMemorySupported()) {
|
||||
throw new UnsupportedOperationException(
|
||||
"Thread allocated memory measurement is not supported.");
|
||||
}
|
||||
ensureThreadAllocatedMemorySupported();
|
||||
|
||||
Util.checkControlAccess();
|
||||
synchronized (this) {
|
||||
|
@ -511,6 +530,7 @@ public class ThreadImpl implements ThreadMXBean {
|
|||
private static native void getThreadTotalCpuTime1(long[] ids, long[] result);
|
||||
private static native long getThreadUserCpuTime0(long id);
|
||||
private static native void getThreadUserCpuTime1(long[] ids, long[] result);
|
||||
private static native long getThreadAllocatedMemory0(long id);
|
||||
private static native void getThreadAllocatedMemory1(long[] ids, long[] result);
|
||||
private static native void setThreadCpuTimeEnabled0(boolean enable);
|
||||
private static native void setThreadAllocatedMemoryEnabled0(boolean enable);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -94,6 +94,13 @@ Java_sun_management_ThreadImpl_getThreadUserCpuTime1
|
|||
JNI_FALSE /* user */);
|
||||
}
|
||||
|
||||
JNIEXPORT jlong JNICALL
|
||||
Java_sun_management_ThreadImpl_getThreadAllocatedMemory0
|
||||
(JNIEnv *env, jclass cls, jlong tid)
|
||||
{
|
||||
return jmm_interface->GetOneThreadAllocatedMemory(env, tid);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_sun_management_ThreadImpl_getThreadAllocatedMemory1
|
||||
(JNIEnv *env, jclass cls, jlongArray ids, jlongArray sizeArray)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue