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