changeset 1074:c486d1241cc1

7901231: Fix FindBugs warnings in JMH
author shade
date Thu, 25 Dec 2014 19:15:07 +0300
parents fd7425c74b22
children 553ca5963167
files jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java jmh-core/src/main/java/org/openjdk/jmh/profile/HotspotCompilationProfiler.java jmh-core/src/main/java/org/openjdk/jmh/profile/HotspotRuntimeProfiler.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/runner/AbstractResourceReader.java jmh-core/src/main/java/org/openjdk/jmh/runner/BaseBenchmarkHandler.java jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedMain.java jmh-core/src/main/java/org/openjdk/jmh/util/FileUtils.java jmh-core/src/main/java/org/openjdk/jmh/util/Utils.java
diffstat 10 files changed, 54 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java	Thu Dec 25 17:16:33 2014 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java	Thu Dec 25 19:15:07 2014 +0300
@@ -690,17 +690,7 @@
     public List<String> getStateGetters(MethodInfo method) {
         List<String> result = new ArrayList<String>();
         for (StateObject so : stateOrder(method, true)) {
-            switch (so.scope) {
-                case Benchmark:
-                case Thread:
-                    result.add(so.type + " " + so.localIdentifier + " = _jmh_tryInit_" + so.fieldIdentifier + "(control, threadParams" + soDependency_Args(so) + ");");
-                    break;
-                case Group:
-                    result.add(so.type + " " + so.localIdentifier + " = _jmh_tryInit_" + so.fieldIdentifier + "(control, threadParams" + soDependency_Args(so) + ");");
-                    break;
-                default:
-                    throw new IllegalStateException("Unhandled scope: " + so.scope);
-            }
+            result.add(so.type + " " + so.localIdentifier + " = _jmh_tryInit_" + so.fieldIdentifier + "(control, threadParams" + soDependency_Args(so) + ");");
         }
         return result;
     }
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/HotspotCompilationProfiler.java	Thu Dec 25 17:16:33 2014 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/HotspotCompilationProfiler.java	Thu Dec 25 19:15:07 2014 +0300
@@ -59,7 +59,7 @@
         Map<String, Long> current = counters().getCurrent();
         return Arrays.asList(
                 new ProfilerResult("@compiler.totalTime",
-                        TimeUnit.NANOSECONDS.toMillis(current.get("java.ci.totalTime")),
+                        current.get("java.ci.totalTime") * 1D / TimeUnit.MILLISECONDS.toNanos(1),
                         "ms", AggregationPolicy.MAX),
 
                 new ProfilerResult("@compiler.totalCompiles",
@@ -75,11 +75,11 @@
                         "methods", AggregationPolicy.MAX),
 
                 new ProfilerResult("@compiler.nmethodCodeSize",
-                        current.get("sun.ci.nmethodCodeSize")/ 1024,
+                        current.get("sun.ci.nmethodCodeSize") / 1024d,
                         "Kb", AggregationPolicy.MAX),
 
                 new ProfilerResult("@compiler.nmethodSize",
-                        current.get("sun.ci.nmethodSize") / 1024,
+                        current.get("sun.ci.nmethodSize") / 1024d,
                         "Kb", AggregationPolicy.MAX),
 
                 new ProfilerResult("@compiler.osrCompiles",
@@ -87,11 +87,11 @@
                         "methods", AggregationPolicy.MAX),
 
                 new ProfilerResult("@compiler.osrBytes",
-                        current.get("sun.ci.osrBytes") / 1024,
+                        current.get("sun.ci.osrBytes") / 1024d,
                         "Kb", AggregationPolicy.MAX),
 
                 new ProfilerResult("@compiler.osrTime",
-                        TimeUnit.NANOSECONDS.toMillis(current.get("sun.ci.osrTime")),
+                        current.get("sun.ci.osrTime") * 1d / TimeUnit.MILLISECONDS.toNanos(1),
                         "ms", AggregationPolicy.MAX),
 
                 new ProfilerResult("@compiler.standardCompiles",
@@ -99,11 +99,11 @@
                         "methods", AggregationPolicy.MAX),
 
                 new ProfilerResult("@compiler.standardBytes",
-                        current.get("sun.ci.standardBytes") / 1024,
+                        current.get("sun.ci.standardBytes") / 1024d,
                         "Kb", AggregationPolicy.MAX),
 
                 new ProfilerResult("@compiler.standardTime",
-                        TimeUnit.NANOSECONDS.toMillis(current.get("sun.ci.standardTime")),
+                        current.get("sun.ci.standardTime") * 1d / TimeUnit.MILLISECONDS.toNanos(1),
                         "ms", AggregationPolicy.MAX)
         );
     }
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/HotspotRuntimeProfiler.java	Thu Dec 25 17:16:33 2014 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/HotspotRuntimeProfiler.java	Thu Dec 25 19:15:07 2014 +0300
@@ -91,11 +91,11 @@
                         "counts", AggregationPolicy.MAX),
 
                 new ProfilerResult("@rt.safepointSyncTime",
-                        TimeUnit.NANOSECONDS.toMillis(current.get("sun.rt.safepointSyncTime")),
+                        current.get("sun.rt.safepointSyncTime") * 1d / TimeUnit.MILLISECONDS.toNanos(1),
                         "ms", AggregationPolicy.MAX),
 
                 new ProfilerResult("@rt.safepointTime",
-                        TimeUnit.NANOSECONDS.toMillis(current.get("sun.rt.safepointTime")),
+                        current.get("sun.rt.safepointTime") * 1d / TimeUnit.MILLISECONDS.toNanos(1),
                         "ms", AggregationPolicy.MAX)
         );
     }
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfAsmProfiler.java	Thu Dec 25 17:16:33 2014 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfAsmProfiler.java	Thu Dec 25 19:15:07 2014 +0300
@@ -630,13 +630,16 @@
     }
 
     Collection<Collection<String>> splitAssembly(File stdOut) {
+        FileReader in = null;
         try {
             Multimap<Long, String> writerToLines = new HashMultimap<Long, String>();
             Long writerId = -1L;
 
             Pattern pWriterThread = Pattern.compile("(.*)<writer thread='(.*)'>(.*)");
             String line;
-            BufferedReader br = new BufferedReader(new FileReader(stdOut));
+
+            in = new FileReader(stdOut);
+            BufferedReader br = new BufferedReader(in);
             while ((line = br.readLine()) != null) {
                 // Parse the writer threads IDs:
                 //    <writer thread='140703710570240'/>
@@ -661,6 +664,8 @@
             return r;
         } catch (IOException e) {
             return Collections.emptyList();
+        } finally {
+            FileUtils.safelyClose(in);
         }
     }
 
@@ -719,11 +724,12 @@
     }
 
     PerfEvents readEvents(double skipSec) {
+        FileReader fr = null;
         try {
             Deduplicator<String> dedup = new Deduplicator<String>();
 
-            FileInputStream fis = new FileInputStream(perfParsedData);
-            BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
+            fr = new FileReader(perfParsedData);
+            BufferedReader reader = new BufferedReader(fr);
 
             Map<Long, String> methods = new HashMap<Long, String>();
             Map<Long, String> libs = new HashMap<Long, String>();
@@ -794,6 +800,8 @@
             return new PerfEvents(events, methods, libs);
         } catch (IOException e) {
             return new PerfEvents();
+        } finally {
+            FileUtils.safelyClose(fr);
         }
     }
 
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfProfiler.java	Thu Dec 25 17:16:33 2014 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfProfiler.java	Thu Dec 25 19:15:07 2014 +0300
@@ -29,12 +29,12 @@
 import org.openjdk.jmh.results.Aggregator;
 import org.openjdk.jmh.results.Result;
 import org.openjdk.jmh.results.ResultRole;
