2017-07-12 22:17:23 +00:00
|
|
|
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 != "" {
|
2017-08-10 01:18:58 +00:00
|
|
|
stoppers = append(stoppers, profile.Start(profile.CPUProfile, profile.ProfilePath(cpuProfile), profile.NoShutdownHook))
|
2017-07-12 22:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if memProfile := clicontext.String("profile-memory"); memProfile != "" {
|
2017-08-10 01:18:58 +00:00
|
|
|
stoppers = append(stoppers, profile.Start(profile.MemProfile, profile.ProfilePath(memProfile), profile.NoShutdownHook, profile.MemProfileRate(clicontext.Int("profile-memoryrate"))))
|
2017-07-12 22:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if blockProfile := clicontext.String("profile-block"); blockProfile != "" {
|
2017-08-10 01:18:58 +00:00
|
|
|
stoppers = append(stoppers, profile.Start(profile.BlockProfile, profile.ProfilePath(blockProfile), profile.NoShutdownHook))
|
2017-07-12 22:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if mutexProfile := clicontext.String("profile-mutex"); mutexProfile != "" {
|
2017-08-10 01:18:58 +00:00
|
|
|
stoppers = append(stoppers, profile.Start(profile.MutexProfile, profile.ProfilePath(mutexProfile), profile.NoShutdownHook))
|
2017-07-12 22:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if traceProfile := clicontext.String("profile-trace"); traceProfile != "" {
|
2017-08-10 01:18:58 +00:00
|
|
|
stoppers = append(stoppers, profile.Start(profile.TraceProfile, profile.ProfilePath(traceProfile), profile.NoShutdownHook))
|
2017-07-12 22:17:23 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|