changeset 1254:136a27b73ec5

7901463: Print VM version in "VM invokers/command" information block
author shade
date Mon, 13 Jul 2015 23:36:49 +0300
parents e801861b0f09
children 17c1ab8b1528
files jmh-core/src/main/java/org/openjdk/jmh/runner/BaseRunner.java jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java jmh-core/src/main/java/org/openjdk/jmh/runner/VersionMain.java jmh-core/src/main/java/org/openjdk/jmh/util/Utils.java jmh-core/src/test/java/org/openjdk/jmh/runner/RunnerTest.java
diffstat 5 files changed, 122 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/BaseRunner.java	Wed Jul 08 00:30:15 2015 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/BaseRunner.java	Mon Jul 13 23:36:49 2015 +0300
@@ -94,8 +94,8 @@
             }
 
             Version.printVersion(out);
-            out.println("# VM invoker: " + params.getJvm());
-            out.println("# VM invoker: " + params.getJvm());
+            out.println("# VM version: " + Utils.getCurrentJvmVersion());
+            out.println("# VM invoker: " + Utils.getCurrentJvm());
             out.println("# VM options: " + realOpts + (opts.equals(realOpts) ? "" : " *** WARNING: some JVM options are ignored in non-forked runs ***"));
 
             out.startBenchmark(params);
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Wed Jul 08 00:30:15 2015 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Mon Jul 13 23:36:49 2015 +0300
@@ -574,13 +574,17 @@
             printOut = forcePrint || printOut;
             printErr = forcePrint || printErr;
 
-            List<String> commandString = getSeparateExecutionCommand(params, server.getHost(), server.getPort(), javaInvokeOptions, javaOptions);
+            List<String> commandString = getSeparateExecutionCommand(params, javaInvokeOptions, javaOptions);
+            List<String> forkedString  = getForkedMainCommand(commandString, server.getHost(), server.getPort());
+            List<String> versionString = getVersionMainCommand(commandString);
+
             String opts = Utils.join(params.getJvmArgs(), " ");
             if (opts.trim().isEmpty()) {
                 opts = "<none>";
             }
 
             Version.printVersion(out);
+            out.print("# VM version: " + Utils.join(Utils.runWith(versionString), "\n"));
             out.println("# VM invoker: " + params.getJvm());
             out.println("# VM options: " + opts);
             out.startBenchmark(params);
@@ -589,7 +593,7 @@
             int forkCount = params.getForks();
             int warmupForkCount = params.getWarmupForks();
             if (warmupForkCount > 0) {
-                out.verbosePrintln("Warmup forking " + warmupForkCount + " times using command: " + commandString);
+                out.verbosePrintln("Warmup forking " + warmupForkCount + " times using command: " + forkedString);
                 for (int i = 0; i < warmupForkCount; i++) {
                     etaBeforeBenchmark();
                     out.println("# Warmup Fork: " + (i + 1) + " of " + warmupForkCount);
@@ -597,14 +601,14 @@
                     File stdErr = FileUtils.tempFile("stderr");
                     File stdOut = FileUtils.tempFile("stdout");
 
-                    doFork(server, commandString, stdOut, stdErr, printOut, printErr);
+                    doFork(server, forkedString, stdOut, stdErr, printOut, printErr);
 
                     etaAfterBenchmark(params);
                     out.println("");
                 }
             }
 
-            out.verbosePrintln("Forking " + forkCount + " times using command: " + commandString);
+            out.verbosePrintln("Forking " + forkCount + " times using command: " + forkedString);
             for (int i = 0; i < forkCount; i++) {
                 etaBeforeBenchmark();
                 out.println("# Fork: " + (i + 1) + " of " + forkCount);
@@ -628,7 +632,7 @@
                     }
                 }
 
-                List<IterationResult> result = doFork(server, commandString, stdOut, stdErr, printOut, printErr);
+                List<IterationResult> result = doFork(server, forkedString, stdOut, stdErr, printOut, printErr);
                 if (!result.isEmpty()) {
                     long pid = server.getClientPid();
 
@@ -747,20 +751,17 @@
             FileUtils.safelyClose(fosErr);
             FileUtils.safelyClose(fosOut);
         }
-
     }
 
     /**
      * Helper method for assembling the command to execute the forked JVM with
      *
      * @param benchmark benchmark to execute
-     * @param host host VM host
-     * @param port host VM port
      * @param javaInvokeOptions prepend these commands before JVM invocation
      * @param javaOptions add these options to JVM command string
      * @return the final command to execute
      */
-    List<String> getSeparateExecutionCommand(BenchmarkParams benchmark, String host, int port, List<String> javaInvokeOptions, List<String> javaOptions) {
+    List<String> getSeparateExecutionCommand(BenchmarkParams benchmark, List<String> javaInvokeOptions, List<String> javaOptions) {
 
         List<String> command = new ArrayList<String>();
 
@@ -786,6 +787,19 @@
         } else {
             command.add(System.getProperty("java.class.path"));
         }
+
+        return command;
+    }
+
+    /**
+     * @param baseLine base options
+     * @param host host VM host
+     * @param port host VM port
+     * @return
+     */
+    List<String> getForkedMainCommand(List<String> baseLine, String host, int port) {
+        List<String> command = new ArrayList<String>(baseLine);
+
         command.add(ForkedMain.class.getName());
 
         // Forked VM assumes the exact order of arguments:
@@ -797,4 +811,14 @@
         return command;
     }
 
