pprof.go 543 B

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