ubuntu - 为什么探查器不适合我?

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

给出以下玩具代码,我希望能够运行

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)
}

最佳答案

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

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()

关于ubuntu - 为什么探查器不适合我?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47135559/

相关文章:

postgresql - Postgres 9.4 0 undefined symbol - PQhostaddr

ios - 是否可以使用 gomobile 呈现/查看 html?

security - 在没有管理员权限的情况下在 Windows 10 中设置开发环境

windows - Chrome 中不显示僧伽罗语

windows-10 - 如何将 gcloud 添加到 Windows 10 中的路径

python - TensorFlow:libcudart.so.7.5:无法打开共享对象文件:没有这样的文件或目录

linux - 以非交互方式运行 rustup 的 curl-fetched 安装程序脚本

node.js - 我应该使用包管理器还是从源代码在 Ubuntu 上安装 node.js?

go - 为什么我不能使用 flag.StringVar 将指针传递给 fmt.Println?

go - GoLand找不到引用: “Unresolved reference ' NewRGBA' ”