2007년 6월 4일 월요일

pyprof: Mono profiler for IronPython

CPython distribution comes with debuggers and profilers. More importantly, it comes with hooks so that you can write your own debuggers and profilers. You can set these hooks by built-in functions sys.settrace and sys.setprofile.

Here is what Python Library Reference says about sys.settrace (link):

settrace(tracefunc)

Set the system's trace function, which allows you to implement a Python source code debugger in Python. See section 24.2, "How It Works," in the chapter on the Python debugger. The function is thread-specific; for a debugger to support multiple threads, it must be registered using settrace() for each thread being debugged. Note: The settrace() function is intended only for implementing debuggers, profilers, coverage tools and the like. Its behavior is part of the implementation platform, rather than part of the language definition, and thus may not be available in all Python implementations.


In other words, another Python implementation like IronPython is within its own right not to implement these functions. As a matter of fact:

IronPython 1.1 (1.1) on .NET 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
>>> sys.settrace(f)
Traceback (most recent call last):
NotImplementedError: sys.settrace is not yet supported by IronPython
>>> sys.setprofile(f)
Traceback (most recent call last):
AttributeError: 'module' object has no attribute 'setprofile'


Don't get this wrong, this is fully okay. Both .NET Framework and Mono have their set of tools to do debugging, profiling, tracing, and coverage, often better than what CPython provides.

But when you debug a Python program, usually you don't need to single step inside Python runtime. When you trace a Python program, you don't necessarily want to know how a single line of Python code expands to 20 low-level function calls. So it's desirable to restrict tools to Python methods only.

Enter pyprof.

pyprof is a Mono profiler for IronPython. Mono provides an interface to write custom profilers, and there are quite some of them. pyprof uses this API to query a custom attribute, IronPython.Runtime.PythonModuleAttribute from method's defining assembly. Messages are printed only if the attribute is present.

The code is here:
https://fepy.svn.sourceforge.net/svnroot/fepy/trunk/pyprof/

Type "make test" to see what it can do. It should print the output in test.out file. (This assumes that you have blank site.py. The output will differ if you don't.)

Disclaimer: pyprof is a less-than-a-day old software. Don't expect much. But I do have great plans for this baby. :-)

댓글 1개:

Michael Foord :

Congratulations. Don't forget about us poor folk using .NET though. :-)