--- old/src/hotspot/share/include/jmm.h 2019-08-30 15:23:54.000000000 -0700 +++ new/src/hotspot/share/include/jmm.h 2019-08-30 15:23:54.000000000 -0700 @@ -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 @@ -239,6 +239,9 @@ jobject (JNICALL *GetMemoryPoolUsage) (JNIEnv* env, jobject pool); jobject (JNICALL *GetPeakMemoryPoolUsage) (JNIEnv* env, jobject pool); + jlong (JNICALL *GetOneThreadAllocatedMemory) + (JNIEnv *env, + jlong thread_id); void (JNICALL *GetThreadAllocatedMemory) (JNIEnv *env, jlongArray ids, --- old/src/hotspot/share/services/management.cpp 2019-08-30 15:23:56.000000000 -0700 +++ new/src/hotspot/share/services/management.cpp 2019-08-30 15:23:56.000000000 -0700 @@ -2068,6 +2068,34 @@ } #endif // INCLUDE_MANAGEMENT +// Gets the amount of memory allocated on the Java heap for a single thread. +// Returns -1 if the thread does not exist or has terminated. +JVM_ENTRY(jlong, jmm_GetOneThreadAllocatedMemory(JNIEnv *env, jlong thread_id)) + if (thread_id < 0) { + THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), + "Invalid thread ID", -1); + } + + JavaThread* java_thread; + + if (thread_id == 0) { + // current thread + java_thread = (JavaThread*)THREAD; + if (java_thread->is_Java_thread()) { + return java_thread->cooked_allocated_bytes(); + } + return -1; + } + + ThreadsListHandle tlh; + java_thread = tlh.list()->find_JavaThread_from_java_tid(thread_id); + + if (java_thread != NULL) { + return java_thread->cooked_allocated_bytes(); + } + return -1; +JVM_END + // Gets an array containing the amount of memory allocated on the Java // heap for a set of threads (in bytes). Each element of the array is // the amount of memory allocated for the thread ID specified in the @@ -2192,6 +2220,7 @@ jmm_GetMemoryManagers, jmm_GetMemoryPoolUsage, jmm_GetPeakMemoryPoolUsage, + jmm_GetOneThreadAllocatedMemory, jmm_GetThreadAllocatedMemory, jmm_GetMemoryUsage, jmm_GetLongAttribute, --- old/src/java.management/share/classes/java/lang/management/ThreadMXBean.java 2019-08-30 15:23:58.000000000 -0700 +++ new/src/java.management/share/classes/java/lang/management/ThreadMXBean.java 2019-08-30 15:23:57.000000000 -0700 @@ -394,7 +394,7 @@ * the current thread has executed in user mode or system mode. * *
- * 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: *
* {@link #getThreadCpuTime getThreadCpuTime}(Thread.currentThread().getId()); @@ -421,7 +421,7 @@ * not necessarily nanoseconds accuracy. * *- * 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: *
* {@link #getThreadUserTime getThreadUserTime}(Thread.currentThread().getId()); --- old/src/java.management/share/classes/sun/management/ThreadImpl.java 2019-08-30 15:23:59.000000000 -0700 +++ new/src/java.management/share/classes/sun/management/ThreadImpl.java 2019-08-30 15:23:59.000000000 -0700 @@ -112,11 +112,15 @@ return cpuTimeEnabled; } - protected boolean isThreadAllocatedMemoryEnabled() { + private void throwIfThreadAllocatedMemoryNotSupported() { if (!isThreadAllocatedMemorySupported()) { throw new UnsupportedOperationException( - "Thread allocated memory measurement is not supported"); + "Thread allocated memory measurement is not supported."); } + } + + protected boolean isThreadAllocatedMemoryEnabled() { + throwIfThreadAllocatedMemoryNotSupported(); return allocatedMemoryEnabled; } @@ -155,16 +159,24 @@ return getThreadInfo(ids, 0); } - private void verifyThreadIds(long[] ids) { + private void throwIfNullThreadIds(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) { + throwIfNullThreadIds(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 +354,41 @@ } } - protected long getThreadAllocatedBytes(long id) { - long[] ids = new long[1]; - ids[0] = id; - final long[] sizes = getThreadAllocatedBytes(ids); - return sizes[0]; + protected long getCurrentThreadAllocatedBytes() { + if (isThreadAllocatedMemoryEnabled()) { + return getThreadAllocatedMemory0(0); + } + return -1; } - private boolean verifyThreadAllocatedMemory(long[] ids) { - verifyThreadIds(ids); + private boolean verifyThreadAllocatedMemory(long id) { + verifyThreadId(id); + return isThreadAllocatedMemoryEnabled(); + } - // check if Thread allocated memory measurement is supported. - if (!isThreadAllocatedMemorySupported()) { - throw new UnsupportedOperationException( - "Thread allocated memory measurement is not supported."); + protected long getThreadAllocatedBytes(long id) { + boolean verified = verifyThreadAllocatedMemory(id); + + if (verified) { + return getThreadAllocatedMemory0( + Thread.currentThread().getId() == id ? 0 : id); } + return -1; + } + private boolean verifyThreadAllocatedMemory(long[] ids) { + verifyThreadIds(ids); return isThreadAllocatedMemoryEnabled(); } protected long[] getThreadAllocatedBytes(long[] ids) { + throwIfNullThreadIds(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 +401,7 @@ } protected void setThreadAllocatedMemoryEnabled(boolean enable) { - if (!isThreadAllocatedMemorySupported()) { - throw new UnsupportedOperationException( - "Thread allocated memory measurement is not supported."); - } + throwIfThreadAllocatedMemoryNotSupported(); Util.checkControlAccess(); synchronized (this) { @@ -511,6 +535,7 @@ 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); --- old/src/java.management/share/native/libmanagement/ThreadImpl.c 2019-08-30 15:24:01.000000000 -0700 +++ new/src/java.management/share/native/libmanagement/ThreadImpl.c 2019-08-30 15:24:01.000000000 -0700 @@ -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 @@ 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) --- old/src/jdk.management/share/classes/com/sun/management/ThreadMXBean.java 2019-08-30 15:24:03.000000000 -0700 +++ new/src/jdk.management/share/classes/com/sun/management/ThreadMXBean.java 2019-08-30 15:24:03.000000000 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 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 @@ -109,13 +109,43 @@ /** * Returns an approximation of the total amount of memory, in bytes, - * allocated in heap memory for the thread of the specified ID. + * allocated in heap memory for the current thread. + * The returned value is an approximation because some Java virtual machine + * implementations may use object allocation mechanisms that result in a + * delay between the time an object is allocated and the time its size is + * recorded. + * + *+ * This is a convenience method for local management use and is + * equivalent to calling: + *
+ * + * @return an approximation of the total memory allocated, in bytes, in + * heap memory for the current thread + * if thread memory allocation measurement is enabled; + * {@code -1} otherwise. + * + * @throws java.lang.UnsupportedOperationException if the Java virtual + * machine implementation does not support thread memory allocation + * measurement. + * + * @see #isThreadAllocatedMemorySupported + * @see #isThreadAllocatedMemoryEnabled + * @see #setThreadAllocatedMemoryEnabled + */ + public long getCurrentThreadAllocatedBytes(); + + /** + * Returns an approximation of the total amount of memory, in bytes, + * allocated in heap memory for the thread with the specified ID. * The returned value is an approximation because some Java virtual machine * implementations may use object allocation mechanisms that result in a * delay between the time an object is allocated and the time its size is * recorded. *+ * {@link #getThreadAllocatedBytes getThreadAllocatedBytes}(Thread.currentThread().getId()); + *- * If the thread of the specified ID is not alive or does not exist, + * If the thread with the specified ID is not alive or does not exist, * this method returns {@code -1}. If thread memory allocation measurement * is disabled, this method returns {@code -1}. * A thread is alive if it has been started and has not yet died. @@ -127,8 +157,8 @@ * * @param id the thread ID of a thread * @return an approximation of the total memory allocated, in bytes, in - * heap memory for a thread of the specified ID - * if the thread of the specified ID exists, the thread is alive, + * heap memory for the thread with the specified ID + * if the thread with the specified ID exists, the thread is alive, * and thread memory allocation measurement is enabled; * {@code -1} otherwise. * --- old/src/jdk.management/share/classes/com/sun/management/internal/HotSpotThreadImpl.java 2019-08-30 15:24:05.000000000 -0700 +++ new/src/jdk.management/share/classes/com/sun/management/internal/HotSpotThreadImpl.java 2019-08-30 15:24:05.000000000 -0700 @@ -58,6 +58,11 @@ } @Override + public long getCurrentThreadAllocatedBytes() { + return super.getCurrentThreadAllocatedBytes(); + } + + @Override public long getThreadAllocatedBytes(long id) { return super.getThreadAllocatedBytes(id); } --- old/test/jdk/com/sun/management/ThreadMXBean/ThreadAllocatedMemory.java 2019-08-30 15:24:06.000000000 -0700 +++ new/test/jdk/com/sun/management/ThreadMXBean/ThreadAllocatedMemory.java 2019-08-30 15:24:06.000000000 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 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 @@ -34,8 +34,8 @@ private static com.sun.management.ThreadMXBean mbean = (com.sun.management.ThreadMXBean)ManagementFactory.getThreadMXBean(); private static boolean testFailed = false; - private static boolean done = false; - private static boolean done1 = false; + private static volatile boolean done = false; + private static volatile boolean done1 = false; private static Object obj = new Object(); private static final int NUM_THREADS = 10; private static Thread[] threads = new Thread[NUM_THREADS]; @@ -55,7 +55,7 @@ if (mbean.isThreadAllocatedMemoryEnabled()) { throw new RuntimeException( - "ThreadAllocatedMemory is expected to be disabled"); + "TEST FAILED: ThreadAllocatedMemory is expected to be disabled"); } Thread curThread = Thread.currentThread(); @@ -64,7 +64,7 @@ long s = mbean.getThreadAllocatedBytes(id); if (s != -1) { throw new RuntimeException( - "Invalid ThreadAllocatedBytes returned = " + + "TEST FAILED: Invalid ThreadAllocatedBytes returned = " + s + " expected = -1"); } @@ -75,15 +75,18 @@ if (!mbean.isThreadAllocatedMemoryEnabled()) { throw new RuntimeException( - "ThreadAllocatedMemory is expected to be enabled"); + "TEST FAILED: ThreadAllocatedMemory is expected to be enabled"); } - long size = mbean.getThreadAllocatedBytes(id); + // Test current thread two ways + + // First way, getCurrentThreadAllocatedBytes + long size = mbean.getCurrentThreadAllocatedBytes(); // implementation could have started measurement when // measurement was enabled, in which case size can be 0 if (size < 0) { throw new RuntimeException( - "Invalid allocated bytes returned = " + size); + "TEST FAILED: Invalid allocated bytes returned = " + size); } doit(); @@ -91,22 +94,118 @@ // Expected to be size1 >= size long size1 = mbean.getThreadAllocatedBytes(id); if (size1 < size) { - throw new RuntimeException("Allocated bytes " + size1 + + throw new RuntimeException("TEST FAILED: Allocated bytes " + size1 + " expected >= " + size); } System.out.println(curThread.getName() + - " Current thread allocated bytes = " + size + - " allocated bytes = " + size1); + " Previous allocated bytes = " + size + + " Current allocated bytes = " + size1); + + // back-to-back calls shouldn't allocate any memory + size = mbean.getThreadAllocatedBytes(id); + size1 = mbean.getThreadAllocatedBytes(id); + if (size1 != size) { + throw new RuntimeException("TEST FAILED: Allocated bytes " + size1 + + " expected == " + size); + } + System.out.println(curThread.getName() + + " Previous allocated bytes = " + size + + " Current allocated bytes = " + size1); + // Second way, getThreadAllocatedBytes + size = mbean.getThreadAllocatedBytes(id); + // implementation could have started measurement when + // measurement was enabled, in which case size can be 0 + if (size < 0) { + throw new RuntimeException( + "TEST FAILED: Invalid allocated bytes returned = " + size); + } + + doit(); + + // Expected to be size1 >= size + size1 = mbean.getThreadAllocatedBytes(id); + if (size1 < size) { + throw new RuntimeException("TEST FAILED: Allocated bytes " + size1 + + " expected >= " + size); + } + System.out.println(curThread.getName() + + " Previous allocated bytes = " + size + + " Current allocated bytes = " + size1); + + + // back-to-back calls shouldn't allocate any memory + size = mbean.getThreadAllocatedBytes(id); + size1 = mbean.getThreadAllocatedBytes(id); + if (size1 != size) { + throw new RuntimeException("TEST FAILED: Allocated bytes " + size1 + + " expected == " + size); + } + System.out.println(curThread.getName() + + " Previous allocated bytes = " + size + + " Current allocated bytes = " + size1); + + + // Test a single thread that isn't ourself + + // Start one thread, wait for it to block + // after doing some allocation + done = false; done1 = false; + curThread = new MyThread("MyThread"); + curThread.start(); + id = curThread.getId(); + waitUntilThreadBlocked(curThread); + size = mbean.getThreadAllocatedBytes(id); + + // let thread go to do some more allocation + synchronized (obj) { + done = true; + obj.notifyAll(); + } + + // wait for thread to get going again and sample it + goSleep(400); + size1 = mbean.getThreadAllocatedBytes(id); + if (size > size1) { + throw new RuntimeException("TEST FAILED: " + + curThread.getName() + + " previous allocated bytes = " + size + + " > current allocated bytes = " + size1); + } + System.out.println(curThread.getName() + + " Previous allocated bytes = " + size + + " Current allocated bytes = " + size1); + + // let thread exit + synchronized (obj) { + done1 = true; + obj.notifyAll(); + } + + try { + curThread.join(); + } catch (InterruptedException e) { + System.out.println("Unexpected exception is thrown."); + e.printStackTrace(System.out); + testFailed = true; + } + if (testFailed) { + throw new RuntimeException("TEST FAILED"); + } + + + // Test many threads + // start threads, wait for them to block + done = false; done1 = false; for (int i = 0; i < NUM_THREADS; i++) { threads[i] = new MyThread("MyThread-" + i); threads[i].start(); } // threads block after doing some allocation - waitUntilThreadBlocked(); + waitUntilThreadsBlocked(); for (int i = 0; i < NUM_THREADS; i++) { sizes[i] = mbean.getThreadAllocatedBytes(threads[i].getId()); @@ -156,6 +255,7 @@ throw new RuntimeException("TEST FAILED"); } + System.out.println("Test passed"); } @@ -169,7 +269,18 @@ } } - private static void waitUntilThreadBlocked() + private static void waitUntilThreadBlocked(Thread thread) + throws Exception { + while (true) { + goSleep(100); + ThreadInfo info = mbean.getThreadInfo(thread.getId()); + if (info.getThreadState() == Thread.State.WAITING) { + break; + } + } + } + + private static void waitUntilThreadsBlocked() throws Exception { int count = 0; while (count != NUM_THREADS) {