Linux CPU Isolation Issue: Taskset Fails to Distribute Processes Across Isolated Cores (isolcpus Range)


1 views

When working with CPU isolation on Linux (kernel 3.2, Debian Wheezy, AMD64/Xeon E5-2690), I encountered unexpected behavior with taskset over a range of isolated cores. While setting isolcpus=8-15 successfully reserves these cores, the scheduler shows reluctance to distribute processes across the entire range when using taskset with core ranges.

The core issue manifests when attempting to run multiple CPU-bound processes:


# Expected behavior (non-isolated cores)
taskset -c 1-7 bash -c 'while :; do echo test >/dev/null; done' &
taskset -c 1-7 bash -c 'while :; do echo test >/dev/null; done' &
# Processes distribute evenly across cores 1-7

# Problematic behavior (isolated cores)
taskset -c 8-15 bash -c 'while :; do echo test >/dev/null; done' &
taskset -c 8-15 bash -c 'while :; do echo test >/dev/null; done' &
# Both processes stack on core 8 despite available cores 9-15

The Linux scheduler treats isolated CPUs differently from regular CPUs. Key observations:

  • Affinity masks (taskset -p) show correct settings
  • Manual per-core assignment works perfectly
  • Process migration between isolated cores appears disabled
  • Load balancing behaves differently for isolated CPU sets

For multi-threaded applications, consider these approaches:


# Option 1: Explicit core assignment
for core in {8..15}; do
    taskset -c $core your_threaded_app &
done

# Option 2: Use numactl instead
numactl --cpunodebind=1 --localalloc your_threaded_app

# Option 3: Modify application to handle affinity
void set_affinity() {
    cpu_set_t mask;
    CPU_ZERO(&mask);
    for(int i=8;i<=15;i++) CPU_SET(i, &mask);
    sched_setaffinity(0, sizeof(mask), &mask);
}

For production systems needing proper core distribution:

  • Test newer kernel versions (3.2 is quite old)
  • Consider cgroups/cpusets as alternative to isolcpus
  • Examine /proc/sys/kernel/sched_domain/ parameters
  • Check for CPU hotplugging interference

A more reliable setup using cpusets:


mkdir /dev/cpuset
mount -t cpuset none /dev/cpuset
cd /dev/cpuset
mkdir isolated
echo 8-15 > isolated/cpuset.cpus
echo 0 > isolated/cpuset.mems
echo 1 > isolated/cpuset.cpu_exclusive
echo $$ > isolated/tasks  # Move current shell to isolated set

When working with CPU isolation on Linux (specifically Debian Wheezy with kernel 3.2), I encountered an unusual scheduler behavior. After isolating cores 8-15 using isolcpus=8,9,10,11,12,13,14,15 in GRUB, taskset operations don't distribute processes as expected.


# Expected to spread across 8-15 but only uses core 8
taskset -c 8-15 bash -c 'while true ; do echo hello >/dev/null; done' &

# Second instance still sticks to core 8
taskset -c 8-15 bash -c 'while true ; do echo hello >/dev/null; done' &

The workaround of manually assigning specific cores does function correctly:


# Properly utilizes both cores
taskset -c 8 bash -c 'while true ; do echo hello >/dev/null; done' &
taskset -c 9 bash -c 'while true ; do echo hello >/dev/null; done' &

This behavior appears specific to isolated CPUs. Non-isolated cores (1-7) distribute processes normally. The affinity mask (taskset -p) shows correct settings, but the scheduler only uses the lowest-numbered core in the range.

For a multithreaded application, consider these approaches:


# Option 1: Use numactl instead of taskset
numactl --physcpubind=8-15 ./your_application

# Option 2: Set CPU affinity programmatically
#define _GNU_SOURCE
#include 

cpu_set_t mask;
CPU_ZERO(&mask);
for (int i = 8; i <= 15; i++) {
    CPU_SET(i, &mask);
}
sched_setaffinity(0, sizeof(cpu_set_t), &mask);

For more control, use cgroups to manage CPU allocation:


# Create cgroup
mkdir /sys/fs/cgroup/cpuset/isolated
echo 8-15 > /sys/fs/cgroup/cpuset/isolated/cpuset.cpus
echo 0 > /sys/fs/cgroup/cpuset/isolated/cpuset.mems

# Add process to cgroup
echo $PID > /sys/fs/cgroup/cpuset/isolated/tasks

This behavior might be specific to kernel 3.2. Newer kernels (4.x+) have improved CPU isolation features and better scheduler handling of isolated core ranges.