1/*
2 * Copyright (c) 2012, 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 7196045
27 * @bug 8014294
28 * @summary Possible JVM deadlock in ThreadTimesClosure when using HotspotInternal non-public API.
29 * @modules java.management/sun.management
30 * @run main/othervm -XX:+UsePerfData -Xmx32m ThreadCpuTimesDeadlock
31 */
32
33import java.lang.management.ManagementFactory;
34import javax.management.JMException;
35import javax.management.MBeanServer;
36import javax.management.MalformedObjectNameException;
37import javax.management.ObjectName;
38
39public class ThreadCpuTimesDeadlock {
40
41    public static byte[] dummy;
42    public static long duration = 10 * 1000;
43    private static final String HOTSPOT_INTERNAL = "sun.management:type=HotspotInternal";
44
45    public static void main(String[] args) {
46
47        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
48        ObjectName objName= null;
49        try {
50            ObjectName hotspotInternal = new ObjectName(HOTSPOT_INTERNAL);
51            try {
52                server.registerMBean(new sun.management.HotspotInternal(), hotspotInternal);
53            } catch (JMException e) {
54                throw new RuntimeException("HotSpotWatcher: Failed to register the HotspotInternal MBean" + e);
55            }
56            objName= new ObjectName("sun.management:type=HotspotThreading");
57
58        } catch (MalformedObjectNameException e1) {
59            throw new RuntimeException("Bad object name" + e1);
60        }
61
62        // Thread that allocs memory to generate GC's
63        Thread allocThread = new Thread() {
64          public void run() {
65            while (true) {
66              dummy = new byte[4096];
67            }
68          }
69        };
70
71        allocThread.setDaemon(true);
72        allocThread.start();
73
74        long endTime = System.currentTimeMillis() + duration;
75        long i = 0;
76        while (true) {
77            try {
78                server.getAttribute(objName, "InternalThreadCpuTimes");
79            } catch (Exception ex) {
80                System.err.println("Exception while getting attribute: " + ex);
81            }
82            i++;
83            if (i % 10000 == 0) {
84                System.out.println("Successful iterations: " + i);
85            }
86            if (System.currentTimeMillis() > endTime) {
87                break;
88            }
89        }
90        System.out.println("PASSED.");
91    }
92}
93