1 /*
  2  * Copyright (c) 2011, 2015, 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  * @key randomness
 28  * @summary Basic test of ThreadMXBean.getThreadCpuTime(long[]) and
 29  *          getThreadUserTime(long[]).
 30  * @author  Paul Hohensee
 31  */
 32 
 33 import java.lang.management.*;
 34 
 35 public class ThreadCpuTimeArray {
 36     private static com.sun.management.ThreadMXBean mbean =
 37         (com.sun.management.ThreadMXBean)ManagementFactory.getThreadMXBean();
 38     private static boolean testFailed = false;
 39     private static boolean done = false;
 40     private static Object obj = new Object();
 41     private static final int NUM_THREADS = 10;
 42     private static Thread[] threads = new Thread[NUM_THREADS];
 43 
 44     // careful about this value
 45     private static final int DELTA = 100;
 46 
 47     public static void main(String[] argv)
 48         throws Exception {
 49 
 50         if (!mbean.isThreadCpuTimeSupported()) {
 51             return;
 52         }
 53 
 54 
 55         // disable CPU time
 56         if (mbean.isThreadCpuTimeEnabled()) {
 57             mbean.setThreadCpuTimeEnabled(false);
 58         }
 59 
 60         if (mbean.isThreadCpuTimeEnabled()) {
 61             throw new RuntimeException("ThreadCpuTime is expected to be disabled");
 62         }
 63 
 64         // start threads, wait for them to block
 65         long[] ids = new long[NUM_THREADS];
 66 
 67         for (int i = 0; i < NUM_THREADS; i++) {
 68             threads[i] = new MyThread("MyThread-" + i);
 69             threads[i].start();
 70             ids[i] = threads[i].getId();
 71         }
 72 
 73         // threads block after doing some computation
 74         waitUntilThreadBlocked();
 75 
 76 
 77         long times[] = mbean.getThreadCpuTime(ids);
 78         long userTimes[] = mbean.getThreadUserTime(ids);
 79 
 80         if (times == null) {
 81             throw new RuntimeException("Null ThreadCpuTime array returned");
 82         }
 83 
 84         for (int i = 0; i < NUM_THREADS; i++) {
 85             long t = times[i];
 86             if (t != -1) {
 87                 throw new RuntimeException(
 88                     "Invalid ThreadCpuTime returned for thread " +
 89                     threads[i].getName() + " = " +  t + " expected = -1");
 90             }
 91             long ut = userTimes[i];
 92             if (ut != -1) {
 93                 throw new RuntimeException(
 94                     "Invalid ThreadUserTime returned for thread " +
 95                     threads[i].getName() + " = " +  ut + " expected = -1");
 96             }
 97         }
 98 
 99 
100         // Enable CPU Time measurement
101         if (!mbean.isThreadCpuTimeEnabled()) {
102             mbean.setThreadCpuTimeEnabled(true);
103         }
104 
105         if (!mbean.isThreadCpuTimeEnabled()) {
106             throw new RuntimeException("ThreadCpuTime is expected to be enabled");
107         }
108 
109         times = mbean.getThreadCpuTime(ids);
110         userTimes = mbean.getThreadUserTime(ids);
111 
112         goSleep(200);
113 
114         for (int i = 0; i < NUM_THREADS; i++) {
115             long t = times[i];
116             if (t < 0) {
117                 throw new RuntimeException(
118                     "Invalid CPU time returned for thread " +
119                     threads[i].getName() + " = " + t);
120             }
121             long ut = userTimes[i];
122             if (ut < 0) {
123                 throw new RuntimeException(
124                     "Invalid user time returned for thread " +
125                     threads[i].getName() + " = " + ut);
126             }
127         }
128 
129         long[] times1 = mbean.getThreadCpuTime(ids);
130         long[] userTimes1 = mbean.getThreadUserTime(ids);
131 
132         for (int i = 0; i < NUM_THREADS; i++) {
133             long newTime = times1[i];
134             long newUserTime = userTimes1[i];
135 
136             if (times[i] > newTime) {
137                 throw new RuntimeException("TEST FAILED: " +
138                     threads[i].getName() +
139                     " previous CPU time = " + times[i] +
140                     " > current CPU time = " + newTime);
141             }
142             if ((times[i] + DELTA) < newTime) {
143                 throw new RuntimeException("TEST FAILED: " +
144                     threads[i].getName() +
145                     " CPU time = " + newTime +
146                     " previous CPU time " + times[i] +
147                     " out of expected range");
148             }
149 
150             System.out.println(threads[i].getName() +
151                 " Previous Cpu Time = " + times[i] +
152                 " Current CPU time = " + newTime);
153 
154             System.out.println(threads[i].getName() +
155                 " Previous User Time = " + userTimes[i] +
156                 " Current User time = " + newUserTime);
157         }
158 
159 
160         try {
161             times = mbean.getThreadCpuTime(null);
162         } catch (NullPointerException e) {
163             System.out.println(
164                 "Caught expected NullPointerException: " + e.getMessage());
165         }
166 
167         try {
168             ids[0] = 0;
169             times = mbean.getThreadCpuTime(ids);
170         } catch (IllegalArgumentException e) {
171             System.out.println(
172                 "Caught expected IllegalArgumentException: " + e.getMessage());
173         }
174 
175 
176         // let threads exit
177         synchronized (obj) {
178             done = true;
179             obj.notifyAll();
180         }
181 
182         for (int i = 0; i < NUM_THREADS; i++) {
183             try {
184                 threads[i].join();
185             } catch (InterruptedException e) {
186                 System.out.println("Unexpected exception is thrown.");
187                 e.printStackTrace(System.out);
188                 testFailed = true;
189                 break;
190             }
191         }
192 
193         if (testFailed) {
194             throw new RuntimeException("TEST FAILED");
195         }
196 
197         System.out.println("Test passed");
198     }
199 
200 
201     private static void goSleep(long ms) throws Exception {
202         try {
203             Thread.sleep(ms);
204         } catch (InterruptedException e) {
205             System.out.println("Unexpected exception is thrown.");
206             throw e;
207         }
208     }
209 
210     private static void waitUntilThreadBlocked()
211         throws Exception {
212         int count = 0;
213         while (count != NUM_THREADS) {
214             goSleep(100);
215             count = 0;
216             for (int i = 0; i < NUM_THREADS; i++) {
217                 ThreadInfo info = mbean.getThreadInfo(threads[i].getId());
218                 if (info.getThreadState() == Thread.State.WAITING) {
219                     count++;
220                 }
221             }
222         }
223     }
224 
225     public static void doit() {
226         double sum = 0;
227         for (int i = 0; i < 5000; i++) {
228            double r = Math.random();
229            double x = Math.pow(3, r);
230            sum += x - r;
231         }
232         System.out.println(Thread.currentThread().getName() +
233                            " sum = " + sum);
234     }
235 
236     static class MyThread extends Thread {
237         public MyThread(String name) {
238             super(name);
239         }
240 
241         public void run() {
242             ThreadCpuTimeArray.doit();
243 
244             synchronized (obj) {
245                 while (!done) {
246                     try {
247                         obj.wait();
248                     } catch (InterruptedException e) {
249                         System.out.println("Unexpected exception is thrown.");
250                         e.printStackTrace(System.out);
251                         testFailed = true;
252                         break;
253                     }
254                 }
255             }
256 
257             ThreadCpuTimeArray.doit();
258         }
259     }
260 }