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;
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
|
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.backedgeCounterOffset;
29 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.invocationCounterIncrement;
30 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.invocationCounterOffset;
31 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.invocationCounterShift;
32 import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.SLOW_PATH_PROBABILITY;
33 import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.probability;
34 import static org.graalvm.compiler.replacements.SnippetTemplate.DEFAULT_REPLACER;
35
36 import org.graalvm.compiler.api.replacements.Snippet;
37 import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter;
38 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
39 import org.graalvm.compiler.debug.DebugHandlersFactory;
40 import org.graalvm.compiler.debug.GraalError;
41 import org.graalvm.compiler.graph.Node.ConstantNodeParameter;
42 import org.graalvm.compiler.graph.Node.NodeIntrinsic;
43 import org.graalvm.compiler.hotspot.HotSpotBackend;
44 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
45 import org.graalvm.compiler.hotspot.nodes.aot.LoadMethodCountersNode;
46 import org.graalvm.compiler.hotspot.nodes.profiling.ProfileBranchNode;
47 import org.graalvm.compiler.hotspot.nodes.profiling.ProfileInvokeNode;
48 import org.graalvm.compiler.hotspot.nodes.profiling.ProfileNode;
49 import org.graalvm.compiler.hotspot.word.MethodCountersPointer;
50 import org.graalvm.compiler.nodes.ConstantNode;
51 import org.graalvm.compiler.nodes.StructuredGraph;
58 import org.graalvm.compiler.replacements.SnippetTemplate.Arguments;
59 import org.graalvm.compiler.replacements.SnippetTemplate.SnippetInfo;
60 import org.graalvm.compiler.replacements.Snippets;
61
62 import jdk.vm.ci.code.CodeUtil;
63 import jdk.vm.ci.code.TargetDescription;
64
65 public class ProfileSnippets implements Snippets {
66 @NodeIntrinsic(ForeignCallNode.class)
67 public static native void methodInvocationEvent(@ConstantNodeParameter ForeignCallDescriptor descriptor, MethodCountersPointer counters);
68
69 @Snippet
70 protected static int notificationMask(int freqLog, int stepLog) {
71 int stepMask = (1 << stepLog) - 1;
72 int frequencyMask = (1 << freqLog) - 1;
73 return frequencyMask & ~stepMask;
74 }
75
76 @Snippet
77 public static void profileMethodEntry(MethodCountersPointer counters, int step, int stepLog, @ConstantParameter int freqLog) {
78 int counterValue = counters.readInt(invocationCounterOffset(INJECTED_VMCONFIG)) + invocationCounterIncrement(INJECTED_VMCONFIG) * step;
79 counters.writeInt(invocationCounterOffset(INJECTED_VMCONFIG), counterValue);
80 if (freqLog >= 0) {
81 final int mask = notificationMask(freqLog, stepLog);
82 if (probability(SLOW_PATH_PROBABILITY, (counterValue & (mask << invocationCounterShift(INJECTED_VMCONFIG))) == 0)) {
83 methodInvocationEvent(HotSpotBackend.INVOCATION_EVENT, counters);
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 profileBackedge(MethodCountersPointer counters, int step, int stepLog, @ConstantParameter int freqLog, int bci, int targetBci) {
93 int counterValue = counters.readInt(backedgeCounterOffset(INJECTED_VMCONFIG)) + invocationCounterIncrement(INJECTED_VMCONFIG) * step;
94 counters.writeInt(backedgeCounterOffset(INJECTED_VMCONFIG), counterValue);
95 final int mask = notificationMask(freqLog, stepLog);
96 if (probability(SLOW_PATH_PROBABILITY, (counterValue & (mask << invocationCounterShift(INJECTED_VMCONFIG))) == 0)) {
97 methodBackedgeEvent(HotSpotBackend.BACKEDGE_EVENT, counters, bci, targetBci);
98 }
99 }
100
101 @Snippet
102 public static void profileConditionalBackedge(MethodCountersPointer counters, int step, int stepLog, @ConstantParameter int freqLog, boolean branchCondition, int bci, int targetBci) {
103 if (branchCondition) {
104 profileBackedge(counters, step, stepLog, freqLog, bci, targetBci);
105 }
106 }
107
108 public static class Templates extends AbstractTemplates {
109 private final SnippetInfo profileMethodEntry = snippet(ProfileSnippets.class, "profileMethodEntry");
110 private final SnippetInfo profileBackedge = snippet(ProfileSnippets.class, "profileBackedge");
111 private final SnippetInfo profileConditionalBackedge = snippet(ProfileSnippets.class, "profileConditionalBackedge");
112
113 public Templates(OptionValues options, Iterable<DebugHandlersFactory> factories, HotSpotProviders providers, TargetDescription target) {
114 super(options, factories, providers, providers.getSnippetReflection(), target);
115 }
116
|