ThreadCpuTime.java revision 16930:13c06d444258
1185573Srwatson/*
2191273Srwatson * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
3155131Srwatson * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4155131Srwatson *
5155131Srwatson * This code is free software; you can redistribute it and/or modify it
6155131Srwatson * under the terms of the GNU General Public License version 2 only, as
7155131Srwatson * published by the Free Software Foundation.
8155131Srwatson *
9155131Srwatson * This code is distributed in the hope that it will be useful, but WITHOUT
10155131Srwatson * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11155131Srwatson * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12155131Srwatson * version 2 for more details (a copy is included in the LICENSE file that
13185573Srwatson * accompanied this code).
14155131Srwatson *
15155131Srwatson * You should have received a copy of the GNU General Public License version
16155131Srwatson * 2 along with this work; if not, write to the Free Software Foundation,
17155131Srwatson * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18155131Srwatson *
19155131Srwatson * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20155131Srwatson * or visit www.oracle.com if you need additional information or have any
21155131Srwatson * questions.
22155131Srwatson */
23155131Srwatson
24155131Srwatson/*
25155131Srwatson * @test
26155131Srwatson * @bug     4530538
27155131Srwatson * @summary Basic test of ThreadMXBean.getThreadCpuTime and
28155131Srwatson *          getCurrentThreadCpuTime.
29191273Srwatson * @author  Mandy Chung
30155131Srwatson */
31155131Srwatson
32155131Srwatsonimport java.lang.management.*;
33155131Srwatson
34155131Srwatsonpublic class ThreadCpuTime {
35156283Srwatson    private static ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
36156283Srwatson    private static boolean testFailed = false;
37156283Srwatson    private static boolean done = false;
38156283Srwatson    private static Object obj = new Object();
39156283Srwatson    private static final int NUM_THREADS = 10;
40156283Srwatson    private static Thread[] threads = new Thread[NUM_THREADS];
41156283Srwatson    private static long[] times = new long[NUM_THREADS];
42156283Srwatson
43156283Srwatson    // careful about this value
44156283Srwatson    private static final int DELTA = 100;
45155131Srwatson
46155131Srwatson    public static void main(String[] argv)
47156283Srwatson        throws Exception {
48159985Srwatson        if (!mbean.isCurrentThreadCpuTimeSupported()) {
49156283Srwatson            return;
50156283Srwatson        }
51156283Srwatson
52156283Srwatson       // disable CPU time
53156283Srwatson        if (mbean.isThreadCpuTimeEnabled()) {
54155518Srwatson            mbean.setThreadCpuTimeEnabled(false);
55155518Srwatson        }
56155131Srwatson
57155131Srwatson        Thread curThread = Thread.currentThread();
58155131Srwatson        long t = mbean.getCurrentThreadCpuTime();
59155131Srwatson        if (t != -1) {
60155131Srwatson            throw new RuntimeException("Invalid CurrenThreadCpuTime returned = " +
61155131Srwatson                t + " expected = -1");
62155131Srwatson        }
63191273Srwatson
64155131Srwatson        if (mbean.isThreadCpuTimeSupported()) {
65155131Srwatson            long t1 = mbean.getThreadCpuTime(curThread.getId());
66155131Srwatson            if (t1 != -1) {
67155131Srwatson                throw new RuntimeException("Invalid ThreadCpuTime returned = " +
68155131Srwatson                    t1 + " expected = -1");
69168777Srwatson            }
70168777Srwatson        }
71155131Srwatson
72155131Srwatson        // Enable CPU Time measurement
73155131Srwatson        if (!mbean.isThreadCpuTimeEnabled()) {
74155131Srwatson            mbean.setThreadCpuTimeEnabled(true);
75155131Srwatson        }
76155131Srwatson
77155131Srwatson        if (!mbean.isThreadCpuTimeEnabled()) {
78155131Srwatson            throw new RuntimeException("ThreadCpuTime is expected to be enabled");
79155131Srwatson        }
80191273Srwatson
81155131Srwatson        long time = mbean.getCurrentThreadCpuTime();
82155131Srwatson        if (time < 0) {
83155131Srwatson            throw new RuntimeException("Invalid CPU time returned = " + time);
84155131Srwatson        }
85155131Srwatson
86155131Srwatson        if (!mbean.isThreadCpuTimeSupported()) {
87155131Srwatson            return;
88155131Srwatson        }
89155131Srwatson
90155131Srwatson
91155131Srwatson        // Expected to be time1 >= time
92155131Srwatson        long time1 = mbean.getThreadCpuTime(curThread.getId());
93155131Srwatson        if (time1 < time) {
94155131Srwatson            throw new RuntimeException("CPU time " + time1 +
95155131Srwatson                " expected >= " + time);
96155131Srwatson        }
97155131Srwatson        System.out.println(curThread.getName() +
98155131Srwatson            " Current Thread Cpu Time = " + time +
99155131Srwatson            " CPU time = " + time1);
100155131Srwatson
101155131Srwatson        for (int i = 0; i < NUM_THREADS; i++) {
102155131Srwatson            threads[i] = new MyThread("MyThread-" + i);
103155131Srwatson            threads[i].start();
104155131Srwatson        }
105155131Srwatson
106155131Srwatson        waitUntilThreadBlocked();
107155131Srwatson
108155131Srwatson        for (int i = 0; i < NUM_THREADS; i++) {
109155131Srwatson            times[i] = mbean.getThreadCpuTime(threads[i].getId());
110155131Srwatson        }
111155131Srwatson
112168777Srwatson        goSleep(200);
113155131Srwatson
114155131Srwatson        for (int i = 0; i < NUM_THREADS; i++) {
115155131Srwatson            long newTime = mbean.getThreadCpuTime(threads[i].getId());
116155131Srwatson            if (times[i] > newTime) {
117155131Srwatson                throw new RuntimeException("TEST FAILED: " +
118155131Srwatson                    threads[i].getName() +
119155131Srwatson                    " previous CPU time = " + times[i] +
120155131Srwatson                    " > current CPU time = " + newTime);
121155131Srwatson            }
122155131Srwatson            if ((times[i] + DELTA) < newTime) {
123155131Srwatson                throw new RuntimeException("TEST FAILED: " +
124155131Srwatson                    threads[i].getName() +
125155131Srwatson                    " CPU time = " + newTime +
126155131Srwatson                    " previous CPU time " + times[i] +
127155131Srwatson                    " out of expected range");
128155131Srwatson            }
129155131Srwatson
130155131Srwatson            System.out.println(threads[i].getName() +
131155131Srwatson                " Previous Cpu Time = " + times[i] +
132155131Srwatson                " Current CPU time = " + newTime);
133155131Srwatson        }
134155131Srwatson
135155131Srwatson        synchronized (obj) {
136155131Srwatson            done = true;
137155131Srwatson            obj.notifyAll();
138155131Srwatson        }
139155131Srwatson
140191273Srwatson        for (int i = 0; i < NUM_THREADS; i++) {
141155131Srwatson            try {
142155131Srwatson                threads[i].join();
143155131Srwatson            } catch (InterruptedException e) {
144155131Srwatson                System.out.println("Unexpected exception is thrown.");
145155131Srwatson                e.printStackTrace(System.out);
146155131Srwatson                testFailed = true;
147155131Srwatson                break;
148155131Srwatson            }
149155131Srwatson        }
150155131Srwatson        if (testFailed) {
151155131Srwatson            throw new RuntimeException("TEST FAILED");
152155131Srwatson        }
153155131Srwatson
154155131Srwatson        System.out.println("Test passed");
155155131Srwatson    }
156155131Srwatson
157155131Srwatson
158155131Srwatson    private static void goSleep(long ms) throws Exception {
159155131Srwatson        try {
160155518Srwatson            Thread.sleep(ms);
161155131Srwatson        } catch (InterruptedException e) {
162155518Srwatson            System.out.println("Unexpected exception is thrown.");
163155518Srwatson            throw e;
164155518Srwatson        }
165155518Srwatson    }
166155518Srwatson
167155518Srwatson    private static void waitUntilThreadBlocked()
168191273Srwatson        throws Exception {
169155518Srwatson        int count = 0;
170191273Srwatson        while (count != NUM_THREADS) {
171155518Srwatson            goSleep(100);
172155518Srwatson            count = 0;
173155518Srwatson            for (int i = 0; i < NUM_THREADS; i++) {
174155518Srwatson                ThreadInfo info = mbean.getThreadInfo(threads[i].getId());
175155518Srwatson                if (info.getThreadState() == Thread.State.WAITING) {
176155518Srwatson                    count++;
177191273Srwatson                }
178155518Srwatson            }
179155518Srwatson        }
180155518Srwatson    }
181155518Srwatson
182    static class MyThread extends Thread {
183        public MyThread(String name) {
184            super(name);
185        }
186
187        public void run() {
188            double sum = 0;
189            for (int i = 0; i < 5000; i++) {
190               double r = Math.random();
191               double x = Math.pow(3, r);
192               sum += x - r;
193            }
194            synchronized (obj) {
195                while (!done) {
196                    try {
197                        obj.wait();
198                    } catch (InterruptedException e) {
199                        System.out.println("Unexpected exception is thrown.");
200                        e.printStackTrace(System.out);
201                        testFailed = true;
202                        break;
203                    }
204                }
205            }
206
207            sum = 0;
208            for (int i = 0; i < 5000; i++) {
209               double r = Math.random();
210               double x = Math.pow(3, r);
211               sum += x - r;
212            }
213
214            long utime1 = mbean.getCurrentThreadUserTime();
215            long utime2 = mbean.getThreadUserTime(getId());
216            long time1 = mbean.getCurrentThreadCpuTime();
217            long time2 = mbean.getThreadCpuTime(getId());
218
219            System.out.println(getName() + ": " +
220                "CurrentThreadUserTime = " + utime1 +
221                " ThreadUserTime = " + utime2);
222            System.out.println(getName() + ": " +
223                "CurrentThreadCpuTime  = " + time1 +
224                " ThreadCpuTime  = " + time2);
225
226            if (time1 > time2) {
227                throw new RuntimeException("TEST FAILED: " + getName() +
228                    " CurrentThreadCpuTime = " + time1 +
229                    " > ThreadCpuTime = " + time2);
230            }
231/*************
232 * FIXME: Seems that on Solaris-sparc,
233 * It occasionally returns a different current thread user time > thread user time
234            if (utime1 > utime2) {
235                throw new RuntimeException("TEST FAILED: " + getName() +
236                    " CurrentThreadUserTime = " + utime1 +
237                    " > ThreadUserTime = " + utime2);
238            }
239*/
240        }
241    }
242
243}
244