【问题标题】:Why is the profiler not working for me?为什么探查器不适合我?
【发布时间】:2017-11-06 11:08:44
【问题描述】:

鉴于以下玩具代码,我希望能够运行

go tool pprof cpu.prof

并获得有关waster1和waster2的有用信息,但是当我在pprof中运行top时,我得到的只是:

Showing nodes accounting for 0, 0% of 0 total
      flat  flat%   sum%        cum   cum%

问题可能是我使用 WSL 在 Windows 10 上运行 Ubuntu。

这是我正在使用的代码:

package main

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

func waster2() int {
    j := 0;
    for i := 0; i < 100; i++ {
        j += waster1()
    }
    return j
}

func waster1() int {
    j := 0;
    for i := 0; i < 10000; i++ {
        j++
    }
    return j
}

func main() {
    f, err := os.Create("cpu.prof")

    if err != nil {
        log.Fatal("could not create CPU profile: ", err)
    }

    if err := pprof.StartCPUProfile(f); err != nil {
        log.Fatal("could not start CPU profile: ", err)
    }

    defer pprof.StopCPUProfile()

    j := waster2()

    fmt.Println(j)
}

【问题讨论】:

  • 看起来它在自己运行的 Debian 上做同样的事情,所以 Windows 10 的事情是红鲱鱼。
  • 首先您需要将二进制文件作为参数提供给pprof。你的程序运行多长时间?您可能没有足够的样本来制作有意义的个人资料。
  • @JimB 为函数添加睡眠(现在超过 10 秒运行时间)和工具命令的二进制文件,仍然没有输出。
  • SIGPROF 可能在 WSL 中不起作用,我不确定如何在 Windows 上跟踪它。

标签: ubuntu go windows-10 profiling windows-subsystem-for-linux


【解决方案1】:

在 Windows 上运行也不会在分析器中产生样本。以下来自 Go 博客的 article 声明如下:

When CPU profiling is enabled, the Go program stops about 100 times per second and 
records a sample consisting of the program counters on the currently executing 
goroutine's stack.

运行您的代码所需的时间少于 2 毫秒,因此不允许分析器进行采样。将循环计数从 100 增加到 10000,然后您应该会在输出中看到一些样本。

另外,请记住关闭您的文件f。像这样:

if err != nil {
    log.Fatal("could not create CPU profile: ", err)
}
defer f.Close()

【讨论】:

  • 我在函数和延迟中添加了睡眠。它运行了 10 多秒,在 pprof 中仍然没有输出。
  • 分析器不跟踪 CPU 不工作的时间。您是否尝试过增加最大循环数?
猜你喜欢
  • 2014-01-03
  • 2011-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多