This repository has been archived by the owner on Mar 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 242
/
main.go
189 lines (158 loc) · 6.11 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// Package main is the entry point of go-torch, a stochastic flame graph
// profiler for Go programs.
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/uber/go-torch/pprof"
"github.com/uber/go-torch/renderer"
"github.com/uber/go-torch/torchlog"
gflags "github.com/jessevdk/go-flags"
)
// options are the parameters for go-torch.
type options struct {
PProfOptions pprof.Options `group:"pprof Options"`
OutputOpts outputOptions `group:"Output Options"`
}
type outputOptions struct {
File string `short:"f" long:"file" default:"torch.svg" description:"Output file name (must be .svg)"`
Print bool `short:"p" long:"print" description:"Print the generated svg to stdout instead of writing to file"`
Raw bool `short:"r" long:"raw" description:"Print the raw call graph output to stdout instead of creating a flame graph; use with Brendan Gregg's flame graph perl script (see https://github.com/brendangregg/FlameGraph)"`
Title string `long:"title" default:"Flame Graph" description:"Graph title to display in the output file"`
Width int64 `long:"width" default:"1200" description:"Generated graph width"`
Hash bool `long:"hash" description:"Colors are keyed by function name hash"`
Colors string `long:"colors" default:"" description:"set color palette. choices are: hot (default), mem, io, wakeup, chain, java, js, perl, red, green, blue, aqua, yellow, purple, orange"`
ConsistentPalette bool `long:"cp" description:"Use consistent palette (palette.map)"`
Reverse bool `long:"reverse" description:"Generate stack-reversed flame graph"`
Inverted bool `long:"inverted" description:"icicle graph"`
}
// main is the entry point of the application
func main() {
if err := runWithArgs(os.Args[1:]...); err != nil {
torchlog.Fatalf("Failed: %v", err)
}
}
func runWithArgs(args ...string) error {
opts := &options{}
parser := gflags.NewParser(opts, gflags.Default|gflags.IgnoreUnknown)
parser.Usage = "[options] [binary] <profile source>"
remaining, err := parser.ParseArgs(args)
if err != nil {
if flagErr, ok := err.(*gflags.Error); ok && flagErr.Type == gflags.ErrHelp {
os.Exit(0)
}
return fmt.Errorf("could not parse options: %v", err)
}
if err := validateOptions(opts); err != nil {
return fmt.Errorf("invalid options: %v", err)
}
return runWithOptions(opts, remaining)
}
func runWithOptions(allOpts *options, remaining []string) error {
pprofRawOutput, err := pprof.GetRaw(allOpts.PProfOptions, remaining)
if err != nil {
return fmt.Errorf("could not get raw output from pprof: %v", err)
}
profile, err := pprof.ParseRaw(pprofRawOutput)
if err != nil {
return fmt.Errorf("could not parse raw pprof output: %v", err)
}
sampleIndex := pprof.SelectSample(remaining, profile.SampleNames)
flameInput, err := renderer.ToFlameInput(profile, sampleIndex)
if err != nil {
return fmt.Errorf("could not convert stacks to flamegraph input: %v", err)
}
opts := allOpts.OutputOpts
if opts.Raw {
torchlog.Print("Printing raw flamegraph input to stdout")
fmt.Printf("%s\n", flameInput)
return nil
}
var flameGraphArgs = buildFlameGraphArgs(opts)
flameGraph, err := renderer.GenerateFlameGraph(flameInput, flameGraphArgs...)
if err != nil {
return fmt.Errorf("could not generate flame graph: %v", err)
}
if opts.Print {
torchlog.Print("Printing svg to stdout")
fmt.Printf("%s\n", flameGraph)
return nil
}
torchlog.Printf("Writing svg to %v", opts.File)
if err := ioutil.WriteFile(opts.File, flameGraph, 0666); err != nil {
return fmt.Errorf("could not write output file: %v", err)
}
return nil
}
func validateOptions(opts *options) error {
file := opts.OutputOpts.File
if file != "" && !strings.HasSuffix(file, ".svg") {
return fmt.Errorf("output file must end in .svg")
}
if opts.PProfOptions.TimeSeconds < 1 {
return fmt.Errorf("seconds must be an integer greater than 0")
}
// extra FlameGraph options
if opts.OutputOpts.Title == "" {
return fmt.Errorf("flamegraph title should not be empty")
}
if opts.OutputOpts.Width <= 0 {
return fmt.Errorf("flamegraph default width is 1200 pixels")
}
if opts.OutputOpts.Colors != "" {
switch opts.OutputOpts.Colors {
case "hot", "mem", "io", "wakeup", "chain", "java", "js", "perl", "red", "green", "blue", "aqua", "yellow", "purple", "orange":
// valid
default:
return fmt.Errorf("unknown flamegraph colors %q", opts.OutputOpts.Colors)
}
}
return nil
}
func buildFlameGraphArgs(opts outputOptions) []string {
var args []string
if opts.Title != "" {
args = append(args, "--title", opts.Title)
}
if opts.Width > 0 {
args = append(args, "--width", strconv.FormatInt(opts.Width, 10))
}
if opts.Colors != "" {
args = append(args, "--colors", opts.Colors)
}
if opts.Hash {
args = append(args, "--hash")
}
if opts.ConsistentPalette {
args = append(args, "--cp")
}
if opts.Reverse {
args = append(args, "--reverse")
}
if opts.Inverted {
args = append(args, "--inverted")
}
return args
}