首页 > 解决方案 > Pprof 探查器没有捕获任何东西

问题描述


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
}

我有一个 go-app,我必须在其中比较 pprof 输出。为此main,我在函数中创建了一个cpuProfiler并启动它。我有一个跟踪退出信号的程序。之后,我启动我infinitely正在运行的函数,除非它们被使用输入中断(应用程序无限运行,除非有键盘中断)。我让应用程序运行了一段时间,希望在指定的 pprof 文件中捕获元数据,但该文件始终为空。请注意,我确实通过调用来终止分析stopCPUProfile

我究竟做错了什么?

提前致谢。

标签: godebuggingprofilinggoroutinepprof

解决方案


我的错。在信号处理中,我们要确保在退出之前停止分析。所以我们不需要使用defer.


推荐阅读