1 /* 2 * Copyright (c) 2015, 2016, 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.method; 24 25 import java.io.File; 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 import java.io.OutputStream; 29 import java.io.PrintStream; 30 import java.util.Arrays; 31 import java.util.Collection; 32 import java.util.List; 33 34 import org.graalvm.compiler.debug.DebugMethodMetrics; 35 import org.graalvm.compiler.debug.TTY; 36 import org.graalvm.compiler.options.Option; 37 import org.graalvm.compiler.options.OptionType; 38 import org.graalvm.compiler.options.OptionValue; 39 40 /** 41 * Interface for printing a collection of method metrics (e.g. during shutdown). 42 */ 43 public interface MethodMetricsPrinter { 44 45 class Options { 46 // @formatter:off 47 @Option(help = "Dump method metrics to stdout on shutdown.", type = OptionType.Debug) 48 public static final OptionValue<Boolean> MethodMeterPrintAscii = new OptionValue<>(false); 49 @Option(help = "Dump method metrics to the given file in CSV format on shutdown.", type = OptionType.Debug) 50 public static final OptionValue<String> MethodMeterFile = new OptionValue<>(null); 51 // @formatter:on 52 } 53 54 static boolean methodMetricsDumpingEnabled() { 55 return MethodMetricsPrinter.Options.MethodMeterPrintAscii.getValue() || MethodMetricsPrinter.Options.MethodMeterFile.getValue() != null; 56 } 57 58 /** 59 * Prints the metrics to a destination specified by the implementor of this interface. 60 * 61 * @param metrics the set of collected method metrics during execution as defined by 62 * {@link DebugMethodMetrics}. 63 */ 64 void printMethodMetrics(Collection<DebugMethodMetrics> metrics); 65 66 class MethodMetricsASCIIPrinter implements MethodMetricsPrinter { 67 private final OutputStream out; 68 69 public MethodMetricsASCIIPrinter(OutputStream out) { 70 this.out = out; 71 } 72 73 @Override 74 public void printMethodMetrics(Collection<DebugMethodMetrics> metrics) { 75 PrintStream p = new PrintStream(out); 76 for (DebugMethodMetrics m : metrics) { 77 ((MethodMetricsImpl) m).dumpASCII(p); 78 p.println(); 79 } 80 } 81 82 } 83 84 class MethodMetricsCompositePrinter implements MethodMetricsPrinter { 85 private final List<MethodMetricsPrinter> printers; 86 87 public MethodMetricsCompositePrinter(MethodMetricsPrinter... p) { 88 printers = Arrays.asList(p); 89 } 90 91 public void registerPrinter(MethodMetricsPrinter printer) { 92 printers.add(printer); 93 } 94 95 public void unregisterPrinter(MethodMetricsPrinter printer) { 96 printers.remove(printer); 97 } 98 99 @Override 100 public void printMethodMetrics(Collection<DebugMethodMetrics> metrics) { 101 for (MethodMetricsPrinter p : printers) { 102 p.printMethodMetrics(metrics); 103 } 104 } 105 106 } 107 108 class MethodMetricsCSVFilePrinter implements MethodMetricsPrinter { 109 private FileOutputStream fw; 110 111 public MethodMetricsCSVFilePrinter() { 112 try { 113 fw = new FileOutputStream(new File(Options.MethodMeterFile.getValue())); 114 } catch (IOException e) { 115 TTY.println("Cannot create file %s for method metrics dumping:%s", Options.MethodMeterFile.getValue(), e); 116 throw new Error(e); 117 } 118 } 119 120 @Override 121 public void printMethodMetrics(Collection<DebugMethodMetrics> metrics) { 122 // mm printing creates simple (R-parsable) csv files in long data format 123 if (fw != null && metrics != null) { 124 try (PrintStream p = new PrintStream(fw)) { 125 for (DebugMethodMetrics m : metrics) { 126 if (m instanceof MethodMetricsImpl) { 127 ((MethodMetricsImpl) m).dumpCSV(p); 128 p.println(); 129 } 130 } 131 } 132 try { 133 fw.close(); 134 } catch (IOException e) { 135 throw new Error(e); 136 } 137 } 138 } 139 140 } 141 }