调度器状态的查看方法

在Go中调度器状态的查看方法是用:

  1. > GODEBUG=schedtrace=1000 godoc -http=:6060
  2. SCHED 0ms: gomaxprocs=4 idleprocs=2 threads=5 spinningthreads=1 idlethreads=0 runqueue=0 [0 0 0 0]
  3. SCHED 1007ms: gomaxprocs=4 idleprocs=4 threads=27 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]
  4. SCHED 2016ms: gomaxprocs=4 idleprocs=3 threads=27 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]
  5. SCHED 3017ms: gomaxprocs=4 idleprocs=3 threads=27 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]
  6. SCHED 4018ms: gomaxprocs=4 idleprocs=1 threads=27 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]
  7. SCHED 5018ms: gomaxprocs=4 idleprocs=1 threads=27 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]
  8. SCHED 6018ms: gomaxprocs=4 idleprocs=2 threads=27 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]
  9. SCHED 7018ms: gomaxprocs=4 idleprocs=3 threads=27 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]
  10. SCHED 8019ms: gomaxprocs=4 idleprocs=3 threads=27 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]
  11. SCHED 9020ms: gomaxprocs=4 idleprocs=0 threads=27 spinningthreads=1 idlethreads=4 runqueue=0 [0 0 0 0]
  12. SCHED 10020ms: gomaxprocs=4 idleprocs=3 threads=27 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]

GODEBUG这个Go运行时环境变量非常有用,我们通过给其传入不同的key=value,…组合,可以查看Go的runtime会输出不同的调试信息,比如在这里我们给GODEBUG传入了”schedtrace=1000″,其含义就是每1000ms,打印输出一次goroutine scheduler的状态,每次一行.其他的状态如下:

  1. SCHED 1007ms: gomaxprocs=4 idleprocs=4 threads=27 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]
  2. SCHED 调试信息输出标志字符串,代表本行是goroutine scheduler的输出.
  3. 1007ms 即从程序启动到输出这行日志的时间.
  4. gomaxprocs: P的数量.
  5. idleprocs: 处于idle状态的P的数量;通过gomaxprocsidleprocs的差值,我们就可知道执行go代码的P的数量.
  6. threads: os threads的数量,包含scheduler使用的m数量,加上runtime自用的类似sysmon这样的thread的数量.
  7. spinningthreads: 处于自旋状态的os thread数量.
  8. idlethread: 处于idle状态的os thread的数量.
  9. runqueue=0 go scheduler全局队列中G的数量.
  10. [0 0 0 0]: 分别为0Plocal queue中的G的数量.