+import org.openjdk.jmh.util.FileUtils;
 
 import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileInputStream;
+import java.io.FileReader;
 import java.io.IOException;
-import java.io.InputStreamReader;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.text.NumberFormat;
@@ -120,9 +120,10 @@
         StringWriter sw = new StringWriter();
         PrintWriter pw = new PrintWriter(sw);
 
+        FileReader fr = null;
         try {
-            FileInputStream fis = new FileInputStream(stdErr);
-            BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
+            fr = new FileReader(stdErr);
+            BufferedReader reader = new BufferedReader(fr);
 
             long cycles = 0;
             long insns = 0;
@@ -173,6 +174,8 @@
             );
         } catch (IOException e) {
             throw new IllegalStateException(e);
+        } finally {
+            FileUtils.safelyClose(fr);
         }
     }
 
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/AbstractResourceReader.java	Thu Dec 25 17:16:33 2014 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/AbstractResourceReader.java	Thu Dec 25 19:15:07 2014 +0300
@@ -30,7 +30,6 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
-import java.io.StringReader;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -73,7 +72,7 @@
                 throw new RuntimeException("ERROR: While obtaining resource: " + resource, e);
             }
 
-            if (urls != null && urls.hasMoreElements()) {
+            if (urls.hasMoreElements()) {
                 List<Reader> readers = new ArrayList<Reader>();
                 URL url = null;
                 try {
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/BaseBenchmarkHandler.java	Thu Dec 25 17:16:33 2014 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/BaseBenchmarkHandler.java	Thu Dec 25 19:15:07 2014 +0300
@@ -96,7 +96,7 @@
     static ThreadParams[] distributeThreads(int threads, int[] groups) {
         ThreadParams[] result = new ThreadParams[threads];
         int totalGroupThreads = Utils.sum(groups);
-        int totalGroups = (int) Math.ceil(threads / totalGroupThreads);
+        int totalGroups = (int) Math.ceil(1D * threads / totalGroupThreads);
         int totalSubgroups = groups.length;
 
         int currentGroupThread = 0;
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedMain.java	Thu Dec 25 17:16:33 2014 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedMain.java	Thu Dec 25 19:15:07 2014 +0300
@@ -35,7 +35,7 @@
 class ForkedMain {
 
     private static volatile boolean gracefullyFinished;
-    private static volatile Exception exception;
+    private static volatile Throwable exception;
 
     /**
      * Application main entry point
@@ -68,7 +68,7 @@
                 runner.run();
 
                 gracefullyFinished = true;
-            } catch (Exception ex) {
+            } catch (Throwable ex) {
                 exception = ex;
                 System.exit(1);
             }
@@ -81,7 +81,7 @@
                     @Override
                     public void run() {
                         if (!gracefullyFinished) {
-                            Exception ex = exception;
+                            Throwable ex = exception;
                             if (ex == null) {
                                 ex = new IllegalStateException(
                                         "<failure: VM prematurely exited before JMH had finished with it, " +
--- a/jmh-core/src/main/java/org/openjdk/jmh/util/FileUtils.java	Thu Dec 25 17:16:33 2014 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/util/FileUtils.java	Thu Dec 25 19:15:07 2014 +0300
@@ -35,6 +35,7 @@
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.PrintWriter;
+import java.io.Reader;
 import java.nio.channels.Channel;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -232,6 +233,16 @@
         }
     }
 
+    public static void safelyClose(Reader reader) {
+        if (reader != null) {
+            try {
+                reader.close();
+            } catch (IOException e) {
+                // do nothing
+            }
+        }
+    }
+
     public static void touch(String f) throws IOException {
         File file = new File(f);
         try {
--- a/jmh-core/src/main/java/org/openjdk/jmh/util/Utils.java	Thu Dec 25 17:16:33 2014 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/util/Utils.java	Thu Dec 25 19:15:07 2014 +0300
@@ -175,14 +175,12 @@
 
         try {
             Field f = Console.class.getDeclaredField("cs");
-            if (f != null) {
-                f.setAccessible(true);
-                Console console = System.console();
-                if (console != null) {
-                    Object res = f.get(console);
-                    if (res instanceof Charset) {
-                        return ((Charset) res).name();
-                    }
+            f.setAccessible(true);
+            Console console = System.console();
+            if (console != null) {
+                Object res = f.get(console);
+                if (res instanceof Charset) {
+                    return ((Charset) res).name();
                 }
             }
         } catch (NoSuchFieldException e) {
@@ -193,12 +191,10 @@
 
         try {
             Method m = Console.class.getDeclaredMethod("encoding");
-            if (m != null) {
-                m.setAccessible(true);
-                Object res = m.invoke(null);
-                if (res instanceof String) {
-                    return (String) res;
-                }
+            m.setAccessible(true);
+            Object res = m.invoke(null);
+            if (res instanceof String) {
+                return (String) res;
             }
         } catch (NoSuchMethodException e) {
             // fall-through