How Do I Set vCPU Affinity for Processes Using taskset?
Scenarios
Command taskset allows you to set vCPU affinity for processes running on an ECS to optimize vCPU utilization.
Procedure
- Run the following command to query the information about the vCPUs of the ECS:
The key vCPU parameters are as follows:
- processor specifies the sequence number of a vCPU.
- cpu cores specifies the number of cores of each vCPU.
- Run the following command to check the status of a process, for example, test.sh with PID 23989:
ps aux | grep test.sh
- Run the following command to query the vCPU that process test.sh is running on:
taskset -p PID
For example, run taskset -p 23989.
The returned value is 1 in decimal notation, which is 1 in binary notation.
- Run the following command to assign the second vCPU (vCPU 1) to the process:
For example, run taskset -pc 1 23989.
The vCPU ID starts from 0. vCPU 1 indicates the second vCPU. After the proceeding command is executed, test.sh is bound to vCPU 1.
You can also run the following command to bind the process to vCPU 1 at startup:
taskset -c 1 ./test.sh&
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.