goroutines.go 709 B

1234567891011121314151617181920212223242526272829303132333435
  1. // +build linux darwin
  2. package proc
  3. import (
  4. "fmt"
  5. "os"
  6. "path"
  7. "runtime/pprof"
  8. "syscall"
  9. "time"
  10. "git.i2edu.net/i2/go-zero/core/logx"
  11. )
  12. const (
  13. goroutineProfile = "goroutine"
  14. debugLevel = 2
  15. )
  16. func dumpGoroutines() {
  17. command := path.Base(os.Args[0])
  18. pid := syscall.Getpid()
  19. dumpFile := path.Join(os.TempDir(), fmt.Sprintf("%s-%d-goroutines-%s.dump",
  20. command, pid, time.Now().Format(timeFormat)))
  21. logx.Infof("Got dump goroutine signal, printing goroutine profile to %s", dumpFile)
  22. if f, err := os.Create(dumpFile); err != nil {
  23. logx.Errorf("Failed to dump goroutine profile, error: %v", err)
  24. } else {
  25. defer f.Close()
  26. pprof.Lookup(goroutineProfile).WriteTo(f, debugLevel)
  27. }
  28. }