1 /*
   2  * Copyright (c) 2015, 2015, 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 
  24 
  25 package org.graalvm.compiler.core.common.alloc;
  26 
  27 import java.util.List;
  28 
  29 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
  30 import org.graalvm.compiler.debug.DebugContext;
  31 import org.graalvm.compiler.debug.Indent;
  32 
  33 public final class TraceStatisticsPrinter {
  34     private static final String SEP = ";";
  35 
  36     @SuppressWarnings("try")
  37     public static void printTraceStatistics(DebugContext debug, TraceBuilderResult result, String compilationUnitName) {
  38         try (DebugContext.Scope s = debug.scope("DumpTraceStatistics")) {
  39             if (debug.isLogEnabled(DebugContext.VERBOSE_LEVEL)) {
  40                 print(debug, result, compilationUnitName);
  41             }
  42         } catch (Throwable e) {
  43             debug.handle(e);
  44         }
  45     }
  46 
  47     @SuppressWarnings("try")
  48     protected static void print(DebugContext debug, TraceBuilderResult result, String compilationUnitName) {
  49         List<Trace> traces = result.getTraces();
  50         int numTraces = traces.size();
  51 
  52         try (Indent indent0 = debug.logAndIndent(DebugContext.VERBOSE_LEVEL, "<tracestatistics>")) {
  53             debug.log(DebugContext.VERBOSE_LEVEL, "<name>%s</name>", compilationUnitName != null ? compilationUnitName : "null");
  54             try (Indent indent1 = debug.logAndIndent(DebugContext.VERBOSE_LEVEL, "<traces>")) {
  55                 printRawLine(debug, "tracenumber", "total", "min", "max", "numBlocks");
  56                 for (int i = 0; i < numTraces; i++) {
  57                     AbstractBlockBase<?>[] t = traces.get(i).getBlocks();
  58                     double total = 0;
  59                     double max = Double.NEGATIVE_INFINITY;
  60                     double min = Double.POSITIVE_INFINITY;
  61                     for (AbstractBlockBase<?> block : t) {
  62                         double frequency = block.getRelativeFrequency();
  63                         total += frequency;
  64                         if (frequency < min) {
  65                             min = frequency;
  66                         }
  67                         if (frequency > max) {
  68                             max = frequency;
  69                         }
  70                     }
  71                     printLine(debug, i, total, min, max, t.length);
  72                 }
  73             }
  74             debug.log(DebugContext.VERBOSE_LEVEL, "</traces>");
  75         }
  76         debug.log(DebugContext.VERBOSE_LEVEL, "</tracestatistics>");
  77 
  78     }
  79 
  80     private static void printRawLine(DebugContext debug, Object tracenr, Object totalTime, Object minProb, Object maxProb, Object numBlocks) {
  81         debug.log(DebugContext.VERBOSE_LEVEL, "%s", String.join(SEP, tracenr.toString(), totalTime.toString(), minProb.toString(), maxProb.toString(), numBlocks.toString()));
  82     }
  83 
  84     private static void printLine(DebugContext debug, int tracenr, double totalTime, double minProb, double maxProb, int numBlocks) {
  85         printRawLine(debug, tracenr, totalTime, minProb, maxProb, numBlocks);
  86     }
  87 }