Skip to content

Instantly share code, notes, and snippets.

@TunaCici
Created March 13, 2024 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TunaCici/9cc2e3fd1199f07fef001d718b776eee to your computer and use it in GitHub Desktop.
Save TunaCici/9cc2e3fd1199f07fef001d718b776eee to your computer and use it in GitHub Desktop.
[GNU/Linux] Get All Threads & Core Affinities of a Process w/o ps or taskset
#!/usr/bin/env bash
if [ $# -eq 0 ]; then
echo "Usage ./get-affinities.sh <PID>"
exit 1
fi
pid=$1
if [ ! -d "/proc/${pid}" ]; then
echo "Process with PID ${pid} does not exist."
exit 1
fi
name=$(cat /proc/${pid}/comm)
echo "[x] ${pid} (${name})"
for tid in $(ls /proc/${pid}/task/); do
status="/proc/${pid}/task/${tid}/status"
if [ -e "$status" ]; then
name=$(cat /proc/${pid}/task/${tid}/comm)
affinity=$(cat ${status} | grep -w "Cpus_allowed_list" | grep -oE '[0-9,-]+')
echo "[x] \-- ${tid} ${name}, Core Affinity: ${affinity}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment