Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/management/HotspotThreadMBean/GetInternalThreads.java
38855 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 453053826* @summary Basic Test for HotspotThreadMBean.getInternalThreadCount()27* and getInternalThreadCpuTime()28* @author Mandy Chung29*/3031import sun.management.*;32import java.util.*;33import java.lang.management.ThreadMXBean;34import java.lang.management.ManagementFactory;3536public class GetInternalThreads {37private final static HotspotThreadMBean mbean =38ManagementFactoryHelper.getHotspotThreadMBean();3940// Minimum number of VM internal threads41// VM thread, watcher thread, Low memory detector, compiler thread42private static final long MIN_VALUE_FOR_PASS = 4;43private static final long MAX_VALUE_FOR_PASS = Long.MAX_VALUE;4445public static void main(String[] args) throws Exception {46long value = mbean.getInternalThreadCount();4748if (value < MIN_VALUE_FOR_PASS || value > MAX_VALUE_FOR_PASS) {49throw new RuntimeException("Internal thread count " +50"illegal value: " + value + " " +51"(MIN = " + MIN_VALUE_FOR_PASS + "; " +52"MAX = " + MAX_VALUE_FOR_PASS + ")");53}5455System.out.println("Internal Thread Count = " + value);5657ThreadMXBean thread =58ManagementFactory.getThreadMXBean();59if (!thread.isThreadCpuTimeSupported()) {60System.out.println("Thread Cpu Time is not supported.");61return;62}6364while(!testCPUTime()) {65Thread.sleep(100);66}67}6869private static boolean testCPUTime() {70Map<String, Long> times = mbean.getInternalThreadCpuTimes();71for(Map.Entry<String, Long> entry : times.entrySet()) {72String threadName = entry.getKey();73long cpuTime = entry.getValue();74System.out.println("CPU time = " + cpuTime + " for " + threadName);75if (cpuTime == -1) {76// Can happen when there is a race between a thread being created77// and the request to get its CPU time. The "/proc/..." structure might78// not be ready at that time and the routine will return -1.79System.out.println("Retry, proc structure might not be ready (-1)");80return false;81}82if (cpuTime < 0) {83throw new RuntimeException("Illegal CPU time: " + cpuTime);84}85}86return true;87}88}899091