有没有办法查看每个线程生成多少上下文切换?(如果可能,进出)要么在 X/s 中,要么让它运行并在一段时间后提供聚合数据。(在linux或windows上)
我发现只有工具可以为整个操作系统或每个进程提供聚合的上下文切换数。
我的程序进行了许多上下文切换(50k/s),可能很多都没有必要,但我不确定从哪里开始优化,其中大部分发生在哪里。
有没有办法查看每个线程生成多少上下文切换?(如果可能,进出)要么在 X/s 中,要么让它运行并在一段时间后提供聚合数据。(在linux或windows上)
我发现只有工具可以为整个操作系统或每个进程提供聚合的上下文切换数。
我的程序进行了许多上下文切换(50k/s),可能很多都没有必要,但我不确定从哪里开始优化,其中大部分发生在哪里。
在最近的 GNU/Linux 系统上,您可以使用 SystemTap 在每次调用 sched_switch() 时收集您想要的数据。schedtimes.stp 示例可能是一个好的开始:http: //sourceware.org/systemtap/examples/keyword-index.html#SCHEDULER
我写了一个小脚本来查看进程的特定线程的详细信息。通过执行此脚本,您还可以看到上下文切换。
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
希望这会有所帮助。
我有一个 bash 脚本,它计算线程在特定时间范围内进行的自愿和非自愿上下文切换。我不确定这是否会达到您的目的,但无论如何我都会发布它。
该脚本循环遍历进程的所有线程并"voluntary_ctxt_switches" & "nonvoluntary_ctxt_switches"
从/proc/< process-id>/task/< thread-id>/status
. 我通常做的是在性能运行开始时记录这些计数器并在运行结束时再次记录,然后vol & non-vol
在性能运行期间计算差异作为总 ctx 开关。
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
脚本有点重,在我的情况下,进程大约有 2500 个线程。收集 ctx 开关的总时间约为 10 秒。