go tool trace工具

955 阅读1分钟

pprof用于性能分析,trace用于查看go运行时执行的动作,比如协程因为什么阻塞

package main

import (
        "os"
        "runtime/trace"
)

func main() {
        trace.Start(os.Stderr)
        defer trace.Stop()

        ch := make(chan string)
        go func() {
                ch <- "TRACE"
        }()

        <-ch
}

指定ip和端口启动trace,允许非本机访问,

go build -o demo demo.go 
./demo 2>trace.out 

go tool trace -http=":9999" trace.out

各个栏目介绍:

View trace 
Goroutine analysis //协程分析
Network blocking profile (⬇) //网络阻塞情况
Synchronization blocking profile (⬇) //同步阻塞情况
Syscall blocking profile (⬇) //系统调用阻塞情况
Scheduler latency profile (⬇)
User-defined tasks
User-defined regions
Minimum mutator utilization

详细可以参考: juejin.cn/post/684490…

错误整理

点击 Network blocking profile 报错:

failed to execute go tool pprof: exit status 1
failed to execute dot. Is Graphviz installed? Error: exec: "dot": executable file not found in $PATH

下载安装Graphviz, graphviz.org/download/

yum install graphviz

本地安装k8s导致采用了google yum源,需要修改为阿里源或者网易源:

mv kubernetes.repo kubernetes.repo.bak
yum clean all 
yum makecache 
yum update 
yum install graphviz