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