|
1 /* |
|
2 * Copyright 2003-2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 4530538 |
|
27 * @summary Basic Test for HotspotThreadMBean.getInternalThreadCount() |
|
28 * and getInternalThreadCpuTime() |
|
29 * @author Mandy Chung |
|
30 */ |
|
31 |
|
32 import sun.management.*; |
|
33 import java.util.*; |
|
34 import java.lang.management.ThreadMXBean; |
|
35 |
|
36 public class GetInternalThreads { |
|
37 private static HotspotThreadMBean mbean = |
|
38 ManagementFactory.getHotspotThreadMBean(); |
|
39 |
|
40 // Minimum number of VM internal threads |
|
41 // VM thread, watcher thread, Low memory detector, compiler thread |
|
42 private static final long MIN_VALUE_FOR_PASS = 4; |
|
43 private static final long MAX_VALUE_FOR_PASS = Long.MAX_VALUE; |
|
44 |
|
45 public static void main(String[] args) { |
|
46 long value = mbean.getInternalThreadCount(); |
|
47 |
|
48 if (value < MIN_VALUE_FOR_PASS || value > MAX_VALUE_FOR_PASS) { |
|
49 throw new RuntimeException("Internal thread count " + |
|
50 "illegal value: " + value + " " + |
|
51 "(MIN = " + MIN_VALUE_FOR_PASS + "; " + |
|
52 "MAX = " + MAX_VALUE_FOR_PASS + ")"); |
|
53 } |
|
54 |
|
55 System.out.println("Internal Thread Count = " + value); |
|
56 |
|
57 ThreadMXBean thread = |
|
58 java.lang.management.ManagementFactory.getThreadMXBean(); |
|
59 if (!thread.isThreadCpuTimeSupported()) { |
|
60 System.out.println("Thread Cpu Time is not supported."); |
|
61 return; |
|
62 } |
|
63 |
|
64 Map times = mbean.getInternalThreadCpuTimes(); |
|
65 Iterator iter = times.entrySet().iterator(); |
|
66 for (; iter.hasNext(); ) { |
|
67 Map.Entry entry = (Map.Entry) iter.next(); |
|
68 System.out.println(entry.getKey() + |
|
69 " CPU time = " + entry.getValue() + "ns"); |
|
70 if (((Long) entry.getValue()).longValue() < 0) { |
|
71 throw new RuntimeException("Thread CPU time" + |
|
72 "illegal value: " + entry.getValue()); |
|
73 } |
|
74 } |
|
75 System.out.println("Test passed."); |
|
76 } |
|
77 } |