Skip to main content

execution-tracer

📅 2026-02-05 ✏️ 2026-03-06 CS GO
No related notes

1 · Go Execution Tracer#

go version go1.22.1 linux/amd64

1.1 · 是什么

Go execution traces provide a moment-to-moment view of what happens in a Go program over some duration.
This information is invaluable in understanding program behavior over time and can be leveraged to achieve significant performance improvements.

Execution TracerGo 语言内置的一个工具,用于:

  1. 跟踪、记录程序的执行过程,执行过程由一系列事件组成,包括:函数的调用、Goroutine 的调度、系统调用等等;
  2. 基于1中的跟踪记录,分析、诊断程序中的问题;

Execution Tracer 分为两部分,分别用于实现以上两点:

  1. trace
  2. go tool trace 工具

1.2 · 使用场景

  1. 性能优化: 识别哪些代码路径消耗了大量的时间
  2. 资源分析: 了解程序对 CPU、内存和其他资源的使用情况
  3. 并发调试: 了解程序中 Goroutine 的创建、调度、执行和销毁
  4. 调度器分析: 对调度器(Scheduler)的详细分析, 查看 Goroutine 的调度情况、等待队列、抢占情况等信息
  5. 事件分析: 支持跟踪和分析自定义事件,在程序中插入事件记录代码后进行分析,理解程序的执行逻辑和流程

1.3 · trace 包#

1.3.1 · design#

  1. 时间戳、排序: 操作系统的单调时钟、使用G的id建立事件的部分顺序
  2. 针对线程M, 而不是P
  3. 分割:?
  4. 清理:事件类型更准确的命名…

1.3.1.1 · 1#

1.3.1.2 · 2#

1.3.1.3 · 3#

1.3.1.4 · 4#

1.4 · go tool trace 工具#

1.5 · 示例