1 /*
 2  * Copyright (c) 2023, 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 import jdk.test.lib.process.OutputAnalyzer;
25 import jdk.test.lib.Platform;
26 import jdk.test.lib.process.ProcessTools;
27 
28 import java.lang.management.ManagementFactory;
29 import java.lang.management.ThreadInfo;
30 import java.lang.management.ThreadMXBean;
31 
32 /*
33  * @test
34  * @bug 8318757
35  * @summary Test concurrent monitor deflation by MonitorDeflationThread and thread dumping
36  * @library /test/lib
37  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:GuaranteedAsyncDeflationInterval=2000 -XX:+UnlockExperimentalVMOptions -XX:LockingMode=0 ConcurrentDeflation
38  */
39 
40 public class ConcurrentDeflation {
41     public static final long TOTAL_RUN_TIME_NS = 10_000_000_000L;
42     public static Object[] monitors = new Object[1000];
43     public static int monitorCount;
44 
45     public static void main(String[] args) throws Exception {
46         Thread threadDumper = new Thread(() -> dumpThreads());
47         threadDumper.start();
48         Thread monitorCreator = new Thread(() -> createMonitors());
49         monitorCreator.start();
50 
51         threadDumper.join();
52         monitorCreator.join();
53     }
54 
55     static private void dumpThreads() {
56         ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
57         int dumpCount = 0;
58         long startTime = System.nanoTime();
59         while (System.nanoTime() - startTime < TOTAL_RUN_TIME_NS) {
60             threadBean.dumpAllThreads(true, false);
61             dumpCount++;
62             try {
63                 Thread.sleep(10);
64             } catch (InterruptedException e) {}
65         }
66         System.out.println("Dumped all thread info " + dumpCount + " times");
67     }
68 
69     static private void createMonitors() {
70         int index = 0;
71         long startTime = System.nanoTime();
72         while (System.nanoTime() - startTime < TOTAL_RUN_TIME_NS) {
73             index = index++ % 1000;
74             monitors[index] = new Object();
75             synchronized (monitors[index]) {
76                 monitorCount++;
77             }
78         }
79         System.out.println("Created " + monitorCount + " monitors");
80     }
81 }