changeset 969:b9151815e306

runners: do not intercept System.out in a host VM at all. Do that only in forked VMs where we are the exclusive users.
author shade
date Wed, 20 Aug 2014 12:47:56 +0400
parents ed4b2a0461b2
children a2f6cce2c08a
files jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedMain.java jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedRunner.java jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkClient.java
diffstat 4 files changed, 16 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedMain.java	Wed Aug 20 12:25:51 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedMain.java	Wed Aug 20 12:47:56 2014 +0400
@@ -85,7 +85,7 @@
                             String msg = "<failure: VM prematurely exited before JMH had finished with it, " +
                                     "explicit System.exit was called?>";
                             if (link != null) {
-                                link.getOutStream().println(msg);
+                                link.getOutputFormat().println(msg);
                                 try {
                                     link.pushException(new BenchmarkException(new IllegalStateException(msg)));
                                 } catch (IOException e) {
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedRunner.java	Wed Aug 20 12:25:51 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedRunner.java	Wed Aug 20 12:47:56 2014 +0400
@@ -40,7 +40,7 @@
     private final BinaryLinkClient link;
 
     public ForkedRunner(Options options, BinaryLinkClient link) {
-        super(options, link.getOutputFormatHook());
+        super(options, link.getOutputFormat());
         this.link = link;
     }
 
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Wed Aug 20 12:25:51 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Wed Aug 20 12:47:56 2014 +0400
@@ -49,7 +49,6 @@
 import org.openjdk.jmh.util.UnCloseablePrintStream;
 import org.openjdk.jmh.util.Utils;
 
-import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
@@ -104,13 +103,10 @@
             throw new IllegalArgumentException("Options not allowed to be null.");
         }
 
-        // Intercept the System.out when redirection is requested.
-        // FIXME: We need to properly give up on stream after we are done with it
         PrintStream out;
         if (options.getOutput().hasValue()) {
             try {
                 out = new PrintStream(options.getOutput().get());
-                System.setOut(out); // override to print everything to file
             } catch (FileNotFoundException ex) {
                 throw new IllegalStateException(ex);
             }
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkClient.java	Wed Aug 20 12:25:51 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkClient.java	Wed Aug 20 12:47:56 2014 +0400
@@ -54,6 +54,7 @@
     private final ObjectInputStream ois;
     private final ForwardingPrintStream streamErr;
     private final ForwardingPrintStream streamOut;
+    private final OutputFormat outputFormat;
 
     public BinaryLinkClient(String hostName, int hostPort) throws IOException {
         this.lock = new Object();
@@ -62,6 +63,17 @@
         this.ois = new ObjectInputStream(clientSocket.getInputStream());
         this.streamErr = new ForwardingPrintStream(OutputFrame.Type.ERR);
         this.streamOut = new ForwardingPrintStream(OutputFrame.Type.OUT);
+        this.outputFormat = (OutputFormat) Proxy.newProxyInstance(
+                Thread.currentThread().getContextClassLoader(),
+                new Class[]{OutputFormat.class},
+                new InvocationHandler() {
+                    @Override
+                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+                        pushFrame(new OutputFormatFrame(ClassConventions.getMethodName(method), args));
+                        return null; // expect null
+                    }
+                }
+        );
     }
 
     private void pushFrame(Serializable frame) throws IOException {
@@ -129,18 +141,8 @@
         return streamErr;
     }
 
-    public OutputFormat getOutputFormatHook() {
-        return (OutputFormat) Proxy.newProxyInstance(
-                Thread.currentThread().getContextClassLoader(),
-                new Class[]{OutputFormat.class},
-                new InvocationHandler() {
-                    @Override
-                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
-                        pushFrame(new OutputFormatFrame(ClassConventions.getMethodName(method), args));
-                        return null; // expect null
-                    }
-                }
-        );
+    public OutputFormat getOutputFormat() {
+        return outputFormat;
     }
 
     class ForwardingPrintStream extends PrintStream {