func main() {
runtime.SetCPUProfileRate(500)
cpuProfiler := "profile.prof"
f, _ := os.Create(cpuProfiler)
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
onKill := func(c chan os.Signal) {
select {
case <-c:
defer f.Close()
defer pprof.StopCPUProfile()
defer os.Exit(0)
}
}
go onKill(c)
streamCAN, streamIMU, done := mainfunctions.DialUpConnection(logger, configread.GetString("library.development.grpctarget"))
rawCANData := mainfunctions.GetCANDataFromStream(logger, done, streamCAN)
rawIMUData := mainfunctions.GetIMUDataFromStream(logger, done, streamIMU)
moduleData := mainfunctions.DistributeDataFromStream(logger, done, rawCANData, rawIMUData)
log.Debug(moduleData)
mainfunctions.DistributeDataFromStream(logger, done, rawCANData, rawIMUData)
channelOut := mainfunctions.RunAlgos(logger, done, moduleData)
log.Debug(channelOut)
mainfunctions.ReturnDataToStream(logger, done, channelOut)
// subscribe to system signals
}
I have a go-app in which I have to compare the pprof output. For that purpose in the main
function, I create a cpuProfiler
and start it. I have a go routine that tracks signals for exit. After that I launch my infinitely
running functions unless they are interrupted using using input(The app runs infinitely unless there is a key board interrupt). I let the app run for a while hoping to capture meta in the designated pprof file, but that file is always empty. Please do note that I do terminate the profiling by calling stopCPUProfile
.
What am I doing wrong?
Thanks in advance.
My bad. In the signal handling, we want to ensure that we stop the profiling before we quit. So we need not use
defer
.