I've learned. I'll share.

October 14, 2008

How to DTrace Python in OSX

DTrace is an incredible tool. It basically lets you do profiling of a live application with no performance penatly. I'm writing a Python that needed some profiling, and I found the "normal" techniques like the profile/cProfile module very lacking. Luckily, Mac OSX comes with DTrace and it even works with Python. The only snag is that it's hard to find how to use the darn thing. I finally figured it out, so I figured it pass on the knowledge.

So, here's how you use dtrace on your python application in Mac OSX:

  1. Get DTraceToolkit.
  2. Edit Python/py_cputime.d by replacing "function-entry" with "entry" and "function-return" with "exit".
  3. Call "sudo dtrace -s Python/py_cputime.d"
  4. Let it sit there a while and hit ctrl-c.
  5. Enjoy the results

I can only assume you have to edit the file because of some difference between Solaris and OSX. You can try files other than py_cputime.d, but you might have to edit them too. Not all of them work, but most do.

The last thing to know is that you have to use the python that comes with OSX. A custom-built python doesn't seem to work.

Hope that helps!

3 comments:

  1. Hi,

    this looks interesting but I can't make it work - I just get "dtrace: no probes specified". I tried passing a python script as an argument, but nothing happens - anything I'm missing?

    ReplyDelete
  2. It appears that you no longer need to edit py_cputime.d under Snow Leopard...

    ReplyDelete
  3. Hmmm
    dtrace: error on enabled probe ID 7 (ID 26198: python47:Python:PyEval_EvalFrameEx:function-return): invalid address

    ReplyDelete

Blog Archive

Google Analytics