Tutorial on limiting CPU usage in Linux system


The Linux kernel is a great circus performer. It carefully juggles between processes and system resources and keeps the system running normally. At the same time, the kernel is also fair: it distributes resources fairly among processes.

What should you do when you need to prioritize a process? Or, how to lower the priority of a process? Or, how to limit the resources used by a group of processes?

The answer is that it is up to the user to prioritize the processes for the kernel

Most processes start with the same priority, so the Linux kernel will schedule fairly. If you want a CPU intensive process to run at a lower priority, you have to configure the scheduler in advance.

Here are three methods to control process running time:

  1. Manually lower the priority of a task using the nice command.
    Use the cpulimit command to continuously pause the process to control that the processing capacity occupied by the process does not exceed a specific limit.
    Using the built-in control groups function of Linux, it provides a mechanism to limit the consumption of process resources.

Let’s take a look at the working principle of these three tools and their advantages and disadvantages.
Simulate high CPU usage

Before analyzing these three technologies, we need to install a tool to simulate the scenario of high CPU utilization. We will use CentOS as the test system and useMathomatic toolkitPrime generator in to simulate CPU load.

Unfortunately, there is no pre compiled version of this tool on CentOS, so it must be installed from the source code. First from http://mathomatic.orgserve.de/mathomatic-16.0.5.tar.bz2 This link downloads the source package and unzips it. Then enter the mathomatic-16.0.5/primes folder, run make and sudo make install to compile and install. In this way, the matho primes program is installed in the / usr / local / bin directory.

Next, run from the command line:


Copy code

The code is as follows:

/usr/local/bin/matho-primes 0 9999999999 > /dev/null &

After the program runs, the prime number between 0 and 99999999 will be output. Because we don’t need these output results, just redirect the output to / dev / null.

Now, using the top command, you can see that the matho primes process has drained all your CPU resources.
2015612151254703.jpg (699×377)

OK, then (press the Q key) exit top and kill the matho primes process (use the FG command to switch the process to the foreground, and then press Ctrl + C)
Nice command

Here’s how to use the nice command. The nice command can modify the priority of the process, so that the process can run less frequently. This feature is particularly useful when running CPU intensive background processes or batch jobs. The value range of nice value is [- 20,19], – 20 represents the highest priority and 19 represents the lowest priority. The default nice value for Linux processes is 0. Use the nice command (without any parameters) to set the nice value of the process to 10. In this way, the scheduler will treat this process as a lower priority process, thus reducing the allocation of CPU resources.

Let’s take a look at an example. We run two matho primes processes at the same time. One uses the nice command to start and run, while the other starts and runs normally:


Copy code

The code is as follows:

nice matho-primes 0 9999999999 > /dev/null &
matho-primes 0 9999999999 > /dev/null &

Then run the top command.
2015612151315706.jpg (699×377)

See, normally running processes (nice value of 0) get more CPU running time. On the contrary, processes running with nice command take less CPU time (nice value of 10).

In practice, if you want to run a CPU intensive program, you’d better start it with nice command, so as to ensure that other processes get higher priority. In other words, even if your server or desktop is overloaded, it can respond quickly.

Nice also has an associated command called renice, which can adjust the nice value of the process at run time. When using the renice command, first find the PID of the process. Here is an example:


Copy code

The code is as follows:

renice +10 1234

Where 1234 is the PID of the process.

After testing nice and renice commands, remember to kill all matho primes processes.
Cpulimit command

Next, we introduce the usage of the cpulimit command. The working principle of the cpulimit command is to preset a CPU occupancy threshold for the process and monitor whether the process exceeds this threshold in real time. If it exceeds this threshold, the process will be suspended for a period of time. Cpulimit uses sigstop and sigcont signals to control the process. It does not modify the nice value of the process, but makes dynamic adjustments by monitoring the CPU utilization of the process.

The advantage of cpulimit is that it can control the upper limit of CPU utilization of the process. However, compared with nice, there are also disadvantages, that is, even if the CPU is idle, the process can not fully use the whole CPU resources.

On CentOS, you can install it in the following way:


Copy code

The code is as follows:

wget -O cpulimit.zip https://github.com/opsengine/cpulimit/archive/master.zip
unzip cpulimit.zip
cd cpulimit-master
sudo cp src/cpulimit /usr/bin

The above command line will first download the source code locally from GitHub, and then extract, compile and install it into the / usr / bin directory.

Cpulimit is used in a similar way to the nice command, but requires the user to use the – L option to explicitly define the maximum CPU utilization value of the process. For example:


Copy code

The code is as follows:

cpulimit -l 50 matho-primes 0 9999999999 > /dev/null &

2015612151349187.jpg (699×379)

From the above example, we can see that matho primes only uses 50% of the CPU resources, and the remaining CPU time is idle.

Cpulimit can also dynamically limit the process at runtime, and use the – P option to specify the PID of the process. The following is an example:


Copy code

The code is as follows:

cpulimit -l 50 -p 1234

Where 1234 is the PID of the process.
Cgroups command set

Finally, the usage of the most powerful control group (cgroups) is introduced. Cgroups is a mechanism provided by the Linux kernel, which can be used to specify the resource allocation of a group of processes. Specifically, using cgroups, users can limit the CPU occupancy, system memory consumption, network bandwidth, and the combination of these resources of a group of processes.

Compared with nice and cpulimit, the advantage of cgroups is that it can control a group of processes, unlike the former, which can only control a single process. At the same time, nice and cpulimit can only limit CPU utilization, while cgroups can limit the use of other process resources.

By making good use of cgroups, we can control the resource consumption of the whole subsystem. Take coreos as an example. This is the most simplified Linux distribution designed for large-scale server deployment. Its upgrade process is controlled by cgroups. In this way, the system will not affect the performance of the system when downloading and installing the upgraded version.

As a demonstration, we will create two cgroups and allocate different CPU resources to them. The two control groups are named “cpulimited” and “lesscpulimited” respectively.

Use the cgcreate command to create a control group as follows:


Copy code

The code is as follows:

sudo cgcreate -g cpu:/cpulimited
sudo cgcreate -g cpu:/lesscpulimited

The “- G CPU” option is used to set the upper limit of CPU usage. In addition to CPU, cgroups also provides cpuset, memory, blkio and other controllers. The difference between cpuset controller and CPU controller is that CPU controller can only limit the utilization of one CPU core, while cpuset can control multiple CPU cores.

CPU in CPU controller The shares attribute is used to control CPU usage. Its default value is 1024. We will lesscpulimited the CPU of the control group Shares is set to 1024 (the default value) and cpulimited is set to 512. After configuration, the kernel will allocate resources for the two control groups in a ratio of 2:1.

CPU to set the cpulimited group Shares is 512. Enter the following command:


Copy code

The code is as follows:

sudo cgset -r cpu.shares=512 cpulimited

Use the cgexec command to start the operation of the control group. In order to test the two control groups, we first use the cpulimited control group to start the matho primes process. The command line is as follows:


Copy code

The code is as follows:

sudo cgexec -g cpu:cpulimited /usr/local/bin/matho-primes 0 9999999999 > /dev/null &

When you open top, you can see that the matho primes process takes up all CPU resources.
2015612151412459.jpg (699×377)

Because there is only one process running in the system, no matter which control group it is started, it will use as much CPU resources as possible. CPU resource limits only take effect when two processes compete for CPU resources.

Now, let’s start the second matho primes process. This time, we start it in the lesscpulimited control group:


Copy code

The code is as follows:

sudo cgexec -g cpu:lesscpulimited /usr/local/bin/matho-primes 0 9999999999 > /dev/null &

Open top again and you can see the CPU The control group with a large share value will get more CPU running time.
2015612151432791.jpg (699×377)

Now, we add a matho primes process to the cpulimited control group:


Copy code

The code is as follows:

sudo cgexec -g cpu:cpulimited /usr/local/bin/matho-primes 0 9999999999 > /dev/null &

2015612151524829.jpg (699×378)

See, the CPU utilization ratio of the two control groups is still 2:1. Among them, the two matho primes processes in the cpulimited control group get basically the same CPU time, while the matho primes processes in the other group obviously get more running time.

For more usage methods, you can view the detailed usage of cgroups on red hatexplain。 (of course, CentOS 7 also has)
Use scout to monitor CPU usage

What is the easiest way to monitor CPU usage?Scout The tool can automatically monitor the CPU utilization and memory usage of the process.
2015612151552144.png (560×172)

The trigger function of Scout can also set the usage threshold of CPU and memory. When the threshold is exceeded, an alarm will be generated automatically.

A trial version of Scout is available here.
Computer system resources are very valuable. The three tools described above can help you effectively manage system resources, especially CPU resources:

  1. Nice can adjust the priority of the process at one time.
    Cpulimit can be useful when running CPU intensive tasks and keeping the system responsive.
    Cgroups is a Swiss Army knife for resource management, and it is also very flexible in use.