I have a bash script that calculates voluntary and non-voluntary context switches created by a thread over a period of time. I'm not sure if this will serve your purpose, but I will send it anyway.
This script iterates over all the threads of the process and writes "voluntary_ctxt_switches" & "nonvoluntary_ctxt_switches" from /proc/< process-id>/task/< thread-id>/status . Usually I record these counters at the beginning of a performance run and write again at the end of the run, and then calculate the difference as the resulting vol & non-vol ctx switches during the performance run.
pid=`ps -ef | grep <process name> | grep $USER | grep -v grep | awk '{print $2}'` echo "ThreadId;Vol_Ctx_Switch;Invol_Ctx_Switch" for tid in `ps -L --pid ${pid} | awk '{print $2}'` do if [ -f /proc/$pid/task/$tid/status ] then vol=`cat /proc/$pid/task/$tid/status | grep voluntary_ctxt_switches | grep -v nonvoluntary_ctxt_switches | awk '{print $NF}'` non_vol=`cat /proc/$pid/task/$tid/status | grep nonvoluntary_ctxt_switches | awk '{print $NF}'` fi echo "$tid;$vol;$non_vol" done
The script bit is heavy, in my case the process has about 2500 threads. The total time to collect ctx keys is about 10 seconds.
source share