|
5 | 5 |
|
6 | 6 | package test.jfr; |
7 | 7 |
|
8 | | -import java.util.Random; |
9 | | - |
10 | | -class Ttsp { |
| 8 | +import java.lang.management.ManagementFactory; |
| 9 | +import java.util.Arrays; |
| 10 | + |
| 11 | +public class Ttsp { |
| 12 | + static volatile int sink; |
| 13 | + |
| 14 | + // String.indexOf is a JVM intrinsic. When JIT-compiled, it has no safepoint check inside |
| 15 | + // and therefore may delay safepoint start. |
| 16 | + static int indexOfTest(int length) { |
| 17 | + char[] chars = new char[length * length]; |
| 18 | + Arrays.fill(chars, 'a'); |
| 19 | + String haystack = new String(chars); |
| 20 | + String needle = haystack.substring(0, length) + 'b' + haystack.substring(0, length); |
| 21 | + return haystack.indexOf(needle); |
| 22 | + } |
11 | 23 |
|
12 | | - static private byte loop() { |
13 | | - byte[] byteArray = new byte[1024 * 1024 * 1024]; |
14 | | - for (int i = 0; i < 10000; i++) { |
15 | | - new Random().nextBytes(byteArray); |
| 24 | + static void spoiler(int length, long count) { |
| 25 | + for (long i = 0; i < count; i++) { |
| 26 | + sink = indexOfTest(length); |
16 | 27 | } |
17 | | - return byteArray[0]; |
| 28 | + } |
| 29 | + |
| 30 | + static void requestSafepoint() { |
| 31 | + ManagementFactory.getThreadMXBean().dumpAllThreads(false, false); |
18 | 32 | } |
19 | 33 |
|
20 | 34 | public static void main(String[] args) throws Exception { |
21 | | - new Thread(() -> { |
22 | | - while (true) { |
23 | | - System.gc(); |
24 | | - try { |
25 | | - Thread.sleep(20); |
26 | | - } catch (InterruptedException e) { |
27 | | - throw new RuntimeException(e); |
28 | | - } |
29 | | - } |
30 | | - }).start(); |
31 | | - |
32 | | - Thread.sleep(1000); |
33 | | - |
34 | | - new Thread(() -> { |
35 | | - while (true) { |
36 | | - loop(); |
37 | | - } |
38 | | - }).start(); |
| 35 | + // Warmup with small input to force JIT-compilation of indexOfTest |
| 36 | + spoiler(10, 1000000); |
| 37 | + |
| 38 | + // Run actual workload with large input to cause long time-to-safepoint pauses |
| 39 | + new Thread(() -> spoiler(1000, Long.MAX_VALUE)).start(); |
| 40 | + |
| 41 | + while (true) { |
| 42 | + requestSafepoint(); |
| 43 | + Thread.sleep(50); |
| 44 | + } |
39 | 45 | } |
40 | 46 | } |
41 | | - |
|
0 commit comments