【问题标题】:go tool pprof using application PID instead of http endpointgo tool pprof 使用应用程序 PID 而不是 http 端点
【发布时间】:2015-09-25 16:46:34
【问题描述】:

现在,我使用 go tool pprof 来分析 Go 应用程序,如下所示:

go tool pprof http://localhost:8080/debug/pprof/profile

我想在一个在未知端口上运行 http 服务器的任意 Go 进程上使用 pprof 工具。我拥有的关于该进程的唯一信息是它的 PID。我需要做以下两件事之一:

  • 从其 PID 中获取 Go 进程的端口号。
  • 直接分析正在运行的进程。例如,go tool pprof 10303 之类的 PID 为 10303。

这些都行吗?

【问题讨论】:

    标签: go pprof


    【解决方案1】:

    服务发现旨在解决此类问题。 一个简单的解决方案是为每个 PID 创建一个 tmp 文件,将每个端口写入相应的文件。并在需要时读取端口go tool pprof

    http.ListenAndServe("localhost:" + PORT, nil)
    tmpFilePath := filePath.Join(os.TempDir(), "myport_" + strconv.FormatInt(os.Getpid(), 10))
    f, _ := os.OpenFile(tmpFilePath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
    f.Write(byte[](strconv.FormatInt(PORT, 10)))
    f.Close()
    

    在 go tool prof bash 中:go tool http://localhost:`cat /tmp/myport_10303`/debug/pprof/profile

    未经实际测试,可能有语法错误

    更新:

    另一种不改变go源的方法是,使用诸如netstat/lsof之类的bash命令找出go进程的监听端口。喜欢:

    netstat -antp | grep 10303| grep LISTEN | awk '{ print $4 }' | awk -F: '{print $2}'
    

    我认为不是最好的 bash 脚本,仅供参考。

    【讨论】:

    • 这不是我真正想要的。我想分析一个任意的 Go 进程(不一定是我创建的)。
    • 你一定不知道 pprof 是如何工作的。如果没有嵌入 pprof 服务器,则无法分析 go 进程。意味着您无法分析任意 Go 进程。
    • 我知道该进程公开了一个 pprof 端点。我想找到该进程正在运行的本地端口。您的建议是可能的,但它需要我更改许多 Go 应用程序的代码。
    • 这是个好主意。唯一的问题是这些命令需要 sudo(我将无法使用)。你知道如何在没有 sudo 的情况下做到这一点吗?
    • 在同一用户中运行 go 进程会有所帮助
    猜你喜欢
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 2022-01-15
    • 2019-05-30
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    相关资源
    最近更新 更多