Use nice, cpulimit, and cgroups to limit cpu usage

Source: Internet
Author: User
Tags high cpu usage

Use nice, cpulimit, and cgroups to limit cpu usage

The Linux kernel is a great circus performer who plays tricks between processes and system resources carefully and keeps the system running properly. At the same time, the kernel is fair: it distributes resources fairly to various processes.

But what should you do if you want to increase the priority of an important process? Or, how can we reduce the priority of a process? Or, how do I limit the resources used by a group of processes?

The answer is that you need to specify the process priority for the kernel.

Most processes have the same priority at startup, so the Linux Kernel performs fair scheduling. If you want a CPU-intensive process to run at a lower priority, You have to configure the scheduler in advance.

The following describes three methods to control the running time of a process:

  • Use the nice command to manually reduce the task priority.
  • Use the cpulimit command to continuously pause processes to control the processing capacity occupied by processes not exceeding the specific limits.
  • The control group function built in linux provides a mechanism to limit the resource consumption of processes.

Let's take a look at the working principles and advantages and disadvantages of the three tools.

Simulate high cpu usage

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

Unfortunately, this tool does not have a pre-compiled version on CentOS, so it must be installed from the source code. 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 make install for compilation and installation. In this way, the matho-primes program is installed in the/usr/local/bin directory.

Next, run the following command:

  1. /Usr/local/bin/matho-primes 09999999999>/dev/null &

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

Now, using the top Command, we can see that the matho-primes process has exhausted all your cpu resources.

Okay, then (press the q key) to exit the 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

Next, we will introduce how to use the nice command. The nice command can modify the priority of a process so that the process runs less frequently. This function is particularly useful when running cpu-intensive background processes or batch jobs. The value range of nice is [-20, 19],-20 indicates the highest priority, and 19 indicates the lowest priority. The default nice value of a Linux Process is 0. You can use the nice command (without any parameters) to set the nice value of the process to 10. In this way, the scheduler considers the process as a process with a lower priority, thus reducing the allocation of cpu resources.

The following is an example. We run two matho-primes processes at the same time. One uses the nice command to start and the other runs normally:

  1. Nice matho-primes 09999999999>/dev/null &
  2. Matho-primes 09999999999>/dev/null &

Then run the top command.

No, the normal running process (nice value is 0) gets more cpu running time. On the contrary, the process running with the nice command takes less cpu time (nice value is 10 ).

In actual use, if you want to run a CPU-intensive program, it is best to use the nice command to start it, so that other processes can get a higher priority. That is to say, 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 runtime. When using the renice command, you must first find the PID of the process. The following is an example:

  1. Renice + 101234

Among them, 1234 is the PID of the process.

After testing the nice and renice commands, remember to kill all the matho-primes processes.

Cpulimit command

Next we will introduce the usage of the cpulimit command. The cpulimit command sets a cpu usage threshold for a process and monitors whether the process exceeds the threshold in real time. If the threshold is exceeded, the process is paused for a period of time. Cpulimit uses the SIGSTOP and SIGCONT signals to control the process. It does not modify the nice value of a process, but dynamically adjusts it by monitoring the cpu usage of the process.

The advantage of cpulimit is that it can control the upper limit of cpu usage of processes. However, compared with nice, there are also disadvantages, that is, even if the cpu is idle, the process cannot completely use the entire cpu resource.

On CentOS, you can use the following method to install it:

  1. Wget-O cpulimit.zip https://github.com/opsengine/cpulimit/archive/master.zip
  2. Unzip cpulimit.zip
  3. Cd cpulimit-master
  4. Make
  5. Sudo cp src/cpulimit/usr/bin

The above command line will first download the source code from GitHub to the local, then decompress, compile, and install it in the/usr/bin directory.

The usage of cpulimit is similar to that of the nice command, but you need to use the-l option to explicitly define the upper limit of the cpu usage of the process. Example:

  1. Cpulimit-l 50 matho-primes 09999999999>/dev/null &

The above example shows that matho-primes only uses 50% of cpu resources, and the remaining cpu time is in idle.

Cpulimit can also dynamically restrict processes at runtime. The-p option is used to specify the process PID. The following is an example:

  1. Cpulimit-l 50-p 1234

Among them, 1234 is the PID of the process.

Cgroups command set

Finally, we will introduce the use of the most powerful control group (cgroups. Cgroups is a mechanism provided by the Linux kernel. It can be used to specify the resource allocation of a group of processes. Specifically, cgroups allows you to limit the cpu usage, system memory consumption, network bandwidth, and the combination of these resources of a group of processes.

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

By making good use of cgroups, you can control the resource consumption of the entire subsystem. Taking CoreOS as an example, this is the most simplified Linux release designed for large-scale server deployment. Its upgrade process uses cgroups for control. In this way, the system will not affect the system performance when downloading and installing the upgraded version.

The following example shows how to create two control groups (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 shown below:

  1. Sudo cgcreate-g cpu:/cpulimited
  2. 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 cpu set, memory, blkio, and other controllers. The difference between the cpu set controller and the cpu controller is that the cpu controller can only limit the usage of one cpu core, while the cpu set can control multiple cpu cores.

The cpu. shares attribute in the cpu controller is used to control cpu usage. The default value is 1024. We set the cpu Of The lesscpulimited control group. shares is set to 1024 (default), and cpulimited is set to 512. After the configuration, the kernel allocates resources to the two control groups at a ratio.

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

  1. Sudo cgset-r cpu. shares = 512 cpulimited

Run the cgexec command to start the control group. To test the control groups, use the cpulimited control group to start the matho-primes process. The command line is as follows:

  1. Sudo cgexec-g cpu: cpulimited/usr/local/bin/matho-primes 09999999999>/dev/null &

Open top and you can see that the matho-primes process occupies all cpu resources.

Because only one process runs in the system, no matter which control group it is placed to start, it will use as many cpu resources as possible. The cpu resource limit takes effect only when two processes compete for cpu resources.

Now we start the second matho-primes process. This time we start it in the lesscpulimited control group:

  1. Sudo cgexec-g cpu: lesscpulimited/usr/local/bin/matho-primes 09999999999>/dev/null &

Open top and you will see that the control group with a high cpu. shares value will get more cpu running time.

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

  1. Sudo cgexec-g cpu: cpulimited/usr/local/bin/matho-primes 09999999999>/dev/null &

No, the cpu usage of the two control groups is still. The cpu time obtained by the two matho-primes processes in the cpulimited control group is basically the same, while the matho-primes process in the other group obviously gets more running time.

For more usage methods, you can view detailed cgroups instructions on Red Hat. (Also available in CentOS 7)

Use Scout to monitor cpu usage

What is the easiest way to monitor cpu usage? The Scout tool can automatically monitor the cpu usage and memory usage of processes.

Scout trigger can also set the threshold for cpu and memory usage. When the threshold is exceeded, an alarm is automatically generated.

You can obtain the trial version of Scout from here.

Summary

Computer system resources are very valuable. The three tools described above can help you effectively manage system resources, especially cpu resources:

  • Nice can adjust the priority of a process at one time.
  • Cpulimit is useful when running cpu-intensive tasks and maintaining system responsiveness.
  • Cgroups is a Swiss Army knife for resource management and is flexible in use.

This article permanently updates the link address:

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.