2018-02-02 12:23:47 +00:00
|
|
|
package profiling
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"runtime/pprof"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MemFilename is a filename in which the memory profiling is stored.
|
|
|
|
const MemFilename = "status_mem.prof"
|
|
|
|
|
|
|
|
var memFile *os.File
|
|
|
|
|
|
|
|
// WriteHeapFile writes heap memory to the file.
|
|
|
|
func WriteHeapFile(dataDir string) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if memFile == nil {
|
|
|
|
memFile, err = os.Create(filepath.Join(dataDir, MemFilename))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-08-23 19:30:58 +00:00
|
|
|
defer func() {
|
|
|
|
memFile.Close() //nolint: errcheck
|
|
|
|
memFile = nil
|
|
|
|
}()
|
2018-02-02 12:23:47 +00:00
|
|
|
}
|
2024-08-23 19:30:58 +00:00
|
|
|
|
2018-02-02 12:23:47 +00:00
|
|
|
runtime.GC()
|
|
|
|
err = pprof.WriteHeapProfile(memFile)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|