Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include libpython symbols in profile info #437

Open
anntzer opened this issue Aug 18, 2021 · 5 comments
Open

Include libpython symbols in profile info #437

anntzer opened this issue Aug 18, 2021 · 5 comments

Comments

@anntzer
Copy link
Contributor

anntzer commented Aug 18, 2021

AFAICT, when profiling native extensions, py-spy includes symbols from third-party shared objects, but not from libpython (PyFoo_...) itself. This can be desirable sometimes, and was provided e.g. by the (now somewhat dormant?) vmprof profiler.
(An example application is profiling an extension that calls back into libpython and where these calls are suspected to take a significant amount of time.)

@benfred
Copy link
Owner

benfred commented Aug 18, 2021

Which pythons symbols are you interested in?

We already include symbols from a bunch of builtin python modules

let mut prefixes = HashSet::new();
prefixes.insert("time");
prefixes.insert("sys");
prefixes.insert("gc");
prefixes.insert("os");
prefixes.insert("unicode");
prefixes.insert("thread");
prefixes.insert("stringio");
prefixes.insert("sre");
// likewise reasoning about lock contention inside python is also useful
prefixes.insert("PyGilState");
prefixes.insert("PyThread");
prefixes.insert("lock");
. For instance profling this
import time
def longsleep():
time.sleep(100000)
if __name__ == "__main__":
longsleep()
with the native flags shows the time.sleep builtin to python:

Process 3333: python longsleep.py
Python v3.9.5 (/home/vagrant/miniconda3/bin/python3.9)

Thread 3333 (idle)
    __select_nocancel (libc-2.17.so)
    time_sleep (python3.9)
    longsleep (longsleep.py:5)
    <module> (longsleep.py:9)

vmprof doesn't handle callbacks afaik https://vmprof.readthedocs.io/en/latest/native.html#technical-design - after it detects a python frame it doesn't display any more native frames again.

@anntzer
Copy link
Contributor Author

anntzer commented Aug 18, 2021

Ah, it's nice to see the list here, thanks for the quick reply. Perhaps that whitelist could be made user-configurable?

I was specifically looking for PyOS_string_to_double (to figure out whether it's worth switching to one of the very fast native floating point parsers that exist nowadays). (So in my case, the extension is calling back into a C function implemented in libpython, not into an actual Python frame.)

@Jongy
Copy link
Contributor

Jongy commented Aug 18, 2021

Maybe we can just do what vmprof does? (stopping at PyEval_EvalFrame*)? That's a useful user-configurable flag IMO.

@anntzer
Copy link
Contributor Author

anntzer commented Sep 1, 2021

FWIW I ended up using a patched version of py-spy with just whatever entries I needed in WHITELISTED_PREFIXES (thanks for pointing it out, it's been very helpful), but being able to write e.g. py-spy record --whitelist-prefixes=foo,bar,baz,... -- the-python-process-invocation (with the default --whitelist-prefixes being what it is right now for compat) would be nice. (I am not wedded that the specific API either.)

@smola
Copy link

smola commented May 19, 2022

In #497 I added a flag to disable most filtering. It's a bit noisy. For example, I see _start, Py_BytesMain, py_main_run_python, PyRun_AnyFileExFlags, and a few more above the main Python function. Also function_code_fastcall between Python calls. All these could be filtered out. But it does the job for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants