# HG changeset patch # User Doug Simon # Date 1378727873 -7200 # Node ID 63b4694d3627cfb310e8eee66908553a5fac6f0c # Parent e122183381640f349f59027a3606e0ff7c74a075 split DebugHistogram printing out into separate service diff -r e12218338164 -r 63b4694d3627 graal/com.oracle.graal.debug.test/src/com/oracle/graal/debug/test/DebugHistogramTest.java --- a/graal/com.oracle.graal.debug.test/src/com/oracle/graal/debug/test/DebugHistogramTest.java Fri Sep 06 18:11:30 2013 +0200 +++ b/graal/com.oracle.graal.debug.test/src/com/oracle/graal/debug/test/DebugHistogramTest.java Mon Sep 09 13:57:53 2013 +0200 @@ -27,6 +27,7 @@ import org.junit.*; import com.oracle.graal.debug.*; +import com.oracle.graal.debug.internal.*; public class DebugHistogramTest { @@ -34,7 +35,7 @@ public void testEmptyHistogram() { DebugHistogram histogram = Debug.createHistogram("TestHistogram"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - histogram.print(new PrintStream(outputStream)); + new DebugHistogramAsciiPrinter(new PrintStream(outputStream)).print(histogram); Assert.assertEquals("TestHistogram is empty.\n", outputStream.toString()); } @@ -44,7 +45,7 @@ ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); histogram.add(new Integer(1)); histogram.add(new Integer(1)); - histogram.print(new PrintStream(outputStream)); + new DebugHistogramAsciiPrinter(new PrintStream(outputStream)).print(histogram); String[] lines = outputStream.toString().split("\n"); Assert.assertEquals(4, lines.length); Assert.assertEquals("TestHistogram has 1 unique elements and 2 total elements:", lines[0]); @@ -66,7 +67,7 @@ histogram.add(new Integer(1)); histogram.add(new Integer(2)); histogram.add(new Integer(2)); - histogram.print(new PrintStream(outputStream)); + new DebugHistogramAsciiPrinter(new PrintStream(outputStream)).print(histogram); String[] lines = outputStream.toString().split("\n"); Assert.assertEquals(5, lines.length); Assert.assertEquals("TestHistogram has 2 unique elements and 3 total elements:", lines[0]); @@ -89,7 +90,7 @@ DebugHistogram histogram = Debug.createHistogram("TestHistogram"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); histogram.add("MyCustomValue"); - histogram.print(new PrintStream(outputStream), Integer.MAX_VALUE, 10, 10); + new DebugHistogramAsciiPrinter(new PrintStream(outputStream), Integer.MAX_VALUE, 10, 10).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]); diff -r e12218338164 -r 63b4694d3627 graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java --- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java Fri Sep 06 18:11:30 2013 +0200 +++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java Mon Sep 09 13:57:53 2013 +0200 @@ -342,6 +342,9 @@ } } + /** + * Creates an object for counting value frequencies. + */ public static DebugHistogram createHistogram(String name) { return new DebugHistogramImpl(name); } diff -r e12218338164 -r 63b4694d3627 graal/com.oracle.graal.debug/src/com/oracle/graal/debug/DebugHistogram.java --- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/DebugHistogram.java Fri Sep 06 18:11:30 2013 +0200 +++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/DebugHistogram.java Mon Sep 09 13:57:53 2013 +0200 @@ -22,15 +22,75 @@ */ package com.oracle.graal.debug; -import java.io.*; +import java.util.*; +/** + * Facility for recording value frequencies. + */ public interface DebugHistogram { + /** + * Gets the name specified when this objected was {@linkplain Debug#createHistogram(String) + * created}. + */ String getName(); + /** + * Increments the count for a given value. + */ void add(Object value); - void print(PrintStream os); + /** + * A value and a frequency. The ordering imposed by {@link #compareTo(CountedValue)} places + * values with higher frequencies first. + */ + public class CountedValue implements Comparable { + + private int count; + private final Object value; + + public CountedValue(int count, Object value) { + this.count = count; + this.value = value; + } + + public int compareTo(CountedValue o) { + if (count < o.count) { + return 1; + } else if (count > o.count) { + return -1; + } + return 0; + } - void print(PrintStream os, int limit, int nameSize, int barSize); + @Override + public String toString() { + return count + " -> " + value; + } + + public void inc() { + count++; + } + + public int getCount() { + return count; + } + + public Object getValue() { + return value; + } + } + + /** + * Gets a list of the counted values, sorted in descending order of frequency. + */ + List getValues(); + + /** + * Interface for a service that can render a visualization of a histogram. + */ + public interface Printer { + + void print(DebugHistogram histogram); + } } diff -r e12218338164 -r 63b4694d3627 graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugHistogramAsciiPrinter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugHistogramAsciiPrinter.java Mon Sep 09 13:57:53 2013 +0200 @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.graal.debug.internal; + +import java.io.*; +import java.util.*; + +import com.oracle.graal.debug.*; +import com.oracle.graal.debug.DebugHistogram.*; + +/** + * Renders a textual representation of a histogram to a given print stream. + */ +public class DebugHistogramAsciiPrinter implements Printer { + + public static final int NumberSize = 10; + public static final int DefaultNameSize = 50; + public static final int DefaultBarSize = 100; + + private PrintStream os; + private int limit; + private int nameSize; + private int barSize; + + public DebugHistogramAsciiPrinter(PrintStream os) { + this(os, Integer.MAX_VALUE, DefaultNameSize, DefaultBarSize); + } + + /** + * @param os where to print + * @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 + */ + public DebugHistogramAsciiPrinter(PrintStream os, int limit, int nameSize, int barSize) { + this.os = os; + this.limit = limit; + this.nameSize = nameSize; + this.barSize = barSize; + } + + public void print(DebugHistogram histogram) { + List list = histogram.getValues(); + if (list.isEmpty()) { + os.printf("%s is empty.\n", histogram.getName()); + return; + } + + // Sum up the total number of elements. + int total = 0; + for (CountedValue cv : list) { + total += cv.getCount(); + } + + // Print header. + os.printf("%s has %d unique elements and %d total elements:\n", histogram.getName(), list.size(), total); + + int max = list.get(0).getCount(); + 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(); + char[] bar = new char[(int) (((double) value / (double) max) * barSize)]; + Arrays.fill(bar, '='); + String objectString = String.valueOf(cv.getValue()); + if (objectString.length() > nameSize) { + objectString = objectString.substring(0, nameSize - 3) + "..."; + } + os.printf(formatString, objectString, value, new String(bar)); + } + printLine(os, '-', lineSize); + } + + private static void printLine(PrintStream printStream, char c, int lineSize) { + char[] charArr = new char[lineSize]; + Arrays.fill(charArr, c); + printStream.printf("%s\n", new String(charArr)); + } +} diff -r e12218338164 -r 63b4694d3627 graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugHistogramImpl.java --- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugHistogramImpl.java Fri Sep 06 18:11:30 2013 +0200 +++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugHistogramImpl.java Mon Sep 09 13:57:53 2013 +0200 @@ -22,28 +22,25 @@ */ package com.oracle.graal.debug.internal; -import java.io.*; import java.util.*; import com.oracle.graal.debug.*; public class DebugHistogramImpl implements DebugHistogram { - public static final int NumberSize = 10; - public static final int DefaultNameSize = 50; - public static final int DefaultBarSize = 100; private final String name; - private HashMap map = new HashMap<>(); + private HashMap map = new HashMap<>(); public DebugHistogramImpl(String name) { this.name = name; } public void add(Object value) { - if (!map.containsKey(value)) { - map.put(value, 1); + CountedValue cv = map.get(value); + if (cv == null) { + map.put(value, new CountedValue(1, value)); } else { - map.put(value, map.get(value) + 1); + cv.inc(); } } @@ -52,59 +49,9 @@ return name; } - @Override - public void print(PrintStream os) { - print(os, Integer.MAX_VALUE, DefaultNameSize, DefaultBarSize); - } - - public void print(PrintStream os, int limit, int nameSize, int barSize) { - - List list = new ArrayList<>(map.keySet()); - if (list.size() == 0) { - // No elements in the histogram. - os.printf("%s is empty.\n", name); - return; - } - - // Sort from highest to smallest. - Collections.sort(list, new Comparator() { - - @Override - public int compare(Object o1, Object o2) { - return map.get(o2) - map.get(o1); - } - }); - - // Sum up the total number of elements. - int total = 0; - for (Object o : list) { - total += map.get(o); - } - - // Print header. - os.printf("%s has %d unique elements and %d total elements:\n", name, list.size(), total); - - int max = map.get(list.get(0)); - 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) { - Object o = list.get(i); - int value = map.get(o); - char[] bar = new char[(int) (((double) value / (double) max) * barSize)]; - Arrays.fill(bar, '='); - String objectString = o.toString(); - if (objectString.length() > nameSize) { - objectString = objectString.substring(0, nameSize - 3) + "..."; - } - os.printf(formatString, objectString, value, new String(bar)); - } - printLine(os, '-', lineSize); - } - - private static void printLine(PrintStream printStream, char c, int lineSize) { - char[] charArr = new char[lineSize]; - Arrays.fill(charArr, c); - printStream.printf("%s\n", new String(charArr)); + public List getValues() { + ArrayList res = new ArrayList<>(map.values()); + Collections.sort(res); + return res; } } diff -r e12218338164 -r 63b4694d3627 graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/PartialEvaluator.java --- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/PartialEvaluator.java Fri Sep 06 18:11:30 2013 +0200 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/PartialEvaluator.java Mon Sep 09 13:57:53 2013 +0200 @@ -31,6 +31,7 @@ import com.oracle.graal.api.code.*; import com.oracle.graal.api.meta.*; import com.oracle.graal.debug.*; +import com.oracle.graal.debug.internal.*; import com.oracle.graal.graph.*; import com.oracle.graal.graph.Node; import com.oracle.graal.hotspot.*; @@ -148,7 +149,7 @@ for (Constant c : constantReceivers) { histogram.add(c.asObject().getClass().getSimpleName()); } - histogram.print(TTY.out().out()); + new DebugHistogramAsciiPrinter(TTY.out().out()).print(histogram); } // Additional inlining.