changeset 952:a0b4a12cdc55

runners: profilers are pluggable through SPI. Contributed-by: Aggelos Biboudis <biboudis@gmail.com>
author shade
date Fri, 15 Aug 2014 17:36:26 +0400
parents 858587557d96
children 6e702a334f2e
files jmh-core/src/main/java/org/openjdk/jmh/profile/ProfilerFactory.java jmh-core/src/main/java/org/openjdk/jmh/runner/options/CommandLineOptions.java
diffstat 2 files changed, 25 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/ProfilerFactory.java	Fri Aug 15 17:19:26 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/ProfilerFactory.java	Fri Aug 15 17:36:26 2014 +0400
@@ -29,11 +29,14 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.ServiceLoader;
 
 public class ProfilerFactory {
 
     public static List<Class<? extends Profiler>> getAvailableProfilers() {
         List<Class<? extends Profiler>> profs = new ArrayList<Class<? extends Profiler>>();
+
+        // All built-in profilers:
         profs.add(ClassloaderProfiler.class);
         profs.add(CompilerProfiler.class);
         profs.add(GCProfiler.class);
@@ -45,6 +48,16 @@
         profs.add(StackProfiler.class);
         profs.add(LinuxPerfProfiler.class);
         profs.add(LinuxPerfAsmProfiler.class);
+
+        // Try to discover more profilers through the SPI
+        profs.addAll(getDiscoveredProfilers());
+        return profs;
+    }
+
+    public static List<Class<? extends Profiler>> getDiscoveredProfilers() {
+        List<Class<? extends Profiler>> profs = new ArrayList<Class<? extends Profiler>>();
+        for (Profiler s : ServiceLoader.loadInstalled(Profiler.class))
+            profs.add(s.getClass());
         return profs;
     }
 
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/options/CommandLineOptions.java	Fri Aug 15 17:19:26 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/options/CommandLineOptions.java	Fri Aug 15 17:36:26 2014 +0400
@@ -442,18 +442,28 @@
     public void listProfilers() {
         StringBuilder supported = new StringBuilder();
         StringBuilder unsupported = new StringBuilder();
+
+        List<Class<? extends Profiler>> discoveredProfilers = ProfilerFactory.getDiscoveredProfilers();
+
         for (Class<? extends Profiler> s : ProfilerFactory.getAvailableProfilers()) {
             List<String> initMessages = new ArrayList<String>();
             if (ProfilerFactory.checkSupport(s, initMessages)) {
-                supported.append(String.format("%20s: %s\n", ProfilerFactory.getLabel(s), ProfilerFactory.getDescription(s)));
+                supported.append(String.format("%20s: %s %s\n",
+                        ProfilerFactory.getLabel(s),
+                        ProfilerFactory.getDescription(s),
+                        discoveredProfilers.contains(s) ? "(discovered)" : ""));
             } else {
-                unsupported.append(String.format("%20s: %s\n", ProfilerFactory.getLabel(s), ProfilerFactory.getDescription(s)));
+                unsupported.append(String.format("%20s: %s %s\n",
+                        ProfilerFactory.getLabel(s),
+                        ProfilerFactory.getDescription(s),
+                        discoveredProfilers.contains(s) ? "(discovered)" : ""));
                 for (String im : initMessages) {
                     unsupported.append(String.format("%20s  %s\n", "", im));
                 }
                 unsupported.append("\n");
             }
         }
+
         if (!supported.toString().isEmpty()) {
             System.out.println("Supported profilers:\n" + supported.toString());
         }