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.hotspot;
24
25 import static org.graalvm.compiler.lir.LIRValueUtil.asJavaConstant;
26 import static org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant;
27 import static jdk.vm.ci.code.ValueUtil.isRegister;
28
29 import java.util.Arrays;
30 import java.util.HashMap;
31
32 import org.graalvm.compiler.asm.Assembler;
33 import org.graalvm.compiler.asm.NumUtil;
34 import org.graalvm.compiler.debug.GraalError;
35 import org.graalvm.compiler.hotspot.debug.BenchmarkCounters;
36 import org.graalvm.compiler.hotspot.meta.HotSpotRegistersProvider;
37 import org.graalvm.compiler.lir.LIRInstruction;
38 import org.graalvm.compiler.lir.LIRInstructionClass;
39
40 import jdk.vm.ci.code.Register;
41 import jdk.vm.ci.code.TargetDescription;
42 import jdk.vm.ci.meta.JavaConstant;
43 import jdk.vm.ci.meta.JavaKind;
44 import jdk.vm.ci.meta.Value;
45
46 public abstract class HotSpotCounterOp extends LIRInstruction {
47 public static final LIRInstructionClass<HotSpotCounterOp> TYPE = LIRInstructionClass.create(HotSpotCounterOp.class);
48
49 private final String[] names;
50 private final String[] groups;
51 protected final Register thread;
52 protected final GraalHotSpotVMConfig config;
53 @Alive({OperandFlag.CONST, OperandFlag.REG}) protected Value[] increments;
54
55 public HotSpotCounterOp(LIRInstructionClass<? extends HotSpotCounterOp> c, String name, String group, Value increment, HotSpotRegistersProvider registers, GraalHotSpotVMConfig config) {
56 this(c, new String[]{name}, new String[]{group}, new Value[]{increment}, registers, config);
57 }
58
85 * @param increment Value for increment
86 * @param displacement Displacement in bytes in the counter array
87 */
88 void apply(int counterIndex, Value increment, int displacement);
89 }
90
91 /**
92 * Calls the {@link CounterProcedure} for each counter in ascending order of their displacement
93 * in the counter array.
94 *
95 * @param proc The procedure to be called
96 * @param target Target architecture (used to calculate the array displacements)
97 */
98 protected void forEachCounter(CounterProcedure proc, TargetDescription target) {
99 if (names.length == 1) { // fast path
100 int arrayIndex = getIndex(names[0], groups[0], increments[0]);
101 int displacement = getDisplacementForLongIndex(target, arrayIndex);
102 proc.apply(0, increments[0], displacement);
103 } else { // Slow path with sort by displacements ascending
104 int[] displacements = new int[names.length];
105 HashMap<Integer, Integer> offsetMap = new HashMap<>(names.length);
106 for (int i = 0; i < names.length; i++) {
107 int arrayIndex = getIndex(names[i], groups[i], increments[i]);
108 displacements[i] = getDisplacementForLongIndex(target, arrayIndex);
109 offsetMap.put(displacements[i], i);
110 }
111 Arrays.sort(displacements);
112 // Now apply in order
113 for (int offset : displacements) {
114 int idx = offsetMap.get(offset);
115 proc.apply(idx, increments[idx], displacements[idx]);
116 }
117 }
118 }
119
120 protected int getIndex(String name, String group, Value increment) {
121 if (isJavaConstant(increment)) {
122 // get index for the counter
123 return BenchmarkCounters.getIndexConstantIncrement(name, group, config, asLong(asJavaConstant(increment)));
124 }
125 assert isRegister(increment) : "Unexpected Value: " + increment;
|
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.hotspot;
24
25 import static org.graalvm.compiler.lir.LIRValueUtil.asJavaConstant;
26 import static org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant;
27 import static jdk.vm.ci.code.ValueUtil.isRegister;
28
29 import java.util.Arrays;
30 import org.graalvm.compiler.asm.Assembler;
31 import org.graalvm.compiler.core.common.NumUtil;
32 import org.graalvm.compiler.debug.GraalError;
33 import org.graalvm.compiler.hotspot.debug.BenchmarkCounters;
34 import org.graalvm.compiler.hotspot.meta.HotSpotRegistersProvider;
35 import org.graalvm.compiler.lir.LIRInstruction;
36 import org.graalvm.compiler.lir.LIRInstructionClass;
37 import org.graalvm.util.EconomicMap;
38
39 import jdk.vm.ci.code.Register;
40 import jdk.vm.ci.code.TargetDescription;
41 import jdk.vm.ci.meta.JavaConstant;
42 import jdk.vm.ci.meta.JavaKind;
43 import jdk.vm.ci.meta.Value;
44
45 public abstract class HotSpotCounterOp extends LIRInstruction {
46 public static final LIRInstructionClass<HotSpotCounterOp> TYPE = LIRInstructionClass.create(HotSpotCounterOp.class);
47
48 private final String[] names;
49 private final String[] groups;
50 protected final Register thread;
51 protected final GraalHotSpotVMConfig config;
52 @Alive({OperandFlag.CONST, OperandFlag.REG}) protected Value[] increments;
53
54 public HotSpotCounterOp(LIRInstructionClass<? extends HotSpotCounterOp> c, String name, String group, Value increment, HotSpotRegistersProvider registers, GraalHotSpotVMConfig config) {
55 this(c, new String[]{name}, new String[]{group}, new Value[]{increment}, registers, config);
56 }
57
84 * @param increment Value for increment
85 * @param displacement Displacement in bytes in the counter array
86 */
87 void apply(int counterIndex, Value increment, int displacement);
88 }
89
90 /**
91 * Calls the {@link CounterProcedure} for each counter in ascending order of their displacement
92 * in the counter array.
93 *
94 * @param proc The procedure to be called
95 * @param target Target architecture (used to calculate the array displacements)
96 */
97 protected void forEachCounter(CounterProcedure proc, TargetDescription target) {
98 if (names.length == 1) { // fast path
99 int arrayIndex = getIndex(names[0], groups[0], increments[0]);
100 int displacement = getDisplacementForLongIndex(target, arrayIndex);
101 proc.apply(0, increments[0], displacement);
102 } else { // Slow path with sort by displacements ascending
103 int[] displacements = new int[names.length];
104 EconomicMap<Integer, Integer> offsetMap = EconomicMap.create();
105 for (int i = 0; i < names.length; i++) {
106 int arrayIndex = getIndex(names[i], groups[i], increments[i]);
107 displacements[i] = getDisplacementForLongIndex(target, arrayIndex);
108 offsetMap.put(displacements[i], i);
109 }
110 Arrays.sort(displacements);
111 // Now apply in order
112 for (int offset : displacements) {
113 int idx = offsetMap.get(offset);
114 proc.apply(idx, increments[idx], displacements[idx]);
115 }
116 }
117 }
118
119 protected int getIndex(String name, String group, Value increment) {
120 if (isJavaConstant(increment)) {
121 // get index for the counter
122 return BenchmarkCounters.getIndexConstantIncrement(name, group, config, asLong(asJavaConstant(increment)));
123 }
124 assert isRegister(increment) : "Unexpected Value: " + increment;
|