在Linux上,如何检查进程及其线程的CPU关联性?
这很简单。 收集所有进程ID和线程ID,然后调用taskset 。
pname="java" # for example for pid in $(pgrep "${pname}") do [ "${pid}" != "" ] || exit echo "PID: ${pid}" for tid in \ $(ps --no-headers -ww -p "${pid}" -L -olwp | sed 's/$/ /' | tr -d '\n') do taskset -cp "${tid}" # substitute thread id in place of a process id done done
输出
PID: 15695 pid 15695's current affinity list: 0 pid 15696's current affinity list: 0 pid 15697's current affinity list: 0 ...
这不是一个简单的单线程的原因是因为pgrep命令不能返回线程ID(只处理ID)。 我们使用命令ps --no-headers -ww -p "${pid}" -L -olwp执行一些额外的工作ps --no-headers -ww -p "${pid}" -L -olwp
testingUbuntu 12,bash 4。