changeset 1232:1de24c774498

7901435: Use ProcessBuilders instead of Runtime.exec
author shade
date Tue, 26 May 2015 14:38:22 +0300
parents 3387560164f9
children f535278fe63a
files jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfAsmProfiler.java jmh-core/src/main/java/org/openjdk/jmh/profile/WinPerfAsmProfiler.java jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.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, 28 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfAsmProfiler.java	Tue May 26 08:49:12 2015 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfAsmProfiler.java	Tue May 26 14:38:22 2015 +0300
@@ -91,7 +91,8 @@
     @Override
     protected void parseEvents() {
         try {
-            Process p = Runtime.getRuntime().exec("perf script -f time,event,ip,sym,dso -i " + perfBinData);
+            ProcessBuilder pb = new ProcessBuilder("perf", "script", "-f", "time,event,ip,sym,dso", "-i", perfBinData);
+            Process p = pb.start();
 
             // drain streams, else we might lock up
             FileOutputStream fos = new FileOutputStream(perfParsedData);
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/WinPerfAsmProfiler.java	Tue May 26 08:49:12 2015 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/WinPerfAsmProfiler.java	Tue May 26 14:38:22 2015 +0300
@@ -27,25 +27,10 @@
 import org.openjdk.jmh.infra.BenchmarkParams;
 import org.openjdk.jmh.results.BenchmarkResult;
 import org.openjdk.jmh.results.Result;
-import org.openjdk.jmh.util.Deduplicator;
-import org.openjdk.jmh.util.FileUtils;
-import org.openjdk.jmh.util.InputStreamDrainer;
-import org.openjdk.jmh.util.Multiset;
-import org.openjdk.jmh.util.TreeMultiset;
-import org.openjdk.jmh.util.Utils;
+import org.openjdk.jmh.util.*;
 
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.FileReader;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
+import java.io.*;
+import java.util.*;
 
 /**
  * Windows performance profiler based on "xperf" utility.
@@ -190,8 +175,10 @@
 
         // 2. Convert binary data to text form.
         try {
-            Process p = Runtime.getRuntime().exec(new String[] { PATH, "-i", perfBinData, "-symbols", "-a", "dumper" },
-                new String[] { "_NT_SYMBOL_PATH=" + SYMBOL_DIR });
+            ProcessBuilder pb = new ProcessBuilder(PATH, "-i", perfBinData, "-symbols", "-a", "dumper");
+            pb.environment().put("_NT_SYMBOL_PATH=" + SYMBOL_DIR, "");
+
+            Process p = pb.start();
 
             FileOutputStream fos = new FileOutputStream(perfParsedData);
 
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Tue May 26 08:49:12 2015 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Tue May 26 14:38:22 2015 +0300
@@ -31,11 +31,7 @@
 import org.openjdk.jmh.profile.ExternalProfiler;
 import org.openjdk.jmh.profile.Profiler;
 import org.openjdk.jmh.profile.ProfilerFactory;
-import org.openjdk.jmh.results.BenchmarkResult;
-import org.openjdk.jmh.results.BenchmarkResultMetaData;
-import org.openjdk.jmh.results.IterationResult;
-import org.openjdk.jmh.results.Result;
-import org.openjdk.jmh.results.RunResult;
+import org.openjdk.jmh.results.*;
 import org.openjdk.jmh.results.format.ResultFormatFactory;
 import org.openjdk.jmh.runner.format.OutputFormat;
 import org.openjdk.jmh.runner.format.OutputFormatFactory;
@@ -43,36 +39,14 @@
 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;
-import org.openjdk.jmh.util.Multimap;
-import org.openjdk.jmh.util.TreeMultimap;
-import org.openjdk.jmh.util.UnCloseablePrintStream;
-import org.openjdk.jmh.util.Utils;
-import org.openjdk.jmh.util.Version;
+import org.openjdk.jmh.util.*;
 
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.PrintStream;
-import java.io.RandomAccessFile;
-import java.io.UnsupportedEncodingException;
+import java.io.*;
 import java.lang.management.ManagementFactory;
 import java.nio.channels.FileChannel;
 import java.nio.channels.FileLock;
 import java.nio.channels.OverlappingFileLockException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.SortedSet;
-import java.util.TreeSet;
+import java.util.*;
 import java.util.concurrent.TimeUnit;
 
 /**
@@ -600,7 +574,7 @@
             printOut = forcePrint || printOut;
             printErr = forcePrint || printErr;
 
-            String[] commandString = getSeparateExecutionCommand(params, server.getHost(), server.getPort(), javaInvokeOptions, javaOptions);
+            List<String> commandString = getSeparateExecutionCommand(params, server.getHost(), server.getPort(), javaInvokeOptions, javaOptions);
             String opts = Utils.join(params.getJvmArgs(), " ");
             if (opts.trim().isEmpty()) {
                 opts = "<none>";
@@ -615,7 +589,7 @@
             int forkCount = params.getForks();
             int warmupForkCount = params.getWarmupForks();
             if (warmupForkCount > 0) {
-                out.verbosePrintln("Warmup forking " + warmupForkCount + " times using command: " + Arrays.toString(commandString));
+                out.verbosePrintln("Warmup forking " + warmupForkCount + " times using command: " + commandString);
                 for (int i = 0; i < warmupForkCount; i++) {
                     etaBeforeBenchmark();
                     out.println("# Warmup Fork: " + (i + 1) + " of " + warmupForkCount);
@@ -630,7 +604,7 @@
                 }
             }
 
-            out.verbosePrintln("Forking " + forkCount + " times using command: " + Arrays.toString(commandString));
+            out.verbosePrintln("Forking " + forkCount + " times using command: " + commandString);
             for (int i = 0; i < forkCount; i++) {
                 etaBeforeBenchmark();
                 out.println("# Fork: " + (i + 1) + " of " + forkCount);
@@ -699,12 +673,13 @@
         return results;
     }
 
-    private List<IterationResult> doFork(BinaryLinkServer reader, String[] commandString,
+    private List<IterationResult> doFork(BinaryLinkServer reader, List<String> commandString,
                                                               File stdOut, File stdErr, boolean printOut, boolean printErr) {
         FileOutputStream fosErr = null;
         FileOutputStream fosOut = null;
         try {
-            Process p = Runtime.getRuntime().exec(commandString);
+            ProcessBuilder pb = new ProcessBuilder(commandString);
+            Process p = pb.start();
 
             fosErr = new FileOutputStream(stdErr);
             fosOut = new FileOutputStream(stdOut);
@@ -785,7 +760,7 @@
      * @param javaOptions add these options to JVM command string
      * @return the final command to execute
      */
-    String[] getSeparateExecutionCommand(BenchmarkParams benchmark, String host, int port, List<String> javaInvokeOptions, List<String> javaOptions) {
+    List<String> getSeparateExecutionCommand(BenchmarkParams benchmark, String host, int port, List<String> javaInvokeOptions, List<String> javaOptions) {
 
         List<String> command = new ArrayList<String>();
 
@@ -819,7 +794,7 @@
         command.add(host);
         command.add(String.valueOf(port));
 
-        return command.toArray(new String[command.size()]);
+        return command;
     }
 
 }
--- a/jmh-core/src/main/java/org/openjdk/jmh/util/Utils.java	Tue May 26 08:49:12 2015 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/util/Utils.java	Tue May 26 14:38:22 2015 +0300
@@ -312,7 +312,7 @@
     public static Collection<String> tryWith(String... cmd) {
         Collection<String> messages = new ArrayList<String>();
         try {
-            Process p = Runtime.getRuntime().exec(cmd);
+            Process p = new ProcessBuilder(cmd).start();
 
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
--- a/jmh-core/src/test/java/org/openjdk/jmh/runner/RunnerTest.java	Tue May 26 08:49:12 2015 +0300
+++ b/jmh-core/src/test/java/org/openjdk/jmh/runner/RunnerTest.java	Tue May 26 14:38:22 2015 +0300
@@ -63,10 +63,10 @@
                 Mode.Throughput, null, TimeUnit.SECONDS, 1,
                 Utils.getCurrentJvm(), Collections.<String>emptyList(),
                 TimeValue.days(1));
-        String[] command = blade.getSeparateExecutionCommand(bp, DUMMY_HOST, DUMMY_PORT, Collections.<String>emptyList(), Collections.<String>emptyList());
+        List<String> command = blade.getSeparateExecutionCommand(bp, DUMMY_HOST, DUMMY_PORT, Collections.<String>emptyList(), Collections.<String>emptyList());
 
         // expecting 1 compile command file
-        List<String> files = CompilerHints.getCompileCommandFiles(Arrays.asList(command));
+        List<String> files = CompilerHints.getCompileCommandFiles(command);
         assertEquals(1, files.size());
 
         // file should exist
@@ -92,10 +92,10 @@
                 Mode.Throughput, null, TimeUnit.SECONDS, 1,
                 Utils.getCurrentJvm(), Arrays.asList(CompilerHints.XX_COMPILE_COMMAND_FILE + tempHints),
                 TimeValue.days(1));
-        String[] command = blade.getSeparateExecutionCommand(bp, DUMMY_HOST, DUMMY_PORT, Collections.<String>emptyList(), Collections.<String>emptyList());
+        List<String> command = blade.getSeparateExecutionCommand(bp, DUMMY_HOST, DUMMY_PORT, Collections.<String>emptyList(), Collections.<String>emptyList());
 
         // expecting 1 compile command file
-        List<String> files = CompilerHints.getCompileCommandFiles(Arrays.asList(command));
+        List<String> files = CompilerHints.getCompileCommandFiles(command);
         assertEquals(1, files.size());
 
         // file should exist
@@ -126,10 +126,10 @@
                 Utils.getCurrentJvm(),
                 Arrays.asList(CompilerHints.XX_COMPILE_COMMAND_FILE + tempHints1, CompilerHints.XX_COMPILE_COMMAND_FILE + tempHints2),
                 TimeValue.days(1));
-        String[] command = blade.getSeparateExecutionCommand(bp, DUMMY_HOST, DUMMY_PORT, Collections.<String>emptyList(), Collections.<String>emptyList());
+        List<String> command = blade.getSeparateExecutionCommand(bp, DUMMY_HOST, DUMMY_PORT, Collections.<String>emptyList(), Collections.<String>emptyList());
 
         // expecting 1 compile command file
-        List<String> files = CompilerHints.getCompileCommandFiles(Arrays.asList(command));
+        List<String> files = CompilerHints.getCompileCommandFiles(command);
         assertEquals(1, files.size());
 
         // file should exist