Instrumentation with snippets
Chris Atkin-Granville
me at chrisatk.in
Mon Jun 17 08:02:40 PDT 2013
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