Shell – How to see how many context switches a process makes

linuxprocessshell

I want to see if my process makes a lot of context switches. I also want to see how manpulating task groups affects the number of context switches.

Best Answer

You can view information about your process's context switches in /proc/<pid>/status.

$ pid=307
$ grep ctxt /proc/$pid/status
voluntary_ctxt_switches:        41
nonvoluntary_ctxt_switches:     16

To see these numbers updating continuously, run

$ # Update twice a second.
$ watch -n.5 grep ctxt /proc/$pid/status

To get just the numbers, run

$ grep ctxt /proc/$pid/status | awk '{ print $2 }'