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