1 /*
   2  * Copyright (c) 2016, 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.hotspot.replacements.profiling;
  26 
  27 import static org.graalvm.compiler.hotspot.GraalHotSpotVMConfig.INJECTED_VMCONFIG;
  28 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.config;
  29 import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.SLOW_PATH_PROBABILITY;
  30 import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.probability;
  31 import static org.graalvm.compiler.replacements.SnippetTemplate.DEFAULT_REPLACER;
  32 
  33 import org.graalvm.compiler.api.replacements.Snippet;
  34 import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter;
  35 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
  36 import org.graalvm.compiler.debug.DebugHandlersFactory;
  37 import org.graalvm.compiler.debug.GraalError;
  38 import org.graalvm.compiler.graph.Node.ConstantNodeParameter;
  39 import org.graalvm.compiler.graph.Node.NodeIntrinsic;
  40 import org.graalvm.compiler.hotspot.HotSpotBackend;
  41 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
  42 import org.graalvm.compiler.hotspot.nodes.aot.LoadMethodCountersNode;
  43 import org.graalvm.compiler.hotspot.nodes.profiling.ProfileBranchNode;
  44 import org.graalvm.compiler.hotspot.nodes.profiling.ProfileInvokeNode;
  45 import org.graalvm.compiler.hotspot.nodes.profiling.ProfileNode;
  46 import org.graalvm.compiler.hotspot.word.MethodCountersPointer;
  47 import org.graalvm.compiler.nodes.ConstantNode;
  48 import org.graalvm.compiler.nodes.StructuredGraph;
  49 import org.graalvm.compiler.nodes.extended.ForeignCallNode;
  50 import org.graalvm.compiler.nodes.spi.LoweringTool;
  51 import org.graalvm.compiler.nodes.util.GraphUtil;
  52 import org.graalvm.compiler.options.OptionValues;
  53 import org.graalvm.compiler.replacements.SnippetTemplate;
  54 import org.graalvm.compiler.replacements.SnippetTemplate.AbstractTemplates;
  55 import org.graalvm.compiler.replacements.SnippetTemplate.Arguments;
  56 import org.graalvm.compiler.replacements.SnippetTemplate.SnippetInfo;
  57 import org.graalvm.compiler.replacements.Snippets;
  58 
  59 import jdk.vm.ci.code.CodeUtil;
  60 import jdk.vm.ci.code.TargetDescription;
  61 
  62 public class ProfileSnippets implements Snippets {
  63     @NodeIntrinsic(ForeignCallNode.class)
  64     public static native void methodInvocationEvent(@ConstantNodeParameter ForeignCallDescriptor descriptor, MethodCountersPointer counters);
  65 
  66     @Snippet
  67     protected static int notificationMask(int freqLog, int stepLog) {
  68         int stepMask = (1 << stepLog) - 1;
  69         int frequencyMask = (1 << freqLog) - 1;
  70         return frequencyMask & ~stepMask;
  71     }
  72 
  73     @Snippet
  74     public static void profileMethodEntry(MethodCountersPointer counters, int step, int stepLog, @ConstantParameter int freqLog) {
  75         int counterValue = counters.readInt(config(INJECTED_VMCONFIG).invocationCounterOffset) + config(INJECTED_VMCONFIG).invocationCounterIncrement * step;
  76         counters.writeInt(config(INJECTED_VMCONFIG).invocationCounterOffset, counterValue);
  77         if (freqLog >= 0) {
  78             final int mask = notificationMask(freqLog, stepLog);
  79             if (probability(SLOW_PATH_PROBABILITY, (counterValue & (mask << config(INJECTED_VMCONFIG).invocationCounterShift)) == 0)) {
  80                 methodInvocationEvent(HotSpotBackend.INVOCATION_EVENT, counters);
  81             }
  82         }
  83     }
  84 
  85     @NodeIntrinsic(ForeignCallNode.class)
  86     public static native void methodBackedgeEvent(@ConstantNodeParameter ForeignCallDescriptor descriptor, MethodCountersPointer counters, int bci, int targetBci);
  87 
  88     @Snippet
  89     public static void profileBackedge(MethodCountersPointer counters, int step, int stepLog, @ConstantParameter int freqLog, int bci, int targetBci) {
  90         int counterValue = counters.readInt(config(INJECTED_VMCONFIG).backedgeCounterOffset) + config(INJECTED_VMCONFIG).invocationCounterIncrement * step;
  91         counters.writeInt(config(INJECTED_VMCONFIG).backedgeCounterOffset, counterValue);
  92         final int mask = notificationMask(freqLog, stepLog);
  93         if (probability(SLOW_PATH_PROBABILITY, (counterValue & (mask << config(INJECTED_VMCONFIG).invocationCounterShift)) == 0)) {
  94             methodBackedgeEvent(HotSpotBackend.BACKEDGE_EVENT, counters, bci, targetBci);
  95         }
  96     }
  97 
  98     @Snippet
  99     public static void profileConditionalBackedge(MethodCountersPointer counters, int step, int stepLog, @ConstantParameter int freqLog, boolean branchCondition, int bci, int targetBci) {
 100         if (branchCondition) {
 101             profileBackedge(counters, step, stepLog, freqLog, bci, targetBci);
 102         }
 103     }
 104 
 105     public static class Templates extends AbstractTemplates {
 106         private final SnippetInfo profileMethodEntry = snippet(ProfileSnippets.class, "profileMethodEntry");
 107         private final SnippetInfo profileBackedge = snippet(ProfileSnippets.class, "profileBackedge");
 108         private final SnippetInfo profileConditionalBackedge = snippet(ProfileSnippets.class, "profileConditionalBackedge");
 109 
 110         public Templates(OptionValues options, Iterable<DebugHandlersFactory> factories, HotSpotProviders providers, TargetDescription target) {
 111             super(options, factories, providers, providers.getSnippetReflection(), target);
 112         }
 113 
 114         public void lower(ProfileNode profileNode, LoweringTool tool) {
 115             StructuredGraph graph = profileNode.graph();
 116             LoadMethodCountersNode counters = graph.unique(new LoadMethodCountersNode(profileNode.getProfiledMethod()));
 117             ConstantNode step = ConstantNode.forInt(profileNode.getStep(), graph);
 118             ConstantNode stepLog = ConstantNode.forInt(CodeUtil.log2(profileNode.getStep()), graph);
 119 
 120             if (profileNode instanceof ProfileBranchNode) {
 121                 // Backedge event
 122                 ProfileBranchNode profileBranchNode = (ProfileBranchNode) profileNode;
 123                 SnippetInfo snippet = profileBranchNode.hasCondition() ? profileConditionalBackedge : profileBackedge;
 124                 Arguments args = new Arguments(snippet, graph.getGuardsStage(), tool.getLoweringStage());
 125                 ConstantNode bci = ConstantNode.forInt(profileBranchNode.bci(), graph);
 126                 ConstantNode targetBci = ConstantNode.forInt(profileBranchNode.targetBci(), graph);
 127                 args.add("counters", counters);
 128                 args.add("step", step);
 129                 args.add("stepLog", stepLog);
 130                 args.addConst("freqLog", profileBranchNode.getNotificationFreqLog());
 131                 if (profileBranchNode.hasCondition()) {
 132                     args.add("branchCondition", profileBranchNode.branchCondition());
 133                 }
 134                 args.add("bci", bci);
 135                 args.add("targetBci", targetBci);
 136 
 137                 SnippetTemplate template = template(profileNode, args);
 138                 template.instantiate(providers.getMetaAccess(), profileNode, DEFAULT_REPLACER, args);
 139             } else if (profileNode instanceof ProfileInvokeNode) {
 140                 ProfileInvokeNode profileInvokeNode = (ProfileInvokeNode) profileNode;
 141                 // Method invocation event
 142                 Arguments args = new Arguments(profileMethodEntry, graph.getGuardsStage(), tool.getLoweringStage());
 143                 args.add("counters", counters);
 144                 args.add("step", step);
 145                 args.add("stepLog", stepLog);
 146                 args.addConst("freqLog", profileInvokeNode.getNotificationFreqLog());
 147                 SnippetTemplate template = template(profileNode, args);
 148                 template.instantiate(providers.getMetaAccess(), profileNode, DEFAULT_REPLACER, args);
 149             } else {
 150                 throw new GraalError("Unsupported profile node type: " + profileNode);
 151             }
 152 
 153             assert profileNode.hasNoUsages();
 154             if (!profileNode.isDeleted()) {
 155                 GraphUtil.killWithUnusedFloatingInputs(profileNode);
 156             }
 157         }
 158     }
 159 }