Path: blob/master/test/jdk/sun/management/HotspotThreadMBean/GetInternalThreads.java
41152 views
/*1* Copyright (c) 2003, 2015, 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*30* @run main/othervm -XX:+UsePerfData GetInternalThreads31*/3233import sun.management.*;34import java.util.*;35import java.lang.management.ThreadMXBean;36import java.lang.management.ManagementFactory;3738public class GetInternalThreads {39private final static HotspotThreadMBean mbean =40ManagementFactoryHelper.getHotspotThreadMBean();4142// Minimum number of VM internal threads43// VM thread, watcher thread, Low memory detector, compiler thread44private static final long MIN_VALUE_FOR_PASS = 4;45private static final long MAX_VALUE_FOR_PASS = Long.MAX_VALUE;4647public static void main(String[] args) throws Exception {48long value = mbean.getInternalThreadCount();4950if (value < MIN_VALUE_FOR_PASS || value > MAX_VALUE_FOR_PASS) {51throw new RuntimeException("Internal thread count " +52"illegal value: " + value + " " +53"(MIN = " + MIN_VALUE_FOR_PASS + "; " +54"MAX = " + MAX_VALUE_FOR_PASS + ")");55}5657System.out.println("Internal Thread Count = " + value);5859ThreadMXBean thread =60ManagementFactory.getThreadMXBean();61if (!thread.isThreadCpuTimeSupported()) {62System.out.println("Thread Cpu Time is not supported.");63return;64}6566while(!testCPUTime()) {67Thread.sleep(100);68}69}7071private static boolean testCPUTime() {72Map<String, Long> times = mbean.getInternalThreadCpuTimes();73for(Map.Entry<String, Long> entry : times.entrySet()) {74String threadName = entry.getKey();75long cpuTime = entry.getValue();76System.out.println("CPU time = " + cpuTime + " for " + threadName);77if (cpuTime == -1) {78// Can happen when there is a race between a thread being created79// and the request to get its CPU time. The "/proc/..." structure might80// not be ready at that time and the routine will return -1.81System.out.println("Retry, proc structure might not be ready (-1)");82return false;83}84if (cpuTime < 0) {85throw new RuntimeException("Illegal CPU time: " + cpuTime);86}87}88return true;89}90}919293