Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get the python call stack with the linux perf?

Tags:

python

linux

perf

For example,

    def test():         print "test" 

I used perf record -g -p $pid, but the result was just all about PyEval_EvalFrameEx. How can I get the real name "test" or if can not by using perf?

like image 773
wuwl Avatar asked Nov 13 '14 06:11

wuwl


People also ask

What is perf in Python?

Description. This perf script option is used to process perf script data using perf's built-in Python interpreter. It reads and processes the input file and displays the results of the trace analysis implemented in the given Python script, if any.

What is current call stack in Python?

The Python interpreter uses a call stack to run a Python program. When a function is called in Python, a new frame is pushed onto the call stack for its local execution, and every time a function call returns, its frame is popped off the call stack.

What is perf event?

perf_events provides a command line tool, perf, and subcommands for various profiling activities. This is a single interface for the different instrumentation frameworks that provide the various events. The perf command alone will list the subcommands; here is perf version 4.10 (for the Linux 4.10 kernel):


Video Answer


2 Answers

As of 2018, perf simply doesn't have support for reading the Python stack frames (cf. a 2014 Python mailinglist discussion).

Python 3.6 has some support for Dtrace and Systemtap.

An alternative to this is Pyflame, a stochastic profiler for Python that samples python call stacks via ptrace(). In contrast to Dtrace/Systemtap you don't need extra permissions and it also works with Python versions that are compiled without instrumentalization support.

When you use the --threads option with Pyflame you see Python lines that call into C/C++ extensions, although the stack-trace stops at the last Python frame. But perhaps this is sufficient for your use case.

Edit: Pyflame was abandoned in the end of 2019 or so. A hacker news thread mentions the following alternatives:

  • https://github.com/P403n1x87/austin
  • https://github.com/benfred/py-spy
like image 185
maxschlepzig Avatar answered Sep 28 '22 04:09

maxschlepzig


You won't be able to do this with perf, that's specifically built to interface the Linux process model, decode those stack frames, etc. It's doing what it's supposed to by telling you that it was executing the function PyEval_EvalFrameEx. It would have to be extended with python-specific information to be able to actually decode Python's frame information, which isn't going to happen. Unfortunately I haven't found a really good way to debug both Python and C/C++ modules easily. It's generally pdb for one and gdb for the other.

like image 37
gct Avatar answered Sep 28 '22 05:09

gct