Documentation/translations/zh_CN/admin-guide/cpu-load.rst
Source file repositories/reference/linux-study-clean/Documentation/translations/zh_CN/admin-guide/cpu-load.rst
File Facts
- System
- Linux kernel
- Corpus path
Documentation/translations/zh_CN/admin-guide/cpu-load.rst- Extension
.rst- Size
- 2767 bytes
- Lines
- 106
- Domain
- Support Tooling And Documentation
- Bucket
- Documentation
- Inferred role
- Support Tooling And Documentation: documentation
- Status
- atlas-only
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
time.hlimits.hsignal.hsys/time.h
Detected Declarations
function sighandlerfunction hogfunction main
Annotated Snippet
========
CPU 负载
========
Linux通过``/proc/stat``和``/proc/uptime``导出各种信息,用户空间工具
如top(1)使用这些信息计算系统花费在某个特定状态的平均时间。
例如:
$ iostat
Linux 2.6.18.3-exp (linmac) 02/20/2007
avg-cpu: %user %nice %system %iowait %steal %idle
10.01 0.00 2.92 5.44 0.00 81.63
...
这里系统认为在默认采样周期內有10.01%的时间工作在用户空间,2.92%的时
间用在系统空间,总体上有81.63%的时间是空闲的。
大多数情况下``/proc/stat``的信息几乎真实反映了系统信息,然而,由于内
核采集这些数据的方式/时间的特点,有时这些信息根本不可靠。
那么这些信息是如何被搜集的呢?每当时间中断触发时,内核查看此刻运行的
进程类型,并增加与此类型/状态进程对应的计数器的值。这种方法的问题是
在两次时间中断之间系统(进程)能够在多种状态之间切换多次,而计数器只
增加最后一种状态下的计数。
举例
---
假设系统有一个进程以如下方式周期性地占用cpu::
两个时钟中断之间的时间线
|-----------------------|
^ ^
|_ 开始运行 |
|_ 开始睡眠
(很快会被唤醒)
在上面的情况下,根据``/proc/stat``的信息(由于当系统处于空闲状态时,
时间中断经常会发生)系统的负载将会是0
大家能够想象内核的这种行为会发生在许多情况下,这将导致``/proc/stat``
中存在相当古怪的信息::
/* gcc -o hog smallhog.c */
#include <time.h>
#include <limits.h>
#include <signal.h>
#include <sys/time.h>
#define HIST 10
static volatile sig_atomic_t stop;
static void sighandler (int signr)
{
(void) signr;
stop = 1;
}
static unsigned long hog (unsigned long niters)
{
stop = 0;
while (!stop && --niters);
return niters;
}
int main (void)
{
int i;
struct itimerval it = { .it_interval = { .tv_sec = 0, .tv_usec = 1 },
.it_value = { .tv_sec = 0, .tv_usec = 1 } };
Annotation
- Immediate include surface: `time.h`, `limits.h`, `signal.h`, `sys/time.h`.
- Detected declarations: `function sighandler`, `function hog`, `function main`.
- Atlas domain: Support Tooling And Documentation / Documentation.
- Implementation status: atlas-only.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.