3

How can I set CPU affinity for the specific program (say gzip) to always run on specific core or cores (core 1, for example)?

I read about taskset, but can it be used before program is actually used and creates a process?

1

You can't set the affinity for all invocations of an executable. The affinity is managed by the kernel and inherited from parent process to child process, there's no mechanism that changes the affinity of a process when an executable is executed.

If you want all invocations of gzip to run on CPU 1, put a wrapper script called gzip ahead of the real one in the PATH, e.g. ~/bin/gzip:

#!/bin/sh
exec taskset 1 /bin/gzip "$@"

But this strikes me as completely useless. Explicitly setting a process's affinity usually makes things slower. It can sometimes be useful to confine a CPU-intensive task to certain processors and leave the system more reactive, though nice usually does a better job overall. But doing that indiscriminately for all the invocations of an executable sounds like an XY problem.

  • When used inside Windows Subsystem Linux nice seems to only be able to provide prioryty management only if demanded from inside WSL, so native Windows applications get low priority anyway. This solution was the only way I found to work reliably. – sukhmel Dec 3 '18 at 21:39
1

I had a single-threaded process I wanted to run exclusively on a specified core. It did video encoding- which is CPU intensive- so didn't want other processes time-slicing the core's cycles while that was happening. I achieved this by restricted the OS to cores 0,1 & 2 on the (4) core Pi. The net result was core 3 was now unused and when the taskset was executed pinning "motion" to it, "motion" had exclusive use of the core. It's tested and works a treat.

The script I wrote to automate CPU Affinity as a SystemD service for SINGLE-THREADED processes is offered below and can tweaked accordingly to suit your own purposes. After you're done just paste it into a file, chmod 700 it and you're all set. Hope this saves you solving the same problem I had to figure out- Terrence Houlahan

#!/bin/bash

# Backup system.conf before we modify it with sed:
cp -p /etc/systemd/system.conf /etc/systemd/system.conf.ORIGINAL

# Default allows OS to use ALL cores- we restrict it to only first 3 of 4 cores:
sed -i "s/#CPUAffinity=1 2/CPUAffinity=0 1 2/" /etc/systemd/system.conf

# We pin process motion to remaining core #3:

cat <<'EOF'> /home/pi/scripts/set-cpu-affinity.sh
#!/bin/bash

# Note: the number following cp is the CPU/core number in this case 3
taskset -cp 3 $(pgrep motion|cut -d ' ' -f2)

EOF


chmod 700 /home/pi/scripts/set-cpu-affinity.sh
chown pi:pi /home/pi/scripts/set-cpu-affinity.sh

# Now we configure script to run as a service to make CPU affinity persistent across reboots:
cat <<EOF> /etc/systemd/system/set-cpu-affinity.service
[Unit]
Description=Set CPU Affinity for the Motion process after it starts on boot
Wants=motion.service
After=motion.service

[Service]
User=root
Group=root
Type=oneshot
ExecStart=/home/pi/scripts/set-cpu-affinity.sh

[Install]
WantedBy=multi-user.target

EOF

chmod 644 /etc/systemd/system/set-cpu-affinity.service

systemctl enable set-cpu-affinity.service

chown -R pi:pi /home/pi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.