Restricting the CPU or Memory Usage of a Process (Linux)

This section describes how to restrict the CPU or memory usage of ascend-deployer.

Control groups (cgroups) are a mechanism provided by the Linux kernel to restrict the resources used by a single or multiple processes. It can implement refined control on resources such as the CPU and memory. The procedure is as follows:

  • Restricting the CPU usage of ascend-deployer
    1. Create a directory.
      cd /sys/fs/cgroup/cpu
      mkdir ascend-deployer         # The directory name can be customized.

      After the ascend-deployer directory is created, files such as cpu.cfs_period_us and cpu.cfs_quota_us are automatically generated in the directory.

    2. Go to the ascend-deployer directory and set the values for the corresponding file. The following files are used to restrict the CPU usage:
      Table 1 File description

      File

      Description

      cpu.cfs_period_us

      Period for collecting CPU usage statistics, in microseconds. The value ranges from 1000 to 1000000. The default value is 100000.

      cpu.cfs_quota_us

      CPU time that can be occupied in the configured period (time of a single core, which needs to be accumulated for multiple cores during configuration). The default value is -1, indicating that the CPU time is not limited.

      • For example, if you want to set the limit to 30%, change the value to 30000.
      • For example, to set the maximum number of CPU cores to two, change the value to 200000.

      tasks

      PID list of the processes to be restricted. For a child process derived from a PID after the restriction takes effect, the PID of the child process is automatically added to the file.

      You are advised to add the PID of a program's shell (by running the echo $$ command) to the tasks file and then start the program.

      For example, to set the maximum CPU usage to 50%, perform the following operations:

      Retain the default value in the cpu.cfs_period_us file and change the value in the cpu.cfs_quota_us file to 50000. In the shell window where ascend-deployer is to be run, run the echo $$ command to query the PID and write it to the tasks file.

  • Restricting the memory usage of ascend-deployer
    1. Create a directory.
      cd /sys/fs/cgroup/memory
      mkdir ascend-deployer      # The directory name can be customized.

      After the ascend-deployer directory is created, files such as memory.limit_in_bytes and tasks are automatically generated in the directory.

    2. Go to the ascend-deployer directory and set the values for the corresponding file. The following files are used to restrict the CPU usage:
      Table 2 File description

      File

      Description

      memory.limit_in_bytes

      Limits the memory usage of a process.

      For example, to set the maximum memory usage to 500 MB, change the value in the memory.limit_in_bytes file to 524288000.

      tasks

      PID list of the processes to be restricted. For a child process derived from a PID after the restriction takes effect, the PID of the child process is automatically added to the file.

      You are advised to add the PID of a program's shell (by running the echo $$ command) to the tasks file and then start the program.

      For example, to set the maximum memory usage to 500 MB, change the value in the memory.limit_in_bytes file to the calculated value 524288000. In the shell window where ascend-deployer is to be run, run the echo $$ command to query the PID and write it to the tasks file.