Trace usage example
import (
"fmt"
"log"
"os"
"runtime/trace"
"sync"
)
func main() {
//runtime.GOMAXPROCS(1)
// 1. 创建trace持久化的文件句柄
f, err := os.Create("trace.out")
if err != nil {
log.Fatalf("failed to create trace output file: %v", err)
}
defer func() {
if err := f.Close(); err != nil {
log.Fatalf("failed to close trace file: %v", err)
}
}()
// 2. trace绑定文件句柄
if err := trace.Start(f); err != nil {
log.Fatalf("failed to start trace: %v", err)
}
defer trace.Stop()
var wg sync.WaitGroup
wg.Add(2)
go func() {
fmt.Println(`Hello`)
wg.Done()
}()
go func() {
fmt.Println(`World`)
wg.Done()
}()
wg.Wait()
}
The terminal first executes go run main.go
Then execute go tool trace trace.out and the program will open a page using the default browser
- View trace: View the trace, you can see this execution, how many P (processors) are started, and which P the coroutine is executed on. Assist in understanding the scheduling model GMP
- Goroutine analysis: Goroutine analysis, you can view the total number of coroutines and specific coroutine executions
- Network blocking profile: Network blocking profile
- Synchronization blocking profile: Synchronization blocking profile
- Syscall blocking profile: System call blocking profile. You can see which part of the program takes time.
- Scheduler latency profile: Scheduling latency profile
- User defined tasks: User-defined tasks
- User defined regions: User-defined regions
- Minimum mutator utilization: Minimum mutator utilization
Reference links: