A friend and I have written a Python profile viewer called SnakeViz that runs in a web browser. If you are already successfully using RunSnakeRun SnakeViz may not add that much value, but SnakeViz is much easier to install.
Edit: SnakeViz supports Python 2 and 3 and works on all major systems.
Python Call Graph generates pics very similar to those in maxy’s answer. It also shows total time for each function, for some reason it’s not reflected in the example graphs.
I’ve written a browser-based visualization tool, profile_eye, which operates on the output of gprof2dot.
gprof2dot is great at grokking many profiling-tool outputs, and does a great job at graph-element placement. The final rendering is a static graphic, which is often very cluttered.
Using d3.js it’s possible to remove much of that clutter, through relative fading of unfocused elements, tooltips, and a fisheye distortion.
由于以下原因,获取Python解释器的内存地址可能有点棘手Address Space Layout Randomization如果目标python解释器附带符号,那么通过取消引用interp_head或_PyRuntime变量取决于Python版本。但是,许多Python版本都附带了剥离的二进制文件,或者在Windows上没有相应的PDB符号文件。在这些情况下,我们扫描BSS部分,查找看起来可能指向有效PyInterpreterState的地址,并检查该地址的布局是否符合我们的预期
在Linux上,默认配置是在附加到非子进程时需要root权限。对于py-spy,这意味着您可以通过让py-spy创建进程(py-spy record -- python myprogram.py),但是通过指定PID附加到现有进程通常需要root(sudo py-spy record --pid 123456)。您可以通过在Linux上设置ptrace_scope sysctl variable
如何检测线程是否空闲?
PY-SPY尝试仅包括来自活动运行代码的线程的堆栈跟踪,并排除休眠或空闲的线程。如果可能,py-spy尝试从OS获取此线程活动信息:通过读入/proc/PID/stat在Linux上,通过使用machthread_basic_info调用OSX,并通过查看当前系统调用是否known to be
idle在Windows上