Skip to content

Instantly share code, notes, and snippets.

@ChrisDeadman
Created October 14, 2022 22:51
Show Gist options
  • Save ChrisDeadman/d855bf7d471ebf38350528c6d3cf4291 to your computer and use it in GitHub Desktop.
Save ChrisDeadman/d855bf7d471ebf38350528c6d3cf4291 to your computer and use it in GitHub Desktop.
Redirect CPU Cores for Gaming
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Forces games to run only on 'good' cores - e.g. first CCD on Ryzen CPUs."
echo
echo "Usage: $0 on/off/topo"
echo
echo "Turn On: sudo $0 on"
echo "Turn Off: sudo $0 off"
echo "Show cpu topology: sudo $0 topo"
echo
echo "Start by checking your CPU topology using 'topo' command."
echo "Edit 'gaming_cores' and 'background_cores' variables in this script (lines 20 and 21) to select only the good cores (e.g. all real cores + SMT cores from CCD0)"
echo "Start lutris/heroic/steam or have the game running already and then execute this script with 'on' command."
echo "When done playing execute script with 'off' command."
exit 1
fi
nice_value=-18
gaming_cores="0-5,12-17" # Ryzen 5900X CCD0
background_cores="6-11,18-23" # Ryzen 5900X CCD1
function redirect_on {
if ! [ -d /sys/fs/cgroup/tasks_redirect ]; then
echo "Creating tasks_redirect group"
mkdir /sys/fs/cgroup/tasks_redirect
fi
echo "Enable CPU-related controllers"
echo "+cpu" >> /sys/fs/cgroup/tasks_redirect/cgroup.subtree_control
echo "+cpuset" >> /sys/fs/cgroup/tasks_redirect/cgroup.subtree_control
if ! [ -d /sys/fs/cgroup/tasks_redirect/background_cores ]; then
echo "Creating background_cores child-group"
mkdir /sys/fs/cgroup/tasks_redirect/background_cores
fi
echo "Assigning background cores to background_cores cpuset"
echo "$background_cores" > /sys/fs/cgroup/tasks_redirect/background_cores/cpuset.cpus
echo "Redirecting processes to background_cores cpuset"
while read p; do
echo $p > /sys/fs/cgroup/tasks_redirect/background_cores/cgroup.procs 2>&1
if [ $? -eq 0 ]; then
echo "Redirected PID $p"
fi
done < <(cat /sys/fs/cgroup/cgroup.procs | sort | uniq)
if ! [ -d /sys/fs/cgroup/tasks_redirect/gaming_cores ]; then
echo "Creating gaming_cores child-group"
mkdir /sys/fs/cgroup/tasks_redirect/gaming_cores
fi
echo "Assigning gaming cores to gaming_cores cpuset"
echo "$gaming_cores" > /sys/fs/cgroup/tasks_redirect/gaming_cores/cpuset.cpus
echo "Setting nice=$nice_value for gaming_cores cpuset"
echo "$nice_value" > /sys/fs/cgroup/tasks_redirect/gaming_cores/cpu.weight.nice
echo "Redirecting game launchers to gaming_cores cpuset"
pids=(`pgrep -f lutris`)
pids+=(`pgrep heroic`)
pids+=(`pgrep wine`)
pids+=(`pgrep steam`)
pids+=(`pgrep .exe`)
for p in "${pids[@]}"
do
echo "Redirecting PID $p"
echo $p > /sys/fs/cgroup/tasks_redirect/gaming_cores/cgroup.procs
done
}
function redirect_off {
if [ -d /sys/fs/cgroup/tasks_redirect/background_cores ]; then
echo "Redirecting processes from background_cores back to main cpuset"
while read p; do
echo "Redirecting PID $p"
echo $p > /sys/fs/cgroup/cgroup.procs
done < <(cat /sys/fs/cgroup/tasks_redirect/background_cores/cgroup.procs | sort | uniq)
rmdir /sys/fs/cgroup/tasks_redirect/background_cores
fi
if [ -d /sys/fs/cgroup/tasks_redirect/gaming_cores ]; then
echo "Redirecting processes from gaming_cores back to main cpuset"
while read p; do
echo "Redirecting PID $p"
echo $p > /sys/fs/cgroup/cgroup.procs
done < <(cat /sys/fs/cgroup/tasks_redirect/gaming_cores/cgroup.procs | sort | uniq)
rmdir /sys/fs/cgroup/tasks_redirect/gaming_cores
fi
}
if [ "${1,,}" = "on" ]; then
redirect_on
echo "REDIRECT ON"
elif [ "${1,,}" = "topo" ]; then
lstopo
else
redirect_off
echo "REDIRECT OFF"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment