1 /*
   2  * Copyright (c) 2015, 2015, 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.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 
  58     public HotSpotCounterOp(LIRInstructionClass<? extends HotSpotCounterOp> c, String[] names, String[] groups, Value[] increments, HotSpotRegistersProvider registers, GraalHotSpotVMConfig config) {
  59         super(c);
  60 
  61         assert names.length == groups.length;
  62         assert groups.length == increments.length;
  63 
  64         this.names = names;
  65         this.groups = groups;
  66         this.increments = increments;
  67         this.thread = registers.getThreadRegister();
  68         this.config = config;
  69     }
  70 
  71     protected static int getDisplacementForLongIndex(TargetDescription target, long index) {
  72         long finalDisp = index * target.arch.getPlatformKind(JavaKind.Long).getSizeInBytes();
  73         if (!NumUtil.isInt(finalDisp)) {
  74             throw GraalError.unimplemented("cannot deal with indices that big: " + index);
  75         }
  76         return (int) finalDisp;
  77     }
  78 
  79     protected interface CounterProcedure {
  80         /**
  81          * Lambda interface for iterating over counters declared in this op.
  82          *
  83          * @param counterIndex Index in this CounterOp object.
  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;
 125         // get index for the counter
 126         return BenchmarkCounters.getIndex(name, group, config);
 127     }
 128 
 129     /**
 130      * Patches the increment value in the instruction emitted by this instruction. Use only, if
 131      * patching is needed after assembly.
 132      *
 133      * @param asm
 134      * @param increment
 135      */
 136     public void patchCounterIncrement(Assembler asm, int[] increment) {
 137         throw GraalError.unimplemented();
 138     }
 139 
 140     private static long asLong(JavaConstant value) {
 141         JavaKind kind = value.getJavaKind();
 142         switch (kind) {
 143             case Byte:
 144             case Short:
 145             case Char:
 146             case Int:
 147                 return value.asInt();
 148             case Long:
 149                 return value.asLong();
 150             default:
 151                 throw new IllegalArgumentException("not an integer kind: " + kind);
 152         }
 153     }
 154 
 155     protected static int asInt(JavaConstant value) {
 156         long l = asLong(value);
 157         if (!NumUtil.isInt(l)) {
 158             throw GraalError.shouldNotReachHere("value does not fit into int: " + l);
 159         }
 160         return (int) l;
 161     }
 162 
 163     public String[] getNames() {
 164         return names;
 165     }
 166 
 167     public String[] getGroups() {
 168         return groups;
 169     }
 170 }