jdk/test/sun/management/HotspotThreadMBean/GetInternalThreads.java
author ihse
Mon, 09 Mar 2015 10:39:16 +0100
changeset 29304 81a723f8d33c
parent 26195 64b54ed39429
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
8074096: Disable (most) native warnings in JDK on a per-library basis Reviewed-by: erikj, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
21653
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug     4530538
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Basic Test for HotspotThreadMBean.getInternalThreadCount()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          and getInternalThreadCpuTime()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author  Mandy Chung
26195
64b54ed39429 8040692: [TESTBUG] sun/management/jmxremote/bootstrap/JvmstatCountersTest.java requires -XX:+UsePerfData option to pass on embedded platforms
jbachorik
parents: 21653
diff changeset
    30
 * @run main/othervm -XX:+UsePerfData GetInternalThreads
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.management.ThreadMXBean;
403
dc6f4ca08b81 6687508: Update test/sun/management jtreg tests due to sun.management.ManagementFactory class rename
mchung
parents: 2
diff changeset
    36
import java.lang.management.ManagementFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class GetInternalThreads {
21653
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    39
    private final static HotspotThreadMBean mbean =
403
dc6f4ca08b81 6687508: Update test/sun/management jtreg tests due to sun.management.ManagementFactory class rename
mchung
parents: 2
diff changeset
    40
        ManagementFactoryHelper.getHotspotThreadMBean();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    // Minimum number of VM internal threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    //   VM thread, watcher thread, Low memory detector, compiler thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final long MIN_VALUE_FOR_PASS = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final long MAX_VALUE_FOR_PASS = Long.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
21653
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    47
    public static void main(String[] args) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        long value = mbean.getInternalThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        if (value < MIN_VALUE_FOR_PASS || value > MAX_VALUE_FOR_PASS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            throw new RuntimeException("Internal thread count " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                                       "illegal value: " + value + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                                       "(MIN = " + MIN_VALUE_FOR_PASS + "; " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                                       "MAX = " + MAX_VALUE_FOR_PASS + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        System.out.println("Internal Thread Count = " + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        ThreadMXBean thread =
403
dc6f4ca08b81 6687508: Update test/sun/management jtreg tests due to sun.management.ManagementFactory class rename
mchung
parents: 2
diff changeset
    60
            ManagementFactory.getThreadMXBean();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (!thread.isThreadCpuTimeSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            System.out.println("Thread Cpu Time is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
21653
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    66
        while(!testCPUTime()) {
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    67
            Thread.sleep(100);
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    68
        }
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    69
    }
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    70
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    71
    private static boolean testCPUTime() {
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    72
        Map<String, Long> times = mbean.getInternalThreadCpuTimes();
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    73
        for(Map.Entry<String, Long> entry : times.entrySet())  {
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    74
            String threadName = entry.getKey();
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    75
            long cpuTime = entry.getValue();
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    76
            System.out.println("CPU time = " + cpuTime + " for " + threadName);
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    77
            if (cpuTime == -1) {
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    78
                // Can happen when there is a race between a thread being created
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    79
                // and the request to get its CPU time. The "/proc/..." structure might
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    80
                // not be ready at that time and the routine will return -1.
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    81
                System.out.println("Retry, proc structure might not be ready (-1)");
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    82
                return false;
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    83
            }
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    84
            if (cpuTime < 0) {
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    85
                throw new RuntimeException("Illegal CPU time: " + cpuTime);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
21653
8cb51c29ab64 6849945: VM Periodic Task Thread CPU time = -1ns in HotspotThreadMBean.getInternalThreadCpuTimes()
egahlin
parents: 5506
diff changeset
    88
        return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
}