Instrumentation with snippets
Doug Simon
doug.simon at oracle.com
Mon Jun 17 09:05:48 PDT 2013
Hi Chris,
Snippets (as opposed to method substitutions) are currently only used during lowering. So one thing you could do is replace the lowering in HotSpotRuntime.lower() for LoadIndexedNode and StoreIndexedNode to use your snippets instead. However, this means you need to replicate the correct semantics for these operations in your snippet in addition to the instrumentation code. I think a better alternative would be to insert an InvokeNode before/after the array access node that takes the same inputs of the array access and calls a well known (static) instrumentation method.
It might be instructive to look at MonitorSnippets.checkBalancedMonitors() for an example of inserting an invoke node.
-Doug
On Jun 17, 2013, at 5:02 PM, Chris Atkin-Granville <me at chrisatk.in> wrote:
> Hey guys,
>
> I'm trying to instrument memory operations using snippets, but something is going wrong. At the moment I'm focussing on StoreIndexedNode and LoadIndexedNode - array access operations. To be clear I don't want to replace the behaviour associated with these nodes - just augment existing functionality to basically log each operation (but I haven't got to actually writing the instrumentation yet).
>
> What I have at the moment is a new phase that I've added at PhasePosition.HIGH_LEVEL:
>
> public class ArrayAccessInstrumentationPhase extends Phase {
>
> private ArrayAccessSnippets.Templates snippets;
>
> public ArrayAccessInstrumentationPhase(CodeCacheProvider runtime, Replacements replacements, TargetDescription target) {
> snippets = new ArrayAccessSnippets.Templates(runtime, replacements, target);
> }
>
> @Override
> protected void run(StructuredGraph graph) {
> for (Node node: graph.getNodes()) {
> if (node instanceof StoreIndexedNode)
> snippets.lower((StoreIndexedNode) node);
>
> if (node instanceof LoadIndexedNode)
> snippets.lower((LoadIndexedNode) node);
> }
> }
>
> }
>
> And the snippets:
>
> public class ArrayAccessSnippets implements Snippets {
>
> @Snippet
> public static StoreIndexedNode arrayIndexStore(StoreIndexedNode n) {
> // TODO: add instrumentation here
> return n;
> }
>
> @Snippet
> public static LoadIndexedNode arrayIndexLoad(LoadIndexedNode n) {
> // TODO: add instrumentation here
> return n;
> }
>
> public static class Templates extends AbstractTemplates {
>
> private final SnippetInfo load = snippet(ArrayAccessSnippets.class, "arrayIndexLoad");
> private final SnippetInfo store = snippet(ArrayAccessSnippets.class, "arrayIndexStore");
>
> public Templates(CodeCacheProvider runtime, Replacements replacements, TargetDescription target) {
> super(runtime, replacements, target);
> }
>
> public void lower(final StoreIndexedNode node) {
> Arguments args = new Arguments(store) {{
> add("n", node);
> }};
>
> template(args).instantiate(runtime, node, DEFAULT_REPLACER, args);
> }
>
> public void lower(final LoadIndexedNode node) {
> Arguments args = new Arguments(load) {{
> add("n", node);
> }};
>
> template(args).instantiate(runtime, node, DEFAULT_REPLACER, args);
> }
> }
> }
>
> However this causes Java to run out of heap space (java.lang.OutOfMemoryError) - I think because the JVM gets into an "infinite replacement" spiral?
>
> At any rate, I see in HotSpotRuntime:lower() there's behaviour associated with StoreIndexedNode (line 582) and LoadIndexedNode (line 574) so I tried to move that over into my snippet - but that didn't work because I couldn't see any way to get access to a LoweringToolImpl from within my snippets.
>
> Anybody have any thoughts of how I can achieve this access instrumentation?
>
> Thanks, Chris
>
> (P.S. Not sure what went wrong with my last email!)
More information about the graal-dev
mailing list