首页 > 解决方案 > 一个线程的自愿_ctxt_switches怎么会减少呢?

问题描述

我尝试使用以下脚本获取线程的上下文切换:

text=`cat /proc/$pid/task/$tid/sched | grep voluntary_switches`
ts=`date +%s%3N`
vol=`echo "$text" | grep nr_vol | cut -d':' -f2 | awk '{$1=$1};1'`
non_vol=`echo "$text" | grep nr_invol | cut -d':' -f2 | awk '{$1=$1};1'`

echo "$tid,$ts,$vol,$non_vol"

我可以得到一个由 4 列组成的输出,它们是

  1. 线程 ID
  2. 以毫秒为单位的时间戳
  3. 自愿的_ctxt_switches
  4. 非自愿_ctxt_switches

我运行脚本 5 次,得到了第 2、3、4 轮的结果:

<tid>,1531390066833,10,0
<tid>,1531390082618,10,0
<tid>,1531390098127,7,0

如您所见,timestamp 不断增加,但 voluntainty_ctxt_switches 减少了。

我不明白这怎么会发生,因为 自愿性_ctxt_switches 是一个累积计数器。

标签: linuxlinux-kerneldevops

解决方案


推荐阅读