forked from moby/buildkit
/
profiler.go
84 lines (73 loc) · 2.09 KB
/
profiler.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
package profiler
import (
"github.com/pkg/profile"
"github.com/urfave/cli"
)
func Attach(app *cli.App) {
app.Flags = append(app.Flags,
cli.StringFlag{
Name: "profile-cpu",
Hidden: true,
},
cli.StringFlag{
Name: "profile-memory",
Hidden: true,
},
cli.IntFlag{
Name: "profile-memoryrate",
Value: 512 * 1024,
Hidden: true,
},
cli.StringFlag{
Name: "profile-block",
Hidden: true,
},
cli.StringFlag{
Name: "profile-mutex",
Hidden: true,
},
cli.StringFlag{
Name: "profile-trace",
Hidden: true,
},
)
var stoppers = []interface {
Stop()
}{}
before := app.Before
app.Before = func(clicontext *cli.Context) error {
if before != nil {
if err := before(clicontext); err != nil {
return err
}
}
if cpuProfile := clicontext.String("profile-cpu"); cpuProfile != "" {
stoppers = append(stoppers, profile.Start(profile.CPUProfile, profile.ProfilePath(cpuProfile), profile.NoShutdownHook))
}
if memProfile := clicontext.String("profile-memory"); memProfile != "" {
stoppers = append(stoppers, profile.Start(profile.MemProfile, profile.ProfilePath(memProfile), profile.NoShutdownHook, profile.MemProfileRate(clicontext.Int("profile-memoryrate"))))
}
if blockProfile := clicontext.String("profile-block"); blockProfile != "" {
stoppers = append(stoppers, profile.Start(profile.BlockProfile, profile.ProfilePath(blockProfile), profile.NoShutdownHook))
}
if mutexProfile := clicontext.String("profile-mutex"); mutexProfile != "" {
stoppers = append(stoppers, profile.Start(profile.MutexProfile, profile.ProfilePath(mutexProfile), profile.NoShutdownHook))
}
if traceProfile := clicontext.String("profile-trace"); traceProfile != "" {
stoppers = append(stoppers, profile.Start(profile.TraceProfile, profile.ProfilePath(traceProfile), profile.NoShutdownHook))
}
return nil
}
after := app.After
app.After = func(clicontext *cli.Context) error {
if after != nil {
if err := after(clicontext); err != nil {
return err
}
}
for _, stopper := range stoppers {
stopper.Stop()
}
return nil
}
}