tutorial on limiting CPU usage in Linux systems

Source: Internet
Author: User
Tags command line memory usage zip centos cpu usage high cpu usage advantage

The Linux kernel is a great circus performer who plays juggling carefully between process and system resources and keeps the system running properly. At the same time, the kernel is fair: it allocates resources equitably to processes.

But what if you need to prioritize an important process? Or, how do you reduce the priority of a process? Or, how do you limit the resources used by a group of processes?

The answer is that the user is required to specify the priority of the process for the kernel

Most processes start at the same priority level, so the Linux kernel is fairly scheduled. If you want a CPU-intensive process to run at a lower priority, you have to configure the scheduler beforehand.

Here are 3 ways to control the running time of a process:

Manually reduce the priority of a task by using the Nice command.

Use the CPULimit command to pause the process constantly to control that the process is occupied with no more than a specific limit.

Using the Linux built-in control groups function, it provides a mechanism for restricting process resource consumption.

Let's take a look at how these 3 tools work and their pros and cons.

Simulate high CPU usage

Before we analyze these 3 technologies, we need to install a tool to simulate high CPU usage scenarios. We use CentOS as a test system and use the prime number generator in Mathomatic toolkit to simulate CPU load.

Unfortunately, the tool does not have a precompiled version on the CentOS, so it has to be installed from the source. First download the source package from the http://mathomatic.orgserve.de/mathomatic-16.0.5.tar.bz2 link and unzip it. Then go to the Mathomatic-16.0.5/primes folder and run make and Sudo made install for compilation and installation. In this way, the Matho-primes program is installed into the/usr/local/bin directory.

Next, run through the command line:

The code is as follows:

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

When the program runs, it outputs a prime number from 0 to 9999999999. Because we do not need these output results, directly redirect the output to the/dev/null.

Now, using the top command, you can see that the matho-primes process is draining all of your CPU resources.

OK, next (press Q) exit top and kill the Matho-primes process (use FG command to switch the process to the foreground, 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 runs less frequently. This feature is especially useful when running a CPU-intensive background process or batch job. The value range for the nice value is [ -20,19],-20 represents the highest priority, while 19 represents the lowest priority. The default nice value for the Linux process is 0. You can set the process's nice value to 10 with the Nice command, with no arguments. This allows the scheduler to treat the process as a lower-priority process, thereby reducing the allocation of CPU resources.

Here's an example where we run two matho-primes processes at the same time, one with the nice command to start the run, and the other to run normally:

The code is as follows:

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

Matho-primes 0 9999999999 >/dev/null &

Run the top command again.

See, the normal running process (Nice value 0) gets more CPU uptime, whereas processes running with the nice command consume less CPU time (nice value 10).

In practice, if you want to run a CPU-intensive program, it's best to start it with the Nice command, which will ensure that other processes get higher priority. That is, even if your server or desktop is overloaded, you can respond quickly.

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

The code is as follows:

Renice +10 1234

Of these, 1234 is the PID of the process.

After testing the nice and Renice commands, remember to kill the matho-primes process.

CPULimit command

Next, you'll describe the use of the CPULimit command. The CPULimit command works by preset a CPU occupancy threshold for the process and monitoring whether the process is out of this threshold in real time, and allowing the process to pause for a period of time if it is exceeded. CPULimit uses both SIGSTOP and Sigcont to control the process. Instead of modifying the nice value of the process, it makes dynamic adjustments by monitoring the CPU usage of the process.

The advantage of CPULimit is that you can control the upper-bound value of the CPU usage of the process. But there is a disadvantage compared to Nice, that is, even if the CPU is idle, the process can not fully use the entire CPU resources.

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

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 command line above will be downloaded from the GitHub source to the local, and then unzipped, compiled, and installed into the/usr/bin directory.

CPULimit is similar to the nice command, but requires that users use the-l option to explicitly define a process's CPU usage upper-bound value. Examples:

The code is as follows:

Cpulimit-l matho-primes 0 9999999999 >/dev/null &

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

CPULimit can also dynamically restrict processes at run time, using the-P option to specify the PID of the process, and here is an example:

The code is as follows:

Cpulimit-l 50-p 1234

Of these, 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 that allows you to specify a resource allocation for a set of processes. Specifically, with Cgroups, users can qualify a set of processes for CPU usage, system memory consumption, network bandwidth, and a combination of these resources.

The advantage of comparing nice and cpulimit,cgroups is that it controls a set of processes, unlike the former, which can only control a single process. At the same time, nice and cpulimit only limit CPU usage, while cgroups can limit the use of other process resources.

The use of cgroups can control the resource consumption of the whole subsystem. Take CoreOS as an example, this is a simplified version of the Linux distribution designed for large-scale server deployments, and its upgrade process is to use cgroups to control. In this way, the system does not affect the performance of the system when downloading and installing the upgraded version.

Here's a demo, we'll create two control groups (cgroups) and assign them different CPU resources. The two control groups are named "cpulimited" and "lesscpulimited" respectively.

Use the Cgcreate command to create a control group, as follows:

The code is as follows:

sudo cgcreate-g cpu:/cpulimited

sudo cgcreate-g cpu:/lesscpulimited

Where the "-G CPU" option is used to set the CPU usage limit. In addition to the CPU, Cgroups also provides Cpuset, memory, Blkio and other controllers. The difference between a Cpuset controller and a CPU controller is that the CPU controller can only limit the utilization of one CPU core, while the Cpuset can control multiple CPU cores.

The Cpu.shares property in the CPU controller is used to control CPU usage. Its default value is 1024, we set the lesscpulimited control group Cpu.shares to 1024 (the default), and the cpulimited is set to 512, the kernel will allocate resources for the two control groups according to the 2:1 ratio.

To set the cpu.shares for the cpulimited group to 512, enter the following command:

The code is as follows:

sudo cgset-r cpu.shares=512 cpulimited

To run the control group using the Cgexec command, in order to test the two control groups, we start the matho-primes process with the cpulimited control group, which is the following command line:

The code is as follows:

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

Open top to see that the matho-primes process is consuming all of the CPU resources.

Because only one process runs on the system, it uses as much CPU resources as possible, regardless of which control group it is placed in. CPU resource limits take effect only if two processes scramble for CPU resources.

So now we're going to start a second matho-primes process, and this time we'll start it in the Lesscpulimited control group:

The code is as follows:

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

When you turn on top, you can see that the control group with the Cpu.shares value will get more CPU uptime.

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

The code is as follows:

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

See, the CPU occupancy rate for the two control groups is still 2:1. Of these, the two matho-primes processes in the Cpulimited control group have roughly the same CPU time, while the matho-primes process in the other group apparently obtains more uptime.

For more use, you can view detailed cgroups usage instructions on Red Hat. (Of course CentOS 7 also have)

Use Scout to monitor CPU usage

What is the easiest way to monitor CPU usage? The Scout tool is able to monitor the CPU usage and memory usage of the process automatically.

Scout Trigger (trigger) function can also set the CPU and memory use threshold, exceeding the gate limit will automatically generate alarm.

You can get a trial version of Scout from here.


The system resources of the computer are very valuable. The 3 tools described above can help you effectively manage system resources, especially CPU resources:

Nice can adjust the priority of a process at a one-time level.

CPULimit is useful when running CPU-intensive tasks and maintaining the responsiveness of the system.

Cgroups is the Swiss Army knife for resource management and is also flexible in its use.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.