-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathExternalLauncher.java
More file actions
79 lines (77 loc) · 3.38 KB
/
ExternalLauncher.java
File metadata and controls
79 lines (77 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.datadoghq.profiler;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.util.Random;
import java.util.concurrent.atomic.LongAdder;
/**
* External launcher for the profiler under test.
* <p>
* This class is used to launch the profiler in a separate process for testing purposes.
* </p>
* The main method takes the following arguments:
* <ul>
* <li>library - loads the profiler library</li>
* <li>profiler [comma delimited profiler command list] - starts the profiler</li>
* <li>profiler-work:<expectedCpuTime> [comma delimited profiler command list] - starts the profiler and runs a CPU-intensive task</li>
* </ul>
*/
public class ExternalLauncher {
public static void main(String[] args) throws Exception {
Thread worker = null;
try {
if (args.length < 1) {
throw new RuntimeException();
}
if (args[0].equals("library")) {
JVMAccess.getInstance();
} else if (args[0].equals("profiler")) {
JavaProfiler instance = JavaProfiler.getInstance();
if (args.length == 2) {
String commands = args[1];
if (!commands.isEmpty()) {
instance.execute(commands);
}
}
} else if (args[0].startsWith("profiler-work:")) {
long expectedCpuTime = Long.parseLong(args[0].substring("profiler-work:".length()));
ThreadMXBean thrdBean = ManagementFactory.getThreadMXBean();
JavaProfiler instance = JavaProfiler.getInstance();
if (args.length == 2) {
String commands = args[1];
if (!commands.isEmpty()) {
instance.execute(commands);
worker = new Thread(() -> {
Random rnd = new Random();
LongAdder adder = new LongAdder();
long counter = 0;
long cpuTime = thrdBean.getThreadCpuTime(Thread.currentThread().getId());
while (!Thread.currentThread().isInterrupted()) {
adder.add(rnd.nextLong());
// make sure we caused some CPU load and print the progress
if (++counter % 1000000 == 0) {
if (thrdBean.getThreadCpuTime(Thread.currentThread().getId()) - cpuTime > expectedCpuTime * 1_000_000L) {
cpuTime = thrdBean.getThreadCpuTime(Thread.currentThread().getId());
System.out.println("[working]");
System.out.flush();
}
}
}
System.out.println("[async] " + adder.sum());
});
worker.start();
}
}
}
} finally {
System.out.println("[ready]");
System.out.flush();
System.err.flush();
}
// wait for signal to exit
System.in.read();
if (worker != null) {
worker.interrupt();
worker.join();
}
}
}