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.List; 27 28 import org.graalvm.compiler.debug.DebugHistogram; 29 import org.graalvm.compiler.debug.DebugHistogram.CountedValue; 30 import org.graalvm.compiler.debug.DebugHistogram.Printer; 31 32 /** 33 * Renders a histogram as an R script to a given print stream. The R script emitted for a histogram 34 * is a simple set of statements for defining a vector of named objects. 35 */ 36 public class DebugHistogramRPrinter implements Printer { 37 38 private PrintStream os; 39 private int limit; 40 41 public DebugHistogramRPrinter(PrintStream os) { 42 this(os, Integer.MAX_VALUE); 43 } 44 45 /** 46 * @param os where to print 47 * @param limit limits printing to the {@code limit} most frequent values 48 */ 49 public DebugHistogramRPrinter(PrintStream os, int limit) { 50 this.os = os; 51 this.limit = limit; 52 } 53 54 @Override 55 public void print(DebugHistogram histogram) { 56 List<CountedValue> list = histogram.getValues(); 57 if (list.isEmpty()) { 58 return; 59 } 60 61 String var = histogram.getName().replace('-', '.').replace(' ', '_'); 62 os.print(var + " <- c("); 63 for (int i = 0; i < list.size() && i < limit; ++i) { 64 CountedValue cv = list.get(i); 65 if (i != 0) { 66 os.print(", "); 67 } 68 os.print(cv.getCount()); 69 } 70 os.println(");"); 71 72 os.print("names(" + var + ") <- c("); 73 for (int i = 0; i < list.size() && i < limit; ++i) { 74 CountedValue cv = list.get(i); 75 if (i != 0) { 76 os.print(", "); 77 } 78 os.print("\"" + cv.getValue() + "\""); 79 } 80 os.println(");"); 81 } 82 }