package main
import (
"fmt"
"os"
"runtime/trace"
)
func main() {
// 创建trace
f,err := os.Create("trace.out")
if err != nil{
panic(err)
}
defer f.Close()
// 启动trace
err = trace.Start(f)
if err != nil {
panic(err)
}
fmt.Println("hello GMP")
// 停止trace
trace.Stop()
}
执行命令
go tool trace trace.out
点击 view trace
debug查看
GODEBUG-schedtrace=1000 ./main
package main
import (
"fmt"
"time"
)
func main() {
for i:=0;i<5;i++{
fmt.Println(i)
time.Sleep(time.Second)
}
}
╭─deepin@deepin-PC ~/go/test1
╰─$ GODEBUG=schedtrace=1000 ./main
SCHED 0ms: gomaxprocs=6 idleprocs=3 threads=5 spinningthreads=1 idlethreads=0 runqueue=0 [0 0 0 0 0 0]
0
1
SCHED 1002ms: gomaxprocs=6 idleprocs=6 threads=5 spinningthreads=0 idlethreads=3 runqueue=0 [0 0 0 0 0 0]
2
SCHED 2009ms: gomaxprocs=6 idleprocs=6 threads=5 spinningthreads=0 idlethreads=3 runqueue=0 [0 0 0 0 0 0]
3
SCHED 3016ms: gomaxprocs=6 idleprocs=6 threads=5 spinningthreads=0 idlethreads=3 runqueue=0 [0 0 0 0 0 0]
4
SCHED 4022ms: gomaxprocs=6 idleprocs=6 threads=5 spinningthreads=0 idlethreads=3 runqueue=0 [0 0 0 0 0 0]