Path: blob/master/test/jdk/com/sun/management/ThreadMXBean/ThreadAllocatedMemoryArray.java
41153 views
/*1* Copyright (c) 2011, 2015, 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 617367526* @summary Basic test of ThreadMXBean.getThreadAllocatedBytes(long[])27* @author Paul Hohensee28*/2930import java.lang.management.*;3132public class ThreadAllocatedMemoryArray {33private static com.sun.management.ThreadMXBean mbean =34(com.sun.management.ThreadMXBean)ManagementFactory.getThreadMXBean();35private static boolean testFailed = false;36private static boolean done = false;37private static boolean done1 = false;38private static Object obj = new Object();39private static final int NUM_THREADS = 10;40private static Thread[] threads = new Thread[NUM_THREADS];4142public static void main(String[] argv)43throws Exception {4445if (!mbean.isThreadAllocatedMemorySupported()) {46return;47}484950// start threads, wait for them to block51long[] ids = new long[NUM_THREADS];5253for (int i = 0; i < NUM_THREADS; i++) {54threads[i] = new MyThread("MyThread-" + i);55threads[i].start();56ids[i] = threads[i].getId();57}5859waitUntilThreadBlocked();606162// disable allocated memory measurement63if (mbean.isThreadAllocatedMemoryEnabled()) {64mbean.setThreadAllocatedMemoryEnabled(false);65}6667if (mbean.isThreadAllocatedMemoryEnabled()) {68throw new RuntimeException(69"ThreadAllocatedMemory is expected to be disabled");70}7172long sizes[] = mbean.getThreadAllocatedBytes(ids);7374if (sizes == null) {75throw new RuntimeException("Null ThreadAllocatedBytes array returned");76}7778for (int i = 0; i < NUM_THREADS; i++) {79long s = sizes[i];80if (s != -1) {81throw new RuntimeException(82"Invalid ThreadAllocatedBytes returned for thread " +83threads[i].getName() + " = " + s + " expected = -1");84}85}8687// Enable allocated memory measurement88if (!mbean.isThreadAllocatedMemoryEnabled()) {89mbean.setThreadAllocatedMemoryEnabled(true);90}9192if (!mbean.isThreadAllocatedMemoryEnabled()) {93throw new RuntimeException(94"ThreadAllocatedMemory is expected to be enabled");95}9697sizes = mbean.getThreadAllocatedBytes(ids);9899for (int i = 0; i < NUM_THREADS; i++) {100long s = sizes[i];101if (s < 0) {102throw new RuntimeException(103"Invalid allocated bytes returned for thread " +104threads[i].getName() + " = " + s);105}106}107108// let threads go and do some more allocation109synchronized (obj) {110done = true;111obj.notifyAll();112}113114// wait for threads to get going again. we don't care if we115// catch them in mid-execution or if some of them haven't116// restarted after we're done sleeping.117goSleep(400);118119long[] sizes1 = mbean.getThreadAllocatedBytes(ids);120121for (int i = 0; i < NUM_THREADS; i++) {122long newSize = sizes1[i];123if (sizes[i] > newSize) {124throw new RuntimeException("TEST FAILED: " +125threads[i].getName() +126" previous allocated bytes = " + sizes[i] +127" > current allocated bytes = " + newSize);128}129System.out.println(threads[i].getName() +130" Previous allocated bytes = " + sizes[i] +131" Current allocated bytes = " + newSize);132}133134try {135sizes = mbean.getThreadAllocatedBytes(null);136} catch (NullPointerException e) {137System.out.println(138"Caught expected NullPointerException: " + e.getMessage());139}140141try {142ids[0] = 0;143sizes = mbean.getThreadAllocatedBytes(ids);144} catch (IllegalArgumentException e) {145System.out.println(146"Caught expected IllegalArgumentException: " + e.getMessage());147}148149150// let threads exit151synchronized (obj) {152done1 = true;153obj.notifyAll();154}155156for (int i = 0; i < NUM_THREADS; i++) {157try {158threads[i].join();159} catch (InterruptedException e) {160System.out.println("Unexpected exception is thrown.");161e.printStackTrace(System.out);162testFailed = true;163break;164}165}166167if (testFailed) {168throw new RuntimeException("TEST FAILED");169}170171System.out.println("Test passed");172}173174175private static void goSleep(long ms) throws Exception {176try {177Thread.sleep(ms);178} catch (InterruptedException e) {179System.out.println("Unexpected exception is thrown.");180throw e;181}182}183184private static void waitUntilThreadBlocked()185throws Exception {186int count = 0;187while (count != NUM_THREADS) {188goSleep(100);189count = 0;190for (int i = 0; i < NUM_THREADS; i++) {191ThreadInfo info = mbean.getThreadInfo(threads[i].getId());192if (info.getThreadState() == Thread.State.WAITING) {193count++;194}195}196}197}198199public static void doit() {200String tmp = "";201long hashCode = 0;202for (int counter = 0; counter < 1000; counter++) {203tmp += counter;204hashCode = tmp.hashCode();205}206System.out.println(Thread.currentThread().getName() +207" hashcode: " + hashCode);208}209210static class MyThread extends Thread {211public MyThread(String name) {212super(name);213}214215public void run() {216ThreadAllocatedMemoryArray.doit();217218synchronized (obj) {219while (!done) {220try {221obj.wait();222} catch (InterruptedException e) {223System.out.println("Unexpected exception is thrown.");224e.printStackTrace(System.out);225testFailed = true;226break;227}228}229}230231ThreadAllocatedMemoryArray.doit();232233synchronized (obj) {234while (!done1) {235try {236obj.wait();237} catch (InterruptedException e) {238System.out.println("Unexpected exception is thrown.");239e.printStackTrace(System.out);240testFailed = true;241break;242}243}244}245246}247}248}249250251