I'm trying to run some benchmarks on a multicore machine and I'd like to tell the Linux kernel to simply avoid certain cores unless explicitly told to use them.

The idea is that I could set aside a handful of cores (the machine has 6 physical cores) for benchmarking and use cpu mask to allow only benchmark processes onto the given cores.

Is this feasible?


You could isolate some cpu cores from kernel scheduling using isolcpus parameter. Add this parameter to your grub.conf and reboot to take effect.

  • 2
    The link is dead. Might be best to include an example directly in the answer. – pchaigno Jun 11 '18 at 9:02

You want some processor affinity (or CPU affinity).

The relevant syscall is sched_setaffinity(2), but you should use it thru pthread_set_affinity_np(3) if you want to code your benchmarks for that.

The related command is taskset(1) and you might use it on the commands you want to benchmark (or on your shell).

If possible, take care that the machine is not loaded a lot by other tasks unrelated to the benchmark.

Alternatively, use some hypervisor like Xen, and boot your Linux as a guest OS for that hypervisor. Then configure xen to use only some restricted set of CPU cores (I don't know the exact details, you need to find out). On Debian (and related) distributions, you might install packages like xen-linux-system-amd64, xen-hypervisor-amd64, xen-utils etc (I'm using on Debian/Sid xen-hypervisor-4.5-amd64, xen-linux-system-4.0.0-1-amd64, xen-utils-4.5 etc...).

There might be perhaps some way of configuring (perhaps at kernel-compilation time, or by passing specific arguments to the kernel thru Grub loader) your Linux kernel to restrict the number of usable cores.

Of course, it is better, when benchmarking some program -e.g. on a desktop Linux PC-, to take care to avoid having many outside running processes (use ps auxw, pstree -p, top to find these). At least, close and quit most interactive applications (browser like firefox, email, editor, IDEs like Eclipse) and keep only a small number of terminals to benchmark. You might even benchmark in batch mode (using batch or at to run the benchmarks) while you are not even logged in (so don't have any GUI sessions à la Gnome or KDE or Xfce, etc... running).

  • Setting the affinity of the tasks to benchmark is not enough. You've got to also keep other tasks away. Which your hypervisor suggestion does, but in a very heavy-handed way. – Gilles Jun 9 '15 at 21:02
  • @Gilles: why is keeping other tasks away needed for benchmarking purposes? In practice, when benchmarking, the PC is often quite idle... For a costly server things are different! – Basile Starynkevitch Jun 10 '15 at 4:41
  • For example because you're benchmarking on your desktop PC which is also spending resources on Firefox, Eclipse and other highly demanding tasks. – Gilles Jun 10 '15 at 7:48
  • On your own desktop PC, you can quite easily stop most consuming tasks (e.g. close & quit firefox & eclipse) while benchmarking. You could even run the benchmarks in batch mode (using at or batch) while you are not logged in (so don't run any consuming session). – Basile Starynkevitch Jun 10 '15 at 7:54

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.