mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-13 22:30:37 +08:00
commit
62cdde1a20
@ -1,93 +0,0 @@
|
|||||||
[#]: subject: (Use cpulimit to free up your CPU)
|
|
||||||
[#]: via: (https://fedoramagazine.org/use-cpulimit-to-free-up-your-cpu/)
|
|
||||||
[#]: author: (Gregory Bartholomew https://fedoramagazine.org/author/glb/)
|
|
||||||
[#]: collector: (lujun9972)
|
|
||||||
[#]: translator: (geekpi)
|
|
||||||
[#]: reviewer: ( )
|
|
||||||
[#]: publisher: ( )
|
|
||||||
[#]: url: ( )
|
|
||||||
|
|
||||||
Use cpulimit to free up your CPU
|
|
||||||
======
|
|
||||||
|
|
||||||
![][1]
|
|
||||||
|
|
||||||
Photo by [Henning Witzel][2] on [Unsplash][3]
|
|
||||||
|
|
||||||
The recommended tool for managing system resources on Linux systems is [cgroups][4]. 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_][5] 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 _[cpulimit][6]_ 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.
|
|
||||||
|
|
||||||
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 --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)
|
|
||||||
```
|
|
||||||
|
|
||||||
### 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][7]. 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][8]. 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
|
|
||||||
```
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: https://fedoramagazine.org/use-cpulimit-to-free-up-your-cpu/
|
|
||||||
|
|
||||||
作者:[Gregory Bartholomew][a]
|
|
||||||
选题:[lujun9972][b]
|
|
||||||
译者:[译者ID](https://github.com/译者ID)
|
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
||||||
|
|
||||||
[a]: https://fedoramagazine.org/author/glb/
|
|
||||||
[b]: https://github.com/lujun9972
|
|
||||||
[1]: https://fedoramagazine.org/wp-content/uploads/2021/06/cpulimit-816x345.jpg
|
|
||||||
[2]: https://unsplash.com/@henning?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText
|
|
||||||
[3]: https://unsplash.com/s/photos/speed-limit?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText
|
|
||||||
[4]: https://en.wikipedia.org/wiki/Cgroups
|
|
||||||
[5]: https://en.wikipedia.org/wiki/Nice_(Unix)
|
|
||||||
[6]: https://github.com/opsengine/cpulimit
|
|
||||||
[7]: https://en.wikipedia.org/wiki/Fibonacci_number
|
|
||||||
[8]: https://en.wikipedia.org/wiki/Task_parallelism
|
|
93
translated/tech/20210610 Use cpulimit to free up your CPU.md
Normal file
93
translated/tech/20210610 Use cpulimit to free up your CPU.md
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
[#]: subject: (Use cpulimit to free up your CPU)
|
||||||
|
[#]: via: (https://fedoramagazine.org/use-cpulimit-to-free-up-your-cpu/)
|
||||||
|
[#]: author: (Gregory Bartholomew https://fedoramagazine.org/author/glb/)
|
||||||
|
[#]: collector: (lujun9972)
|
||||||
|
[#]: translator: (geekpi)
|
||||||
|
[#]: reviewer: ( )
|
||||||
|
[#]: publisher: ( )
|
||||||
|
[#]: url: ( )
|
||||||
|
|
||||||
|
使用 cpulimit 来释放你的 CPU
|
||||||
|
======
|
||||||
|
|
||||||
|
![][1]
|
||||||
|
|
||||||
|
由 [Henning Witzel][2] 拍摄,发布于 [Unsplash][3]
|
||||||
|
|
||||||
|
在 Linux 系统上管理系统资源的推荐工具是 [cgroups][4]。虽然在可以调整的限制方面(CPU、内存、磁盘I/O、网络等)非常强大,但配置 cgroups 并不简单。[_nice_][5] 命令从 1973 年起就可以使用了。但它只是调整在一个处理器上竞争时间的进程之间的调度优先级。_nice_ 命令不会限制一个进程在单位时间内所能消耗的 CPU 周期的百分比。_[cpulimit][6]_ 命令提供了两个世界的最佳方案。它限制了一个进程在每单位时间内可以分配的 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 个[斐波那契数][7]的值。该脚本在两种情况下都作为 _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**键。按 **Q** 键,退出 _top_ 命令。
|
||||||
|
|
||||||
|
设置高于 100% 的限制只对能够进行[任务并行化][8]的程序有意义。对于这样的程序,高于 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][a]
|
||||||
|
选题:[lujun9972][b]
|
||||||
|
译者:[geekpi](https://github.com/译者ID)
|
||||||
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
[a]: https://fedoramagazine.org/author/glb/
|
||||||
|
[b]: https://github.com/lujun9972
|
||||||
|
[1]: https://fedoramagazine.org/wp-content/uploads/2021/06/cpulimit-816x345.jpg
|
||||||
|
[2]: https://unsplash.com/@henning?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText
|
||||||
|
[3]: https://unsplash.com/s/photos/speed-limit?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText
|
||||||
|
[4]: https://en.wikipedia.org/wiki/Cgroups
|
||||||
|
[5]: https://en.wikipedia.org/wiki/Nice_(Unix)
|
||||||
|
[6]: https://github.com/opsengine/cpulimit
|
||||||
|
[7]: https://en.wikipedia.org/wiki/Fibonacci_number
|
||||||
|
[8]: https://en.wikipedia.org/wiki/Task_parallelism
|
Loading…
Reference in New Issue
Block a user