使用 cpulimit 来释放你的 CPU
CPU
![](/data/attachment/album/202106/19/091410dnlcfpccy33l7378.jpg)


在 Linux 系统上管理系统资源的推荐工具是 [cgroups](https://en.wikipedia.org/wiki/Cgroups)。虽然在可以调整的限制方面（CPU、内存、磁盘 I/O、网络等）非常强大，但配置 cgroups 并不简单。[nice](https://en.wikipedia.org/wiki/Nice_(Unix)) 命令从 1973 年起就可以使用了。但它只是调整在一个处理器上竞争时间的进程之间的调度优先级。`nice` 命令不会限制一个进程在单位时间内所能消耗的 CPU 周期的百分比。[cpulimit](https://github.com/opsengine/cpulimit) 命令提供了两个世界的最佳方案。它限制了一个进程在每单位时间内可以分配的 CPU 周期的百分比，而且相对容易调用。


`cpulimit` 命令主要对长期运行的和 CPU 密集型的进程有用。编译软件和转换视频是长期运行的进程的常见例子，它们可以使计算机的 CPU 使用率达到最大。限制这类进程的 CPU 使用率将释放出处理器时间，供计算机上可能运行的其他任务使用。限制 CPU 密集型进程也将减少功耗及热输出，并可能减少系统的风扇噪音。限制一个进程的 CPU 使用率的代价是，它需要更多的时间来完成运行。


### 安装 cpulimit


`cpulimit` 命令在默认的 Fedora Linux 仓库中可用。运行下面的命令，在 Fedora Linux 系统上安装 `cpulimit`：



```
$ sudo dnf install cpulimit

```

### 查看 cpulimit 的文档


`cpulimit` 软件包并没有附带的手册页。使用下面的命令来查看 `cpulimit` 的内置文档。输出结果在下面提供。但你可能需要在你自己的系统上运行该命令，以防止自本文编写以来选项发生变化。



```
$ cpulimit --help
Usage: cpulimit [OPTIONS…] TARGET
   OPTIONS
      -l, --limit=N percentage of cpu allowed from 0 to 800 (required)
      -v, --verbose show control statistics
      -z, --lazy exit if there is no target process, or if it dies
      -i, --include-children limit also the children processes
      -h, --help display this help and exit
   TARGET must be exactly one of these:
      -p, --pid=N pid of the process (implies -z)
      -e, --exe=FILE name of the executable program file or path name
      COMMAND [ARGS] run this command and limit it (implies -z)

```

### 演示


为了演示 `cpulimit` 命令的使用方式，下面提供了一个精心设计的、计算量很大的 Python 脚本。该脚本首先在没有限制的情况下运行，然后在限制为 50% 的情况下运行。它计算的是第 42 个 [斐波那契数](https://en.wikipedia.org/wiki/Fibonacci_number) 的值。该脚本在这两种情况下都作为 `time` 命令的子进程运行，以显示计算答案所需的总时间。



```
$ /bin/time -f '(computed in %e seconds)' /bin/python -c 'f = lambda n: n if n<2 else f(n-1)+f(n-2); print(f(42), end=" ")'
267914296 (computed in 51.80 seconds)
$ /bin/cpulimit -i -l 50 /bin/time -f '(computed in %e seconds)' /bin/python -c 'f = lambda n: n if n<2 else f(n-1)+f(n-2); print(f(42), end=" ")'
267914296 (computed in 127.38 seconds)

```

当运行第一个版本的命令时，你可能会听到电脑上的 CPU 风扇转动起来。但在运行第二个版本时，你应该不会。第一个版本的命令不受 CPU 的限制，但它不应该导致你的电脑陷入瘫痪。它是以这样一种方式编写的：它最多只能使用一个 CPU 核心。大多数现代 PC 都有多个 CPU 核心，当其中一个 CPU 100% 繁忙时，可以毫无困难地同时运行其他任务。为了验证第一条命令是否使你的一个处理器达到最大，在一个单独的终端窗口中运行 `top` 命令并按下 `1` 键。要退出 `top` 命令可以按 `Q` 键。


设置高于 100% 的限制只对能够进行 [任务并行化](https://en.wikipedia.org/wiki/Task_parallelism) 的程序有意义。对于这样的程序，高于 100% 的增量代表一个 CPU 的全部利用率（200%=2 个CPU，300%=3 个CPU，等等）。


注意，在上面的例子中，`-i` 选项已经传递给 `cpulimit` 命令。这是必要的，因为要限制的命令不是 `cpulimit` 命令的直接子进程。相反，它是 `time` 命令的一个子进程，而后者又是 `cpulimit` 命令的一个子进程。如果没有 `-i` 选项，`cpulimit` 将只限制 `time` 命令。


### 最后说明


如果你想限制一个从桌面图标启动的图形程序，请将该程序的 `.desktop` 文件（通常位于 `/usr/share/applications` 目录下）复制到你的 `~/.local/share/applications` 目录下，并相应修改 `Exec` 行。然后运行下面的命令来应用这些变化：



```
$ update-desktop-database ~/.local/share/applications

```



---


via: <https://fedoramagazine.org/use-cpulimit-to-free-up-your-cpu/>


作者：[Gregory Bartholomew](https://fedoramagazine.org/author/glb/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
The recommended tool for managing system resources on Linux systems is [cgroups](https://en.wikipedia.org/wiki/Cgroups). While very powerful in terms of what sorts of limits can be tuned (CPU, memory, disk I/O, network, etc.), configuring cgroups is non-trivial. The [ nice](https://en.wikipedia.org/wiki/Nice_(Unix)) command has been available since 1973. But it only adjusts the scheduling priority among processes that are competing for time on a processor. The

*nice*command will not limit the percentage of CPU cycles that a process can consume per unit of time. The

*command provides the best of both worlds. It limits the percentage of CPU cycles that a process can allocate per unit of time and it is relatively easy to invoke.*

[cpulimit](https://github.com/opsengine/cpulimit)The *cpulimit* command is mainly useful for long-running and CPU-intensive processes. Compiling software and converting videos are common examples of long-running processes that can max out a computer’s CPU. Limiting the CPU usage of such processes will free up processor time for use by other tasks that may be running on the computer. Limiting CPU-intensive processes will also reduce the power consumption, heat output, and possibly the fan noise of the system. The trade-off for limiting a process’s CPU usage is that it will require more time to run to completion.

## Install cpulimit

The *cpulimit* command is available in the default Fedora Linux repositories. Run the following command to install *cpulimit* on a Fedora Linux system.

$sudo dnf install cpulimit

## View the documentation for cpulimit

The cpulimit package does not come with a man page. Use the following command to view cpulimit’s built-in documentation. The output is provided below. But you may want to run the command on your own system in case the options have changed since this article was written.

$cpulimit --helpUsage: cpulimit [OPTIONS…] TARGET OPTIONS -l, --limit=N percentage of cpu allowed from 0 to 800 (required) -v, --verbose show control statistics -z, --lazy exit if there is no target process, or if it dies -i, --include-children limit also the children processes -h, --help display this help and exit TARGET must be exactly one of these: -p, --pid=N pid of the process (implies -z) -e, --exe=FILE name of the executable program file or path name COMMAND [ARGS] run this command and limit it (implies -z)

## A demonstration

To demonstrate using the *cpulimit* command, a contrived, computationally-intensive Python script is provided below. The script is run first with no limit and then with a limit of 50%. It computes the value of the 42nd [Fibonacci number](https://en.wikipedia.org/wiki/Fibonacci_number). The script is run as a child process of the *time* command in both cases to show the total time that was required to compute the answer.

$/bin/time -f '(computed in %e seconds)' /bin/python -c 'f = lambda n: n if n<2 else f(n-1)+f(n-2); print(f(42), end=" ")'267914296 (computed in 51.80 seconds) $/bin/cpulimit -i -l 50 /bin/time -f '(computed in %e seconds)' /bin/python -c 'f = lambda n: n if n<2 else f(n-1)+f(n-2); print(f(42), end=" ")'267914296 (computed in 127.38 seconds)

You might hear the CPU fan on your PC rev up when running the first version of the command. But you should not when running the second version. The first version of the command is not CPU limited but it should not cause your PC to become bogged down. It is written in such a way that it can only use at most one CPU. Most modern PCs have multiple CPUs and can simultaneously run other tasks without difficulty when one of the CPUs is 100% busy. To verify that the first command is maxing out one of your processors, run the *top* command in a separate terminal window and press the **1** key. Press the **Q** key to quit the *top* command.

Setting a limit above 100% is only meaningful on a program that is capable of [task parallelism](https://en.wikipedia.org/wiki/Task_parallelism). For such programs, each increment of 100% represents full utilization of a CPU (200% = 2 CPUs, 300% = 3 CPUs, etc.).

Notice that the **-i** option has been passed to the *cpulimit* command in the above example. This is necessary because the command to be limited is not a direct child process of the *cpulimit* command. Rather it is a child process of the *time* command which in turn is a child process of the *cpulimit* command. Without the **-i** option, *cpulimit* would only limit the *time* command.

## Final notes

If you want to limit a graphical application that you start from a desktop icon, copy the application’s *.desktop* file (often located under the */usr/share/applications* directory) to your *~/.local/share/applications* directory and modify the *Exec* line accordingly. Then run the following command to apply the changes.

$update-desktop-database ~/.local/share/applications

## Thomas Mittelstaedt

Thanks for the ‘just-in-time’ hint. Will try that on the Android emulator virtual machine process qemu, which ran ‘wild’ yesterday.

## Ben

The version of CPU Limit you linked to in the article hasn’t been maintained in years. Most distributions/OSes (Debian, Ubuntu, openSUSE, FreeBSD) use the LimitCPU fork these days. LimitCPU does the same thing, has the same syntax, and features a manual page. Plus it’s still maintained and occasionally improved to provide more features/compiler tweaks. Its latest release was about a month ago.

http://limitcpu.sourceforge.net/

## Gregory Bartholomew

Thanks Ben. I didn’t know about LimitCPU.

## Gregory Bartholomew

For those who may be interested, here is a one-liner written in Perl that computes the 42nd Fibonacci number. It isn’t as useful for testing cpulimit though 😛.

## Ben

Thank you for this, very useful for my workloads (running long computations while keeping system interactive). I was wondering if you could comment on how this interacts with taskset[1] (cpu pinning).

From my understanding of your article cpulimit 100% would be equivalent to pinning to 1 CPU, but I’m wondering if that is 1 core (topology), or the equivalent (for the scheduler) of 1 CPU core. (assuming HT is disabled)

I tend(ed) to use taskset because it ensures while limiting overall CPU use, the running process would still be benefiting from the local caches bound to the selected (cores), but would be interesting to see if they can be combined.

Thanks!

[1]https://linux.die.net/man/1/taskset

## Ben

CPUlimit (or its modern fork LimitCPU) limits the target process(es) to an overall CPU resource limit.

Basically if you run “cpulimit -l 50 program-name” it means program-name won’t go above 50% of your CPU resources, as monitored by a system monitor like top.

It doesn’t limit a process to a specific CPU, just an overall consumption percentage of available resources.

## Gregory Bartholomew

Right. Sorry that I didn’t make that clear in the article. Thanks for the clarification. 🙂

## jama

I do use ‘taskset’ every once in while, and you can confirm the cpu pinning (topology) of your system with ‘lstopo’, which produces graphical image form your system.

Once you have identified the topology, you can assign the wanted cpu-pairs for your process, like ‘taskset –cpu-list 0,1,2,3’ or ‘taskset –cpu-list 0-3’ etc…

To verify, you may use ‘htop’ instead of ‘top’, as ‘htop’ displays the individual cpu core load as well.

## Ben

Thanks, lstopo is really neat!

## Raman Gupta

For others looking for this command, they are in Fedora package hwloc and hwloc-gui.

## Chris

Another command for this sort of thing, and will be more prevalent the more chiplets are employed is numactl.

numactl can limit processes and their children to a single node and direct which node they use.

But thanks for showing me another way.

## jama

Thank you for your excellent article, again, but in my opinion, this command, as well as ‘nice’, are not that much useful nowadays since the amount of cpu cores and memory have increased during the decades since 1973… What comes to the compiling, nowadays it in fact is possible to use ‘make -j <#cores>’ -flag to perform the compilation in parallel by distributing the compilation process among several cpus, making it faster, instead of using only one. In addition, the ‘oom-kill’ -process kills the processes it considers ‘unnecessary’, in order to keep the system stable.

This is my opinion. I would like to have processes to be finished as fast as possible, instead of having ‘nice’ queue of processes waiting to be finished.

The drawback might be, that even you ‘nice’, or otherwise ‘optimize’ a process, especially one having constant disk activity, like a compilation process, the disk activity in fact will not be affected that much, and each disk access (I/O) is taken away from other processes, making those more slower as they need to wait their share…

However, for some large-scale system administrators with 1000s of simultaneous users this kind of ‘optimization’ might be necessary.

## Cecille

Thanks for the article.

Bash does not seems to get limited:

fibonacci_42 () {

perl -e ‘use feature “signatures”; sub f($n) { $f[$n] //= ($n<2) ? $n : f($n-1) + f($n-2) }; print f(42), “\n”‘ 2>/dev/null

}

export -f fibonacci_42

$/bin/bash -E -c “fibonacci_42”

267914296

$cpulimit -i -l 1 /bin/bash -E -c “fibonacci_42”

267914296

## Gregory Bartholomew

It is because the Perl one-liner I provided in my comment does not require 100% CPU usage for any significant length of time. I think if you use the Python script it should work.

## Jesse

cpulimit maintainer here. In the example in your comment I wouldn’t expect limiting to work as I’m pretty sure the process doing the calculation is Perl, not bash. Bash is calling Perl in the function, but would probably just be waiting for Perl to return the answer if I’m reading your example correctly.

To use cpulimit (or LimitCPU) in this instance you’d want to do one of two things. Either run “cpulimit -l 10 perl -e ….” or “cpulimit -m -l 10 bash …”. The -m flag monitors child processes and limits them as well as the parent.

## newton

nosso Fedora ficou maravilhoso em nosso estilo plasma bem feito super rápido e bom demais para navegar ,parabéns

## Mark

Personally I find cpulimit only useful on machines with many cores. I have experienced issued on single core machines (yes, some VMs are setup that way) where using cpulimit to limit a process to 50% does work on the process, but cpulimit itself will use all remaining cpu itself; too much context switching in the OS between those two processes I guess.

However on multi core machines Some common utilities like ‘ffmpeg’ will happily span cores and drive them all to 100% busy.

Apart from the noise of spinning fans, utilities like lm_sensors or the extremely usefull ‘bpytop’ can show the core temperatures sitting at a point where they are probably considering shutting themselves down.

Using cpulimit with the flag to include child processes to throttle it resolves that; although as noted in an earlier reply things will take longer, a ffmpeg run that would take 30mins running hot can take over 2hrs when throttled; but in my opinion better than leaving the cores running at what they report as a critical temp for 30mins when you know in advance that is what will happen.

I have not looked at tasksets so thanks to the comment on that, if they are available on all OS’s I may look into that further.

cpulimit is a nice tool to know about as it is available everywhere, it is in most RedHat and Debian based package repositories and where it is not I have never had problems building it from the github source.

For those like me that do a lot of cpu intensive scheduled batch work it is worth knowing about and installing, or even if just for one annoying program you don’t care about that slows down your machine every day (thinking of you packagekit).

Gregory, thanks for the ‘final notes’ section on how to set it up within a desktop application setting. The ‘update-desktop-database’ command I wasn’t aware of and has probably solved a few unrelated issues :-).

## Gregory Bartholomew

Hi Mark!

FYI to everyone else — Mark is the one who originally suggested this article. Thanks for notifying us about this command. I didn’t use the ffmpeg example from your original proposal because it is not in the default Fedora repositories and we’ve been told in the past not to promote it on the Magazine because of its non-FOSS codecs. Hope that you don’t mind that I took your proposal and wrote up the article. It had been setting in our “approved specifications” category for over a year and we’ve been a little low on contributions lately.

The reason that I included that bit about how to use it with GUI applications at the end is because someone had asked that exact question a while ago on Fedora’s user support forum:

https://ask.fedoraproject.org/t/limit-cpu-utilisation-core/12816/2

I pointed them at the cpulimit command you had mentioned and they said that it worked to fix their problem. So your proposal has been very useful to lots of people. Thanks! And thanks also to Jesse who is maintaining the software of course. 🙂

## jama

Mark,

If you’re concerned about

…’Some common utilities like ‘ffmpeg’ will happily span cores and drive them all to 100% busy.

Apart from the noise of spinning fans, utilities like lm_sensors or the extremely usefull ‘bpytop’ can show the core temperatures sitting at a point where they are probably considering shutting themselves down.’…,

one option could be limiting the cpu frequency itself.

The available cpu frequencies can be checked with:

cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies

3600000 2800000 2200000

Where we can see that the slowest supported speed is 2,2GHz.

By forcing this smallest value to each core, the temperatures will not get high, keeping the fans silent as well:

for i in {0..15} ; do echo 2200000 > /sys/devices/system/cpu/cpu$i/cpufreq/scaling_max_freq ; done

And the cpu frequencies will not exceed 2,2GHz.

To revert the setting, just replace the 2200000 with 3600000.

## Fionn Langhans

Thanks, that’s such a useful feature for background processes!

## Tom

I think it’s a very confusing to understand the CPU percentage help message.

Is 1 CPU core 100%, or is 100% all of the available cores? If 100% is all available cores/threads you should not be able to choose 800%.

If 1 core = 100%, what about hypterthreading cores?

If 100% is one HT core, can you only control a 4 core/ 8 HT thread CPU?

What if you have 16 cores available for example? It says maximum 800%

## Gregory Bartholomew

I think you’ll have to ask the developer to be sure. But, from the results of the demonstration in the article, it appears that 1 core = 100%. The Python script, unrestricted, will use 100% of 1 core. When limited to 50%, the script took twice as long to run (plus context switching overhead). If the 50 were out of a scale of 800 then it would have taken 800/50=16 times longer to compute the answer. My guess is that cpulimit isn’t capable of restricting processes across more than 8 cores.

## Ben

cpulimit judges usage by number of threads available. As it says in the manual page: “percentage of CPU allowed from 1 up. Usually 1 – 100, but can be higher on multi-core CPUs.”

## Andrew

A little new here. In the final notes section, where stated, “modify the Exec line accordingly”, do I just add the cpulimit -l N _name_of_program in that order?

I just want to make sure I’m doing this right.

## Gregory Bartholomew

In the simplest case, yes. If the program forks/spawns other programs that you also want limited, then you would need to add -i (include childern). And if the program is a flatpak then it is even more complicated. See fatka’s explanation here for an explanation of how to get it to work with flatpaks (his post was the reason I added that final note about using cpulimit with GUI programs to this article).

## Andrew

Thank you, that source made me understand a lot better on how to use this program!

The learning process is always appreciated!