Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 13
Add a test for crash-recovery in the 'vm' stackwalker.#216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -4,15 +4,38 @@ | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.locks.LockSupport; | ||
| import java.util.function.Function; | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
| public abstract class AbstractProcessProfilerTest { | ||
| protected final boolean launch(String target, List<String> jvmArgs, String commands, Function<String, Boolean> onStdoutLine, Function<String, Boolean> onStderrLine) throws Exception { | ||
| public static final class LaunchResult { | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice to see we are investing in controlled behaviour tests. I think we basically have different parts
| ||
| public final boolean inTime; | ||
| public final int exitCode; | ||
| public LaunchResult(boolean inTime, int exitCode) { | ||
| this.inTime = inTime; | ||
| this.exitCode = exitCode; | ||
| } | ||
| } | ||
| public enum LineConsumerResult { | ||
| CONTINUE, | ||
| STOP, | ||
| IGNORE | ||
| } | ||
| protected final LaunchResult launch(String target, List<String> jvmArgs, String commands, Function<String, LineConsumerResult> onStdoutLine, Function<String, LineConsumerResult> onStderrLine) throws Exception { | ||
| return launch(target, jvmArgs, commands, Collections.emptyMap(), onStdoutLine, onStderrLine); | ||
| } | ||
| protected final LaunchResult launch(String target, List<String> jvmArgs, String commands, Map<String, String> env, Function<String, LineConsumerResult> onStdoutLine, Function<String, LineConsumerResult> onStderrLine) throws Exception { | ||
| String javaHome = System.getenv("JAVA_TEST_HOME"); | ||
| if (javaHome == null) { | ||
| javaHome = System.getenv("JAVA_HOME"); | ||
| @@ -34,23 +57,34 @@ protected final boolean launch(String target, List<String> jvmArgs, String comma | ||
| } | ||
| ProcessBuilder pb = new ProcessBuilder(args); | ||
| pb.environment().putAll(env); | ||
| Process p = pb.start(); | ||
| Thread stdoutReader = new Thread(() -> { | ||
| Function<String, Boolean> lineProcessor = onStdoutLine != null ? onStdoutLine : l -> true; | ||
| Function<String, LineConsumerResult> lineProcessor = onStdoutLine != null ? onStdoutLine : l -> LineConsumerResult.CONTINUE; | ||
| try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) { | ||
| String line; | ||
| while ((line = br.readLine()) != null) { | ||
| System.out.println("[out] " + line); | ||
| if (!lineProcessor.apply(line)) { | ||
| try { | ||
| p.getOutputStream().write(1); | ||
| p.getOutputStream().flush(); | ||
| } catch (IOException ignored) { | ||
| LineConsumerResult lResult = lineProcessor.apply(line); | ||
| switch (lResult) { | ||
| case STOP: { | ||
| try { | ||
| p.getOutputStream().write(1); | ||
| p.getOutputStream().flush(); | ||
| } catch (IOException ignored) { | ||
| } | ||
| break; | ||
| } | ||
| case CONTINUE: { | ||
| if (line.contains("[ready]")) { | ||
| p.getOutputStream().write(1); | ||
| p.getOutputStream().flush(); | ||
| } | ||
| break; | ||
| } | ||
| } else { | ||
| if (line.contains("[ready]")) { | ||
| p.getOutputStream().write(1); | ||
| p.getOutputStream().flush(); | ||
| case IGNORE: { | ||
| // ignore | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| @@ -60,16 +94,27 @@ protected final boolean launch(String target, List<String> jvmArgs, String comma | ||
| } | ||
| }, "stdout-reader"); | ||
| Thread stderrReader = new Thread(() -> { | ||
| Function<String, Boolean> lineProcessor = onStderrLine != null ? onStderrLine : l -> true; | ||
| Function<String, LineConsumerResult> lineProcessor = onStderrLine != null ? onStderrLine : l -> LineConsumerResult.CONTINUE; | ||
| try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()))) { | ||
| String line; | ||
| while ((line = br.readLine()) != null) { | ||
| System.out.println("[err] " + line); | ||
| if (!lineProcessor.apply(line)) { | ||
| try { | ||
| p.getOutputStream().write(1); | ||
| p.getOutputStream().flush(); | ||
| } catch (IOException ignored) { | ||
| LineConsumerResult lResult = lineProcessor.apply(line); | ||
| switch (lResult) { | ||
| case STOP: { | ||
| try { | ||
| p.getOutputStream().write(1); | ||
| p.getOutputStream().flush(); | ||
| } catch (IOException ignored) { | ||
| } | ||
| break; | ||
| } | ||
| case CONTINUE: { | ||
| break; | ||
| } | ||
| case IGNORE: { | ||
| // ignore | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| @@ -89,6 +134,6 @@ protected final boolean launch(String target, List<String> jvmArgs, String comma | ||
| if (!val) { | ||
| p.destroyForcibly(); | ||
| } | ||
| return val; | ||
| return new LaunchResult(val, p.exitValue()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,25 @@ | ||
| 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(); | ||
| @@ -16,6 +34,35 @@ public static void main(String[] args) throws Exception { | ||
| 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(); | ||
jbachorik marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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]"); | ||
| @@ -24,5 +71,9 @@ public static void main(String[] args) throws Exception { | ||
| } | ||
| // wait for signal to exit | ||
| System.in.read(); | ||
| if (worker != null) { | ||
| worker.interrupt(); | ||
| worker.join(); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
simple but efficient 👍