Skip to content

Commit e210c66

Browse files
Tao ZhouJonathan Corbet
authored andcommitted
doc/zh_CN: add cpu-load Chinese version
Add cpu-load Chinese version and link it into admin-guide. Based on Alex's recent thread: https://lkml.kernel.org/r/[email protected] Reviewed-by: Alex Shi <[email protected]> Signed-off-by: Tao Zhou <[email protected]> Link: https://lore.kernel.org/r/BL0PR14MB37798BBF2307910DE73EF6D49A770@BL0PR14MB3779.namprd14.prod.outlook.com Signed-off-by: Jonathan Corbet <[email protected]>
1 parent 2cb3188 commit e210c66

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
=======
2+
CPU 负载
3+
=======
4+
5+
Linux通过``/proc/stat``和``/proc/uptime``导出各种信息,用户空间工具
6+
如top(1)使用这些信息计算系统花费在某个特定状态的平均时间。
7+
例如:
8+
9+
$ iostat
10+
Linux 2.6.18.3-exp (linmac) 02/20/2007
11+
12+
avg-cpu: %user %nice %system %iowait %steal %idle
13+
10.01 0.00 2.92 5.44 0.00 81.63
14+
15+
...
16+
17+
这里系统认为在默认采样周期內有10.01%的时间工作在用户空间,2.92%的时
18+
间用在系统空间,总体上有81.63%的时间是空闲的。
19+
20+
大多数情况下``/proc/stat``的信息几乎真实反映了系统信息,然而,由于内
21+
核采集这些数据的方式/时间的特点,有时这些信息根本不可靠。
22+
23+
那么这些信息是如何被搜集的呢?每当时间中断触发时,内核查看此刻运行的
24+
进程类型,并增加与此类型/状态进程对应的计数器的值。这种方法的问题是
25+
在两次时间中断之间系统(进程)能够在多种状态之间切换多次,而计数器只
26+
增加最后一种状态下的计数。
27+
28+
举例
29+
---
30+
31+
假设系统有一个进程以如下方式周期性地占用cpu::
32+
33+
两个时钟中断之间的时间线
34+
|-----------------------|
35+
^ ^
36+
|_ 开始运行 |
37+
|_ 开始睡眠
38+
(很快会被唤醒)
39+
40+
在上面的情况下,根据``/proc/stat``的信息(由于当系统处于空闲状态时,
41+
时间中断经常会发生)系统的负载将会是0
42+
43+
大家能够想象内核的这种行为会发生在许多情况下,这将导致``/proc/stat``
44+
中存在相当古怪的信息::
45+
46+
/* gcc -o hog smallhog.c */
47+
#include <time.h>
48+
#include <limits.h>
49+
#include <signal.h>
50+
#include <sys/time.h>
51+
#define HIST 10
52+
53+
static volatile sig_atomic_t stop;
54+
55+
static void sighandler (int signr)
56+
{
57+
(void) signr;
58+
stop = 1;
59+
}
60+
static unsigned long hog (unsigned long niters)
61+
{
62+
stop = 0;
63+
while (!stop && --niters);
64+
return niters;
65+
}
66+
int main (void)
67+
{
68+
int i;
69+
struct itimerval it = { .it_interval = { .tv_sec = 0, .tv_usec = 1 },
70+
.it_value = { .tv_sec = 0, .tv_usec = 1 } };
71+
sigset_t set;
72+
unsigned long v[HIST];
73+
double tmp = 0.0;
74+
unsigned long n;
75+
signal (SIGALRM, &sighandler);
76+
setitimer (ITIMER_REAL, &it, NULL);
77+
78+
hog (ULONG_MAX);
79+
for (i = 0; i < HIST; ++i) v[i] = ULONG_MAX - hog (ULONG_MAX);
80+
for (i = 0; i < HIST; ++i) tmp += v[i];
81+
tmp /= HIST;
82+
n = tmp - (tmp / 3.0);
83+
84+
sigemptyset (&set);
85+
sigaddset (&set, SIGALRM);
86+
87+
for (;;) {
88+
hog (n);
89+
sigwait (&set, &i);
90+
}
91+
return 0;
92+
}
93+
94+
95+
参考
96+
---
97+
98+
- http://lkml.org/lkml/2007/2/12/6
99+
- Documentation/filesystems/proc.rst (1.8)
100+
101+
102+
谢谢
103+
---
104+
105+
Con Kolivas, Pavel Machek

Documentation/translations/zh_CN/admin-guide/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Todolist:
5555
:maxdepth: 1
5656

5757
clearing-warn-once
58+
cpu-load
5859

5960
Todolist:
6061

@@ -71,7 +72,6 @@ Todolist:
7172
cgroup-v1/index
7273
cgroup-v2
7374
cifs/index
74-
cpu-load
7575
cputopology
7676
dell_rbu
7777
device-mapper/index

0 commit comments

Comments
 (0)