profile.go 443 B

123456789101112131415161718192021222324252627
  1. package etcd
  2. import (
  3. "os"
  4. "os/signal"
  5. "runtime/pprof"
  6. "github.com/coreos/etcd/log"
  7. )
  8. // profile starts CPU profiling.
  9. func profile(path string) {
  10. f, err := os.Create(path)
  11. if err != nil {
  12. log.Fatal(err)
  13. }
  14. pprof.StartCPUProfile(f)
  15. c := make(chan os.Signal, 1)
  16. signal.Notify(c, os.Interrupt)
  17. go func() {
  18. sig := <-c
  19. log.Infof("captured %v, stopping profiler and exiting..", sig)
  20. pprof.StopCPUProfile()
  21. os.Exit(1)
  22. }()
  23. }