pprof.go 534 B

123456789101112131415161718192021222324252627282930313233
  1. package util
  2. import (
  3. "os"
  4. "runtime/pprof"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. )
  7. func SetupProfiling(cpuProfile, memProfile string) {
  8. if cpuProfile != "" {
  9. f, err := os.Create(cpuProfile)
  10. if err != nil {
  11. glog.Fatal(err)
  12. }
  13. pprof.StartCPUProfile(f)
  14. defer pprof.StopCPUProfile()
  15. OnInterrupt(func() {
  16. pprof.StopCPUProfile()
  17. })
  18. }
  19. if memProfile != "" {
  20. f, err := os.Create(memProfile)
  21. if err != nil {
  22. glog.Fatal(err)
  23. }
  24. OnInterrupt(func() {
  25. pprof.WriteHeapProfile(f)
  26. f.Close()
  27. })
  28. }
  29. }