我正在开发一个小功能,它可以让我的用户了解 CPU 的占用情况。
我正在使用 cat/proc/loadavg
,它返回众所周知的 3 个数字。
我的问题是在我开发时 CPU 现在什么都不做。
有没有一种在 CPU 上产生一些负载的好方法,我在想像 makecpudosomething 30
这样的东西,用于 0.3 或类似的负载。是否存在这样的应用程序?
还有,有什么办法可以控制地消耗 RAM 吗?
最佳答案
while true;
do openssl speed;
done
压力程序还可以让您将 cpu/mem/disk 加载到您想要模拟的水平:
stress is a deliberately simple workload generator for POSIX systems. It imposes a configurable amount of CPU, memory, I/O, and disk stress on the system. It is written in C, and is free software licensed under the GPLv2.
要保持特定水平的 cpu 利用率,比如 30%,请尝试 cpulimit:
它将适应当前的系统环境并针对系统上的任何其他事件进行调整。
此处还有一个针对 native CPU 速率限制的内核补丁:http://lwn.net/Articles/185489/
关于Linux:如何加载系统内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1971422/