Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
What OS? What version? What kernel? –  Mikel May 25 '12 at 16:48
    
GNU/Linux 2.6.18 –  luntain May 25 '12 at 17:28
4  
Check /proc/[pid]/status –  Kevin May 25 '12 at 17:34

4 Answers 4

pidstat(1) - Report statistics for Linux tasks. According to man pidstat it's so easy as just pidstat -w …

share|improve this 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 }'
share|improve this answer

To get a record of an entire process run, you can use the GNU time utility (don't confuse it with the bash builtin) with the -v option. Here's an example with unrelated lines of output removed:

$ `which time` -v ls
a.out  exception_finder.cpp  log.txt
    Command being timed: "ls"
               ...
    Voluntary context switches: 1
    Involuntary context switches: 2
               ...
    Exit status: 0
share|improve this answer

Write the following script to file (ctx.sh). with ctx.sh <core> you will see all the processes running on a given core and changing nv-context switches will be highlighted. Looking at this, you will be able to identify which are the competing processes for the core.

#!/bin/bash

if [[ $# -eq 0 ]]
then
   echo "Usage:"
   echo "$0 <core>"
   exit 1
fi

if [[ -z $2 ]]
then
   watch -d -n .2 $0 $1 nw
fi

ps -Leo lastcpu:1,tid,comm | grep "^$1 " | awk '{printf $3": ";system("cut -d\" \" -f3  /proc/"$2"/task/"$2"/schedstat 2>/dev/null")}' | sort -k 1 | column -t
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.