一、怎么查看系统的上下文切换情况
使用vmstat
[root@localhost ~]# vmstat 5
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
2 0 0 151772 276 617972 0 0 10 29 23 35 0 0 100 0 0
0 0 0 151772 276 617972 0 0 0 0 12 14 0 0 100 0 0
0 0 0 151772 276 617972 0 0 0 0 13 16 0 0 100 0 0
cs:context switch 每秒上下文切换次数
in(interrupts):每秒中断次数
r(Running or Runnable):就绪队列的长度,正在运行的进程数
b(blocked):处于不可中断阻塞的进程数
使用pidstat -w 5
04时18分31秒 UID PID cswch/s nvcswch/s Command
04时18分36秒 0 6 0.40 0.00 ksoftirqd/0
04时18分36秒 0 9 1.00 0.00 rcu_sched
04时18分36秒 0 11 0.20 0.00 watchdog/0
04时18分36秒 0 401 19.80 0.00 xfsaild/dm-0
04时18分36秒 0 402 0.20 0.00 kworker/0:1H
04时18分36秒 0 2144 1.00 0.00 kworker/u256:1
04时18分36秒 0 2269 2.80 0.00 kworker/0:2
04时18分36秒 0 2333 0.60 0.00 kworker/0:0
04时18分36秒 0 2355 0.20 0.00 pidstat
cswch:每秒自愿上下文切换次数,程序无法获取资源导致的上下文切换
nvcswch:每秒非自愿上下文切换次数。时间片已到或被系统强制调度(中断、高优先级抢占)
二、使用基准测试工具压测
#使用sysbench 开启10个线程,持续时间300秒
/usr/local/bin/sysbench --threads=10 --time=300 threads run
#查看上下文切换情况
vmstat 1
#查看自愿和非自愿上下文切换情况
pidstat -w -u -t 1
#查看中断类型
watch -d cat /proc/interrupts