Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/management/HotspotThreadMBean/GetInternalThreads.java
38855 views
1
/*
2
* Copyright (c) 2003, 2013, 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 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
import java.lang.management.ManagementFactory;
36
37
public class GetInternalThreads {
38
private final static HotspotThreadMBean mbean =
39
ManagementFactoryHelper.getHotspotThreadMBean();
40
41
// Minimum number of VM internal threads
42
// VM thread, watcher thread, Low memory detector, compiler thread
43
private static final long MIN_VALUE_FOR_PASS = 4;
44
private static final long MAX_VALUE_FOR_PASS = Long.MAX_VALUE;
45
46
public static void main(String[] args) throws Exception {
47
long value = mbean.getInternalThreadCount();
48
49
if (value < MIN_VALUE_FOR_PASS || value > MAX_VALUE_FOR_PASS) {
50
throw new RuntimeException("Internal thread count " +
51
"illegal value: " + value + " " +
52
"(MIN = " + MIN_VALUE_FOR_PASS + "; " +
53
"MAX = " + MAX_VALUE_FOR_PASS + ")");
54
}
55
56
System.out.println("Internal Thread Count = " + value);
57
58
ThreadMXBean thread =
59
ManagementFactory.getThreadMXBean();
60
if (!thread.isThreadCpuTimeSupported()) {
61
System.out.println("Thread Cpu Time is not supported.");
62
return;
63
}
64
65
while(!testCPUTime()) {
66
Thread.sleep(100);
67
}
68
}
69
70
private static boolean testCPUTime() {
71
Map<String, Long> times = mbean.getInternalThreadCpuTimes();
72
for(Map.Entry<String, Long> entry : times.entrySet()) {
73
String threadName = entry.getKey();
74
long cpuTime = entry.getValue();
75
System.out.println("CPU time = " + cpuTime + " for " + threadName);
76
if (cpuTime == -1) {
77
// Can happen when there is a race between a thread being created
78
// and the request to get its CPU time. The "/proc/..." structure might
79
// not be ready at that time and the routine will return -1.
80
System.out.println("Retry, proc structure might not be ready (-1)");
81
return false;
82
}
83
if (cpuTime < 0) {
84
throw new RuntimeException("Illegal CPU time: " + cpuTime);
85
}
86
}
87
return true;
88
}
89
}
90
91