Find out which task is generating a lot of context switches on linux

Solution 1:

Try using

pidstat -wt

The 't' option shows the threads also. It might be a thread who is doing the context switches .

Solution 2:

Well, quite interesting case. Try observing watch -tdn1 cat /proc/interrupts. Do you see any valuable changes there?

Solution 3:

In newer kernel version

sudo perf record -e context-switches -a  # record the events

# then ctrl+c

sudo perf report # inspect the result

This will give you the exactly result about context-switches events.

And you may be find the reason caused the context-switches by append "-g" flags (The readable result determined by symbol information)

sudo perf record -e context-switches -a -g