benfred / py-spy

Sampling profiler for Python programs
MIT License
12.13k stars 401 forks source link

Option to get native thread ID with best effort #625

Open ErnestChan opened 8 months ago

ErnestChan commented 8 months ago

When running py-spy with --native sometimes I get failed to get os threadid. See related issue. I'm running this on bare-metal Ubuntu servers.

It would be great to have the option to not error out when py-spy fails to get the thread id. Maybe modify this line so when the option is set we log an exception, or add a fake stack trace that says py-spy failed to get the thread ID

benfred commented 8 months ago

With the --native option, we need to be able to get a mapping between pthread_id and the OS thread_id - so that we can join the native stack (which we have the OS thread_id for) with the python stack (which we have the pthread_id for). If we fail to get the os threadid, there isn't enough information to do this -

What version of python and ubuntu are you running? This code should work to get the os threadid on ubuntu (aside from in cases like profiling from the host os into a docker container - which doesn't sound like it applies to you).

ErnestChan commented 8 months ago

Thanks for the quick reply! This is on Ubuntu 22.04.1 LTS profiling python3.8. If the code should work on Ubuntu do you have pointers on how to debug why it sometimes fails?

As for joining pthread_id and OS thread_id, for us we're ok with missing some native thread stack traces. We'd rather get some native traces than none, which is why a best effort option with --native would be nice.