1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     6173675
  27  * @summary Basic test of ThreadMXBean.getThreadCpuTime(long[]) and
  28  *          getThreadUserTime(long[]).
  29  * @author  Paul Hohensee
  30  */
  31 
  32 import java.lang.management.*;
  33 
  34 public class ThreadCpuTimeArray {
  35     private static com.sun.management.ThreadMXBean mbean =
  36         (com.sun.management.ThreadMXBean)ManagementFactory.getThreadMXBean();
  37     private static boolean testFailed = false;
  38     private static boolean done = false;
  39     private static Object obj = new Object();
  40     private static final int NUM_THREADS = 10;
  41     private static Thread[] threads = new Thread[NUM_THREADS];
  42 
  43     // careful about this value
  44     private static final int DELTA = 100;
  45 
  46     public static void main(String[] argv)
  47         throws Exception {
  48 
  49         if (!mbean.isThreadCpuTimeSupported()) {
  50             return;
  51         }
  52 
  53 
  54         // disable CPU time
  55         if (mbean.isThreadCpuTimeEnabled()) {
  56             mbean.setThreadCpuTimeEnabled(false);
  57         }
  58 
  59         if (mbean.isThreadCpuTimeEnabled()) {
  60             throw new RuntimeException("ThreadCpuTime is expected to be disabled");
  61         }
  62 
  63         // start threads, wait for them to block
  64         long[] ids = new long[NUM_THREADS];
  65 
  66         for (int i = 0; i < NUM_THREADS; i++) {
  67             threads[i] = new MyThread("MyThread-" + i);
  68             threads[i].start();
  69             ids[i] = threads[i].getId();
  70         }
  71 
  72         // threads block after doing some computation
  73         waitUntilThreadBlocked();
  74 
  75 
  76         long times[] = mbean.getThreadCpuTime(ids);
  77         long userTimes[] = mbean.getThreadUserTime(ids);
  78 
  79         if (times == null) {
  80             throw new RuntimeException("Null ThreadCpuTime array returned");
  81         }
  82 
  83         for (int i = 0; i < NUM_THREADS; i++) {
  84             long t = times[i];
  85             if (t != -1) {
  86                 throw new RuntimeException(
  87                     "Invalid ThreadCpuTime returned for thread " +
  88                     threads[i].getName() + " = " +  t + " expected = -1");
  89             }
  90             long ut = userTimes[i];
  91             if (ut != -1) {
  92                 throw new RuntimeException(
  93                     "Invalid ThreadUserTime returned for thread " +
  94                     threads[i].getName() + " = " +  ut + " expected = -1");
  95             }
  96         }
  97 
  98 
  99         // Enable CPU Time measurement
 100         if (!mbean.isThreadCpuTimeEnabled()) {
 101             mbean.setThreadCpuTimeEnabled(true);
 102         }
 103 
 104         if (!mbean.isThreadCpuTimeEnabled()) {
 105             throw new RuntimeException("ThreadCpuTime is expected to be enabled");
 106         }
 107 
 108         times = mbean.getThreadCpuTime(ids);
 109         userTimes = mbean.getThreadUserTime(ids);
 110 
 111         goSleep(200);
 112 
 113         for (int i = 0; i < NUM_THREADS; i++) {
 114             long t = times[i];
 115             if (t < 0) {
 116                 throw new RuntimeException(
 117                     "Invalid CPU time returned for thread " +
 118                     threads[i].getName() + " = " + t);
 119             }
 120             long ut = userTimes[i];
 121             if (ut < 0) {
 122                 throw new RuntimeException(
 123                     "Invalid user time returned for thread " +
 124                     threads[i].getName() + " = " + ut);
 125             }
 126         }
 127 
 128         long[] times1 = mbean.getThreadCpuTime(ids);
 129         long[] userTimes1 = mbean.getThreadUserTime(ids);
 130 
 131         for (int i = 0; i < NUM_THREADS; i++) {
 132             long newTime = times1[i];
 133             long newUserTime = userTimes1[i];
 134 
 135             if (times[i] > newTime) {
 136                 throw new RuntimeException("TEST FAILED: " +
 137                     threads[i].getName() +
 138                     " previous CPU time = " + times[i] +
 139                     " > current CPU time = " + newTime);
 140             }
 141             if ((times[i] + DELTA) < newTime) {
 142                 throw new RuntimeException("TEST FAILED: " +
 143                     threads[i].getName() +
 144                     " CPU time = " + newTime +
 145                     " previous CPU time " + times[i] +
 146                     " out of expected range");
 147             }
 148 
 149             System.out.println(threads[i].getName() +
 150                 " Previous Cpu Time = " + times[i] +
 151                 " Current CPU time = " + newTime);
 152 
 153             System.out.println(threads[i].getName() +
 154                 " Previous User Time = " + userTimes[i] +
 155                 " Current User time = " + newUserTime);
 156         }
 157 
 158 
 159         try {
 160             times = mbean.getThreadCpuTime(null);
 161         } catch (NullPointerException e) {
 162             System.out.println(
 163                 "Caught expected NullPointerException: " + e.getMessage());
 164         }
 165 
 166         try {
 167             ids[0] = 0;
 168             times = mbean.getThreadCpuTime(ids);
 169         } catch (IllegalArgumentException e) {
 170             System.out.println(
 171                 "Caught expected IllegalArgumentException: " + e.getMessage());
 172         }
 173 
 174 
 175         // let threads exit
 176         synchronized (obj) {
 177             done = true;
 178             obj.notifyAll();
 179         }
 180 
 181         for (int i = 0; i < NUM_THREADS; i++) {
 182             try {
 183                 threads[i].join();
 184             } catch (InterruptedException e) {
 185                 System.out.println("Unexpected exception is thrown.");
 186                 e.printStackTrace(System.out);
 187                 testFailed = true;
 188                 break;
 189             }
 190         }
 191 
 192         if (testFailed) {
 193             throw new RuntimeException("TEST FAILED");
 194         }
 195 
 196         System.out.println("Test passed");
 197     }
 198 
 199 
 200     private static void goSleep(long ms) throws Exception {
 201         try {
 202             Thread.sleep(ms);
 203         } catch (InterruptedException e) {
 204             System.out.println("Unexpected exception is thrown.");
 205             throw e;
 206         }
 207     }
 208 
 209     private static void waitUntilThreadBlocked()
 210         throws Exception {
 211         int count = 0;
 212         while (count != NUM_THREADS) {
 213             goSleep(100);
 214             count = 0;
 215             for (int i = 0; i < NUM_THREADS; i++) {
 216                 ThreadInfo info = mbean.getThreadInfo(threads[i].getId());
 217                 if (info.getThreadState() == Thread.State.WAITING) {
 218                     count++;
 219                 }
 220             }
 221         }
 222     }
 223 
 224     public static void doit() {
 225         double sum = 0;
 226         for (int i = 0; i < 5000; i++) {
 227            double r = Math.random();
 228            double x = Math.pow(3, r);
 229            sum += x - r;
 230         }
 231         System.out.println(Thread.currentThread().getName() +
 232                            " sum = " + sum);
 233     }
 234 
 235     static class MyThread extends Thread {
 236         public MyThread(String name) {
 237             super(name);
 238         }
 239 
 240         public void run() {
 241             ThreadCpuTimeArray.doit();
 242 
 243             synchronized (obj) {
 244                 while (!done) {
 245                     try {
 246                         obj.wait();
 247                     } catch (InterruptedException e) {
 248                         System.out.println("Unexpected exception is thrown.");
 249                         e.printStackTrace(System.out);
 250                         testFailed = true;
 251                         break;
 252                     }
 253                 }
 254             }
 255 
 256             ThreadCpuTimeArray.doit();
 257         }
 258     }
 259 }