26 * @bug 8356870
27 * @summary Test HotSpotDiagnosticMXBean.dumpThreads with a thread owning a monitor for
28 * an object that is scalar replaced
29 * @requires vm.compMode != "Xcomp"
30 * @requires (vm.opt.TieredStopAtLevel == null | vm.opt.TieredStopAtLevel == 4)
31 * @modules jdk.management
32 * @library /test/lib
33 * @run main/othervm -XX:CompileCommand=inline,java/lang/String*.* DumpThreadsWithEliminatedLock plain platform
34 * @run main/othervm -XX:CompileCommand=inline,java/lang/String*.* DumpThreadsWithEliminatedLock plain virtual
35 * @run main/othervm -XX:CompileCommand=inline,java/lang/String*.* DumpThreadsWithEliminatedLock json platform
36 * @run main/othervm -XX:CompileCommand=inline,java/lang/String*.* DumpThreadsWithEliminatedLock json virtual
37 */
38
39 import java.io.IOException;
40 import java.io.UncheckedIOException;
41 import java.lang.management.ManagementFactory;
42 import java.nio.file.Files;
43 import java.nio.file.Path;
44 import java.time.Instant;
45 import java.util.List;
46 import java.util.concurrent.ThreadFactory;
47 import java.util.concurrent.atomic.AtomicBoolean;
48 import java.util.concurrent.atomic.AtomicReference;
49 import java.util.stream.Stream;
50 import com.sun.management.HotSpotDiagnosticMXBean;
51 import jdk.test.lib.threaddump.ThreadDump;
52 import jdk.test.lib.thread.VThreadRunner;
53
54 public class DumpThreadsWithEliminatedLock {
55
56 public static void main(String[] args) throws Exception {
57 boolean plain = switch (args[0]) {
58 case "plain" -> true;
59 case "json" -> false;
60 default -> throw new RuntimeException("Unknown dump format");
61 };
62
63 ThreadFactory factory = switch (args[1]) {
64 case "platform" -> Thread.ofPlatform().factory();
65 case "virtual" -> Thread.ofVirtual().factory();
66 default -> throw new RuntimeException("Unknown thread kind");
67 };
68
69 // need at least two carriers for JTREG_TEST_THREAD_FACTORY=Virtual
70 if (Thread.currentThread().isVirtual()) {
71 VThreadRunner.ensureParallelism(2);
72 }
73
74 // A thread that spins creating and adding to a StringBuffer. StringBuffer is
75 // synchronized, assume object will be scalar replaced and the lock eliminated.
76 var done = new AtomicBoolean();
77 var ref = new AtomicReference<String>();
78 Thread thread = factory.newThread(() -> {
79 while (!done.get()) {
80 StringBuffer sb = new StringBuffer();
81 sb.append(System.currentTimeMillis());
82 String s = sb.toString();
83 ref.set(s);
84 }
85 });
86 try {
87 thread.start();
88 if (plain) {
89 testPlainFormat();
90 } else {
91 testJsonFormat(thread.threadId());
92 }
93 } finally {
94 done.set(true);
95 thread.join();
96 }
97 }
98
99 /**
100 * Invoke HotSpotDiagnosticMXBean.dumpThreads to generate a thread dump in plain text
101 * format until "lock is eliminated" is found in the output.
102 */
103 private static void testPlainFormat() {
104 try {
105 Path file = genOutputPath(".txt");
106 boolean found = false;
107 int attempts = 0;
|
26 * @bug 8356870
27 * @summary Test HotSpotDiagnosticMXBean.dumpThreads with a thread owning a monitor for
28 * an object that is scalar replaced
29 * @requires vm.compMode != "Xcomp"
30 * @requires (vm.opt.TieredStopAtLevel == null | vm.opt.TieredStopAtLevel == 4)
31 * @modules jdk.management
32 * @library /test/lib
33 * @run main/othervm -XX:CompileCommand=inline,java/lang/String*.* DumpThreadsWithEliminatedLock plain platform
34 * @run main/othervm -XX:CompileCommand=inline,java/lang/String*.* DumpThreadsWithEliminatedLock plain virtual
35 * @run main/othervm -XX:CompileCommand=inline,java/lang/String*.* DumpThreadsWithEliminatedLock json platform
36 * @run main/othervm -XX:CompileCommand=inline,java/lang/String*.* DumpThreadsWithEliminatedLock json virtual
37 */
38
39 import java.io.IOException;
40 import java.io.UncheckedIOException;
41 import java.lang.management.ManagementFactory;
42 import java.nio.file.Files;
43 import java.nio.file.Path;
44 import java.time.Instant;
45 import java.util.List;
46 import java.util.concurrent.CountDownLatch;
47 import java.util.concurrent.ThreadFactory;
48 import java.util.concurrent.atomic.AtomicBoolean;
49 import java.util.concurrent.atomic.AtomicReference;
50 import java.util.stream.Stream;
51 import com.sun.management.HotSpotDiagnosticMXBean;
52 import jdk.test.lib.threaddump.ThreadDump;
53 import jdk.test.lib.thread.VThreadRunner;
54
55 public class DumpThreadsWithEliminatedLock {
56
57 public static void main(String[] args) throws Exception {
58 boolean plain = switch (args[0]) {
59 case "plain" -> true;
60 case "json" -> false;
61 default -> throw new RuntimeException("Unknown dump format");
62 };
63
64 ThreadFactory factory = switch (args[1]) {
65 case "platform" -> Thread.ofPlatform().factory();
66 case "virtual" -> Thread.ofVirtual().factory();
67 default -> throw new RuntimeException("Unknown thread kind");
68 };
69
70 // need at least two carriers for JTREG_TEST_THREAD_FACTORY=Virtual
71 if (Thread.currentThread().isVirtual()) {
72 VThreadRunner.ensureParallelism(2);
73 }
74
75 // A thread that spins creating and adding to a StringBuffer. StringBuffer is
76 // synchronized, assume object will be scalar replaced and the lock eliminated.
77 var started = new CountDownLatch(1);
78 var done = new AtomicBoolean();
79 var ref = new AtomicReference<String>();
80 Thread thread = factory.newThread(() -> {
81 started.countDown();
82 while (!done.get()) {
83 StringBuffer sb = new StringBuffer();
84 sb.append(System.currentTimeMillis());
85 String s = sb.toString();
86 ref.set(s);
87 }
88 });
89 try {
90 thread.start();
91 started.await();
92 if (plain) {
93 testPlainFormat();
94 } else {
95 testJsonFormat(thread.threadId());
96 }
97 } finally {
98 done.set(true);
99 thread.join();
100 }
101 }
102
103 /**
104 * Invoke HotSpotDiagnosticMXBean.dumpThreads to generate a thread dump in plain text
105 * format until "lock is eliminated" is found in the output.
106 */
107 private static void testPlainFormat() {
108 try {
109 Path file = genOutputPath(".txt");
110 boolean found = false;
111 int attempts = 0;
|