1 /*
   2  * Copyright (c) 2017, 2018, 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.replacements;
  26 
  27 /**
  28  * A histogram that can (only) be {@linkplain #inc(long) incremented} from within a snippet for
  29  * gathering snippet specific metrics.
  30  */
  31 public final class SnippetIntegerHistogram {
  32     public static final SnippetIntegerHistogram DISABLED_COUNTER = new SnippetIntegerHistogram(null, 1, "Disabled", "Disabled");
  33 
  34     private final SnippetCounter.Group group;
  35     private final String name;
  36 
  37     private final SnippetCounter counter0;
  38     private final SnippetCounter counter1;
  39     private final SnippetCounter counter2;
  40     private final SnippetCounter counter3;
  41     private final SnippetCounter counter4;
  42     private final SnippetCounter counter5;
  43     private final SnippetCounter counter6;
  44     private final SnippetCounter counter7;
  45     private final SnippetCounter counter8;
  46     private final SnippetCounter counter9;
  47     private final SnippetCounter counter10;
  48 
  49     private final int counter0UpperBound;
  50     private final int counter1UpperBound;
  51     private final int counter2UpperBound;
  52     private final int counter3UpperBound;
  53     private final int counter4UpperBound;
  54     private final int counter5UpperBound;
  55     private final int counter6UpperBound;
  56     private final int counter7UpperBound;
  57     private final int counter8UpperBound;
  58     private final int counter9UpperBound;
  59 
  60     public SnippetIntegerHistogram(SnippetCounter.Group group, int log2StepLength, String name, String description) {
  61         assert log2StepLength > 0;
  62 
  63         this.group = group;
  64         this.name = name;
  65 
  66         int lowerBound = 0;
  67         counter0UpperBound = 0;
  68         counter0 = createCounter(group, name, description, lowerBound, counter0UpperBound);
  69 
  70         lowerBound = counter0UpperBound + 1;
  71         counter1UpperBound = Math.max(1, lowerBound - 1) << log2StepLength;
  72         counter1 = createCounter(group, name, description, lowerBound, counter1UpperBound);
  73 
  74         lowerBound = counter1UpperBound + 1;
  75         counter2UpperBound = Math.max(1, lowerBound - 1) << log2StepLength;
  76         counter2 = createCounter(group, name, description, lowerBound, counter2UpperBound);
  77 
  78         lowerBound = counter2UpperBound + 1;
  79         counter3UpperBound = Math.max(1, lowerBound - 1) << log2StepLength;
  80         counter3 = createCounter(group, name, description, lowerBound, counter3UpperBound);
  81 
  82         lowerBound = counter3UpperBound + 1;
  83         counter4UpperBound = Math.max(1, lowerBound - 1) << log2StepLength;
  84         counter4 = createCounter(group, name, description, lowerBound, counter4UpperBound);
  85 
  86         lowerBound = counter4UpperBound + 1;
  87         counter5UpperBound = Math.max(1, lowerBound - 1) << log2StepLength;
  88         counter5 = createCounter(group, name, description, lowerBound, counter5UpperBound);
  89 
  90         lowerBound = counter5UpperBound + 1;
  91         counter6UpperBound = Math.max(1, lowerBound - 1) << log2StepLength;
  92         counter6 = createCounter(group, name, description, lowerBound, counter6UpperBound);
  93 
  94         lowerBound = counter6UpperBound + 1;
  95         counter7UpperBound = Math.max(1, lowerBound - 1) << log2StepLength;
  96         counter7 = createCounter(group, name, description, lowerBound, counter7UpperBound);
  97 
  98         lowerBound = counter7UpperBound + 1;
  99         counter8UpperBound = Math.max(1, lowerBound - 1) << log2StepLength;
 100         counter8 = createCounter(group, name, description, lowerBound, counter8UpperBound);
 101 
 102         lowerBound = counter8UpperBound + 1;
 103         counter9UpperBound = Math.max(1, lowerBound - 1) << log2StepLength;
 104         counter9 = createCounter(group, name, description, lowerBound, counter9UpperBound);
 105 
 106         lowerBound = counter9UpperBound + 1;
 107         counter10 = createCounter(group, name, description, lowerBound, Long.MAX_VALUE);
 108     }
 109 
 110     private static SnippetCounter createCounter(SnippetCounter.Group group, String name, String description, long lowerBound, long upperBound) {
 111         if (group != null) {
 112             SnippetCounter snippetCounter = new SnippetCounter(group, name + "[" + lowerBound + ", " + upperBound + "]", description);
 113             return snippetCounter;
 114         }
 115         return null;
 116     }
 117 
 118     /**
 119      * Increments the value of the matching histogram element. This method can only be used in a
 120      * snippet on a compile-time constant {@link SnippetIntegerHistogram} object.
 121      */
 122     public void inc(long value) {
 123         if (group != null) {
 124             if (value <= counter0UpperBound) {
 125                 counter0.inc();
 126             } else if (value <= counter1UpperBound) {
 127                 counter1.inc();
 128             } else if (value <= counter2UpperBound) {
 129                 counter2.inc();
 130             } else if (value <= counter3UpperBound) {
 131                 counter3.inc();
 132             } else if (value <= counter4UpperBound) {
 133                 counter4.inc();
 134             } else if (value <= counter5UpperBound) {
 135                 counter5.inc();
 136             } else if (value <= counter6UpperBound) {
 137                 counter6.inc();
 138             } else if (value <= counter7UpperBound) {
 139                 counter7.inc();
 140             } else if (value <= counter8UpperBound) {
 141                 counter8.inc();
 142             } else if (value <= counter9UpperBound) {
 143                 counter9.inc();
 144             } else {
 145                 counter10.inc();
 146             }
 147         }
 148     }
 149 
 150     @Override
 151     public String toString() {
 152         if (group != null) {
 153             return "SnippetHistogram-" + group.name + ":" + name;
 154         }
 155         return super.toString();
 156     }
 157 }