This is the original link http://serverfault.com/questions/190049/find-out-which-task-is-generating-a-lot-of-context-switches-on-linux
The main thing is to use pidstat-w to view context switches,
[email protected]:~# pidstat -w 1linux 4.2.0-16-generic (Wis-virtual-machine) April 08, 2016 _i686_ (1 CPU) 10 19 minutes and 27 seconds UID pid cswch/s nvcswch/s command10 19 minutes, 28 seconds 0 3 3.88 0.00 ksoftirqd/010 19 minutes 28 seconds 0 7 13.59 0.00 rcu_sched10 19 minutes, 28 seconds 0 10 0.97 0.00 watchdog/010 19 minutes 28 seconds 0 833 7.77 0.00 xorg10 19 minutes, 28 seconds 1000 1564 6.80 7.77 compiz
The-t option lets you view the processes under the process.
[Email protected]:~# pidstat-w-T
Linux 4.2.0-16-generic (wis-virtual-machine) April 19, 2016 _i686_ (1 CPU)
08:34 24 sec UID tgid TID cswch/s nvcswch/s Command
08:34 24 sec 0 1-1.63 1.39 systemd
08:34 24 sec 0-1 1.63 1.39 |__systemd
08:34 24 sec 0 2-0.32 0.00 Kthreadd
08:34 24 sec 0-2 0.32 0.00 |__kthreadd
08:34 24 sec 0 3-3.00 0.01 ksoftirqd/0
08:34 24 sec 0-3 3.00 0.01 |__ksoftirqd/0
There is also a watch command when viewing the data, and you can have a man look at it, (watch-tdn0.5 pidstat-w)
View multiple processes for context switching (find which process take the most context switch)