大网数据广告活动
服务器运维问答

3 different methods to count the number of context switches for a specific workload provide 2 different answers

· 52CE 我爱测

I have a Centos7 system on a Google cloud machine, running a multithreaded database server that executes a particular set of queries in 70 secs (plus or minus 1 sec). I want to know how many context switches the server makes runn…

问题原文

原帖链接 · Richard Gostanian · CC BY-SA 4.0

I have a Centos7 system on a Google cloud machine, running a multithreaded database server that executes a particular set of queries in 70 secs (plus or minus 1 sec). I want to know how many context switches the server makes running this workload.

The server pid is 11850.

I used 3 different counting methods:

method 1:

start

perf -e context-switches -p 11850

in one window and immediately run the database client in another window. Then kill perf as soon as the client completes.

The resulting output is

 Performance counter stats for process id '11850':

     5,831,206      context-switches          #    0.004 M/sec

    70.607962486 seconds time elapsed

Method 2

start

pidstat -tw -p 11850 70 1 >pidstat.out

in one window and immediately run the client in another window.

Both pidstat and the client will complete within a second of each other. When that happens add all the values of cswch/s and nvcswch/s in pidstat.out and multiply the result by 70.

This gives an almost identical result to what Method 1 does.

Method 3

Run a script containing the following commands in one window

vmstat -s|grep "CPU context switches" 
sleep 70
vmstat -s|grep "CPU context switches" 

and then immediately run the client in another window. Both the client and the 2nd vmstat will finish within a second of each other.

The vmstat outputs are

    439394923 CPU context switches
    450457926 CPU context switches

Subtracting the first number from the 2nd gives 11,063,003 which is almost double the result of the other two methods.

Since nothing else was running on the system, and the normal rate of context switches on an idle system is about 100/sec, it appears that the vmstat method is double counting context switches.

Is this a bug, or am I missing something?

已采纳解答

原帖链接 · John Mahowald · CC BY-SA 4.0

The assumption that only the examined PID does context switches needs examining.

Review context switches across all CPUs, such as with perf top -e context-switches -a. Probably you will see other tasks, including other database threads and kernel things, jumping on and off CPU.

More useful than just a context switch counter would be stats like instructions per cycle. Get those with perf stat. Good IPC is greater than one. Of course, for CPU bound workloads. Slow storage means an eternity of the CPU waiting.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-11-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。