Path: blob/master/test/jdk/com/sun/management/ThreadMXBean/ThreadCpuTimeArray.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* @key randomness27* @summary Basic test of ThreadMXBean.getThreadCpuTime(long[]) and28* getThreadUserTime(long[]).29* @author Paul Hohensee30*/3132import java.lang.management.*;3334public class ThreadCpuTimeArray {35private static com.sun.management.ThreadMXBean mbean =36(com.sun.management.ThreadMXBean)ManagementFactory.getThreadMXBean();37private static boolean testFailed = false;38private static boolean done = false;39private static Object obj = new Object();40private static final int NUM_THREADS = 10;41private static Thread[] threads = new Thread[NUM_THREADS];4243// careful about this value44private static final int DELTA = 100;4546public static void main(String[] argv)47throws Exception {4849if (!mbean.isThreadCpuTimeSupported()) {50return;51}525354// disable CPU time55if (mbean.isThreadCpuTimeEnabled()) {56mbean.setThreadCpuTimeEnabled(false);57}5859if (mbean.isThreadCpuTimeEnabled()) {60throw new RuntimeException("ThreadCpuTime is expected to be disabled");61}6263// start threads, wait for them to block64long[] ids = new long[NUM_THREADS];6566for (int i = 0; i < NUM_THREADS; i++) {67threads[i] = new MyThread("MyThread-" + i);68threads[i].start();69ids[i] = threads[i].getId();70}7172// threads block after doing some computation73waitUntilThreadBlocked();747576long times[] = mbean.getThreadCpuTime(ids);77long userTimes[] = mbean.getThreadUserTime(ids);7879if (times == null) {80throw new RuntimeException("Null ThreadCpuTime array returned");81}8283for (int i = 0; i < NUM_THREADS; i++) {84long t = times[i];85if (t != -1) {86throw new RuntimeException(87"Invalid ThreadCpuTime returned for thread " +88threads[i].getName() + " = " + t + " expected = -1");89}90long ut = userTimes[i];91if (ut != -1) {92throw new RuntimeException(93"Invalid ThreadUserTime returned for thread " +94threads[i].getName() + " = " + ut + " expected = -1");95}96}979899// Enable CPU Time measurement100if (!mbean.isThreadCpuTimeEnabled()) {101mbean.setThreadCpuTimeEnabled(true);102}103104if (!mbean.isThreadCpuTimeEnabled()) {105throw new RuntimeException("ThreadCpuTime is expected to be enabled");106}107108times = mbean.getThreadCpuTime(ids);109userTimes = mbean.getThreadUserTime(ids);110111goSleep(200);112113for (int i = 0; i < NUM_THREADS; i++) {114long t = times[i];115if (t < 0) {116throw new RuntimeException(117"Invalid CPU time returned for thread " +118threads[i].getName() + " = " + t);119}120long ut = userTimes[i];121if (ut < 0) {122throw new RuntimeException(123"Invalid user time returned for thread " +124threads[i].getName() + " = " + ut);125}126}127128long[] times1 = mbean.getThreadCpuTime(ids);129long[] userTimes1 = mbean.getThreadUserTime(ids);130131for (int i = 0; i < NUM_THREADS; i++) {132long newTime = times1[i];133long newUserTime = userTimes1[i];134135if (times[i] > newTime) {136throw new RuntimeException("TEST FAILED: " +137threads[i].getName() +138" previous CPU time = " + times[i] +139" > current CPU time = " + newTime);140}141if ((times[i] + DELTA) < newTime) {142throw new RuntimeException("TEST FAILED: " +143threads[i].getName() +144" CPU time = " + newTime +145" previous CPU time " + times[i] +146" out of expected range");147}148149System.out.println(threads[i].getName() +150" Previous Cpu Time = " + times[i] +151" Current CPU time = " + newTime);152153System.out.println(threads[i].getName() +154" Previous User Time = " + userTimes[i] +155" Current User time = " + newUserTime);156}157158159try {160times = mbean.getThreadCpuTime(null);161} catch (NullPointerException e) {162System.out.println(163"Caught expected NullPointerException: " + e.getMessage());164}165166try {167ids[0] = 0;168times = mbean.getThreadCpuTime(ids);169} catch (IllegalArgumentException e) {170System.out.println(171"Caught expected IllegalArgumentException: " + e.getMessage());172}173174175// let threads exit176synchronized (obj) {177done = true;178obj.notifyAll();179}180181for (int i = 0; i < NUM_THREADS; i++) {182try {183threads[i].join();184} catch (InterruptedException e) {185System.out.println("Unexpected exception is thrown.");186e.printStackTrace(System.out);187testFailed = true;188break;189}190}191192if (testFailed) {193throw new RuntimeException("TEST FAILED");194}195196System.out.println("Test passed");197}198199200private static void goSleep(long ms) throws Exception {201try {202Thread.sleep(ms);203} catch (InterruptedException e) {204System.out.println("Unexpected exception is thrown.");205throw e;206}207}208209private static void waitUntilThreadBlocked()210throws Exception {211int count = 0;212while (count != NUM_THREADS) {213goSleep(100);214count = 0;215for (int i = 0; i < NUM_THREADS; i++) {216ThreadInfo info = mbean.getThreadInfo(threads[i].getId());217if (info.getThreadState() == Thread.State.WAITING) {218count++;219}220}221}222}223224public static void doit() {225double sum = 0;226for (int i = 0; i < 5000; i++) {227double r = Math.random();228double x = Math.pow(3, r);229sum += x - r;230}231System.out.println(Thread.currentThread().getName() +232" sum = " + sum);233}234235static class MyThread extends Thread {236public MyThread(String name) {237super(name);238}239240public void run() {241ThreadCpuTimeArray.doit();242243synchronized (obj) {244while (!done) {245try {246obj.wait();247} catch (InterruptedException e) {248System.out.println("Unexpected exception is thrown.");249e.printStackTrace(System.out);250testFailed = true;251break;252}253}254}255256ThreadCpuTimeArray.doit();257}258}259}260261262