changeset 878:f860b4c92d13

profilers: rework checkSupport() to make it more obvious.
author shade
date Mon, 30 Jun 2014 12:05:26 +0400
parents 10465dfbb1e9
children 3c8d4f23d112
files jmh-core/src/main/java/org/openjdk/jmh/profile/AbstractHotspotProfiler.java jmh-core/src/main/java/org/openjdk/jmh/profile/ClassloaderProfiler.java jmh-core/src/main/java/org/openjdk/jmh/profile/CompilerProfiler.java jmh-core/src/main/java/org/openjdk/jmh/profile/GCProfiler.java 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/Profiler.java jmh-core/src/main/java/org/openjdk/jmh/profile/ProfilerFactory.java jmh-core/src/main/java/org/openjdk/jmh/profile/StackProfiler.java jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java jmh-core/src/main/java/org/openjdk/jmh/runner/options/CommandLineOptions.java
diffstat 11 files changed, 48 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/AbstractHotspotProfiler.java	Sun Jun 29 23:51:17 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/AbstractHotspotProfiler.java	Mon Jun 30 12:05:26 2014 +0400
@@ -33,8 +33,8 @@
 import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
 
@@ -53,12 +53,13 @@
      * @return true, if accessible; false otherwise
      */
     @Override
-    public Collection<String> checkSupport() {
+    public boolean checkSupport(List<String> msgs) {
         try {
             Class.forName("sun.management.ManagementFactoryHelper");
-            return Collections.emptyList();
+            return true;
         } catch (ClassNotFoundException e) {
-            return Collections.singleton("Class not found: " + e.getMessage() + ", are you running HotSpot VM?");
+            msgs.add("Class not found: " + e.getMessage() + ", are you running HotSpot VM?");
+            return false;
         }
     }
 
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/ClassloaderProfiler.java	Sun Jun 29 23:51:17 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/ClassloaderProfiler.java	Mon Jun 30 12:05:26 2014 +0400
@@ -33,7 +33,7 @@
 import java.lang.management.ManagementFactory;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
+import java.util.List;
 
 public class ClassloaderProfiler implements InternalProfiler {
 
@@ -46,8 +46,8 @@
     }
 
     @Override
-    public Collection<String> checkSupport() {
-        return Collections.emptyList();
+    public boolean checkSupport(List<String> msgs) {
+        return true;
     }
 
     @Override
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/CompilerProfiler.java	Sun Jun 29 23:51:17 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/CompilerProfiler.java	Mon Jun 30 12:05:26 2014 +0400
@@ -33,7 +33,7 @@
 import java.lang.management.ManagementFactory;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
+import java.util.List;
 
 public class CompilerProfiler implements InternalProfiler {
 
@@ -50,12 +50,13 @@
     }
 
     @Override
-    public Collection<String> checkSupport() {
+    public boolean checkSupport(List<String> msgs) {
         CompilationMXBean comp = ManagementFactory.getCompilationMXBean();
         if (comp.isCompilationTimeMonitoringSupported()) {
-            return Collections.emptyList();
+            return true;
         } else {
-            return Collections.singleton("The MXBean is available, but compilation time monitoring is disabled.");
+            msgs.add("The MXBean is available, but compilation time monitoring is disabled.");
+            return false;
         }
     }
 
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/GCProfiler.java	Sun Jun 29 23:51:17 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/GCProfiler.java	Mon Jun 30 12:05:26 2014 +0400
@@ -33,7 +33,7 @@
 import java.lang.management.ManagementFactory;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
+import java.util.List;
 import java.util.concurrent.TimeUnit;
 
 public class GCProfiler implements InternalProfiler {
@@ -47,8 +47,8 @@
     }
 
     @Override
-    public Collection<String> checkSupport() {
-        return Collections.emptyList();
+    public boolean checkSupport(List<String> msgs) {
+        return true;
     }
 
     @Override
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfAsmProfiler.java	Sun Jun 29 23:51:17 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfAsmProfiler.java	Mon Jun 30 12:05:26 2014 +0400
@@ -158,8 +158,13 @@
     }
 
     @Override
-    public Collection<String> checkSupport() {
-        return IS_SUPPORTED ? Collections.<String>emptyList() : FAIL_MSGS;
+    public boolean checkSupport(List<String> msgs) {
+        if (IS_SUPPORTED) {
+            return true;
+        } else {
+            msgs.addAll(FAIL_MSGS);
+            return false;
+        }
     }
 
     static Collection<String> tryWith(String... cmd) {
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfProfiler.java	Sun Jun 29 23:51:17 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfProfiler.java	Mon Jun 30 12:05:26 2014 +0400
@@ -43,6 +43,7 @@
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.List;
 import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -99,8 +100,13 @@
     }
 
     @Override
-    public Collection<String> checkSupport() {
-        return IS_SUPPORTED ? Collections.<String>emptyList() : FAIL_MSGS;
+    public boolean checkSupport(List<String> msgs) {
+        if (IS_SUPPORTED) {
+            return true;
+        } else {
+            msgs.addAll(FAIL_MSGS);
+            return false;
+        }
     }
 
     private static Collection<String> tryWith(String... cmd) {
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/Profiler.java	Sun Jun 29 23:51:17 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/Profiler.java	Mon Jun 30 12:05:26 2014 +0400
@@ -24,7 +24,7 @@
  */
 package org.openjdk.jmh.profile;
 
-import java.util.Collection;
+import java.util.List;
 
 /**
  * Root profiler interface.
@@ -37,9 +37,10 @@
 
     /**
      * Check the support for a given profiler.
-     * @return error messages; empty collections if no errors detected, and profiler is available.
+     * @param msgs where to put the diagnostic messages
+     * @return true, if suppported
      */
-    Collection<String> checkSupport();
+    boolean checkSupport(List<String> msgs);
 
     /**
      * Unique label for the profiler.
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/ProfilerFactory.java	Sun Jun 29 23:51:17 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/ProfilerFactory.java	Mon Jun 30 12:05:26 2014 +0400
@@ -28,7 +28,6 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.List;
 
 public class ProfilerFactory {
@@ -49,14 +48,16 @@
         return profs;
     }
 
-    public static Collection<String> checkSupport(Class<? extends Profiler> klass) {
+    public static boolean checkSupport(Class<? extends Profiler> klass, List<String> msgs) {
         try {
             Profiler prof = klass.newInstance();
-            return prof.checkSupport();
+            return prof.checkSupport(msgs);
         } catch (InstantiationException e) {
-            return Collections.singleton("Unable to instantiate " + klass);
+            msgs.add("Unable to instantiate " + klass);
+            return false;
         } catch (IllegalAccessException e) {
-            return Collections.singleton("Unable to instantiate " + klass);
+            msgs.add("Unable to instantiate " + klass);
+            return false;
         }
     }
 
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/StackProfiler.java	Sun Jun 29 23:51:17 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/StackProfiler.java	Mon Jun 30 12:05:26 2014 +0400
@@ -40,7 +40,7 @@
 import java.lang.management.ThreadInfo;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
+import java.util.List;
 import java.util.concurrent.TimeUnit;
 
 /**
@@ -86,11 +86,10 @@
     }
 
     @Override
-    public Collection<String> checkSupport() {
-        return Collections.emptyList();
+    public boolean checkSupport(List<String> msg) {
+        return true;
     }
 
-
     @Override
     public String label() {
         return "stack";
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Sun Jun 29 23:51:17 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Mon Jun 30 12:05:26 2014 +0400
@@ -161,8 +161,8 @@
      */
     public Collection<RunResult> run() throws RunnerException {
         for (Class<? extends Profiler> p : options.getProfilers()) {
-            Collection<String> initMessages = ProfilerFactory.checkSupport(p);
-            if (!initMessages.isEmpty()) {
+            List<String> initMessages = new ArrayList<String>();
+            if (!ProfilerFactory.checkSupport(p, initMessages)) {
                 StringBuilder sb = new StringBuilder();
                 for (String im : initMessages) {
                     sb.append(String.format("%5s %s\n", "", im));
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/options/CommandLineOptions.java	Sun Jun 29 23:51:17 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/options/CommandLineOptions.java	Mon Jun 30 12:05:26 2014 +0400
@@ -443,8 +443,8 @@
         StringBuilder supported = new StringBuilder();
         StringBuilder unsupported = new StringBuilder();
         for (Class<? extends Profiler> s : ProfilerFactory.getAvailableProfilers()) {
-            Collection<String> initMessages = ProfilerFactory.checkSupport(s);
-            if (initMessages.isEmpty()) {
+            List<String> initMessages = new ArrayList<String>();
+            if (ProfilerFactory.checkSupport(s, initMessages)) {
                 supported.append(String.format("%20s: %s\n", ProfilerFactory.getLabel(s), ProfilerFactory.getDescription(s)));
             } else {
                 unsupported.append(String.format("%20s: %s\n", ProfilerFactory.getLabel(s), ProfilerFactory.getDescription(s)));