Track CPU Affinity with htop
This article provides a straightforward guide on how to view and
track the CPU affinity of a process using the htop
interactive process viewer in Linux. CPU affinity binds a process or
thread to specific central processing units, which can optimize
performance and resource allocation. While htop does not
display this information by default, you can easily customize its
interface to monitor which CPU cores are assigned to active processes in
real time.
Step 1: Open htop
Launch the utility from your terminal. If it is not already
installed, you can typically install it via your distribution’s package
manager (e.g., sudo apt install htop or
sudo dnf install htop).
htopStep 2: Access the Setup Menu
Once htop is running, open the configuration menu by
pressing the F2 key on your keyboard. Alternatively,
you can press S or use your mouse to click on
Setup at the bottom left corner of the screen.
Step 3: Navigate to Columns
In the Setup menu, use the arrow keys to navigate the options:
- Move down to the Setup column and select Columns.
- Press the right arrow key to move into the Available Columns list on the far right.
Step 4: Add the CPU Affinity Column
Scroll down through the Available Columns list until you find the option labeled PROCESSOR. This represents the ID of the CPU core currently executing the process, which reflects its real-time affinity.
- Press Enter to add PROCESSOR to your active Output columns list.
- Use the F7 (Move Up) or F8 (Move Down) keys to reposition the column to your preferred location in the display hierarchy.
Step 5: Save and View
Press F10 or Esc to save your changes and return to the main process monitor screen.
You will now see a new column labeled CPU or PROCESSOR in your main display. The numbers in this column indicate the specific CPU core (starting from 0) to which each process is currently assigned, allowing you to track CPU affinity dynamically as system loads shift.