github.com/lingyao2333/mo-zero@v1.4.1/core/proc/goroutines.go (about)

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