perf stat
displays some interesting statistics that can be gathered from examining hardware and software counters.
In my research, I couldn't find any reliable information about what counts as a context-switch in perf stat
. In spite of my efforts, I was unable to understand the kernel code in its entirety.
Suppose my InfiniBand network application calls a blocking read
system call in the event mode 2000 times and perf stat
counts 1,241 context switches. The context-switches refer to either the schedule-in process or the schedule-out process, or both?
The __schedule()
function (kernel/sched/core.c
) increments the switch_count
counter whenever prev != next
.
It seems that perf stats
' context-switches include involuntary switches as well as voluntary switches.
It seems to me that only deschedule events are counted if the current context runs the schedule code and increases the nvcsw
and nivcsw
counters in the task_struct
.
output from perf stat -- my_application
:
1,241 context-switches
Meanwhile, if I only count the sched:sched_switch
event the output is close to the expected number.
output from perf stat -e sched:sched_switch -- my_application
:
2,168 sched:sched_switch
Is there a difference between context-switches
and the sched_switch
- event?