Linux性能学习(1.4):CPU_如何查看CPU上下文切换参数


参考资料:

  1. vmstat:一个标准的报告虚拟内存统计工具

在前面大致了解了上下文切换的相关知识,那么如何在系统中查看上下文切换相关的参数?

1 系统总体上下文参数

使用vmstat可以查看系统总体的上下文切换参数。


# vmstat 5  //每5秒输出一次数据
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0    780 195104 923332 1057900    0    0    21     5   30   52  2  0 97  1  0
 0  0    780 195096 923332 1057900    0    0     0     0   36   72  0  0 100  0  0

上面各个字段含义如下:
在这里插入图片描述

2 进程的上下文切换参数

使用pidstate -w可以查看当前系统中所有进程的上下文切换参数:

# pidstat -w
Linux 4.15.0-232-generic (st) 	2020年1月28日 	_x86_64_	(2 CPU)

23时15分43秒   UID       PID   cswch/s nvcswch/s  Command
23时15分43秒     0         1      0.05      0.04  systemd
23时15分43秒     0         2      0.00      0.00  kthreadd
23时15分43秒     0         4      0.00      0.00  kworker/0:0H
23时15分43秒     0         6      0.00      0.00  mm_percpu_wq
23时15分43秒     0         7      0.07      0.00  ksoftirqd/0
23时15分43秒     0         8      3.40      0.00  rcu_sched
23时15分43秒     0         9      0.00      0.00  rcu_bh
23时15分43秒     0        10      0.01      0.00  migration/0
23时15分43秒     0        11      0.25      0.00  watchdog/0
23时15分43秒     0        12      0.00      0.00  cpuhp/0
23时15分43秒     0        13      0.00      0.00  cpuhp/1
23时15分43秒     0        23      0.25      0.00  watchdog/1
23时15分43秒     0        15      0.01      0.00  migration/1
23时15分43秒     0        16      0.40      0.01  ksoftirqd/1

cswch表示每秒自愿上下文切换,指进程无法获取所需要的资源,而导致的上下文切换。比如IO、内存等系统资源不足时;
nvcswch表示每秒非自愿上下文切换,指因为调度算法导致的上下文切换。比如时间片已到、大量进程争抢、发送中断等事件。

3 其它

过多的上下文切换会导致系统不断的保存和恢复数据上,因此可能会导致CPU使用率过高,因此遇到CPU使用率过高时,可查看是否是上下文切换导致的。

猜你喜欢

转载自blog.csdn.net/u011003120/article/details/128286059