Skip to content

Commit

Permalink
升级依赖版本&&重构代码
Browse files Browse the repository at this point in the history
  • Loading branch information
JellyTony committed Mar 24, 2024
1 parent b2be0d8 commit eb24170
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions pkg/proc/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ func (p *Profile) startBlockProfile() {
runtime.SetBlockProfileRate(1)
logger.Infof("profile: block profiling enabled, %s", fn)
p.closers = append(p.closers, func() {
pprof.Lookup("block").WriteTo(f, 0)
f.Close()
if err = pprof.Lookup("block").WriteTo(f, 0); err != nil {
logger.Errorf("profile: could not write block profile: %v", err)
}
if err = f.Close(); err != nil {
logger.Errorf("profile: could not close block profile file: %v", err)
}
runtime.SetBlockProfileRate(0)
logger.Infof("profile: block profiling disabled, %s", fn)
})
Expand All @@ -66,10 +70,15 @@ func (p *Profile) startCpuProfile() {
}

logger.Infof("profile: cpu profiling enabled, %s", fn)
pprof.StartCPUProfile(f)
if err = pprof.StartCPUProfile(f); err != nil {
logger.Errorf("profile: could not start cpu profile: %v", err)
return
}
p.closers = append(p.closers, func() {
pprof.StopCPUProfile()
f.Close()
if err = f.Close(); err != nil {
logger.Errorf("profile: could not close cpu profile file: %v", err)
}
logger.Infof("profile: cpu profiling disabled, %s", fn)
})
}
Expand All @@ -86,8 +95,12 @@ func (p *Profile) startMemProfile() {
runtime.MemProfileRate = DefaultMemProfileRate
logger.Infof("profile: memory profiling enabled (rate %d), %s", runtime.MemProfileRate, fn)
p.closers = append(p.closers, func() {
pprof.Lookup("heap").WriteTo(f, 0)
f.Close()
if err = pprof.Lookup("heap").WriteTo(f, 0); err != nil {
logger.Errorf("profile: could not write memory profile: %v", err)
}
if err = f.Close(); err != nil {
logger.Errorf("profile: could not close memory profile file: %v", err)
}
runtime.MemProfileRate = old
logger.Infof("profile: memory profiling disabled, %s", fn)
})
Expand All @@ -105,9 +118,12 @@ func (p *Profile) startMutexProfile() {
logger.Infof("profile: mutex profiling enabled, %s", fn)
p.closers = append(p.closers, func() {
if mp := pprof.Lookup("mutex"); mp != nil {
mp.WriteTo(f, 0)
err = mp.WriteTo(f, 0)
}
err = f.Close()
if err != nil {
logger.Errorf("profile: could not close mutex profile file: %v", err)
}
f.Close()
runtime.SetMutexProfileFraction(0)
logger.Infof("profile: mutex profiling disabled, %s", fn)
})
Expand All @@ -124,9 +140,12 @@ func (p *Profile) startThreadCreateProfile() {
logger.Infof("profile: threadcreate profiling enabled, %s", fn)
p.closers = append(p.closers, func() {
if mp := pprof.Lookup("threadcreate"); mp != nil {
mp.WriteTo(f, 0)
err = mp.WriteTo(f, 0)
}
err = f.Close()
if err != nil {
logger.Errorf("profile: could not close threadcreate profile file: %v", err)
}
f.Close()
logger.Infof("profile: threadcreate profiling disabled, %s", fn)
})
}
Expand Down Expand Up @@ -187,7 +206,9 @@ func StartProfile() Stopper {
prof.Stop()

signal.Reset()
syscall.Kill(os.Getpid(), syscall.SIGINT)
if err := syscall.Kill(os.Getpid(), syscall.SIGINT); err != nil {
logger.Errorf("profile: failed to send interrupt signal: %v", err)
}
}()

return &prof
Expand Down

0 comments on commit eb24170

Please sign in to comment.