benfred / py-spy

Sampling profiler for Python programs
MIT License
12.66k stars 422 forks source link

[feature request] Dump intermediate flamegraph svg while recording #286

Open ncolomer opened 4 years ago

ncolomer commented 4 years ago

We just used py-spy to profile some ephemeral but long running (hours) jobs. Outcomes was really useful, thanks for this tool!

It would be very handy if we were able to generate intermediate flamegraph SVGs, eg by sending a signal to the py-spy process (as does SIGQUIT on a JVM process for thread dump).

This would have allow us to start analysis of first traces without having to wait for the process being profiled to exit.

benfred commented 4 years ago

I'm working on something that will let you view flamegraph while the process is running. You'll need to install from the latest dev branch, but once you've done that you can use the serve command to start an interactive web server that will let see flamegraphs from the running process, select the time range to view flame graphs off, look at the source code for each function annotated by how long it takes etc:

pip install py-spy==0.4.0.dev1
py-spy serve --pid 12345

# open a browser with http://localhost:8000 to see results

This still need a bunch of work obviously (and I haven't had a chance to work on it recently). I'm thinking about adding an 'export' functionality to this view - where you can select a time range from the running process and dump out a flamegraph you can share with other people.

ncolomer commented 4 years ago

you can use the serve command to start an interactive web server

Sounds very promising! I gonna give it a try

I'm thinking about adding an 'export' functionality to this view - where you can select a time range from the running process and dump out a flamegraph you can share with other people.

Would be useful feature for sure

boxed commented 4 years ago

I have a similar but also different use case in mind. I would like to have py-spy running all the time on all batch and web machines and profiling all python processes, then sending all that data somewhere to be joined into a live view of the entire product.

It seems like the parts could mostly be available for this. I think we might need:

I am totally unfamiliar with the code and have written a total of like 20 lines of rust, but from using the tool it sounds like most of the parts for doing all of the above exists in the current code in some form.

Jongy commented 3 years ago

@benfred , did you advance with this idea?

I'm particularly interested in the signal idea - I'm using py-spy in our continuous profiler and we currently invoke it once per some interval (say, 60 seconds) for each target process. I prefer to invoke py-spy once per target process, and simply signal it every interval to dump its existing samples buffer. This is done:

Linux's perf has a similar option, --switch-output=signal; on every SIGUSR2 it will write a perf.data.{timestamp} output file with last samples, and empty its buffer.

I'd be happy to take it and add the signal handling capability (on which py-spy shall write the raw collapsed file, or flamegraph, depending on the output format you've asked for)

Btw @boxed , our continuous profiler sounds exactly like what you have in mind :)