0N/A/*
2362N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4997799
0N/A * @summary Basic test of ThreadMXBean.getThreadUserTime and
0N/A * getCurrentThreadUserTime.
0N/A * @author Mandy Chung
0N/A */
0N/A
0N/Aimport java.lang.management.*;
0N/A
0N/Apublic class ThreadUserTime {
0N/A private static ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
0N/A private static boolean testFailed = false;
0N/A private static boolean done = false;
0N/A private static Object obj = new Object();
0N/A private static final int NUM_THREADS = 10;
0N/A private static Thread[] threads = new Thread[NUM_THREADS];
0N/A private static long[] times = new long[NUM_THREADS];
0N/A
0N/A // careful about this value
0N/A private static final int DELTA = 100;
0N/A
0N/A public static void main(String[] argv)
0N/A throws Exception {
0N/A if (!mbean.isCurrentThreadCpuTimeSupported()) {
0N/A return;
0N/A }
0N/A
0N/A // disable user time
0N/A if (mbean.isThreadCpuTimeEnabled()) {
0N/A mbean.setThreadCpuTimeEnabled(false);
0N/A }
0N/A
0N/A Thread curThread = Thread.currentThread();
0N/A long t = mbean.getCurrentThreadUserTime();
0N/A if (t != -1) {
0N/A throw new RuntimeException("Invalid CurrenThreadUserTime returned = " +
0N/A t + " expected = -1");
0N/A }
0N/A
0N/A if (mbean.isThreadCpuTimeSupported()) {
0N/A long t1 = mbean.getThreadUserTime(curThread.getId());
0N/A if (t1 != -1) {
0N/A throw new RuntimeException("Invalid ThreadUserTime returned = " +
0N/A t1 + " expected = -1");
0N/A }
0N/A }
0N/A
0N/A // Enable CPU Time measurement
0N/A if (!mbean.isThreadCpuTimeEnabled()) {
0N/A mbean.setThreadCpuTimeEnabled(true);
0N/A }
0N/A
0N/A if (!mbean.isThreadCpuTimeEnabled()) {
0N/A throw new RuntimeException("ThreadUserTime is expected to be enabled");
0N/A }
0N/A
0N/A long time = mbean.getCurrentThreadUserTime();
0N/A if (time < 0) {
0N/A throw new RuntimeException("Invalid user time returned = " + time);
0N/A }
0N/A
0N/A if (!mbean.isThreadCpuTimeSupported()) {
0N/A return;
0N/A }
0N/A
0N/A
0N/A // Expected to be time1 >= time
0N/A long time1 = mbean.getThreadUserTime(curThread.getId());
0N/A if (time1 < time) {
0N/A throw new RuntimeException("User time " + time1 +
0N/A " expected >= " + time);
0N/A }
0N/A System.out.println(curThread.getName() +
0N/A " Current Thread User Time = " + time +
0N/A " user time = " + time1);
0N/A
0N/A for (int i = 0; i < NUM_THREADS; i++) {
0N/A threads[i] = new MyThread("MyThread-" + i);
0N/A threads[i].start();
0N/A }
0N/A
0N/A waitUntilThreadBlocked();
0N/A
0N/A for (int i = 0; i < NUM_THREADS; i++) {
0N/A times[i] = mbean.getThreadUserTime(threads[i].getId());
0N/A }
0N/A
0N/A goSleep(200);
0N/A
0N/A for (int i = 0; i < NUM_THREADS; i++) {
0N/A long newTime = mbean.getThreadUserTime(threads[i].getId());
0N/A if (times[i] > newTime) {
0N/A throw new RuntimeException("TEST FAILED: " +
0N/A threads[i].getName() +
0N/A " previous user user time = " + times[i] +
0N/A " > current user user time = " + newTime);
0N/A }
0N/A if ((times[i] + DELTA) < newTime) {
0N/A throw new RuntimeException("TEST FAILED: " +
0N/A threads[i].getName() +
0N/A " user time = " + newTime +
0N/A " previous user time " + times[i] +
0N/A " out of expected range");
0N/A }
0N/A
0N/A System.out.println(threads[i].getName() +
0N/A " Previous User Time = " + times[i] +
0N/A " Current User time = " + newTime);
0N/A }
0N/A
0N/A synchronized (obj) {
0N/A done = true;
0N/A obj.notifyAll();
0N/A }
0N/A
0N/A for (int i = 0; i < NUM_THREADS; i++) {
0N/A try {
0N/A threads[i].join();
0N/A } catch (InterruptedException e) {
0N/A System.out.println("Unexpected exception is thrown.");
0N/A e.printStackTrace(System.out);
0N/A testFailed = true;
0N/A break;
0N/A }
0N/A }
0N/A if (testFailed) {
0N/A throw new RuntimeException("TEST FAILED");
0N/A }
0N/A
0N/A System.out.println("Test passed");
0N/A }
0N/A
0N/A
0N/A private static void goSleep(long ms) throws Exception {
0N/A try {
0N/A Thread.sleep(ms);
0N/A } catch (InterruptedException e) {
0N/A System.out.println("Unexpected exception is thrown.");
0N/A throw e;
0N/A }
0N/A }
0N/A
0N/A private static void waitUntilThreadBlocked()
0N/A throws Exception {
0N/A int count = 0;
0N/A while (count != NUM_THREADS) {
0N/A goSleep(100);
0N/A count = 0;
0N/A for (int i = 0; i < NUM_THREADS; i++) {
0N/A ThreadInfo info = mbean.getThreadInfo(threads[i].getId());
0N/A if (info.getThreadState() == Thread.State.WAITING) {
0N/A count++;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A static class MyThread extends Thread {
0N/A public MyThread(String name) {
0N/A super(name);
0N/A }
0N/A
0N/A public void run() {
0N/A double sum = 0;
0N/A for (int i = 0; i < 5000; i++) {
0N/A double r = Math.random();
0N/A double x = Math.pow(3, r);
0N/A sum += x - r;
0N/A }
0N/A synchronized (obj) {
0N/A while (!done) {
0N/A try {
0N/A obj.wait();
0N/A } catch (InterruptedException e) {
0N/A System.out.println("Unexpected exception is thrown.");
0N/A e.printStackTrace(System.out);
0N/A testFailed = true;
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A sum = 0;
0N/A for (int i = 0; i < 5000; i++) {
0N/A double r = Math.random();
0N/A double x = Math.pow(3, r);
0N/A sum += x - r;
0N/A }
0N/A
0N/A long time1 = mbean.getCurrentThreadCpuTime();
0N/A long utime1 = mbean.getCurrentThreadUserTime();
0N/A long time2 = mbean.getThreadCpuTime(getId());
0N/A long utime2 = mbean.getThreadUserTime(getId());
0N/A
0N/A System.out.println(getName() + ":");
0N/A System.out.println("CurrentThreadUserTime = " + utime1 +
0N/A " ThreadUserTime = " + utime2);
0N/A System.out.println("CurrentThreadCpuTime = " + time1 +
0N/A " ThreadCpuTime = " + time2);
0N/A
0N/A if (time1 > time2) {
0N/A throw new RuntimeException("TEST FAILED: " + getName() +
0N/A " CurrentThreadCpuTime = " + time1 +
0N/A " > ThreadCpuTime = " + time2);
0N/A }
0N/A if (utime1 > utime2) {
0N/A throw new RuntimeException("TEST FAILED: " + getName() +
0N/A " CurrentThreadUserTime = " + utime1 +
0N/A " > ThreadUserTime = " + utime2);
0N/A }
0N/A }
0N/A }
0N/A
0N/A}