py-spy icon indicating copy to clipboard operation
py-spy copied to clipboard

Add --syscalls option for tracing syscalls (linux only)

Open manuels opened this issue 4 years ago • 6 comments
trafficstars

This commit adds a new option (--syscalls/-S) to trace what time your python programm spends in kernel space. It is only supported on linux where current syscall can be determined using /proc/{pid}/task/{tid}/syscall. --syscalls implies --idle

manuels avatar Jul 14 '21 16:07 manuels

Example output of top:

  %Own   %Total  OwnTime  TotalTime  Function (filename:line)                                   
 99.00%  99.00%   67.60s    67.60s   select syscall (<kernel>)
  1.00%   1.00%   0.200s    0.200s   write syscall (<kernel>)
  0.00%   1.00%   0.130s    0.390s   x (x.py:5)
  0.00%   0.00%   0.060s    0.060s   brk syscall (<kernel>)
  0.00%   0.00%   0.010s    0.010s   x (x.py:3)
  0.00%  99.00%   0.000s    67.60s   x (x.py:4)
  0.00% 100.00%   0.000s    68.00s   <module> (x.py:7)

manuels avatar Jul 14 '21 19:07 manuels

Nice!

I had something similar in mind a while back - to add /proc/pid/stack entries as traces (so you have kernel stacks in addition to the Python stacks). It's simpler (because the kernel does symbolication for you) but requires root IIRC (unlike /proc/pid/syscall).

Also, I'm not sure it's useful unless you are in --nonblocking mode - otherwise, the kernel stack would be "stopped in ptrace".

Jongy avatar Jul 14 '21 19:07 Jongy

Yes, my first idea was to add /proc/pid/stack, too, but then I thought that a programmer that only sees the python stacktrace (and not the c stacktrace of cpython) is probably barely interested in the internals of the kernel ;)

manuels avatar Jul 14 '21 20:07 manuels

Hey, @benfred, let me know if there is anything in this pull request that I should change if you are not happy with the current state. :D

manuels avatar Jul 25 '21 15:07 manuels

Hey, @benfred is there anything else I can do to have this merged?

manuels avatar Sep 22 '21 15:09 manuels

Hey, @benfred, maybe you missed my last message. Is there anything else I can do to have this merged?

manuels avatar Mar 29 '22 08:03 manuels