"perf sched record" 如何计算上下文切换?

How does "perf sched record" count context switches?

我使用 perf sched record -p 8827 记录了特定程序的调度程序 activity。但是当我生成延迟报告时,它显示只有一个上下文切换,而要求它转储原始事件时显示许多切换(下面摘录)。这怎么可能有意义?我假设我以某种方式误解了事情?切换到 kworker 不算数吗?

perf sched latency 输出:

-----------------------------------------------------------------------------------------------------------------
Task                  |   Runtime ms  | Switches | Average delay ms | Maximum delay ms | Maximum delay at       |
-----------------------------------------------------------------------------------------------------------------
:4320:4320            |      0.000 ms |        1 | avg:    0.000 ms | max:    0.000 ms | max at:      0.000000 s
-----------------------------------------------------------------------------------------------------------------
TOTAL:                |      0.000 ms |        1 |
---------------------------------------------------

perf sched script 输出:

test_program  8827 [008] 1727201.302534:       sched:sched_switch: test_program:8827 [100] R ==> kworker/8:0:4320 [120]
test_program  8827 [008] 1727201.303524: sched:sched_stat_runtime: comm=test_program pid=8827 runtime=954318 [ns] vruntime=30559299144
test_program  8827 [008] 1727202.303521: sched:sched_stat_runtime: comm=test_program pid=8827 runtime=999997430 [ns] vruntime=30559414
test_program  8827 [008] 1727202.303528: sched:sched_stat_runtime: comm=test_program pid=8827 runtime=6419 [ns] vruntime=3055941451126
test_program  8827 [008] 1727202.303529:       sched:sched_wakeup: kworker/8:0:4320 [120] success=1 CPU:008
test_program  8827 [008] 1727202.303532:       sched:sched_switch: test_program:8827 [100] R ==> kworker/8:0:4320 [120]
test_program  8827 [008] 1727202.303738: sched:sched_stat_runtime: comm=test_program pid=8827 runtime=192062 [ns] vruntime=30559414533
test_program  8827 [008] 1727202.303739: sched:sched_stat_runtime: comm=test_program pid=8827 runtime=908 [ns] vruntime=3055941453351
test_program  8827 [008] 1727202.304471: sched:sched_stat_runtime: comm=test_program pid=8827 runtime=731656 [ns] vruntime=30559414617
test_program  8827 [008] 1727203.304469: sched:sched_stat_runtime: comm=test_program pid=8827 runtime=999997968 [ns] vruntime=30559529
test_program  8827 [008] 1727203.304531: sched:sched_stat_runtime: comm=test_program pid=8827 runtime=62107 [ns] vruntime=305595299908
test_program  8827 [008] 1727203.304532: sched:sched_stat_runtime: comm=test_program pid=8827 runtime=1006 [ns] vruntime=3055952999092
test_program  8827 [008] 1727204.302373:       sched:sched_switch: test_program:8827 [100] R ==> kworker/8:0:4320 [120]
test_program  8827 [008] 1727204.303365: sched:sched_stat_runtime: comm=test_program pid=8827 runtime=979718 [ns] vruntime=30559645220
test_program  8827 [008] 1727205.303363: sched:sched_stat_runtime: comm=test_program pid=8827 runtime=999997445 [ns] vruntime=30559760
test_program  8827 [008] 1727205.303368: sched:sched_stat_runtime: comm=test_program pid=8827 runtime=5898 [ns] vruntime=3055976058680
test_program  8827 [008] 1727205.303370:       sched:sched_wakeup: kworker/8:0:4320 [120] success=1 CPU:008
test_program  8827 [008] 1727205.303372:       sched:sched_switch: test_program:8827 [100] R ==> kworker/8:0:4320 [120]

perf sched latency 'Switches' 列仅包含 involuntary/nonvoluntary 上下文切换。而 perf sched script 列出所有上下文切换(即自愿和非自愿的)。

您可以使用例如GNU 时间。例如:

perf sched record /usr/bin/time -v yourtestprogram

或者通过其他方式监控 /proc/$pid/status 中上下文切换(每种)的数量。

另请参阅 how perf sched latency counts(即 nb_atoms 是 'Switches' 值,例如从该源文件中的该函数追溯)。