gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 02:11:04 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com