001/*
002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.debug.internal;
024
025import java.io.*;
026import java.util.*;
027
028import com.oracle.graal.debug.*;
029import com.oracle.graal.debug.DebugHistogram.*;
030
031/**
032 * Renders a textual representation of a histogram to a given print stream.
033 */
034public class DebugHistogramAsciiPrinter implements Printer {
035
036    public static final int NumberSize = 10;
037    public static final int DefaultNameSize = 50;
038    public static final int DefaultBarSize = 100;
039    public static final int DefaultScale = 1;
040
041    private final PrintStream os;
042    private final int limit;
043    private final int nameSize;
044    private final int barSize;
045    private final int scale;
046
047    public DebugHistogramAsciiPrinter(PrintStream os) {
048        this(os, Integer.MAX_VALUE, DefaultNameSize, DefaultBarSize, DefaultScale);
049    }
050
051    /**
052     * @param os where to print
053     * @param limit limits printing to the {@code limit} most frequent values
054     * @param nameSize the width of the value names column
055     * @param barSize the width of the value frequency column
056     * @param scale a factor by which every result is divided
057     */
058    public DebugHistogramAsciiPrinter(PrintStream os, int limit, int nameSize, int barSize, int scale) {
059        this.os = os;
060        this.limit = limit;
061        this.nameSize = nameSize;
062        this.barSize = barSize;
063        this.scale = scale;
064    }
065
066    public void print(DebugHistogram histogram) {
067        List<CountedValue> list = histogram.getValues();
068        if (list.isEmpty()) {
069            os.printf("%s is empty.%n", histogram.getName());
070            return;
071        }
072
073        // Sum up the total number of elements.
074        long total = list.stream().mapToLong(CountedValue::getCount).sum();
075
076        // Print header.
077        os.printf("%s has %d unique elements and %d total elements:%n", histogram.getName(), list.size(), total / scale);
078
079        long max = list.get(0).getCount() / scale;
080        final int lineSize = nameSize + NumberSize + barSize + 10;
081        printLine(os, '-', lineSize);
082        String formatString = "| %-" + nameSize + "s | %-" + NumberSize + "d | %-" + barSize + "s |\n";
083        for (int i = 0; i < list.size() && i < limit; ++i) {
084            CountedValue cv = list.get(i);
085            long value = cv.getCount() / scale;
086            char[] bar = new char[(int) (((double) value / (double) max) * barSize)];
087            Arrays.fill(bar, '=');
088            String objectString = String.valueOf(cv.getValue());
089            if (objectString.length() > nameSize) {
090                objectString = objectString.substring(0, nameSize - 3) + "...";
091            }
092            os.printf(formatString, objectString, value, new String(bar));
093        }
094        printLine(os, '-', lineSize);
095    }
096
097    private static void printLine(PrintStream printStream, char c, int lineSize) {
098        char[] charArr = new char[lineSize];
099        Arrays.fill(charArr, c);
100        printStream.printf("%s%n", new String(charArr));
101    }
102}