Path: blob/master/test/jdk/com/sun/management/ThreadMXBean/ThreadAllocatedMemory.java
41153 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6173675 823120926* @summary Basic test of ThreadMXBean.getThreadAllocatedBytes27* @author Paul Hohensee28*/2930import java.lang.management.*;3132public class ThreadAllocatedMemory {33private static com.sun.management.ThreadMXBean mbean =34(com.sun.management.ThreadMXBean)ManagementFactory.getThreadMXBean();35private static volatile boolean done = false;36private static volatile boolean done1 = false;37private static Object obj = new Object();38private static final int NUM_THREADS = 10;39private static Thread[] threads = new Thread[NUM_THREADS];40private static long[] sizes = new long[NUM_THREADS];4142public static void main(String[] argv)43throws Exception {4445testSupportEnableDisable();4647// Test current thread two ways48testGetCurrentThreadAllocatedBytes();49testCurrentThreadGetThreadAllocatedBytes();5051// Test a single thread that is not this one52testGetThreadAllocatedBytes();5354// Test many threads that are not this one55testGetThreadsAllocatedBytes();5657System.out.println("Test passed");58}5960private static void testSupportEnableDisable() {61if (!mbean.isThreadAllocatedMemorySupported()) {62return;63}6465// disable allocated memory measurement66if (mbean.isThreadAllocatedMemoryEnabled()) {67mbean.setThreadAllocatedMemoryEnabled(false);68}6970if (mbean.isThreadAllocatedMemoryEnabled()) {71throw new RuntimeException(72"ThreadAllocatedMemory is expected to be disabled");73}7475long s = mbean.getCurrentThreadAllocatedBytes();76if (s != -1) {77throw new RuntimeException(78"Invalid ThreadAllocatedBytes returned = " +79s + " expected = -1");80}8182// enable allocated memory measurement83if (!mbean.isThreadAllocatedMemoryEnabled()) {84mbean.setThreadAllocatedMemoryEnabled(true);85}8687if (!mbean.isThreadAllocatedMemoryEnabled()) {88throw new RuntimeException(89"ThreadAllocatedMemory is expected to be enabled");90}91}9293private static void testGetCurrentThreadAllocatedBytes() {94long size = mbean.getCurrentThreadAllocatedBytes();95ensureValidSize(size);9697// do some more allocation98doit();99100checkResult(Thread.currentThread(), size,101mbean.getCurrentThreadAllocatedBytes());102}103104private static void testCurrentThreadGetThreadAllocatedBytes() {105Thread curThread = Thread.currentThread();106long id = curThread.getId();107108long size = mbean.getThreadAllocatedBytes(id);109ensureValidSize(size);110111// do some more allocation112doit();113114checkResult(curThread, size, mbean.getThreadAllocatedBytes(id));115}116117private static void testGetThreadAllocatedBytes()118throws Exception {119120// start a thread121done = false; done1 = false;122Thread curThread = new MyThread("MyThread");123curThread.start();124long id = curThread.getId();125126// wait for thread to block after doing some allocation127waitUntilThreadBlocked(curThread);128129long size = mbean.getThreadAllocatedBytes(id);130ensureValidSize(size);131132// let thread go to do some more allocation133synchronized (obj) {134done = true;135obj.notifyAll();136}137138// wait for thread to get going again. we don't care if we139// catch it in mid-execution or if it hasn't140// restarted after we're done sleeping.141goSleep(400);142143checkResult(curThread, size, mbean.getThreadAllocatedBytes(id));144145// let thread exit146synchronized (obj) {147done1 = true;148obj.notifyAll();149}150151try {152curThread.join();153} catch (InterruptedException e) {154System.out.println("Unexpected exception is thrown.");155e.printStackTrace(System.out);156}157}158159private static void testGetThreadsAllocatedBytes()160throws Exception {161162// start threads163done = false; done1 = false;164for (int i = 0; i < NUM_THREADS; i++) {165threads[i] = new MyThread("MyThread-" + i);166threads[i].start();167}168169// wait for threads to block after doing some allocation170waitUntilThreadsBlocked();171172for (int i = 0; i < NUM_THREADS; i++) {173sizes[i] = mbean.getThreadAllocatedBytes(threads[i].getId());174ensureValidSize(sizes[i]);175}176177// let threads go to do some more allocation178synchronized (obj) {179done = true;180obj.notifyAll();181}182183// wait for threads to get going again. we don't care if we184// catch them in mid-execution or if some of them haven't185// restarted after we're done sleeping.186goSleep(400);187188for (int i = 0; i < NUM_THREADS; i++) {189checkResult(threads[i], sizes[i],190mbean.getThreadAllocatedBytes(threads[i].getId()));191}192193// let threads exit194synchronized (obj) {195done1 = true;196obj.notifyAll();197}198199for (int i = 0; i < NUM_THREADS; i++) {200try {201threads[i].join();202} catch (InterruptedException e) {203System.out.println("Unexpected exception is thrown.");204e.printStackTrace(System.out);205break;206}207}208}209210private static void ensureValidSize(long size) {211// implementation could have started measurement when212// measurement was enabled, in which case size can be 0213if (size < 0) {214throw new RuntimeException(215"Invalid allocated bytes returned = " + size);216}217}218219private static void checkResult(Thread curThread,220long prev_size, long curr_size) {221if (curr_size < prev_size) {222throw new RuntimeException("Allocated bytes " + curr_size +223" expected >= " + prev_size);224}225System.out.println(curThread.getName() +226" Previous allocated bytes = " + prev_size +227" Current allocated bytes = " + curr_size);228}229230private static void goSleep(long ms) throws Exception {231try {232Thread.sleep(ms);233} catch (InterruptedException e) {234System.out.println("Unexpected exception is thrown.");235throw e;236}237}238239private static void waitUntilThreadBlocked(Thread thread)240throws Exception {241while (true) {242goSleep(100);243ThreadInfo info = mbean.getThreadInfo(thread.getId());244if (info.getThreadState() == Thread.State.WAITING) {245break;246}247}248}249250private static void waitUntilThreadsBlocked()251throws Exception {252int count = 0;253while (count != NUM_THREADS) {254goSleep(100);255count = 0;256for (int i = 0; i < NUM_THREADS; i++) {257ThreadInfo info = mbean.getThreadInfo(threads[i].getId());258if (info.getThreadState() == Thread.State.WAITING) {259count++;260}261}262}263}264265public static void doit() {266String tmp = "";267long hashCode = 0;268for (int counter = 0; counter < 1000; counter++) {269tmp += counter;270hashCode = tmp.hashCode();271}272System.out.println(Thread.currentThread().getName() +273" hashcode: " + hashCode);274}275276static class MyThread extends Thread {277public MyThread(String name) {278super(name);279}280281public void run() {282ThreadAllocatedMemory.doit();283284synchronized (obj) {285while (!done) {286try {287obj.wait();288} catch (InterruptedException e) {289System.out.println("Unexpected exception is thrown.");290e.printStackTrace(System.out);291break;292}293}294}295296long size1 = mbean.getThreadAllocatedBytes(getId());297ThreadAllocatedMemory.doit();298long size2 = mbean.getThreadAllocatedBytes(getId());299300System.out.println(getName() + ": " +301"ThreadAllocatedBytes = " + size1 +302" ThreadAllocatedBytes = " + size2);303304if (size1 > size2) {305throw new RuntimeException(getName() +306" ThreadAllocatedBytes = " + size1 +307" > ThreadAllocatedBytes = " + size2);308}309310synchronized (obj) {311while (!done1) {312try {313obj.wait();314} catch (InterruptedException e) {315System.out.println("Unexpected exception is thrown.");316e.printStackTrace(System.out);317break;318}319}320}321}322}323}324325326