+    /**
+     * @param baseLine base options
+     * @return
+     */
+    List<String> getVersionMainCommand(List<String> baseLine) {
+        List<String> command = new ArrayList<String>(baseLine);
+        command.add(VersionMain.class.getName());
+        return command;
+    }
+
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/VersionMain.java	Mon Jul 13 23:36:49 2015 +0300
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2005, 2015, 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.util.Utils;
+
+/**
+ * Main program entry point detecting the VM version.
+ */
+class VersionMain {
+
+    /**
+     * @param argv Command line arguments
+     */
+    public static void main(String[] argv) throws Exception {
+        System.err.println(Utils.getCurrentJvmVersion());
+    }
+
+}
--- a/jmh-core/src/main/java/org/openjdk/jmh/util/Utils.java	Wed Jul 08 00:30:15 2015 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/util/Utils.java	Mon Jul 13 23:36:49 2015 +0300
@@ -284,6 +284,17 @@
                 (isWindows() ? ".exe" : "");
     }
 
+    public static String getCurrentJvmVersion() {
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        pw.print(System.getProperty("java.vm.name"));
+        pw.print(" (");
+        pw.print(System.getProperty("java.vm.version"));
+        pw.print("), JDK ");
+        pw.print(System.getProperty("java.version"));
+        return sw.toString();
+    }
+
     /**
      * Gets PID of the current JVM.
      *
@@ -338,4 +349,33 @@
         }
         return messages;
     }
+
+    public static Collection<String> runWith(List<String> cmd) {
+        Collection<String> messages = new ArrayList<String>();
+        try {
+            Process p = new ProcessBuilder(cmd).start();
+
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+            // drain streams, else we might lock up
+            InputStreamDrainer errDrainer = new InputStreamDrainer(p.getErrorStream(), baos);
+            InputStreamDrainer outDrainer = new InputStreamDrainer(p.getInputStream(), baos);
+
+            errDrainer.start();
+            outDrainer.start();
+
+            int err = p.waitFor();
+
+            errDrainer.join();
+            outDrainer.join();
+
+            messages.add(baos.toString());
+        } catch (IOException ex) {
+            return Collections.singleton(ex.getMessage());
+        } catch (InterruptedException ex) {
+            throw new IllegalStateException(ex);
+        }
+        return messages;
+    }
+
 }
--- a/jmh-core/src/test/java/org/openjdk/jmh/runner/RunnerTest.java	Wed Jul 08 00:30:15 2015 +0300
+++ b/jmh-core/src/test/java/org/openjdk/jmh/runner/RunnerTest.java	Mon Jul 13 23:36:49 2015 +0300
@@ -63,7 +63,8 @@
                 Mode.Throughput, null, TimeUnit.SECONDS, 1,
                 Utils.getCurrentJvm(), Collections.<String>emptyList(),
                 TimeValue.days(1));
-        List<String> command = blade.getSeparateExecutionCommand(bp, DUMMY_HOST, DUMMY_PORT, Collections.<String>emptyList(), Collections.<String>emptyList());
+        List<String> command = blade.getSeparateExecutionCommand(bp, Collections.<String>emptyList(), Collections.<String>emptyList());
+        command = blade.getForkedMainCommand(command, DUMMY_HOST, DUMMY_PORT);
 
         // expecting 1 compile command file
         List<String> files = CompilerHints.getCompileCommandFiles(command);
@@ -92,7 +93,8 @@
                 Mode.Throughput, null, TimeUnit.SECONDS, 1,
                 Utils.getCurrentJvm(), Arrays.asList(CompilerHints.XX_COMPILE_COMMAND_FILE + tempHints),
                 TimeValue.days(1));
-        List<String> command = blade.getSeparateExecutionCommand(bp, DUMMY_HOST, DUMMY_PORT, Collections.<String>emptyList(), Collections.<String>emptyList());
+        List<String> command = blade.getSeparateExecutionCommand(bp, Collections.<String>emptyList(), Collections.<String>emptyList());
+        command = blade.getForkedMainCommand(command, DUMMY_HOST, DUMMY_PORT);
 
         // expecting 1 compile command file
         List<String> files = CompilerHints.getCompileCommandFiles(command);
@@ -126,7 +128,8 @@
                 Utils.getCurrentJvm(),
                 Arrays.asList(CompilerHints.XX_COMPILE_COMMAND_FILE + tempHints1, CompilerHints.XX_COMPILE_COMMAND_FILE + tempHints2),
                 TimeValue.days(1));
-        List<String> command = blade.getSeparateExecutionCommand(bp, DUMMY_HOST, DUMMY_PORT, Collections.<String>emptyList(), Collections.<String>emptyList());
+        List<String> command = blade.getSeparateExecutionCommand(bp, Collections.<String>emptyList(), Collections.<String>emptyList());
+        command = blade.getForkedMainCommand(command, DUMMY_HOST, DUMMY_PORT);
 
         // expecting 1 compile command file
         List<String> files = CompilerHints.getCompileCommandFiles(command);