Skip to content

Commit

Permalink
NXIO-332 add a way to start/stop profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
Damien Metzler committed Dec 12, 2014
1 parent f597015 commit 8164728
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion config.go
Expand Up @@ -42,7 +42,7 @@ func parseConfig() *Config {
flag.StringVar(&config.UrlHeaderParam, "UrlHeaderParam", "", "Name of the param to inject the originating url")
flag.IntVar(&config.lastAccessInterval, "lastAccessInterval", 10, "Interval (in seconds to refresh last access time of a service")
flag.BoolVar(&config.forceFwSsl, "forceFwSsl", false, "If not x-forwarded-proto set to https, then redirecto to the equivalent https url")
flag.StringVar(&config.cpuProfile, "cpuProfile", "", "File to dump cpuProfile")
flag.StringVar(&config.cpuProfile, "cpuProfile", "/tmp/gogeta.prof", "File to dump cpuProfile")
flag.Parse()

glog.Infof("Dumping Configuration")
Expand Down
30 changes: 27 additions & 3 deletions main.go
Expand Up @@ -42,7 +42,7 @@ func main() {
defer pprof.StopCPUProfile()
}

handleSignals()
handleSignals(c)

resolver, error := getResolver(c)
if error != nil {
Expand All @@ -57,11 +57,21 @@ func main() {

}

func handleSignals() {
func handleSignals(config *Config) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)
signal.Notify(signals, os.Interrupt, syscall.SIGUSR1)
signal.Notify(signals, os.Interrupt, syscall.SIGUSR2)

go func() {
isProfiling := false

defer func() {
if isProfiling {
pprof.StopCPUProfile()
}
}()

sig := <-signals
switch sig {
case syscall.SIGTERM, syscall.SIGINT:
Expand All @@ -70,8 +80,22 @@ func handleSignals() {
os.Exit(0)
case syscall.SIGUSR1:
pprof.Lookup("goroutine").WriteTo(os.Stdout, 2)
case syscall.SIGUSR2:
if !isProfiling {
f, err := os.Create(config.cpuProfile)
if err != nil {
glog.Fatal(err)
} else {
pprof.StartCPUProfile(f)
isProfiling = true
}
} else {
pprof.StopCPUProfile()
isProfiling = false
}

}

}()
}

}

0 comments on commit 8164728

Please sign in to comment.