dpkk8687 2017-11-06 11:08
浏览 79

为什么探查器对我不起作用?

Given the following toy code, I would expect to be able to run

go tool pprof cpu.prof

and get useful information about waster1 and waster2, but when I run top in pprof, all I get is:

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

It is possible the problem is that I am using WSL to run Ubuntu on Windows 10.

Here is the code I am using:

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)
}
  • 写回答

1条回答 默认 最新

  • doucuyu2259 2017-11-06 11:52
    关注

    Running on windows also produces no samples in the profiler. The following article from the go blog states the following:

    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.
    

    When running your code takes less that 2ms, so does not allow the profiler to take samples. Increase your loop count from 100 to 10000 and then you should see some samples in your output.

    On another note, remember to close your file f. Like so:

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

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)