enable profiling for shared libraries

This commit is contained in:
Patryk Osmaczko 2024-07-23 08:07:36 +02:00
parent 977a93b005
commit 167f88e20e
1 changed files with 10 additions and 2 deletions

View File

@ -1,9 +1,13 @@
package profiling
import (
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime/pprof"
"syscall"
"time"
)
// CPUFilename is a filename in which the CPU profiling is stored.
@ -15,8 +19,10 @@ var cpuFile *os.File
// the profile will be buffered and written to the file in folder dataDir.
func StartCPUProfile(dataDir string) error {
if cpuFile == nil {
signal.Notify(make(chan os.Signal), syscall.SIGPROF) // enable profiling for shared libraries
var err error
cpuFile, err = os.Create(filepath.Join(dataDir, CPUFilename))
cpuFile, err = os.Create(filepath.Join(dataDir, fmt.Sprintf("status_cpu_%d.prof", time.Now().Unix())))
if err != nil {
return err
}
@ -31,5 +37,7 @@ func StopCPUProfile() error {
return nil
}
pprof.StopCPUProfile()
return cpuFile.Close()
err := cpuFile.Close()
cpuFile = nil
return err
}