Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to source annotate python when using qcachegrind to process profilestats output

Kcachegrind serves as a wonderful utility to visually represent the hotspot to the source line level when profiling code. I found it pretty useful when micro optimizing my C++ code base. For my latest python project I started using Kcachegrind to process the output from profilestats. Kcachegrind is a linux only utility but various unofficial ports are available and one I am using is qcachegrind. Generally it works to a large extent and suffices for most issues except I am having a hard time getting the source annotation work.

On the source Tab I am being greeted with the familiar source missing message

There is no source available for the following function:
   'main C:\Projects\module\src\source.py:397'
This is because no debug information is present
Recompile source and redo the profile run.
The function is located in the ELF Object:
  '(unknown)'

Using the option

Settings -> Configure -> Source Annotation 

and adding the Source Base Directory was not useful.

I have a feeling that the utility wants an ELF Object which is not relevant for Python. Any help in this regard would be useful.

Relevant Information:

  • Python 2.7
  • profilestats (2.0)
  • QCachegrind 0.7.4
  • Windows 2012R2
like image 548
Abhijit Avatar asked Jun 29 '15 06:06

Abhijit


1 Answers

I second @taleniat comment. I am an OSX user. I was having some trouble getting qcachegrind to work so I ended up using pyprof2calltree and it works perfectly, source code tab included. YMMV.

First run your script with cProfile

python -m cProfile -o report.profile report.py

Then you can use pyprof2calltree to launch qcachegrind (no need for intermediate conversion).

pyprof2calltree -k -i report.profile

by the way, Python 2.7.10 and qcachegrind 0.7.4 installed via homebrew on OSX 10.11

like image 101
Josep Valls Avatar answered Oct 07 '22 22:10

Josep Valls