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