LinuxCommandLibrary

taskset

Assign a process to specific CPUs

TLDR

Get a running process' CPU affinity by PID

$ taskset [[-p|--pid]] [[-c|--cpu-list]] [pid]
copy

Set a running process' CPU affinity by PID
$ taskset [[-p|--pid]] [[-c|--cpu-list]] [cpu_id] [pid]
copy

Start a new process with affinity for a single CPU
$ taskset [[-c|--cpu-list]] [cpu_id] [command]
copy

Start a new process with affinity for multiple non-sequential CPUs
$ taskset [[-c|--cpu-list]] [cpu_id_1],[cpu_id_2],[cpu_id_3]
copy

Start a new process with affinity for CPUs 1 through 4
$ taskset [[-c|--cpu-list]] [cpu_id_1]-[cpu_id_4]
copy

SYNOPSIS

taskset [options] [mask | list] [pid]
taskset [options] mask|list command [arguments...]
taskset -p [options] [mask | list] pid
taskset -c [options] [mask | list] pid

PARAMETERS

-p, --pid
    Operate on an existing PID (Process ID). When used, taskset queries or sets the affinity of the specified process.

-c, --cpu-list
    Interpret the affinity argument as a numerical list of CPUs instead of a hexadecimal bitmask. For example, '0,1,5-7' refers to CPUs 0, 1, 5, 6, and 7.

-a, --all-tasks
    Operate on all tasks (threads) associated with the given PID. This option requires either -p or -c to be specified, as it's only relevant for existing processes.

-s, --status
    Query the current CPU affinity of the specified PID and exit. This option requires either -p or -c.

-h, --help
    Display a help message and exit, showing usage information for the command.

-V, --version
    Output version information and exit.

mask
    A hexadecimal bitmask representing the allowed CPUs. Each bit corresponds to a CPU, starting from the rightmost bit for CPU 0. For example, 0x00000001 for CPU 0, 0x00000003 for CPUs 0 and 1.

list
    A comma-separated list of CPU numbers or ranges (e.g., '0,1,4-6'). This format is used when the -c or --cpu-list option is active.

pid
    The Process ID of the target process when querying or setting the affinity of an already running process.

command
    The command to execute with the specified CPU affinity. This is used when launching a new process.

arguments...
    Additional arguments passed directly to the command being executed.

DESCRIPTION

taskset allows users to control which CPUs a process or command can run on. This is known as CPU affinity, a scheduler property that dictates which physical or logical processors a task can execute on.

By assigning a specific set of CPUs to a process, one can optimize performance for CPU-bound tasks, reduce context switching overhead, or isolate critical processes to specific cores to avoid interference. It uses either a hexadecimal bitmask or a comma-separated list of CPU IDs to define the allowed processors. taskset can be used to query the current affinity of a running process (using the -p or --pid option) or to launch a new command with a predefined affinity. This capability is particularly useful in multi-core and NUMA systems for performance tuning, real-time applications, and managing resource contention.

CAVEATS

Using taskset can sometimes degrade performance if not used carefully, especially if it leads to an imbalance in CPU utilization or prevents the scheduler from optimizing task placement.

CPU affinity settings are inherited by child processes unless explicitly changed by the child or a subsequent taskset command.

Setting affinity on a multi-threaded application often requires using the --all-tasks (-a) option to apply the affinity to all individual threads (tasks) within the process.

The specified mask or list must correspond to available CPUs on the system. Specifying non-existent CPUs will result in an error and the operation failing.

CPU AFFINITY VS. SCHEDULING PRIORITY

While both CPU affinity (taskset) and scheduling priority (e.g., using nice or chrt) influence how processes run, they serve different purposes.

Affinity dictates which CPUs a process is allowed to run on, effectively partitioning the available CPU resources. Priority, on the other hand, determines when a process runs relative to others on its assigned or available CPUs. They can be used together for comprehensive process management.

BITMASK VS. CPU LIST

Older versions of taskset primarily used hexadecimal bitmasks to specify CPU affinity. The introduction of the -c or --cpu-list option provided a more human-readable and convenient way to specify CPU affinity, especially for systems with a large number of CPUs.

A bitmask of 0x1 corresponds to CPU 0, 0x2 to CPU 1, 0x4 to CPU 2, and so on. The CPU list format (e.g., '0,1,4-6') is often simpler to understand and manage for users working with individual CPU IDs.

HISTORY

The concept of CPU affinity has been a core part of operating system schedulers for optimizing performance on multi-processor systems. The taskset utility provides a user-space interface to the underlying sched_setaffinity system call, which has been present in Linux since its early multi-processor days.

Its usage became more prevalent with the widespread adoption of multi-core processors, enabling fine-grained control over process execution on specific cores. This control is particularly useful for high-performance computing (HPC), real-time applications, and isolating critical services to dedicated CPU resources, ensuring predictable performance.

SEE ALSO

chrt(1), nice(1), renice(1), numactl(8), sched_setaffinity(2), cpuset(7)

Copied to clipboard