【问题标题】:`go tool pprof` reports error `unrecognized profile format``go tool pprof` 报告错误`unrecognized profile format`
【发布时间】:2022-01-15 18:58:00
【问题描述】:

我正在尝试使用此演示生成 goroutine 配置文件的 PNG,但它报告错误 parsing profile: unrecognized profile format. failed to fetch any source profiles

package main

import (
    "fmt"
    "log"
    "os"
    "os/exec"
    "runtime/pprof"
    "time"
)

func main()  {
    // start demo go routine
    go func() {
        for {
            time.Sleep(time.Second)
        }
    }()

    // get executable binary path
    exePath, err := os.Executable()
    if err != nil {
        log.Println(err.Error())
        return
    }
    log.Println("exePath", exePath)

    // generate goroutine profile
    profilePath := exePath + ".profile"
    f, err := os.Create(profilePath)
    if err != nil {
        log.Println(err.Error())
        return
    }
    defer f.Close()
    if err := pprof.Lookup("goroutine").WriteTo(f, 2); err != nil {
        log.Println(err.Error())
        return
    }

    // generate PNG
    pngPath := exePath + ".png"
    result, err := exec.Command("go", "tool", "pprof", "-png", "-output", pngPath, exePath, profilePath).CombinedOutput()
    if err != nil {
        log.Println("make error:", err.Error())
    }
    log.Println("make result:", string(result))
}

【问题讨论】:

    标签: go goroutine pprof


    【解决方案1】:

    您在pprof.Lookup("goroutine").WriteTo(f, 2) 中使用了2 的调试值。这将产生一个 pprof 工具无法解析的输出,它意味着人类可以直接阅读。

    来自pprof.(*Profile).WriteTo 文档:

    调试参数启用附加输出。传递 debug=0 会写入 https://github.com/google/pprof/tree/master/proto#overview 中描述的 gzip 压缩协议缓冲区。传递 debug=1 会写入带有 cmets 的传统文本格式,将地址转换为函数名称和行号,以便程序员无需工具即可读取配置文件。

    预定义的配置文件可以为其他调试值指定含义;例如,在打印“goroutine”配置文件时,debug=2 表示以 Go 程序因未恢复的恐慌而死时使用的相同形式打印 goroutine 堆栈。

    将此行更改为 pprof.Lookup("goroutine").WriteTo(f, 0) 即可解决问题。


    此外,与问题无关,但您可能需要考虑在 pprof 命令中使用文件 f.Close() 之前关闭它。 WriteTo 应该将文件刷新到磁盘,但不会造成伤害。

    if err := pprof.Lookup("goroutine").WriteTo(f, 2); err != nil {
        log.Println(err.Error())
        return
    }
    
    if err := f.Close(); err != nil {
        log.Println(err.Error())
        return
    }
    
    // generate PNG
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-30
      • 2015-08-14
      • 2015-09-25
      • 2014-04-07
      • 1970-01-01
      • 2023-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多