Tracing and visualizing the Python GIL with perf and VizTracer

Tracing and visualizing the Python GIL with perf and VizTracer

9 comments · 97% upvoted · source · other discussions

comments

flying-sheep

Nice! A convenient alternative (but maybe not as powerful?):

I recently discovered py-spy for profiling python.

py-spy record --format=speedscope -o profile.speedscope.json -- python my-script.py

And then visualize it via speedscope.app

maartenbreddels [OP]

I think profiling and tracing (what I do here) are complementary. I use py-spy a lot, but it only gives you a 'statistical' view of where time is spent or how much the GIL is held. To gain a deep understanding of the flow of your program and the GIL in a multithreaded environment I needed a tool as I presented, and unfortunately had to make it myself :)

flying-sheep

Thank you! I know the feeling: “This should exist, why doesn’t it yet? Sigh.” *rolls up sleeves*

chirisalex

Helpful article

[deleted]

Good stuff, thanks

apockill

This looks awesome! How does it handle cythonized libraries and/or C bindings such as tensorflow?

maartenbreddels [OP]

Thanks! You will see the call to the C library (this is what CPython and VizTracer do), and you should see if the GIL is being released, like with NumPy, one of the examples in the article.

apockill

That's fantastic. Actually, my application for this is to find naughty C bindings that don't release the GIL. I have a big application that's heavily multithreaded with many C bindings (think gstreamer + tensorflow + OpenVINO), and something is causing a lockup somewhere. It's quite frustrating!

I'll give this tutorial go.

maartenbreddels [OP]

Indeed, that's one of the things I'd like to automate (and I'm very close now), feel free to contact me, we could maybe collaborate on this https://github.com/maartenbreddels/per4m