Python support for the Linux perf profiler

作者 :

Pablo Galindo

The Linux perf profiler is a very powerful tool that allows you to profile and obtain information about the performance of your application. perf also has a very vibrant ecosystem of tools that aid with the analysis of the data that it produces.

The main problem with using the perf profiler with Python applications is that perf only gets information about native symbols, that is, the names of functions and procedures written in C. This means that the names and file names of Python functions in your code will not appear in the output of perf .

Since Python 3.12, the interpreter can run in a special mode that allows Python functions to appear in the output of the perf profiler. When this mode is enabled, the interpreter will interpose a small piece of code compiled on the fly before the execution of every Python function and it will teach perf the relationship between this piece of code and the associated Python function using perf map files .

注意

支持 perf profiler is currently only available for Linux on select architectures. Check the output of the configure build step or check the output of python -m sysconfig | grep HAVE_PERF_TRAMPOLINE to see if your system is supported.

For example, consider the following script:

def foo(n):
    result = 0
    for _ in range(n):
        result += 1
    return result
def bar(n):
    foo(n)
def baz(n):
    bar(n)
if __name__ == "__main__":
    baz(1000000)
									

We can run perf to sample CPU stack traces at 9999 hertz:

$ perf record -F 9999 -g -o perf.data python my_script.py