go-torch

repository·master·Indexed 26 days ago

https://github.com/uber-archive/go-torch

A tool for stochastically profiling Go programs by collecting stack traces and synthesizing them into flame graph visualizations. It supports generating graphs from running programs via pprof endpoints or from local profile files. Note that go-torch is deprecated in favor of the built-in flamegraph visualizations available in go tool pprof as of Go 1.11.

Tokens
1.4K
Snippets
5
Records
8
Agent score
37%

What's inside go-torch

  1. Use pprof instead of go-torch (Recommended)

    master

    go-torch is deprecated. As of Go 1.11, flamegraph visualizations are available directly in go tool pprof.

    To listen on a specific port and open a browser for visualization, use:

    $ go tool pprof -http=":8081" [binary] [profile]

    If you are on an older version of Go, you can install the standalone pprof tool:

    $ go get -u github.com/google/pprof
    $ pprof -http=":8081" [binary] [profile]
    $ go tool pprof -http=":8081" [binary] [profile
  2. Expose pprof endpoints in your Go application

    master

    To allow go-torch to profile your application, you must expose the net/http/pprof endpoints.

    If your application uses the DefaultServeMux, simply add this blank import:

    import _ "net/http/pprof"

    If you are using a custom multiplexer, you must manually register the net/http/pprof handlers.

    import _ "net/http/pprof"
  3. Install go-torch

    master

    You can install go-torch using go get or run it via Docker.

    Using Go:

    $ go get github.com/uber/go-torch

    Using Docker: To avoid mounting volumes, use the -p flag to print the SVG to stdout and redirect it to a file:

    $ docker run uber/go-torch -u http://[address-of-host] -p > torch.svg
    $ go get github.com/uber/go-torch
  4. Use go-torch CLI to generate flame graphs

    master

    The go-torch CLI is a stochastic flame graph profiler for Go programs. It takes a binary and a profile source (typically from pprof) and generates an SVG flame graph.

    Usage Syntax: [options] [binary] <profile source>

    Example: To generate a flame graph from a profile source using a specific binary:

    go-torch ./my-binary profile.pb.gz
  5. Generate a flame graph from a running Go program

    master

    By default, go-torch hits http://localhost:8080/debug/pprof/profile for a 30-second CPU profile and writes the result to torch.svg.

    Basic usage (default settings):

    $ go-torch

    Customize the base URL:

    $ go-torch -u http://my-service:8080/

    Customize the profiling duration:

    $ go-torch --seconds 5
    $ go-torch -u http://my-service:8080/
    INFO[19:10:58] Run pprof command: go tool pprof -raw -seconds 30 http://my-service:8080/debug/pprof/profile
    INFO[19:11:03] Writing svg to torch.svg
  6. Generate a flame graph from local profile files

    master

    go-torch supports passing arguments directly to go tool pprof. This is useful if you have already generated a profile file (e.g., from a benchmark).

    Example using a benchmark profile:

    # 1. Create the profile
    $ go test -bench . -cpuprofile=cpu.prof
    
    # 2. Generate flame graph using go-torch
    $ go-torch main.test cpu.prof

    Passing extra pprof arguments: Arguments not handled by go-torch are passed through to pprof:

    $ go-torch --alloc_objects main.test mem.prof
    $ go-torch main.test cpu.prof
    INFO[19:00:29] Run pprof command: go tool pprof -raw -seconds 30 main.test cpu.prof
    INFO[19:00:29] Writing svg to torch.svg
  7. go-torch CLI Reference

    master

    go-torch is a tool for stochastically profiling Go programs by collecting stack traces and synthesizing them into a flame graph.

    Usage: go-torch [options] [binary] <profile source>

    pprof Options:

    • -u, --url=: Base URL of your Go program (default: http://localhost:8080)
    • -s, --suffix=: URL path of pprof profile (default: /debug/pprof/profile)
    • -b, --binaryinput=: File path of previously saved binary profile.
    • --binaryname=: File path of the binary that the binaryinput is for.
    • -t, --seconds=: Number of seconds to profile for (default: 30)
    • --pprofArgs=: Extra arguments for pprof

    Output Options:

    • -f, --file=: Output file name (must be .svg) (default: torch.svg)
    • -p, --print: Print the generated svg to stdout instead of writing to file
    • -r, --raw: Print the raw call graph output to stdout (use with Brendan Gregg's flame graph perl script)
    • --title=: Graph title to display in the output file (default: Flame Graph)
    • --width=: Generated graph width (default: 1200)
    • --hash: Colors are keyed by function name hash
    • --colors=: Set color palette. Valid choices: hot (default), mem, io, wakeup, chain, java, js, perl, red, green, blue, aqua, yellow, purple, orange
    • --cp: Graph use consistent palette (palette.map)
    • --inverted: Icicle graph

    Help Options:

    • -h, --help: Show this help message
  8. Configure go-torch output options

    master

    Use the following flags to control how the flame graph is rendered and where it is saved:

    FlagLong FlagDefaultDescription
    -f--filetorch.svgOutput file name (must end in .svg)
    -p--printfalsePrint the generated SVG to stdout instead of writing to a file
    -r--rawfalsePrint the raw call graph output to stdout (compatible with Brendan Gregg's FlameGraph script)
    --titleFlame GraphGraph title to display in the output file
    --width1200Generated graph width in pixels
    --hashfalseColors are keyed by function name hash
    --colors""Set color palette. Choices: hot (default), mem, io, wakeup, chain, java, js, perl, red, green, blue, aqua, yellow, purple, orange
    --cpfalseUse consistent palette (palette.map)
    --reversefalseGenerate stack-reversed flame graph
    --invertedfalseGenerate icicle graph