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