changeset 227:d43fbb55c77b

Rebuild the SamplingBuffer interface: - let the benchmark loop figure out when to backoff - do not throw out the samples, but "just" halve them
author shade
date Mon, 04 Nov 2013 12:44:38 +0100
parents 0c5bc599e661
children 964327327052
files jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java jmh-core/src/main/java/org/openjdk/jmh/util/internal/SampleBuffer.java
diffstat 2 files changed, 17 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Mon Nov 04 12:09:04 2013 +0100
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Mon Nov 04 12:44:38 2013 +0100
@@ -938,6 +938,7 @@
             writer.println("        long rnd = System.nanoTime();");
             writer.println("        long rndMask = 0;");
             writer.println("        long time = 0;");
+            writer.println("        int currentStride = 0;");
             writer.println("        do {");
 
             invocationProlog(writer, 4, method, states, true);
@@ -949,8 +950,10 @@
             writer.println("            }");
             writer.println("            " + emitCall(method, states) + ';');
             writer.println("            if (sample) {");
-            writer.println("                boolean flipped = buffer.add(System.nanoTime() - time);");
-            writer.println("                if (flipped) {");
+            writer.println("                buffer.add(System.nanoTime() - time);");
+            writer.println("                if (currentStride++ > 1000000) {");
+            writer.println("                    buffer.half();");
+            writer.println("                    currentStride = 0;");
             writer.println("                    rndMask = (rndMask << 1) + 1;");
             writer.println("                }");
             writer.println("            }");
--- a/jmh-core/src/main/java/org/openjdk/jmh/util/internal/SampleBuffer.java	Mon Nov 04 12:09:04 2013 +0100
+++ b/jmh-core/src/main/java/org/openjdk/jmh/util/internal/SampleBuffer.java	Mon Nov 04 12:44:38 2013 +0100
@@ -34,12 +34,9 @@
 public class SampleBuffer implements Serializable {
 
     private static final int PRECISION_BITS = 10;
-    private static final int SIZE_LIMIT = 1000000;
 
     private final int[][] hdr;
 
-    private int size;
-
     public SampleBuffer() {
         hdr = new int[64][];
         clear();
@@ -49,22 +46,23 @@
         for (int p = 0; p < 64; p++) {
             hdr[p] = new int[1 << PRECISION_BITS];
         }
-        size = 0;
     }
 
-    public boolean add(long sample) {
-        if (size++ > SIZE_LIMIT) {
-            clear();
-            return true;
-        } else {
-            int msb = 64 - Long.numberOfLeadingZeros(sample);
-            int bucket = Math.max(0, msb - PRECISION_BITS);
-            int subBucket = (int) (sample >> bucket);
-            hdr[bucket][subBucket]++;
-            return false;
+    public void half() {
+        for (int i = 0; i < 64; i++) {
+            for (int j = 0; j < hdr[i].length; j++) {
+                hdr[i][j] = Math.max(hdr[i][j], hdr[i][j] / 2); // prevent halving to zero
+            }
         }
     }
 
+    public void add(long sample) {
+        int msb = 64 - Long.numberOfLeadingZeros(sample);
+        int bucket = Math.max(0, msb - PRECISION_BITS);
+        int subBucket = (int) (sample >> bucket);
+        hdr[bucket][subBucket]++;
+    }
+
     public Statistics getStatistics() {
         MultisetStatistics stat = new MultisetStatistics();
         for (int i = 0; i < hdr.length; i++) {