changeset 886:e1d00e02caa8

runners: print out stdout/stderr streams to console, if no profilers are claiming it.
author shade
date Tue, 01 Jul 2014 20:41:46 +0400
parents f2397b69946d
children edf1134d97f3
files jmh-core/src/main/java/org/openjdk/jmh/profile/ExternalProfiler.java jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfAsmProfiler.java jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfProfiler.java jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java
diffstat 4 files changed, 65 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/ExternalProfiler.java	Tue Jul 01 20:09:46 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/ExternalProfiler.java	Tue Jul 01 20:41:46 2014 +0400
@@ -74,4 +74,21 @@
      */
     Collection<? extends Result> afterTrial(BenchmarkParams benchmarkParams, File stdOut, File stdErr);
 
+    /**
+     * If target VM communicates with profiler with standard output, this method
+     * can be used to shun the output to console. Profiler is responsible for consuming
+     * the standard output and printing the relevant data from there.
+     *
+     * @return returns true, if profiler allows harness to print out the standard output
+     */
+    boolean allowPrintOut();
+
+    /**
+     * If target VM communicates with profiler with standard error, this method
+     * can be used to shun the output to console. Profiler is responsible for consuming
+     * the standard error and printing the relevant data from there.
+     *
+     * @return returns true, if profiler allows harness to print out the standard errpr
+     */
+    boolean allowPrintErr();
 }
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfAsmProfiler.java	Tue Jul 01 20:09:46 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfAsmProfiler.java	Tue Jul 01 20:41:46 2014 +0400
@@ -164,6 +164,16 @@
     }
 
     @Override
+    public boolean allowPrintOut() {
+        return false;
+    }
+
+    @Override
+    public boolean allowPrintErr() {
+        return false;
+    }
+
+    @Override
     public boolean checkSupport(List<String> msgs) {
         if (IS_SUPPORTED) {
             return true;
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfProfiler.java	Tue Jul 01 20:09:46 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfProfiler.java	Tue Jul 01 20:41:46 2014 +0400
@@ -100,6 +100,16 @@
     }
 
     @Override
+    public boolean allowPrintOut() {
+        return true;
+    }
+
+    @Override
+    public boolean allowPrintErr() {
+        return false;
+    }
+
+    @Override
     public boolean checkSupport(List<String> msgs) {
         if (IS_SUPPORTED) {
             return true;
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Tue Jul 01 20:09:46 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Tue Jul 01 20:41:46 2014 +0400
@@ -42,6 +42,7 @@
 import org.openjdk.jmh.runner.link.BinaryLinkServer;
 import org.openjdk.jmh.runner.options.Options;
 import org.openjdk.jmh.runner.options.TimeValue;
+import org.openjdk.jmh.runner.options.VerboseMode;
 import org.openjdk.jmh.util.FileUtils;
 import org.openjdk.jmh.util.HashMultimap;
 import org.openjdk.jmh.util.InputStreamDrainer;
@@ -487,6 +488,8 @@
 
             BenchmarkParams params = actionPlan.getMeasurementActions().get(0).getParams();
 
+            boolean printOut = true;
+            boolean printErr = true;
             List<ExternalProfiler> profilers = new ArrayList<ExternalProfiler>();
 
             List<String> javaInvokeOptions = new ArrayList<String>();
@@ -497,8 +500,14 @@
                 profilers.add(prof);
                 javaInvokeOptions.addAll(prof.addJVMInvokeOptions(params));
                 javaOptions.addAll(prof.addJVMOptions(params));
+                printOut &= prof.allowPrintOut();
+                printErr &= prof.allowPrintErr();
             }
 
+            boolean forcePrint = options.verbosity().orElse(Defaults.VERBOSITY).equalsOrHigherThan(VerboseMode.EXTRA);
+            printOut = forcePrint || printOut;
+            printErr = forcePrint || printErr;
+
             String[] commandString = getSeparateExecutionCommand(params, server.getHost(), server.getPort(), javaInvokeOptions, javaOptions);
             String opts = Utils.join(getJvmArgs(params), " ");
             if (opts.trim().isEmpty()) {
@@ -521,7 +530,7 @@
                     File stdErr = FileUtils.tempFile("stderr");
                     File stdOut = FileUtils.tempFile("stdout");
 
-                    doFork(server, commandString, stdOut, stdErr);
+                    doFork(server, commandString, stdOut, stdErr, printOut, printErr);
 
                     out.endBenchmark(null);
                     afterBenchmark(params);
@@ -547,9 +556,16 @@
                         profiler.beforeTrial(params);
                     }
                     out.println("");
+
+                    if (!printOut) {
+                        out.println("# Profilers consume stdout from target VM, use -v " + VerboseMode.EXTRA + " to copy to console");
+                    }
+                    if (!printErr) {
+                        out.println("# Profilers consume stderr from target VM, use -v " + VerboseMode.EXTRA + " to copy to console");
+                    }
                 }
 
-                Multimap<BenchmarkParams, BenchmarkResult> result = doFork(server, commandString, stdOut, stdErr);
+                Multimap<BenchmarkParams, BenchmarkResult> result = doFork(server, commandString, stdOut, stdErr, printOut, printErr);
 
                 if (!profilers.isEmpty()) {
                     out.print("# Processing profiler results: ");
@@ -587,7 +603,8 @@
         return results;
     }
 
-    private Multimap<BenchmarkParams, BenchmarkResult> doFork(BinaryLinkServer reader, String[] commandString, File stdOut, File stdErr) {
+    private Multimap<BenchmarkParams, BenchmarkResult> doFork(BinaryLinkServer reader, String[] commandString,
+                                                              File stdOut, File stdErr, boolean printOut, boolean printErr) {
         try {
             Process p = Runtime.getRuntime().exec(commandString);
 
@@ -595,6 +612,14 @@
             InputStreamDrainer errDrainer = new InputStreamDrainer(p.getErrorStream(), new FileOutputStream(stdErr));
             InputStreamDrainer outDrainer = new InputStreamDrainer(p.getInputStream(), new FileOutputStream(stdOut));
 
+            if (printErr) {
+                errDrainer.addOutputStream(System.err);
+            }
+
+            if (printOut) {
+                outDrainer.addOutputStream(System.out);
+            }
+
             errDrainer.start();
             outDrainer.start();