2020-08-07 14:18:01 +03:00
|
|
|
// Package pprof provides a pprof profiler which writes output to /tmp/[name].{cpu,mem}.pprof
|
2021-10-02 19:55:07 +03:00
|
|
|
package pprof // import "go.unistack.org/micro/v3/profiler/pprof"
|
2019-11-06 22:36:04 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"runtime/pprof"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
profile "go.unistack.org/micro/v3/profiler"
|
2019-11-06 22:36:04 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type profiler struct {
|
|
|
|
exit chan bool
|
|
|
|
cpuFile *os.File
|
|
|
|
memFile *os.File
|
2021-03-06 19:45:13 +03:00
|
|
|
opts profile.Options
|
|
|
|
sync.Mutex
|
|
|
|
running bool
|
2019-11-06 22:36:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *profiler) writeHeap(f *os.File) {
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
t := time.NewTicker(time.Second * 30)
|
|
|
|
defer t.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
runtime.GC()
|
2021-09-28 23:43:43 +03:00
|
|
|
_ = pprof.WriteHeapProfile(f)
|
2019-11-06 22:36:04 +03:00
|
|
|
case <-p.exit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *profiler) Start() error {
|
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
|
|
|
if p.running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// create exit channel
|
|
|
|
p.exit = make(chan bool)
|
|
|
|
|
2022-11-07 14:53:58 +03:00
|
|
|
cpuFile := filepath.Join(string(os.PathSeparator)+"tmp", "cpu.pprof")
|
|
|
|
memFile := filepath.Join(string(os.PathSeparator)+"tmp", "mem.pprof")
|
2019-11-06 22:36:04 +03:00
|
|
|
|
|
|
|
if len(p.opts.Name) > 0 {
|
2022-11-07 14:53:58 +03:00
|
|
|
cpuFile = filepath.Join(string(os.PathSeparator)+"tmp", p.opts.Name+".cpu.pprof")
|
|
|
|
memFile = filepath.Join(string(os.PathSeparator)+"tmp", p.opts.Name+".mem.pprof")
|
2019-11-06 22:36:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
f1, err := os.Create(cpuFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f2, err := os.Create(memFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// start cpu profiling
|
|
|
|
if err := pprof.StartCPUProfile(f1); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the heap periodically
|
|
|
|
go p.writeHeap(f2)
|
|
|
|
|
|
|
|
// set cpu file
|
|
|
|
p.cpuFile = f1
|
|
|
|
// set mem file
|
|
|
|
p.memFile = f2
|
|
|
|
|
|
|
|
p.running = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *profiler) Stop() error {
|
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-p.exit:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
close(p.exit)
|
|
|
|
pprof.StopCPUProfile()
|
|
|
|
p.cpuFile.Close()
|
|
|
|
p.running = false
|
|
|
|
p.cpuFile = nil
|
|
|
|
p.memFile = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 23:31:16 +03:00
|
|
|
func (p *profiler) String() string {
|
|
|
|
return "pprof"
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// NewProfile create new profiler
|
|
|
|
func NewProfile(opts ...profile.Option) profile.Profiler {
|
|
|
|
options := profile.Options{}
|
2019-11-06 22:36:04 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2021-02-14 16:16:01 +03:00
|
|
|
return &profiler{opts: options}
|
2019-11-06 22:36:04 +03:00
|
|
|
}
|