pprof icon indicating copy to clipboard operation
pprof copied to clipboard

Support flame graph as an output format

Open aalexand opened this issue 7 years ago • 11 comments

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.

aalexand avatar Jul 17 '18 03:07 aalexand

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)

ShaneHarvey avatar Oct 12 '18 23:10 ShaneHarvey

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

itczl22 avatar Jan 14 '19 14:01 itczl22

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"?

wyk9787 avatar Apr 07 '20 20:04 wyk9787

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).

aalexand avatar Apr 07 '20 20:04 aalexand

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.

nolanmar511 avatar Apr 07 '20 20:04 nolanmar511

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

cben avatar Jun 26 '20 11:06 cben

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.

  • a minor benefit of .html over .svg: it's easy to open .svg in an image viewer, or to embed it as <img> somewhere, which misses out on the zooming by JavaScript :sparkles:, so I have to explicity recommend people to only open it in a browser (because I want them to explore it, not just take my conclusions).

cben avatar Dec 06 '20 18:12 cben

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).

  • [ ] So, anybody else wants a -html flag?
  • [x] Search already works too!
  • [x] Sorting by clicking columns already works!
  • [ ] "Refine" menu doesn't work and probably can't as it requires backend re-computation. E.g. "Hide" reloads the page with ?h=... appended.
  • [ ] The links in "View" & "Sample" menus do not work.
    • [ ] Strawman: Just drop the menu (or most of it) in export?
    • [ ] For heap, would be neat to embed all 4 sample types into one HTML, and avoid reloads when switching from menu.
    • [ ] Similar strategy could allow all visualizations to be packed into 1 HTML? Would need deeper changes... And including everything is too big & slow for most needs.

cben avatar Dec 06 '20 22:12 cben

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.

aalexand avatar Dec 07 '20 01:12 aalexand

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

mhansen avatar Oct 07 '21 21:10 mhansen

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

fubss avatar Apr 05 '22 14:04 fubss