This was very cool, adding a profiler using MH

Mark Roos mroos at roos.com
Thu Feb 16 20:43:14 PST 2012


So my code runs slow ( faster now with 23b15 by 2x but still 2x too slow) 
and I was wondering how to look for the issue.
I was going to look for a java profiler but then I remembered a blog by 
Rémi on a code coverage monitor
using method handles.  So I decided to write my own profiler.

The key code is below and it shows the power of methodHandles is ways one 
may not expect.  And it
added almost nothing to the run time ( maybe a few %).  It inserts itself 
into each callsite target and
collect entry and exit data.

pretty cool
thanks for the toolset

mark
        by the was 'this' is the callSite,  and this handles all airities
      // now add the profile stuff collect number of calls and the entry 
and exit times
      if( RtDebugger._profileEnable){
        mt=MethodType.methodType(RtObject[].class, RtCallSite.class, 
RtObject[].class);
        try {
          profileEntry = lookup.findStatic(RtDebugger.class, 
"profileEntry", mt); 
        }
        catch (Throwable e) {
          e.printStackTrace();
        } 
          profileEntry = MethodHandles.insertArguments(profileEntry, 0, 
this);
        // I have to collect the invoker, bind the call site to the entry 
filter
        // filter the invoker and then spread the invoker 
        invoker = invoker.asSpreader(RtObject[].class, _airity);
        invoker = MethodHandles.filterArguments(invoker , 0, 
profileEntry);
        invoker = invoker.asCollector(RtObject[].class, _airity);
      // now the profile exit which collects exit time
        mt=MethodType.methodType(RtObject.class, RtCallSite.class, 
RtObject.class);
        try {
          profileExit = lookup.findStatic(RtDebugger.class, "profileExit", 
mt); 
        }
        catch (Throwable e) {
          e.printStackTrace();
        } 
        profileExit = MethodHandles.insertArguments(profileExit, 0, this);
        invoker = MethodHandles.filterReturnValue(invoker , profileExit);
      }

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/mlvm-dev/attachments/20120216/3b6d317c/attachment.html 


More information about the mlvm-dev mailing list