Skip to content

Instantly share code, notes, and snippets.

@nutrino
Created March 15, 2023 09:20
Show Gist options
  • Save nutrino/3cc524c797f3908eb8c5fdf9f1cb3b21 to your computer and use it in GitHub Desktop.
Save nutrino/3cc524c797f3908eb8c5fdf9f1cb3b21 to your computer and use it in GitHub Desktop.
Set unmatched CPU affinity with the help of the ChatGPT
#!/bin/bash
# Use a while loop to read each line of output from ps
while read -r line; do
if [[ $line == *-cpu* ]]; then
#echo "$line"
pid=$(echo $line | awk '{print $1}')
no=$(echo $line | awk '{print $2}')
cmd=$(echo $line | awk '{$1=$2=""; print $0}')
cpu=$(echo $cmd | grep -oE '\-cpu [0-9]+' | awk '{print $2}')
if [[ "$no" != "$cpu" ]]; then
echo "PID: $pid, REALCPU: $no, CPUID: $cpu"
taskset -cp $cpu $pid
fi
fi
done < <(ps -ax -o pid,psr,cmd)
@nutrino
Copy link
Author

nutrino commented Mar 16, 2023

top_by_cpuid.sh

#!/bin/bash
pgrep -d',' -P $1 | tr ',' '\n' | paste -sd',,,,,,,,,,,,,,,,,,,\n' - | xargs -I{} top -bn1 -p {}

@nutrino
Copy link
Author

nutrino commented Mar 16, 2023

ps_by_cpuid.sh

#!/bin/bash
CPUID=$1
ps -eo pid,psr,pcpu,command | awk 'NR>1' | awk -v cpuid="$CPUID" '$2 == cpuid' | sort -k3,3n

@nutrino
Copy link
Author

nutrino commented Mar 22, 2023

mpstat -P ALL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment