Overview: this is probably the most reluctant to see most programmers indicators, as far as I know simple and crude to solve a problem only if you want the kill, but kill the moment may not be process safety point of the business, can’t kill any time accurately, because you don’t know where the business process, we can do is try to avoid the loss

Inspired by the road

Have you ever suddenly connected two unrelated things in your mind, and if so, the inspiration came from studying the basic principle of Docker recently – Linux Cgroups (resource control groups).

Please go to: juejin.cn/post/696363…

Curve to save the country

Reduce the CPU utilization of a process by limiting its CPU resources

Which resources of the process are controlled? Where was it controlled?

Process resources are centrally managed into a resource control group, which contains devices, cups, memory, and so on. Each resource is presented in the form of a folder

You can enter any one of the resource folder, create a belongs to their own resource limit group (folder), then the system automatically generated for you the group of corresponding resources limit file, for example

  1. Create a folder in the corresponding resource group and view its directory structure

  1. Compare files within two folders that qualify for resources

Conclusion: The contents of the Memory folder in the system resource control group are found to be the same as the contents of the newly created Memory_test folder.

Start the curve to save the country.

System: centos7

Environment: GCC Version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC)

Note: This operation requires the creation of multiple Linux session connections

  1. We need to create a cup.c to simulate a process that can drive CPU utilization up to 99%
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <thread>

void test_cpu(a) {
    printf("thread: test_cpu start\n");
    int total = 0;
    while (1) { ++total; }}int main(int argc, char** argv) {
    std::thread t1(test_cpu);
    t1.join();
    return 0;
}


Copy the code
  1. Compile (in cup.c current path)
[root@localhost ~]# g++ cpu.c -o cpu.out -std=c++11 -pthread -D_GLIBCXX_USE_NANOSLEEP 
Copy the code
  1. Create test_CPU directory under /sys/fs/cgroup/ CPU
[root@localhost cpu]# mkdir test_cpu
Copy the code
  1. Modify cpu.cfs_period_us, cpu.cfs_quota_us in test_CPU file
[root@localhost test_cpu]# sudo echo 100000 > cpu.cfs_period_us 
[root@localhost test_cpu]# sudo echo 20000 > cpu.cfs_quota_us 
#Explanation:
#Cpu. cfs_period_us = 100000 Divide the CPU into 100000 pieces
#Cpu. cfs_quota_us =20000 The maximum number of CPU resources in a resource group is 20000
#This means that a process is limited to that group and can use up to 20% of the CPU resources
Copy the code
  1. Start cup.out, and your computer’s fan starts to roar wildly
[root@localhost desktop]#./ CPU. Out thread: test_CPU start#It's going to block all the time unless youkillOff the process
Copy the code
  1. Top Command Observation

  1. Add the process ID to the test_cup resource limit group
[root@localhost ~]# cd /sys/fs/cgroup/cpu/test_cpu/
[root@localhost test_cpu]# sudo echo 16759 > cgroup.procs 
[root@localhost test_cpu]# 
Copy the code
  1. The fan is back to its normal calm

Homework after class

When the process starts, please see the cat/sys/fs/cgroup/CPU/cgroup procs whether there is the process ID?

When I add the process ID to the cgroup test_cpu folder. The procs file, please look again at the cat/sys/fs/cgroup/CPU/cgroup procs exist?

Why is that?

conclusion

This is an exercise in treating the symptoms rather than the root cause, and the point is that in this case maybe we can struggle and make sure that other applications work

Warm prompt

If you follow the steps to play congratulations you are only half successful, because the other half is I can’t seem to delete the resource restriction group I created, haha ~~

You are welcome to comment, learn and progress together