1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.debug.internal;
  24 
  25 import java.io.PrintStream;
  26 import java.util.Arrays;
  27 import java.util.List;
  28 
  29 import org.graalvm.compiler.debug.DebugHistogram;
  30 import org.graalvm.compiler.debug.DebugHistogram.CountedValue;
  31 import org.graalvm.compiler.debug.DebugHistogram.Printer;
  32 
  33 /**
  34  * Renders a textual representation of a histogram to a given print stream.
  35  */
  36 public class DebugHistogramAsciiPrinter implements Printer {
  37 
  38     public static final int NumberSize = 10;
  39     public static final int DefaultNameSize = 50;
  40     public static final int DefaultBarSize = 100;
  41     public static final int DefaultScale = 1;
  42 
  43     private final PrintStream os;
  44     private final int limit;
  45     private final int nameSize;
  46     private final int barSize;
  47     private final int scale;
  48 
  49     public DebugHistogramAsciiPrinter(PrintStream os) {
  50         this(os, Integer.MAX_VALUE, DefaultNameSize, DefaultBarSize, DefaultScale);
  51     }
  52 
  53     /**
  54      * @param os where to print
  55      * @param limit limits printing to the {@code limit} most frequent values
  56      * @param nameSize the width of the value names column
  57      * @param barSize the width of the value frequency column
  58      * @param scale a factor by which every result is divided
  59      */
  60     public DebugHistogramAsciiPrinter(PrintStream os, int limit, int nameSize, int barSize, int scale) {
  61         this.os = os;
  62         this.limit = limit;
  63         this.nameSize = nameSize;
  64         this.barSize = barSize;
  65         this.scale = scale;
  66     }
  67 
  68     @Override
  69     public void print(DebugHistogram histogram) {
  70         List<CountedValue> list = histogram.getValues();
  71         if (list.isEmpty()) {
  72             os.printf("%s is empty.%n", histogram.getName());
  73             return;
  74         }
  75 
  76         // Sum up the total number of elements.
  77         long total = list.stream().mapToLong(CountedValue::getCount).sum();
  78 
  79         // Print header.
  80         os.printf("%s has %d unique elements and %d total elements:%n", histogram.getName(), list.size(), total / scale);
  81 
  82         long max = list.get(0).getCount() / scale;
  83         final int lineSize = nameSize + NumberSize + barSize + 10;
  84         printLine(os, '-', lineSize);
  85         String formatString = "| %-" + nameSize + "s | %-" + NumberSize + "d | %-" + barSize + "s |\n";
  86         for (int i = 0; i < list.size() && i < limit; ++i) {
  87             CountedValue cv = list.get(i);
  88             long value = cv.getCount() / scale;
  89             char[] bar = new char[(int) (((double) value / (double) max) * barSize)];
  90             Arrays.fill(bar, '=');
  91             String objectString = String.valueOf(cv.getValue());
  92             if (objectString.length() > nameSize) {
  93                 objectString = objectString.substring(0, nameSize - 3) + "...";
  94             }
  95             os.printf(formatString, objectString, value, new String(bar));
  96         }
  97         printLine(os, '-', lineSize);
  98     }
  99 
 100     private static void printLine(PrintStream printStream, char c, int lineSize) {
 101         char[] charArr = new char[lineSize];
 102         Arrays.fill(charArr, c);
 103         printStream.printf("%s%n", new String(charArr));
 104     }
 105 }