性能分析方法总结之trace
1、trace可以解决哪些问题
- Goroutine 在执行时会做哪些操作?
- 执行/阻塞了多长时间?
- 在什么时候阻止?
- 在哪里被阻止的?
- 谁又锁/解锁了它们?
- GC 是怎么影响到 Goroutine 的执行的?
2、代码实例
package main
import (
"os"
"runtime/trace"
)
func main() {
trace.Start(os.Stderr)
defer trace.Stop()
ch := make(chan string)
go func() {
ch <- "Go 语言编程之旅"
}()
<-ch
}
3、使用方法
首先执行命令,go run main.go 2> trace.out。
然后,执行命令 go tool trace trace.out,启动可视化界面。
可视化界面如下所示:

-
- View trace:查看跟踪
- Goroutine analysis:Goroutine 分析
- Network blocking profile:网络阻塞概况
- Synchronization blocking profile:同步阻塞概况
- Syscall blocking profile:系统调用阻塞概况
- Scheduler latency profile:调度延迟概况
- User defined tasks:用户自定义任务
- User defined regions:用户自定义区域
- Minimum mutator utilization:最低 Mutator 利用率

浙公网安备 33010602011771号