Skip to content

Commit 1bce27d

Browse files
author
Paul Hohensee
committed
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
1 parent 916bbc2 commit 1bce27d

File tree

11 files changed

+325
-123
lines changed

11 files changed

+325
-123
lines changed

src/hotspot/share/include/jmm.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -50,8 +50,9 @@ enum {
5050
JMM_VERSION_1_2 = 0x20010200, // JDK 7
5151
JMM_VERSION_1_2_1 = 0x20010201, // JDK 7 GA
5252
JMM_VERSION_1_2_2 = 0x20010202,
53-
JMM_VERSION_2 = 0x20020000, // JDK 10
54-
JMM_VERSION = 0x20020000
53+
JMM_VERSION_2 = 0x20020000, // JDK 10
54+
JMM_VERSION_3 = 0x20030000, // JDK 14
55+
JMM_VERSION = JMM_VERSION_3
5556
};
5657

5758
typedef struct {
@@ -239,6 +240,9 @@ typedef struct jmmInterface_1_ {
239240
jobject (JNICALL *GetMemoryPoolUsage) (JNIEnv* env, jobject pool);
240241
jobject (JNICALL *GetPeakMemoryPoolUsage) (JNIEnv* env, jobject pool);
241242

243+
jlong (JNICALL *GetOneThreadAllocatedMemory)
244+
(JNIEnv *env,
245+
jlong thread_id);
242246
void (JNICALL *GetThreadAllocatedMemory)
243247
(JNIEnv *env,
244248
jlongArray ids,

src/hotspot/share/services/management.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,6 +2068,31 @@ jlong Management::ticks_to_ms(jlong ticks) {
20682068
}
20692069
#endif // INCLUDE_MANAGEMENT
20702070

2071+
// Gets the amount of memory allocated on the Java heap for a single thread.
2072+
// Returns -1 if the thread does not exist or has terminated.
2073+
JVM_ENTRY(jlong, jmm_GetOneThreadAllocatedMemory(JNIEnv *env, jlong thread_id))
2074+
if (thread_id < 0) {
2075+
THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
2076+
"Invalid thread ID", -1);
2077+
}
2078+
2079+
if (thread_id == 0) {
2080+
// current thread
2081+
if (THREAD->is_Java_thread()) {
2082+
return ((JavaThread*)THREAD)->cooked_allocated_bytes();
2083+
}
2084+
return -1;
2085+
}
2086+
2087+
ThreadsListHandle tlh;
2088+
JavaThread* java_thread = tlh.list()->find_JavaThread_from_java_tid(thread_id);
2089+
2090+
if (java_thread != NULL) {
2091+
return java_thread->cooked_allocated_bytes();
2092+
}
2093+
return -1;
2094+
JVM_END
2095+
20712096
// Gets an array containing the amount of memory allocated on the Java
20722097
// heap for a set of threads (in bytes). Each element of the array is
20732098
// the amount of memory allocated for the thread ID specified in the
@@ -2192,6 +2217,7 @@ const struct jmmInterface_1_ jmm_interface = {
21922217
jmm_GetMemoryManagers,
21932218
jmm_GetMemoryPoolUsage,
21942219
jmm_GetPeakMemoryPoolUsage,
2220+
jmm_GetOneThreadAllocatedMemory,
21952221
jmm_GetThreadAllocatedMemory,
21962222
jmm_GetMemoryUsage,
21972223
jmm_GetLongAttribute,

src/java.management/share/classes/java/lang/management/ThreadMXBean.java

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
160160
*
161161
* @return an array of {@code long}, each is a thread ID.
162162
*
163-
* @throws java.lang.SecurityException if a security manager
163+
* @throws SecurityException if a security manager
164164
* exists and the caller does not have
165165
* ManagementPermission("monitor").
166166
*/
@@ -199,7 +199,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
199199
* it does not exist.
200200
*
201201
* @throws IllegalArgumentException if {@code id <= 0}.
202-
* @throws java.lang.SecurityException if a security manager
202+
* @throws SecurityException if a security manager
203203
* exists and the caller does not have
204204
* ManagementPermission("monitor").
205205
*/
@@ -237,7 +237,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
237237
*
238238
* @throws IllegalArgumentException if any element in the input array
239239
* {@code ids} is {@code <= 0}.
240-
* @throws java.lang.SecurityException if a security manager
240+
* @throws SecurityException if a security manager
241241
* exists and the caller does not have
242242
* ManagementPermission("monitor").
243243
*/
@@ -284,7 +284,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
284284
*
285285
* @throws IllegalArgumentException if {@code id <= 0}.
286286
* @throws IllegalArgumentException if {@code maxDepth is negative}.
287-
* @throws java.lang.SecurityException if a security manager
287+
* @throws SecurityException if a security manager
288288
* exists and the caller does not have
289289
* ManagementPermission("monitor").
290290
*
@@ -337,7 +337,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
337337
* @throws IllegalArgumentException if {@code maxDepth is negative}.
338338
* @throws IllegalArgumentException if any element in the input array
339339
* {@code ids} is {@code <= 0}.
340-
* @throws java.lang.SecurityException if a security manager
340+
* @throws SecurityException if a security manager
341341
* exists and the caller does not have
342342
* ManagementPermission("monitor").
343343
*
@@ -360,7 +360,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
360360
* @return {@code true} if thread contention monitoring is enabled;
361361
* {@code false} otherwise.
362362
*
363-
* @throws java.lang.UnsupportedOperationException if the Java virtual
363+
* @throws UnsupportedOperationException if the Java virtual
364364
* machine does not support thread contention monitoring.
365365
*
366366
* @see #isThreadContentionMonitoringSupported
@@ -374,10 +374,10 @@ public interface ThreadMXBean extends PlatformManagedObject {
374374
* @param enable {@code true} to enable;
375375
* {@code false} to disable.
376376
*
377-
* @throws java.lang.UnsupportedOperationException if the Java
377+
* @throws UnsupportedOperationException if the Java
378378
* virtual machine does not support thread contention monitoring.
379379
*
380-
* @throws java.lang.SecurityException if a security manager
380+
* @throws SecurityException if a security manager
381381
* exists and the caller does not have
382382
* ManagementPermission("control").
383383
*
@@ -394,7 +394,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
394394
* the current thread has executed in user mode or system mode.
395395
*
396396
* <p>
397-
* This is a convenient method for local management use and is
397+
* This is a convenience method for local management use and is
398398
* equivalent to calling:
399399
* <blockquote><pre>
400400
* {@link #getThreadCpuTime getThreadCpuTime}(Thread.currentThread().getId());
@@ -403,7 +403,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
403403
* @return the total CPU time for the current thread if CPU time
404404
* measurement is enabled; {@code -1} otherwise.
405405
*
406-
* @throws java.lang.UnsupportedOperationException if the Java
406+
* @throws UnsupportedOperationException if the Java
407407
* virtual machine does not support CPU time measurement for
408408
* the current thread.
409409
*
@@ -421,7 +421,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
421421
* not necessarily nanoseconds accuracy.
422422
*
423423
* <p>
424-
* This is a convenient method for local management use and is
424+
* This is a convenience method for local management use and is
425425
* equivalent to calling:
426426
* <blockquote><pre>
427427
* {@link #getThreadUserTime getThreadUserTime}(Thread.currentThread().getId());
@@ -430,7 +430,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
430430
* @return the user-level CPU time for the current thread if CPU time
431431
* measurement is enabled; {@code -1} otherwise.
432432
*
433-
* @throws java.lang.UnsupportedOperationException if the Java
433+
* @throws UnsupportedOperationException if the Java
434434
* virtual machine does not support CPU time measurement for
435435
* the current thread.
436436
*
@@ -467,7 +467,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
467467
* {@code -1} otherwise.
468468
*
469469
* @throws IllegalArgumentException if {@code id <= 0}.
470-
* @throws java.lang.UnsupportedOperationException if the Java
470+
* @throws UnsupportedOperationException if the Java
471471
* virtual machine does not support CPU time measurement for
472472
* other threads.
473473
*
@@ -502,7 +502,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
502502
* {@code -1} otherwise.
503503
*
504504
* @throws IllegalArgumentException if {@code id <= 0}.
505-
* @throws java.lang.UnsupportedOperationException if the Java
505+
* @throws UnsupportedOperationException if the Java
506506
* virtual machine does not support CPU time measurement for
507507
* other threads.
508508
*
@@ -548,7 +548,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
548548
* @return {@code true} if thread CPU time measurement is enabled;
549549
* {@code false} otherwise.
550550
*
551-
* @throws java.lang.UnsupportedOperationException if the Java virtual
551+
* @throws UnsupportedOperationException if the Java virtual
552552
* machine does not support CPU time measurement for other threads
553553
* nor for the current thread.
554554
*
@@ -564,11 +564,11 @@ public interface ThreadMXBean extends PlatformManagedObject {
564564
* @param enable {@code true} to enable;
565565
* {@code false} to disable.
566566
*
567-
* @throws java.lang.UnsupportedOperationException if the Java
567+
* @throws UnsupportedOperationException if the Java
568568
* virtual machine does not support CPU time measurement for
569569
* any threads nor for the current thread.
570570
*
571-
* @throws java.lang.SecurityException if a security manager
571+
* @throws SecurityException if a security manager
572572
* exists and the caller does not have
573573
* ManagementPermission("control").
574574
*
@@ -604,7 +604,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
604604
* @return an array of IDs of the threads that are monitor
605605
* deadlocked, if any; {@code null} otherwise.
606606
*
607-
* @throws java.lang.SecurityException if a security manager
607+
* @throws SecurityException if a security manager
608608
* exists and the caller does not have
609609
* ManagementPermission("monitor").
610610
*
@@ -616,7 +616,7 @@ public interface ThreadMXBean extends PlatformManagedObject {
616616
* Resets the peak thread count to the current number of
617617
* live threads.
618618
*
619-
* @throws java.lang.SecurityException if a security manager
619+
* @throws SecurityException if a security manager
620620
* exists and the caller does not have
621621
* ManagementPermission("control").
622622
*
@@ -642,10 +642,10 @@ public interface ThreadMXBean extends PlatformManagedObject {
642642
* deadlocked waiting for object monitors or ownable synchronizers, if any;
643643
* {@code null} otherwise.
644644
*
645-
* @throws java.lang.SecurityException if a security manager
645+
* @throws SecurityException if a security manager
646646
* exists and the caller does not have
647647
* ManagementPermission("monitor").
648-
* @throws java.lang.UnsupportedOperationException if the Java virtual
648+
* @throws UnsupportedOperationException if the Java virtual
649649
* machine does not support monitoring of ownable synchronizer usage.
650650
*
651651
* @see #isSynchronizerUsageSupported
@@ -704,10 +704,10 @@ public interface ThreadMXBean extends PlatformManagedObject {
704704
* information about a thread whose ID is in the corresponding
705705
* element of the input array of IDs.
706706
*
707-
* @throws java.lang.SecurityException if a security manager
707+
* @throws SecurityException if a security manager
708708
* exists and the caller does not have
709709
* ManagementPermission("monitor").
710-
* @throws java.lang.UnsupportedOperationException
710+
* @throws UnsupportedOperationException
711711
* <ul>
712712
* <li>if {@code lockedMonitors} is {@code true} but
713713
* the Java virtual machine does not support monitoring
@@ -794,10 +794,10 @@ public ThreadInfo[] getThreadInfo(long[] ids, boolean lockedMonitors,
794794
* element of the input array of IDs.
795795
*
796796
* @throws IllegalArgumentException if {@code maxDepth} is negative.
797-
* @throws java.lang.SecurityException if a security manager
797+
* @throws SecurityException if a security manager
798798
* exists and the caller does not have
799799
* ManagementPermission("monitor").
800-
* @throws java.lang.UnsupportedOperationException
800+
* @throws UnsupportedOperationException
801801
* <ul>
802802
* <li>if {@code lockedMonitors} is {@code true} but
803803
* the Java virtual machine does not support monitoring
@@ -835,10 +835,10 @@ public default ThreadInfo[] getThreadInfo(long[] ids, boolean lockedMonitors,
835835
*
836836
* @return an array of {@link ThreadInfo} for all live threads.
837837
*
838-
* @throws java.lang.SecurityException if a security manager
838+
* @throws SecurityException if a security manager
839839
* exists and the caller does not have
840840
* ManagementPermission("monitor").
841-
* @throws java.lang.UnsupportedOperationException
841+
* @throws UnsupportedOperationException
842842
* <ul>
843843
* <li>if {@code lockedMonitors} is {@code true} but
844844
* the Java virtual machine does not support monitoring
@@ -884,10 +884,10 @@ public default ThreadInfo[] getThreadInfo(long[] ids, boolean lockedMonitors,
884884
* @return an array of {@link ThreadInfo} for all live threads.
885885
*
886886
* @throws IllegalArgumentException if {@code maxDepth} is negative.
887-
* @throws java.lang.SecurityException if a security manager
887+
* @throws SecurityException if a security manager
888888
* exists and the caller does not have
889889
* ManagementPermission("monitor").
890-
* @throws java.lang.UnsupportedOperationException
890+
* @throws UnsupportedOperationException
891891
* <ul>
892892
* <li>if {@code lockedMonitors} is {@code true} but
893893
* the Java virtual machine does not support monitoring

0 commit comments

Comments
 (0)