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