google / pprof

pprof is a tool for visualization and analysis of profiling data
Apache License 2.0
7.99k stars 606 forks source link

Support flame graph as an output format #401

Open aalexand opened 6 years ago

aalexand commented 6 years ago

pprof currently supports flame graph visualization in the HTTP server mode. Sometimes it is desired to generate the flame graph visualization as a persistent report, likely as an SVG file.

ShaneHarvey commented 6 years ago

To accomplish this right now, one has to:

  1. Install go-torch: go get github.com/uber/go-torch
  2. Install brendangregg/FlameGraph
  3. Finally, generate the svg: go-torch -b ./profile.pprof --binaryname ./binary. (make sure FlameGraph is on your PATH)
itczl22 commented 5 years ago

go-torch is deprecated, use pprof instead after go1.11. We can browse web ui as follow:

server nginx:

   server_name pprof.graph.com
   location / {
     proxy_pass http://127.0.0.1:8031
  }

local host: your.server.ip pprof.graph.com

start pprof: go tool pprof -http=:8031 "http://your.server.ip:port/debug/pprof/profile"

access local browser: pprof.graph.com

wyk9787 commented 4 years ago

I think the easiest way to implement this feature is to use scripts in https://github.com/brendangregg/FlameGraph and have a standalone command to indicate this is what the user wants. Is "-flamegraph" a good candidate due to the internal conflicting flag "-flame"?

aalexand commented 4 years ago

We should not bring in dependency on external scripts that wouldn't be a part of the executable. pprof is a part of Go runtime distribution, so everything should be contained within the pprof executable, either in the form of Go or JS code (like we do for the web UI).

nolanmar511 commented 4 years ago

We currently use https://github.com/spiermar/d3-flame-graph as our flame graph; it would be ideal to continue to use that code.

I'm not sure of the licenses for https://github.com/brendangregg/FlameGraph. And, as aalexand noted, pprof is a part of Go, so we need to be very careful with dependencies.

cben commented 4 years ago

ref https://github.com/spiermar/d3-flame-graph/issues/33 (but nobody ever replied there)

cben commented 3 years ago

How about exporting self-contained HTML? That sounds easier with d3-flame-graph than interactive SVG... Would that satisfy the same needs?

The only reason I want SVG is to be able to send/attach a single file that co-workers can open and zoom into.

cben commented 3 years ago

Huh, saving the HTML from the browser, in simple "just HTML" mode already works! :tada: As does downloading it with curl:

# The redirect avoids it being suspended for reading from terminal.
go tool pprof --http :9000 --no_browser ./CMD PROFILE.heap < /dev/null &
sleep 1
for sample in {alloc,inuse}_{objects,space}; do 
  curl "http://localhost:9000/ui/?si=$sample" -o graph-$sample.html
  curl "http://localhost:9000/ui/flamegraph?si=$sample" -o flamegraph-$sample.html
  # add `source` to pages loop for annotated source view, but it's _really_ slow!
  for page in top peek disasm; do
    time curl "http://localhost:9000/ui/$page?si=$sample" -o $page-$sample.html
  done
done
kill $!

This works because data is not served in separate requests, it's simply inlined into the HTML: https://github.com/google/pprof/blob/427632fa3b1c4f7ee2ec76e79aa1f7dc5bb25e28/internal/driver/webhtml.go#L1324 (and CSS and JS are inlined too).

aalexand commented 3 years ago

I think we shouldn't bake in a requirement that the -http mode generates a single HTML page. This may easily change in the future so this seems too fragile of an assumption.

mhansen commented 3 years ago

Some prior art: @felixge's https://github.com/felixge/pprofutils#folded can generate "Folded Stacks" format from pprof for feeding into FlameGraph toolkit.

fubss commented 2 years ago

My way which I used to send FlameGraph file to my colleagues.

  1. show collected data in a browser go tool pprof -http :8081 filename.pb.gz

  2. download html page with flamegraph as a file with all data wget -O flamegraph.htm http://localhost:8081/ui/flamegraph