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