Help Center> Elastic Cloud Server> Troubleshooting> Linux ECS Issues> How Do I Set vCPU Affinity for Processes Using taskset?
Updated on 2024-02-29 GMT+08:00

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

  1. Run the following command to query the information about the vCPUs of the ECS:

    cat /proc/cpuinfo

    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.
  1. Run the following command to check the status of a process, for example, test.sh with PID 23989:

    ps aux | grep test.sh

  1. 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.

  1. Run the following command to assign the second vCPU (vCPU 1) to the process:

    taskset -pc 1 PID

    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&