changeset 1041:9c2f09aec48d

7901111: Binary link failures in forked VM are shunned
author shade
date Thu, 20 Nov 2014 17:19:54 +0300
parents c60c03ff0993
children ce0549e576c1
files jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedMain.java jmh-core/src/main/java/org/openjdk/jmh/runner/OutputFormatAdapter.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, 108 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedMain.java	Mon Nov 17 17:14:41 2014 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedMain.java	Thu Nov 20 17:19:54 2014 +0300
@@ -35,17 +35,17 @@
 class ForkedMain {
 
     private static volatile boolean gracefullyFinished;
+    private static volatile Exception exception;
 
     /**
      * Application main entry point
      *
      * @param argv Command line arguments
      */
-    public static void main(String[] argv) {
-        if (argv.length == 0) {
-            throw new IllegalArgumentException("Empty arguments for forked VM");
+    public static void main(String[] argv) throws Exception {
+        if (argv.length != 2) {
+            throw new IllegalArgumentException("Expected two arguments for forked VM");
         } else {
-            BinaryLinkClient link = null;
             try {
                 // This assumes the exact order of arguments:
                 //   1) host name to back-connect
@@ -54,7 +54,7 @@
                 int hostPort = Integer.valueOf(argv[1]);
 
                 // establish the link to host VM and pull the options
-                link = new BinaryLinkClient(hostName, hostPort);
+                BinaryLinkClient link = new BinaryLinkClient(hostName, hostPort);
                 addShutdownHook(link);
 
                 Options options = link.requestOptions();
@@ -68,10 +68,9 @@
                 runner.run();
 
                 gracefullyFinished = true;
-            } catch (IOException ex) {
-                throw new IllegalArgumentException(ex.getMessage());
-            } catch (ClassNotFoundException ex) {
-                throw new IllegalArgumentException(ex.getMessage());
+            } catch (Exception ex) {
+                exception = ex;
+                System.exit(1);
             }
         }
     }
@@ -82,19 +81,26 @@
                     @Override
                     public void run() {
                         if (!gracefullyFinished) {
-                            String msg = "<failure: VM prematurely exited before JMH had finished with it, " +
-                                    "explicit System.exit was called?>";
+                            Exception ex = exception;
+                            if (ex == null) {
+                                ex = new IllegalStateException(
+                                        "<failure: VM prematurely exited before JMH had finished with it, " +
+                                        "explicit System.exit was called?>");
+                            }
+
+                            String msg = ex.getMessage();
+
                             if (link != null) {
-                                link.getOutputFormat().println(msg);
                                 try {
-                                    link.pushException(new BenchmarkException(new IllegalStateException(msg)));
-                                } catch (IOException e) {
+                                    link.getOutputFormat().println(msg);
+                                    link.pushException(new BenchmarkException(ex));
+                                } catch (Exception e) {
                                     // last resort
-                                    System.err.println(msg);
+                                    ex.printStackTrace(System.err);
                                 }
                             } else {
                                 // last resort
-                                System.err.println(msg);
+                                ex.printStackTrace(System.err);
                             }
                         }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/OutputFormatAdapter.java	Thu Nov 20 17:19:54 2014 +0300
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.openjdk.jmh.runner;
+
+import org.openjdk.jmh.runner.format.OutputFormat;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+class OutputFormatAdapter extends OutputStream {
+    private final OutputFormat out;
+
+    public OutputFormatAdapter(OutputFormat out) {
+        this.out = out;
+    }
+
+    @Override
+    public void write(int b) throws IOException {
+        out.write(b);
+    }
+
+    @Override
+    public void write(byte[] b) throws IOException {
+        out.write(b);
+    }
+}
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Mon Nov 17 17:14:41 2014 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Thu Nov 20 17:19:54 2014 +0300
@@ -695,11 +695,11 @@
             InputStreamDrainer outDrainer = new InputStreamDrainer(p.getInputStream(), fosOut);
 
             if (printErr) {
-                errDrainer.addOutputStream(System.err);
+                errDrainer.addOutputStream(new OutputFormatAdapter(out));
             }
 
             if (printOut) {
-                outDrainer.addOutputStream(System.out);
+                outDrainer.addOutputStream(new OutputFormatAdapter(out));
             }
 
             errDrainer.start();
@@ -728,8 +728,17 @@
                 out.println("</stderr>");
 
                 out.println("");
+            }
 
-                throw new BenchmarkException(new IllegalStateException("Forked VM failed with exit code " + ecode));
+            BenchmarkException exception = reader.getException();
+            if (exception == null) {
+                if (ecode == 0) {
+                    return reader.getResults();
+                } else {
+                    throw new BenchmarkException(new IllegalStateException("Forked VM failed with exit code " + ecode));
+                }
+            } else {
+                throw exception;
             }
 
         } catch (IOException ex) {
@@ -745,12 +754,6 @@
             FileUtils.safelyClose(fosOut);
         }
 
-        BenchmarkException exception = reader.getException();
-        if (exception == null) {
-            return reader.getResults();
-        } else {
-            throw exception;
-        }
     }
 
     /**
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkClient.java	Mon Nov 17 17:14:41 2014 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkClient.java	Thu Nov 20 17:19:54 2014 +0300
@@ -55,6 +55,7 @@
     private final ForwardingPrintStream streamErr;
     private final ForwardingPrintStream streamOut;
     private final OutputFormat outputFormat;
+    private volatile boolean failed;
 
     public BinaryLinkClient(String hostName, int hostPort) throws IOException {
         this.lock = new Object();
@@ -77,9 +78,30 @@
     }
 
     private void pushFrame(Serializable frame) throws IOException {
+        if (failed) {
+            throw new IOException("Link had failed already");
+        }
+
         synchronized (lock) {
-            oos.writeObject(frame);
-            oos.flush();
+            try {
+                oos.writeObject(frame);
+                oos.flush();
+            } catch (IOException e) {
+                failed = true;
+                throw e;
+            }
+        }
+    }
+
+    private Object readFrame() throws IOException, ClassNotFoundException {
+        try {
+            return ois.readObject();
+        } catch (ClassNotFoundException ex) {
+            failed = true;
+            throw ex;
+        } catch (IOException ex) {
+            failed = true;
+            throw ex;
         }
     }
 
@@ -103,7 +125,7 @@
         synchronized (lock) {
             pushFrame(new InfraFrame(InfraFrame.Type.OPTIONS_REQUEST));
 
-            Object reply = ois.readObject();
+            Object reply = readFrame();
             if (reply instanceof OptionsFrame) {
                 return (((OptionsFrame) reply).getOpts());
             } else {
@@ -116,7 +138,7 @@
         synchronized (lock) {
             pushFrame(new InfraFrame(InfraFrame.Type.ACTION_PLAN_REQUEST));
 
-            Object reply = ois.readObject();
+            Object reply = readFrame();
             if (reply instanceof ActionPlanFrame) {
                 return ((ActionPlanFrame) reply).getActionPlan();
             } else {