chcpu - Linux
Overview
chcpu is a command-line utility used to dynamically manage CPU affinity on Linux systems. It allows users to bind specific processes or threads to particular CPU cores, optimizing performance and resource utilization.
Syntax
chcpu [-h] [-p PID] [-t TID] [-c CPU] [-C CPU] [-N] [-A] [-b] [-s] [-v]
Options/Flags
- -h, –help: Display help information.
- -p PID: Specify the Process ID (PID) of the process to manage CPU affinity.
- -t TID: Specify the Thread ID (TID) to manage CPU affinity.
- -c CPU: Set a single CPU core for the process/thread.
- -C CPU: Set multiple CPU cores for the process/thread, separating them with commas.
- -N: Enable CPU affinity for all processes/threads in a task.
- -A: Display current CPU affinity for the specified process/thread.
- -b: Use batch mode for faster operation, disabling confirmation prompts.
- -s: Silence output, suppressing all notifications.
- -v: Enable verbose mode, providing detailed output.
Examples
Binding a Process to CPU Core 3:
chcpu -p 1234 -c 3
Setting CPU Affinity for Thread 5 of Process 4567:
chcpu -t 5 -p 4567 -c 2,4
Displaying Current CPU Affinity for Process 7890:
chcpu -A -p 7890
Common Issues
- Unable to set affinity: Ensure the user has sufficient privileges (sudo) and the specified CPU cores are available.
- Multiple processes with same PID: Use -N to manage all processes in a task.
- Incorrect CPU core specification: Use lscpu to verify available CPU cores.
Integration
With ‘top’:
top -H | chcpu -p <PID> -c <CPU>
With ‘htop’:
htop | chcpu -p <PID> -c <CPU>
Related Commands
- taskset: Similar tool for managing CPU affinity.
- ps, top, htop: Process monitoring tools that display CPU affinity information.
- Linux CPU Affinity Management: Official documentation on CPU affinity.