changeset 926:f60971abb85c

profilers: perf* profilers should force --log-fd, enables old perfs.
author shade
date Tue, 22 Jul 2014 22:47:23 +0400
parents 3e748e623088
children 1a1afdd2d036
files 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/profile/LinuxPerfUtil.java
diffstat 3 files changed, 84 insertions(+), 93 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfAsmProfiler.java	Tue Jul 22 14:50:36 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfAsmProfiler.java	Tue Jul 22 22:47:23 2014 +0400
@@ -39,7 +39,6 @@
 import org.openjdk.jmh.util.Utils;
 
 import java.io.BufferedReader;
-import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -47,7 +46,6 @@
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
-import java.io.StreamTokenizer;
 import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -62,14 +60,11 @@
 import java.util.Set;
 import java.util.SortedMap;
 import java.util.SortedSet;
-import java.util.StringTokenizer;
 import java.util.TreeMap;
 import java.util.TreeSet;
 import java.util.concurrent.TimeUnit;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
-public class LinuxPerfAsmProfiler implements ExternalProfiler {
+public class LinuxPerfAsmProfiler extends LinuxPerfUtil implements ExternalProfiler {
 
     /**
      * Events to gather *
@@ -146,14 +141,6 @@
      */
     private static final String SAVE_LOG_OUTPUT_TO_FILE = System.getProperty("jmh.perfasm.saveLogToFile");
 
-    private static final boolean IS_SUPPORTED;
-    private static final Collection<String> FAIL_MSGS;
-
-    static {
-        FAIL_MSGS = tryWith("perf", "stat", "echo", "1");
-        IS_SUPPORTED = FAIL_MSGS.isEmpty();
-    }
-
     private String hsLog;
     private String perfBinData;
     private String perfParsedData;
@@ -166,7 +153,7 @@
 
     @Override
     public Collection<String> addJVMInvokeOptions(BenchmarkParams params) {
-        return Arrays.asList("perf", "record", "-F " + SAMPLE_FREQUENCY, "-e " + Utils.join(EVENTS, ","), "-o" + perfBinData);
+        return Arrays.asList("perf", "record", "-F" + SAMPLE_FREQUENCY, "-e" + Utils.join(EVENTS, ","), "-o" + perfBinData);
     }
 
     @Override
@@ -217,36 +204,6 @@
         }
     }
 
-    static Collection<String> tryWith(String... cmd) {
-        Collection<String> messages = new ArrayList<String>();
-        try {
-            Process p = Runtime.getRuntime().exec(cmd);
-
-            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();
-
-            if (err > 0) {
-                messages.add(baos.toString());
-            }
-        } catch (IOException ex) {
-            return Collections.singleton(ex.getMessage());
-        } catch (InterruptedException ex) {
-            throw new IllegalStateException(ex);
-        }
-        return messages;
-    }
-
     @Override
     public String label() {
         return "perfasm";
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfProfiler.java	Tue Jul 22 14:50:36 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfProfiler.java	Tue Jul 22 22:47:23 2014 +0400
@@ -29,17 +29,14 @@
 import org.openjdk.jmh.results.Aggregator;
 import org.openjdk.jmh.results.Result;
 import org.openjdk.jmh.results.ResultRole;
-import org.openjdk.jmh.util.InputStreamDrainer;
 
 import java.io.BufferedReader;
-import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
 import java.io.StringWriter;
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
@@ -48,23 +45,11 @@
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-public class LinuxPerfProfiler implements ExternalProfiler {
+public class LinuxPerfProfiler extends LinuxPerfUtil implements ExternalProfiler {
 
     /** Delay collection for given time; -1 to detect automatically */
     private static final int DELAY_MSEC = Integer.getInteger("jmh.perf.delayMs", -1);
 
-    private static final boolean IS_SUPPORTED;
-    private static final boolean IS_DELAYED;
-    private static final Collection<String> FAIL_MSGS;
-
-    static {
-        FAIL_MSGS = tryWith("perf", "stat", "echo", "1");
-        IS_SUPPORTED = FAIL_MSGS.isEmpty();
-
-        Collection<String> delay = tryWith("perf", "stat", "-D 1", "echo", "1");
-        IS_DELAYED = delay.isEmpty();
-    }
-
     @Override
     public Collection<String> addJVMInvokeOptions(BenchmarkParams params) {
         long delay;
@@ -77,9 +62,9 @@
         }
 
         if (IS_DELAYED) {
-            return Arrays.asList("perf", "stat", "-d", "-d", "-d", "-D " + delay);
+            return Arrays.asList("perf", "stat", "--log-fd", "2", "-d", "-d", "-d", "-D", String.valueOf(delay));
         } else {
-            return Arrays.asList("perf", "stat", "-d", "-d", "-d");
+            return Arrays.asList("perf", "stat", "--log-fd", "2", "-d", "-d", "-d");
         }
     }
 
@@ -119,36 +104,6 @@
         }
     }
 
-    private static Collection<String> tryWith(String... cmd) {
-        Collection<String> messages = new ArrayList<String>();
-        try {
-            Process p = Runtime.getRuntime().exec(cmd);
-
-            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();
-
-            if (err > 0) {
-                messages.add(baos.toString());
-            }
-        } catch (IOException ex) {
-            return Collections.singleton(ex.getMessage());
-        } catch (InterruptedException ex) {
-            throw new IllegalStateException(ex);
-        }
-        return messages;
-    }
-
     @Override
     public String label() {
         return "perf";
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfUtil.java	Tue Jul 22 22:47:23 2014 +0400
@@ -0,0 +1,79 @@
+/*
+ * 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.profile;
+
+import org.openjdk.jmh.util.InputStreamDrainer;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+
+public class LinuxPerfUtil {
+
+    public static final boolean IS_SUPPORTED;
+    public static final boolean IS_DELAYED;
+    public static final Collection<String> FAIL_MSGS;
+
+    static {
+        FAIL_MSGS = tryWith("perf", "stat", "--log-fd", "2", "echo", "1");
+        IS_SUPPORTED = FAIL_MSGS.isEmpty();
+
+        Collection<String> delay = tryWith("perf", "stat", "--log-fd", "2", "-D", "1", "echo", "1");
+        IS_DELAYED = delay.isEmpty();
+    }
+
+    private static Collection<String> tryWith(String... cmd) {
+        Collection<String> messages = new ArrayList<String>();
+        try {
+            Process p = Runtime.getRuntime().exec(cmd);
+
+            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();
+
+            if (err > 0) {
+                messages.add(baos.toString());
+            }
+        } catch (IOException ex) {
+            return Collections.singleton(ex.getMessage());
+        } catch (InterruptedException ex) {
+            throw new IllegalStateException(ex);
+        }
+        return messages;
+    }
+
+}