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