Counting context switches per thread

Is there a way to find out how many variables switch in each thread? (both inside and outside, if possible) either in X / s, or to run and provide aggregated data after some time. (either on Linux or on windows)

I found only tools that give an aggregate context switch number for whole os or for a process.

My program makes a lot of context switches (50 fps), probably a lot is not needed, but I'm not sure where to start the optimization, where most of them occur.

+4
source share
3 answers

On recent GNU / Linux systems, you can use SystemTap to collect the required data each time sched_switch () is called. The schedtimes.stp example is probably a good start: http://sourceware.org/systemtap/examples/keyword-index.html#SCHEDULER

+2
source

Linux

I wrote a small script to see the details of a specific process thread. By executing this script, you can also see the context switch.

if [ "$#" -ne 2 ]; then echo "INVALID ARGUMENT ERROR: Please use ./see_thread.sh processName threadNumber" exit fi ls /proc/`pgrep $1`/task | head -n$2 | tail -n+$2>temp cat /proc/`pgrep $1`/task/`cat temp`/sched 

Hope this helps.

0
source

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.

0
source

Source: https://habr.com/ru/post/1304091/


All Articles