Restricting the CPU or Memory Usage of a Process

Control groups (cgroups) are a mechanism provided by the Linux kernel to limit the resources used by a single or multiple processes. It can implement refined control on resources such as the CPU and memory. By restricting the CPU and memory usage when the Ascend-DMI tool is running, you can prevent the Ascend-DMI tool from occupying too many resources. If the Ascend-DMI tool occupies too many resources, other programs cannot run properly. The operations are as follows:

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

      After the ascend-dmi 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-dmi 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

      CPU cycle, 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, if you want to use a maximum of two CPU cores, change the value to 200000.

      Run the echo command to modify a file, for example, echo 30000 > cpu.cfs_quota_us.

      tasks

      PID list of the processes to be restricted. For a child process derived from the 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 the shell that runs the program to the tasks by running the echo $$ command, 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-DMI is to be run, run the echo $$ command to query the PID and write it to the tasks file.

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

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

    2. Go to the ascend-dmi directory and set the values for the corresponding file. The following files are used to restrict the memory 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.

      Run the echo command to modify a file, for example, echo 1024M > memory.limit_in_bytes.

      tasks

      PID list of the processes to be restricted. For a child process derived from the 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 the shell that runs the program to the tasks by running the echo $$ command, 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-DMI is to be run, run the echo $$ command to query the PID and write it to the tasks file.