Tuesday, March 8, 2011

Profiling Perl with SystemTap on Linux

Perl gained some support for Sun's DTrace profiling system in version 5.10, and 5.12 added support for SystemTap, albeit only via systemtap's dtrace compatibility layer.

I tried using the Linux port of dtrace, but it just hung my machine, so I wanted to try SystemTap instead.

I struggled to find *any* documentation on how to do this with Perl though, so here are my notes on eventually getting it all working.
I'm still struggling to get custom marks in your Perl apps to work though, they seem too closely tied to dtrace's architecture.

These notes apply to Ubuntu Maverick 10.10 64bit.


1)
Grab kernel files from this PPA:
https://launchpad.net/~speijnik/+archive/utrace-kernel
(You'll need both the linux-image and linux-headers)

2)
sudo aptitude install systemtap systemtap-sdt-dev systemtap-doc

2b)
Note, I think I needed to run this too..
sudo make -C /usr/share/systemtap/runtime/uprobes

3)
Add your user to the stapdev and stapusr groups:
sudo usermod -a -G stapdev,stapusr tobyc

4)
Install a custom perl with -Dusedtrace enabled
eg. perlbrew install perl-5.12.3 -D usedtrace -D usethreads

5)
Create perl.stp containing:

probe process("perl").mark("sub__entry") {
printf("%s: pid %d, uid %d\n", execname(), pid(), uid())
printf("-> %s (%s:%d)\n", user_string($arg1), user_string($arg2), $arg3)
}
probe process("perl").mark("sub__return") {
printf("<- %s\n", user_string($arg1))
}


6)
Run this command in one window:
stap perl.stp
and then in another window, run a Perl app.. (Make sure it runs using the custom Perl you built in step 4 though!)
You should see info being dumped about subroutines being entered and left..

No comments:

Post a Comment