1 /*
   2  * Copyright (c) 2012, 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.replacements;
  24 
  25 import java.util.ArrayList;
  26 import java.util.Collections;
  27 import java.util.List;
  28 
  29 import org.graalvm.compiler.core.common.GraalOptions;
  30 
  31 /**
  32  * A counter that can (only) be {@linkplain #inc() incremented} from within a snippet for gathering
  33  * snippet specific metrics.
  34  */
  35 public final class SnippetCounter implements Comparable<SnippetCounter> {
  36     /**
  37      * A group of related counters.
  38      */
  39     public static class Group {
  40 
  41         public interface Factory {
  42             /**
  43              * If snippet counters are {@linkplain GraalOptions#SnippetCounters enabled}, creates
  44              * and registers a {@link Group} with the given name. Otherwise, returns null.
  45              *
  46              * @param name name of the counter group
  47              */
  48             Group createSnippetCounterGroup(String name);
  49         }
  50 
  51         public static final Factory NullFactory = new Factory() {
  52 
  53             @Override
  54             public Group createSnippetCounterGroup(String name) {
  55                 return null;
  56             }
  57         };
  58 
  59         final String name;
  60         final List<SnippetCounter> counters;
  61 
  62         public Group(String name) {
  63             this.name = name;
  64             this.counters = new ArrayList<>();
  65         }
  66 
  67         @Override
  68         public synchronized String toString() {
  69             Collections.sort(counters);
  70 
  71             long total = 0;
  72             int maxNameLen = 0;
  73             for (SnippetCounter c : counters) {
  74                 total += c.value;
  75                 maxNameLen = Math.max(c.name.length(), maxNameLen);
  76             }
  77 
  78             StringBuilder buf = new StringBuilder(String.format("Counters: %s%n", name));
  79 
  80             String formatString = "  %" + maxNameLen + "s: %6.2f%%%," + (String.format("%,d", total).length() + 2) + "d  // %s%n";
  81             for (SnippetCounter c : counters) {
  82                 double percent = total == 0D ? 0D : ((double) (c.value * 100)) / total;
  83                 buf.append(String.format(formatString, c.name, percent, c.value, c.description));
  84             }
  85             buf.append(String.format(formatString, "TOTAL", 100.0D, total, ""));
  86 
  87             return buf.toString();
  88         }
  89     }
  90 
  91     /**
  92      * Sorts counters in descending order of their {@linkplain #value() values}.
  93      */
  94     @Override
  95     public int compareTo(SnippetCounter o) {
  96         return Long.signum(o.value - value);
  97     }
  98 
  99     private final Group group;
 100     private final String name;
 101     private final String description;
 102     private long value;
 103 
 104     /**
 105      * Creates a counter.
 106      *
 107      * @param group the group to which the counter belongs. If this is null, the newly created
 108      *            counter is disabled and {@linkplain #inc() incrementing} is a no-op.
 109      * @param name the name of the counter
 110      * @param description a brief comment describing the metric represented by the counter
 111      */
 112     public SnippetCounter(Group group, String name, String description) {
 113         this.group = group;
 114         this.name = name;
 115         this.description = description;
 116         if (group != null) {
 117             List<SnippetCounter> counters = group.counters;
 118             counters.add(this);
 119         }
 120     }
 121 
 122     public Group getGroup() {
 123         return group;
 124     }
 125 
 126     /**
 127      * Increments the value of this counter. This method can only be used in a snippet on a
 128      * compile-time constant {@link SnippetCounter} object.
 129      */
 130     public void inc() {
 131         if (group != null) {
 132             SnippetCounterNode.increment(this);
 133         }
 134     }
 135 
 136     /**
 137      * Increments the value of this counter. This method can only be used in a snippet on a
 138      * compile-time constant {@link SnippetCounter} object.
 139      */
 140     public void add(int increment) {
 141         if (group != null) {
 142             SnippetCounterNode.add(this, increment);
 143         }
 144     }
 145 
 146     /**
 147      * Gets the value of this counter.
 148      */
 149     public long value() {
 150         return value;
 151     }
 152 
 153     @Override
 154     public String toString() {
 155         if (group != null) {
 156             return "SnippetCounter-" + group.name + ":" + name;
 157         }
 158         return super.toString();
 159     }
 160 }