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 ProbabilisticProfileSnippets implements Snippets { 58 @Snippet 59 public static boolean shouldProfile(@ConstantParameter int probLog, int random) { 60 int probabilityMask = (1 << probLog) - 1; 61 return (random & probabilityMask) == 0; 62 } 63 64 @Snippet 65 public static int notificationMask(int freqLog, int probLog) { 66 int probabilityMask = (1 << probLog) - 1; 67 int frequencyMask = (1 << freqLog) - 1; 68 return frequencyMask & ~probabilityMask; 69 } 70 71 @NodeIntrinsic(ForeignCallNode.class) 72 public static native void methodInvocationEvent(@ConstantNodeParameter ForeignCallDescriptor descriptor, MethodCountersPointer counters); 73 74 @Snippet 75 public static void profileMethodEntryWithProbability(MethodCountersPointer counters, int random, @ConstantParameter int freqLog, @ConstantParameter int probLog) { 76 if (probability(1.0 / (1 << probLog), shouldProfile(probLog, random))) { 77 int counterValue = counters.readInt(config(INJECTED_VMCONFIG).invocationCounterOffset) + (config(INJECTED_VMCONFIG).invocationCounterIncrement << probLog); 78 counters.writeInt(config(INJECTED_VMCONFIG).invocationCounterOffset, counterValue); 79 if (freqLog >= 0) { 80 int mask = notificationMask(freqLog, probLog); 81 if (probability(SLOW_PATH_PROBABILITY, (counterValue & (mask << config(INJECTED_VMCONFIG).invocationCounterShift)) == 0)) { 82 methodInvocationEvent(HotSpotBackend.INVOCATION_EVENT, counters); 83 } 84 } 85 } 86 } 87 88 @NodeIntrinsic(ForeignCallNode.class) 89 public static native void methodBackedgeEvent(@ConstantNodeParameter ForeignCallDescriptor descriptor, MethodCountersPointer counters, int bci, int targetBci); 90 91 @Snippet 92 public static void profileBackedgeWithProbability(MethodCountersPointer counters, int random, @ConstantParameter int freqLog, @ConstantParameter int probLog, int bci, int targetBci) { 93 if (probability(1.0 / (1 << probLog), shouldProfile(probLog, random))) { 94 int counterValue = counters.readInt(config(INJECTED_VMCONFIG).backedgeCounterOffset) + (config(INJECTED_VMCONFIG).invocationCounterIncrement << probLog); 95 counters.writeInt(config(INJECTED_VMCONFIG).backedgeCounterOffset, counterValue); 96 int mask = notificationMask(freqLog, probLog); 97 if (probability(SLOW_PATH_PROBABILITY, (counterValue & (mask << config(INJECTED_VMCONFIG).invocationCounterShift)) == 0)) { 98 methodBackedgeEvent(HotSpotBackend.BACKEDGE_EVENT, counters, bci, targetBci); 99 } 100 } 101 } 102 103 @Snippet 104 public static void profileConditionalBackedgeWithProbability(MethodCountersPointer counters, int random, @ConstantParameter int freqLog, @ConstantParameter int probLog, boolean branchCondition, 105 int bci, int targetBci) { 106 if (branchCondition) { 107 profileBackedgeWithProbability(counters, random, freqLog, probLog, bci, targetBci); 108 } 109 } 110 111 public static class Templates extends AbstractTemplates { 112 private final SnippetInfo profileMethodEntryWithProbability = snippet(ProbabilisticProfileSnippets.class, "profileMethodEntryWithProbability"); 113 private final SnippetInfo profileBackedgeWithProbability = snippet(ProbabilisticProfileSnippets.class, "profileBackedgeWithProbability"); 114 private final SnippetInfo profileConditionalBackedgeWithProbability = snippet(ProbabilisticProfileSnippets.class, "profileConditionalBackedgeWithProbability"); 115 116 public Templates(HotSpotProviders providers, TargetDescription target) { 117 super(providers, providers.getSnippetReflection(), target); 118 } 119 120 public void lower(ProfileNode profileNode, LoweringTool tool) { 121 assert profileNode.getRandom() != null; 122 123 StructuredGraph graph = profileNode.graph(); 124 LoadMethodCountersNode counters = graph.unique(new LoadMethodCountersNode(profileNode.getProfiledMethod())); 125 126 if (profileNode instanceof ProfileBranchNode) { 127 // Backedge event 128 ProfileBranchNode profileBranchNode = (ProfileBranchNode) profileNode; 129 SnippetInfo snippet = profileBranchNode.hasCondition() ? profileConditionalBackedgeWithProbability : profileBackedgeWithProbability; 130 Arguments args = new Arguments(snippet, graph.getGuardsStage(), tool.getLoweringStage()); 131 ConstantNode bci = ConstantNode.forInt(profileBranchNode.bci(), graph); 132 ConstantNode targetBci = ConstantNode.forInt(profileBranchNode.targetBci(), graph); 133 args.add("counters", counters); 134 args.add("random", profileBranchNode.getRandom()); 135 args.addConst("freqLog", profileBranchNode.getNotificationFreqLog()); 136 args.addConst("probLog", profileBranchNode.getProbabilityLog()); 137 if (profileBranchNode.hasCondition()) { 138 args.add("branchCondition", profileBranchNode.branchCondition()); 139 } 140 args.add("bci", bci); 141 args.add("targetBci", targetBci); 142 143 SnippetTemplate template = template(args); 144 template.instantiate(providers.getMetaAccess(), profileNode, DEFAULT_REPLACER, args); 145 } else if (profileNode instanceof ProfileInvokeNode) { 146 ProfileInvokeNode profileInvokeNode = (ProfileInvokeNode) profileNode; 147 // Method invocation event 148 Arguments args = new Arguments(profileMethodEntryWithProbability, graph.getGuardsStage(), tool.getLoweringStage()); 149 args.add("counters", counters); 150 args.add("random", profileInvokeNode.getRandom()); 151 args.addConst("freqLog", profileInvokeNode.getNotificationFreqLog()); 152 args.addConst("probLog", profileInvokeNode.getProbabilityLog()); 153 SnippetTemplate template = template(args); 154 template.instantiate(providers.getMetaAccess(), profileNode, DEFAULT_REPLACER, args); 155 } else { 156 throw new GraalError("Unsupported profile node type: " + profileNode); 157 } 158 159 assert profileNode.hasNoUsages(); 160 if (!profileNode.isDeleted()) { 161 GraphUtil.killWithUnusedFloatingInputs(profileNode); 162 } 163 } 164 } 165 }