Go使用
tracer跟踪器捕获各种执行事件,如 goroutine 创建/阻塞/解阻塞、系统调用进入/退出/阻塞、GC 相关事件、堆大小变化、处理器启动/停止等,并以紧凑的形式将其写入缓冲区。
介绍
Go trace 能大捕获多数事件,并能精确的纳秒级时间戳和堆栈跟踪。
- 代码实现 runtime/trace
使用
net/http/pprof
wget -O trace.out http://localhost:6060/debug/pprof/trace?seconds=5
# 打开结果 web 浏览器
go tool trace trace.out- web 内容
- Event timelines for running goroutines
View trace: 以图形页面的形式渲染和展示tracer的数据,组成- 时间线(timeline)
- 采样状态区(STATS)
- OS 线程
- 等
Goroutine analysisGoroutine 的详细信息
- Profiles
Network blocking profile用pprof profile形式的调用关系图展示同步阻塞耗时情况Synchronization blocking profile用pprof profile形式的调用关系图展示同步阻塞耗时情况Syscall blocking profile用pprof profile形式的调用关系图展示系统调用阻塞耗时情况Scheduler latency profile用pprof profile形式的调用关系图展示调度器延迟情况
- User-defined tasks and regions
User-defined tasks用户自定义trace的taskUser-defined regions用户自定义trace的region
- Garbage collection metrics
Minimum mutator utilization分析GC对应用延迟和吞吐影响情况的曲线图
- Event timelines for running goroutines
程序
- demo.go
package main
import (
"os"
"runtime/trace"
)
func main() {
# 方法一:输出到标准输出
trace.Start(os.Stdout)
defer trace.Stop()
# 方法二:输出到文件
# f, _ := os.Create("trace.out")
# defer f.Close()
# trace.Start(f)
# defer trace.Stop()
// you biz code
...
}- 分析 trace
go tool trace trace.out测试
go test -trace trace.out ./...