changeset 15954:cda2a7d1dcff

long values and scale on DebugHistogram
author Lukas Stadler <lukas.stadler@oracle.com>
date Wed, 28 May 2014 17:19:41 +0200
parents a750e0d83535
children 3f48e9a1016c
files graal/com.oracle.graal.debug.test/src/com/oracle/graal/debug/test/DebugHistogramTest.java graal/com.oracle.graal.debug/src/com/oracle/graal/debug/DebugHistogram.java graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugHistogramAsciiPrinter.java graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugHistogramImpl.java
diffstat 4 files changed, 30 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.debug.test/src/com/oracle/graal/debug/test/DebugHistogramTest.java	Wed May 28 17:14:24 2014 +0200
+++ b/graal/com.oracle.graal.debug.test/src/com/oracle/graal/debug/test/DebugHistogramTest.java	Wed May 28 17:19:41 2014 +0200
@@ -111,7 +111,7 @@
         DebugHistogram histogram = Debug.createHistogram("TestHistogram");
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
         histogram.add("MyCustomValue");
-        new DebugHistogramAsciiPrinter(new PrintStream(outputStream), Integer.MAX_VALUE, 10, 10).print(histogram);
+        new DebugHistogramAsciiPrinter(new PrintStream(outputStream), Integer.MAX_VALUE, 10, 10, 1).print(histogram);
         String[] lines = outputStream.toString().split("\n");
         Assert.assertEquals(4, lines.length);
         Assert.assertEquals("TestHistogram has 1 unique elements and 1 total elements:", lines[0]);
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/DebugHistogram.java	Wed May 28 17:14:24 2014 +0200
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/DebugHistogram.java	Wed May 28 17:19:41 2014 +0200
@@ -40,16 +40,18 @@
      */
     void add(Object value);
 
+    void add(Object value, long count);
+
     /**
      * A value and a frequency. The ordering imposed by {@link #compareTo(CountedValue)} places
      * values with higher frequencies first.
      */
     public class CountedValue implements Comparable<CountedValue> {
 
-        private int count;
+        private long count;
         private final Object value;
 
-        public CountedValue(int count, Object value) {
+        public CountedValue(long count, Object value) {
             this.count = count;
             this.value = value;
         }
@@ -72,11 +74,11 @@
             count++;
         }
 
-        public void add(int n) {
+        public void add(long n) {
             count += n;
         }
 
-        public int getCount() {
+        public long getCount() {
             return count;
         }
 
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugHistogramAsciiPrinter.java	Wed May 28 17:14:24 2014 +0200
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugHistogramAsciiPrinter.java	Wed May 28 17:19:41 2014 +0200
@@ -37,14 +37,16 @@
     public static final int NumberSize = 10;
     public static final int DefaultNameSize = 50;
     public static final int DefaultBarSize = 100;
+    public static final int DefaultScale = 1;
 
-    private PrintStream os;
-    private int limit;
-    private int nameSize;
-    private int barSize;
+    private final PrintStream os;
+    private final int limit;
+    private final int nameSize;
+    private final int barSize;
+    private final int scale;
 
     public DebugHistogramAsciiPrinter(PrintStream os) {
-        this(os, Integer.MAX_VALUE, DefaultNameSize, DefaultBarSize);
+        this(os, Integer.MAX_VALUE, DefaultNameSize, DefaultBarSize, DefaultScale);
     }
 
     /**
@@ -52,12 +54,14 @@
      * @param limit limits printing to the {@code limit} most frequent values
      * @param nameSize the width of the value names column
      * @param barSize the width of the value frequency column
+     * @param scale a factor by which every result is divided
      */
-    public DebugHistogramAsciiPrinter(PrintStream os, int limit, int nameSize, int barSize) {
+    public DebugHistogramAsciiPrinter(PrintStream os, int limit, int nameSize, int barSize, int scale) {
         this.os = os;
         this.limit = limit;
         this.nameSize = nameSize;
         this.barSize = barSize;
+        this.scale = scale;
     }
 
     public void print(DebugHistogram histogram) {
@@ -68,21 +72,18 @@
         }
 
         // Sum up the total number of elements.
-        int total = 0;
-        for (CountedValue cv : list) {
-            total += cv.getCount();
-        }
+        long total = list.stream().mapToLong(CountedValue::getCount).sum();
 
         // Print header.
-        os.printf("%s has %d unique elements and %d total elements:%n", histogram.getName(), list.size(), total);
+        os.printf("%s has %d unique elements and %d total elements:%n", histogram.getName(), list.size(), total / scale);
 
-        int max = list.get(0).getCount();
+        long max = list.get(0).getCount() / scale;
         final int lineSize = nameSize + NumberSize + barSize + 10;
         printLine(os, '-', lineSize);
         String formatString = "| %-" + nameSize + "s | %-" + NumberSize + "d | %-" + barSize + "s |\n";
         for (int i = 0; i < list.size() && i < limit; ++i) {
             CountedValue cv = list.get(i);
-            int value = cv.getCount();
+            long value = cv.getCount() / scale;
             char[] bar = new char[(int) (((double) value / (double) max) * barSize)];
             Arrays.fill(bar, '=');
             String objectString = String.valueOf(cv.getValue());
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugHistogramImpl.java	Wed May 28 17:14:24 2014 +0200
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugHistogramImpl.java	Wed May 28 17:19:41 2014 +0200
@@ -44,6 +44,15 @@
         }
     }
 
+    public void add(Object value, long count) {
+        CountedValue cv = map.get(value);
+        if (cv == null) {
+            map.put(value, new CountedValue(count, value));
+        } else {
+            cv.add(count);
+        }
+    }
+
     @Override
     public String getName() {
         return name;