brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: taskset shows and sets which CPU cores a process may use

Linux
~/tips/tip-linux-taskset-pin-cpu

taskset -c 0,1 ffmpeg -i in.mkv out.mp4

Starts a command on chosen CPU cores or changes the cores of a running process.

Most of the time I leave scheduling to the kernel. When one heavy process keeps dragging everything else down, taskset is a quick way to keep it on a few cores.

Start a command restricted to cores 0 and 1. The -c option makes taskset read the argument as a list of processors instead of a bit mask:

taskset -c 0,1 ffmpeg -i in.mkv out.mp4

The lists can include ranges, so 0-2,6 means processors 0, 1, 2 and 6. Without -c the same cores would be written as a hex mask, where 0x00000003 is processors 0 and 1.

For a process that already runs, use -p with its PID. The man page shows that without a mask it prints the current affinity, and -pc prints it as a list instead of a mask:

taskset -p 1234
taskset -pc 1234
taskset -pc 0-3 1234

The first line prints the current mask, the second the same thing as a list such as 0-47 and the last one changes the affinity of process 1234 to cores 0 to 3.

An affinity is a limit, not a reservation. Pinning a process to two cores does not keep other tasks off them. It can also make the job slower if you give it fewer cores than it could use, so it only pays off when you know why you are doing it. For limits that must survive a restart of a service, the systemd setting CPUAffinity= is a better place than a command you run by hand. When the goal is priority and not placement, nice is the tool to look at.

Further reading: taskset(1) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link