From 9ebc5b1a0b9f4e01c1dee471c8e1064e503fe99f Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Wed, 31 Jul 2013 19:43:44 -0700 Subject: [PATCH] support go prof via flag cpuprofile --- etcd.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/etcd.go b/etcd.go index fd21c47e0..8fd37c61c 100644 --- a/etcd.go +++ b/etcd.go @@ -16,6 +16,8 @@ import ( "net" "net/http" "os" + "os/signal" + "runtime/pprof" "strings" "time" ) @@ -59,6 +61,8 @@ var retryTimes int var maxClusterSize int +var cpuprofile string + func init() { flag.BoolVar(&verbose, "v", false, "verbose logging") flag.BoolVar(&veryVerbose, "vv", false, "very verbose logging") @@ -90,6 +94,8 @@ func init() { flag.IntVar(&retryTimes, "r", 3, "the max retry attempts when trying to join a cluster") flag.IntVar(&maxClusterSize, "maxsize", 9, "the max size of the cluster") + + flag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to file") } // CONSTANTS @@ -160,6 +166,26 @@ var info *Info func main() { flag.Parse() + if cpuprofile != "" { + f, err := os.Create(cpuprofile) + if err != nil { + log.Fatal(err) + } + pprof.StartCPUProfile(f) + defer pprof.StopCPUProfile() + + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + go func() { + for sig := range c { + log.Printf("captured %v, stopping profiler and exiting..", sig) + pprof.StopCPUProfile() + os.Exit(1) + } + }() + + } + if veryVerbose { verbose = true raft.SetLogLevel(raft.Debug)