ddaa bbloggs: /python/lsprof-calltree

lsprof meets KCachegrind

You can now visualize the output of the lsprof Python profiler using KCachegrind, an excellent visualization tool for profiling data.

One useful feature of the hotshot Python profiler is the existence of the hotshot2calltree conversion filter, that produces output suitable for KCachegrind. So after the s/hotshot/lsprof thread occured on the python-devel mailing list, and that Bazaar-NG added support for lsprof, I was itching to use KCachegrind with lsprof data.

In good libre software style, I eventually scratched my itch and wrote a patch to add calltree support to lsprof. The lsprof maintainers are welcome to apply it.

With lsprof and KCachegrind I was able to quickly identify a performance bug in Bazaar-NG that should be easy to fix. The data generated by hotshot completely misses that issue, at least when displayed by KCachegrind. I am not sure why.

One thing I know for sure is that hotshot2calltree has a serious bug. It causes KCachegrind to confuse multiple functions with the same name and from the same Python file. That completely messes up the call graph and greatly reduces the pertinence of the data.

Specifically, the Bazaar-NG code uses two decorators, needs_read_lock and needs_write_lock, defined in the same module, which return closures of local functions called decorated. Each decorator uses a different decorated function, but hostshot2calltree produces data that causes KCachegrind to only see one decorated function. In the example I'm looking at, it's the one in the needs_write_lock decorator.

KCachegrind seems to make C/C++ assumptions, and seems not to expect two different functions with the same name and in the same file. My lsprof patch works around the issue by including the Python module names and line numbers into the function names in the calltree output. Incidentally, that also makes for more informative names.

27 Jan. 2006 lsprof meets KCachegrind (5 comments)

Posted by Zooko at Tue Jan 17 17:19:31 2006

I'm glad to hear it!  This sounds like good work.  But you haven't completed the job until you've:

1.  Submitted the patch to the lsprof maintainers.  (Just mentioning on your blog that they are welcome to it doesn't count.)
2.  Reported the bug in hotshot to the Python maintainers.

Thanks!

--Zooko

Posted by Zooko at Wed Feb 22 03:26:26 2006

So, have you submitted the patch to the lsprof maintainers yet?

Posted by David Allouche at Wed Feb 22 23:00:14 2006

Just after writing it, I mentioned it on in #pypy channel on freenode, but I have not come around making a formal patch submission. For starters it was not obvious to me whom to send the patch.

I have also spent a week-end trying to collect useful profiling information on simple python snippets to write a post to python-dev, but I made some very disturbing measurements and had trouble analying the systematic biases introduced by profilers.

I should definitely go back to it, but I'm on a crunch at the moment so I lack spare cycles.

Posted by Jean-Paul Calderone at Sun Mar 5 03:33:23 2006

Itamar and I updated your patch to work with the cProfile module in current Python svn trunk@HEAD.  It is now a separate module which just defines a new class that takes a cProfile.Profile instance and writes it stats out to a file.  We also fixed a few places where cProfile now seems to hand out strings that the code was not expecting.

The code is at <http://divmod.org/users/exarkun/lsprofcalltree.py>.  BTW, the KCacheGrind package in Debian includes hotshot2calltree.  It'd be cool to have this in that package as well.

Also, re systematic biases, we noticed some timing fixes to cProfile in current trunk.  If you get back to profiling, you might want to check out that version.

Posted by Johan Dahlin at Wed May 17 22:05:01 2006

Another improvement small improvement over Jean-Paul's and Itamar's script can be found here, http://www.gnome.org/~johan/lsprofcalltree.py
It makes the script usable as a standalone module.