Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/ThreadUserTime.java
41152 views
/*1* Copyright (c) 2004, 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 499779926* @summary Basic test of ThreadMXBean.getThreadUserTime and27* getCurrentThreadUserTime.28* @author Mandy Chung29*/3031import java.lang.management.*;3233public class ThreadUserTime {34private static ThreadMXBean mbean = ManagementFactory.getThreadMXBean();35private static boolean testFailed = false;36private static boolean done = 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[] times = new long[NUM_THREADS];4142// careful about this value43private static final int DELTA = 100;4445public static void main(String[] argv)46throws Exception {47if (!mbean.isCurrentThreadCpuTimeSupported()) {48return;49}5051// disable user time52if (mbean.isThreadCpuTimeEnabled()) {53mbean.setThreadCpuTimeEnabled(false);54}5556Thread curThread = Thread.currentThread();57long t = mbean.getCurrentThreadUserTime();58if (t != -1) {59throw new RuntimeException("Invalid CurrenThreadUserTime returned = " +60t + " expected = -1");61}6263if (mbean.isThreadCpuTimeSupported()) {64long t1 = mbean.getThreadUserTime(curThread.getId());65if (t1 != -1) {66throw new RuntimeException("Invalid ThreadUserTime returned = " +67t1 + " expected = -1");68}69}7071// Enable CPU Time measurement72if (!mbean.isThreadCpuTimeEnabled()) {73mbean.setThreadCpuTimeEnabled(true);74}7576if (!mbean.isThreadCpuTimeEnabled()) {77throw new RuntimeException("ThreadUserTime is expected to be enabled");78}7980long time = mbean.getCurrentThreadUserTime();81if (time < 0) {82throw new RuntimeException("Invalid user time returned = " + time);83}8485if (!mbean.isThreadCpuTimeSupported()) {86return;87}888990// Expected to be time1 >= time91long time1 = mbean.getThreadUserTime(curThread.getId());92if (time1 < time) {93throw new RuntimeException("User time " + time1 +94" expected >= " + time);95}96System.out.println(curThread.getName() +97" Current Thread User Time = " + time +98" user time = " + time1);99100for (int i = 0; i < NUM_THREADS; i++) {101threads[i] = new MyThread("MyThread-" + i);102threads[i].start();103}104105waitUntilThreadBlocked();106107for (int i = 0; i < NUM_THREADS; i++) {108times[i] = mbean.getThreadUserTime(threads[i].getId());109}110111goSleep(200);112113for (int i = 0; i < NUM_THREADS; i++) {114long newTime = mbean.getThreadUserTime(threads[i].getId());115if (times[i] > newTime) {116throw new RuntimeException("TEST FAILED: " +117threads[i].getName() +118" previous user user time = " + times[i] +119" > current user user time = " + newTime);120}121if ((times[i] + DELTA) < newTime) {122throw new RuntimeException("TEST FAILED: " +123threads[i].getName() +124" user time = " + newTime +125" previous user time " + times[i] +126" out of expected range");127}128129System.out.println(threads[i].getName() +130" Previous User Time = " + times[i] +131" Current User time = " + newTime);132}133134synchronized (obj) {135done = true;136obj.notifyAll();137}138139for (int i = 0; i < NUM_THREADS; i++) {140try {141threads[i].join();142} catch (InterruptedException e) {143System.out.println("Unexpected exception is thrown.");144e.printStackTrace(System.out);145testFailed = true;146break;147}148}149if (testFailed) {150throw new RuntimeException("TEST FAILED");151}152153System.out.println("Test passed");154}155156157private static void goSleep(long ms) throws Exception {158try {159Thread.sleep(ms);160} catch (InterruptedException e) {161System.out.println("Unexpected exception is thrown.");162throw e;163}164}165166private static void waitUntilThreadBlocked()167throws Exception {168int count = 0;169while (count != NUM_THREADS) {170goSleep(100);171count = 0;172for (int i = 0; i < NUM_THREADS; i++) {173ThreadInfo info = mbean.getThreadInfo(threads[i].getId());174if (info.getThreadState() == Thread.State.WAITING) {175count++;176}177}178}179}180181static class MyThread extends Thread {182public MyThread(String name) {183super(name);184}185186public void run() {187double sum = 0;188for (int i = 0; i < 5000; i++) {189double r = Math.random();190double x = Math.pow(3, r);191sum += x - r;192}193synchronized (obj) {194while (!done) {195try {196obj.wait();197} catch (InterruptedException e) {198System.out.println("Unexpected exception is thrown.");199e.printStackTrace(System.out);200testFailed = true;201break;202}203}204}205sum = 0;206for (int i = 0; i < 5000; i++) {207double r = Math.random();208double x = Math.pow(3, r);209sum += x - r;210}211212long time1 = mbean.getCurrentThreadCpuTime();213long utime1 = mbean.getCurrentThreadUserTime();214long time2 = mbean.getThreadCpuTime(getId());215long utime2 = mbean.getThreadUserTime(getId());216217System.out.println(getName() + ":");218System.out.println("CurrentThreadUserTime = " + utime1 +219" ThreadUserTime = " + utime2);220System.out.println("CurrentThreadCpuTime = " + time1 +221" ThreadCpuTime = " + time2);222223if (time1 > time2) {224throw new RuntimeException("TEST FAILED: " + getName() +225" CurrentThreadCpuTime = " + time1 +226" > ThreadCpuTime = " + time2);227}228if (utime1 > utime2) {229throw new RuntimeException("TEST FAILED: " + getName() +230" CurrentThreadUserTime = " + utime1 +231" > ThreadUserTime = " + utime2);232}233}234}235236}237238239