You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/proc/goroutines.go

46 lines
932 B
Go

//go:build linux || darwin
4 years ago
package proc
import (
"fmt"
"os"
"path"
"runtime/pprof"
"syscall"
"time"
"github.com/zeromicro/go-zero/core/logx"
4 years ago
)
const (
goroutineProfile = "goroutine"
debugLevel = 2
)
type creator interface {
Create(name string) (file *os.File, err error)
}
func dumpGoroutines(ctor creator) {
4 years ago
command := path.Base(os.Args[0])
pid := syscall.Getpid()
dumpFile := path.Join(os.TempDir(), fmt.Sprintf("%s-%d-goroutines-%s.dump",
command, pid, time.Now().Format(timeFormat)))
logx.Infof("Got dump goroutine signal, printing goroutine profile to %s", dumpFile)
if f, err := ctor.Create(dumpFile); err != nil {
4 years ago
logx.Errorf("Failed to dump goroutine profile, error: %v", err)
} else {
defer f.Close()
pprof.Lookup(goroutineProfile).WriteTo(f, debugLevel)
}
}
type fileCreator struct{}
func (fc fileCreator) Create(name string) (file *os.File, err error) {
return os.Create(name)
}