Documentation/cpu-load.txt

Chinese translated version of Documentation/cpu-load.txt


If you have any comment or update to the content, please contact the
original document maintainer directly.  However, if you have a problem
communicating in English you can also ask the Chinese maintainer for
help.  Contact the Chinese maintainer if this translation is outdated
or if there is a problem with the translation.


Chinese maintainer: 李仁亨 <154778998@qq.com>
---------------------------------------------------------------------
Documentation-cpu-load.txt 的中文翻译


如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
译存在问题,请联系中文版维护者。


中文版维护者: 李仁亨  <154778998@qq.com>
中文版翻译者: 李仁亨  <154778998@qq.com>
中文版校译者: 李仁亨  <154778998@qq.com>




以下为正文
---------------------------------------------------------------------


CPU load
CPU 负载
--------


Linux exports various bits of information via `/proc/stat' and
`/proc/uptime' that userland tools, such as top(1), use to calculate
the average time system spent in a particular state, for example:


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


    ...


Here the system thinks that over the default sampling period the
system spent 10.01% of the time doing work in user space, 2.92% in the
kernel, and was overall 81.63% of the time idle.


在系统的默认采样周期里,系统在用户空间中花费了10.01%的时间,2.92%的时间
用在内核,81.63%时间处于空闲。


In most cases the `/proc/stat' information reflects the reality quite
closely, however due to the nature of how/when the kernel collects
this data sometimes it can not be trusted at all.


在大多数情况下,`/proc/stat'的信息能相当紧密地反映真实情况,但是由于
不知道内核是如何及何时收集这些数据的,所以这些信息有时不能完全相信。


So how is this information collected?  Whenever timer interrupt is
signalled the kernel looks what kind of task was running at this
moment and increments the counter that corresponds to this tasks
kind/state.  The problem with this is that the system could have
switched between various states multiple times between two timer
interrupts yet the counter is incremented only for the last state.


那么,这些信息是怎么收集的呢?每当定时器发出中断信号,内核就查看
是那种任务在运行,并且将该任务对应的种类/状态计数器递增。
这种方法的问题是,系统要在两个定时器中断之间,在不同的状态间多次切换,
但是计数器却只在最后的状态增加。




Example
例如
-------


If we imagine the system with one task that periodically burns cycles
in the following manner:


如果我们假设系统的一个任务,以下列方式定期循环运行:


 time line between two timer interrupts
  两个定时器中断之间的时间线
|--------------------------------------|
 ^                                    ^
 |_ something begins working          |
 某一任务开始工作
                                      |_ something goes to sleep
 某一任务进入休眠
                                     (only to be awaken quite soon)
(却又马上被唤醒)


In the above situation the system will be 0% loaded according to the
`/proc/stat' (since the timer interrupt will always happen when the
system is executing the idle handler), but in reality the load is
closer to 99%.


在上述情况下,根据`/proc/stat' 系统的负载将会是0%(因为当系统执行
闲置处理程序时,定时器中断会一直产生),但是真实情况是负载将近99%。


One can imagine many more situations where this behavior of the kernel
will lead to quite erratic information inside `/proc/stat'.


可想而知,更多的情况下,这种内核行为会导致`/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 } };
     sigset_t set;
     unsigned long v[HIST];
     double tmp = 0.0;
     unsigned long n;
     signal (SIGALRM, &sighandler);
     setitimer (ITIMER_REAL, &it, NULL);


     hog (ULONG_MAX);
     for (i = 0; i < HIST; ++i) v[i] = ULONG_MAX - hog (ULONG_MAX);
     for (i = 0; i < HIST; ++i) tmp += v[i];
     tmp /= HIST;
     n = tmp - (tmp / 3.0);


     sigemptyset (&set);
     sigaddset (&set, SIGALRM);


     for (;;) {
         hog (n);
         sigwait (&set, &i);
     }
     return 0;
}




References
参考文献
----------


http://lkml.org/lkml/2007/2/12/6
Documentation/filesystems/proc.txt (1.8)




Thanks
感谢
------


Con Kolivas, Pavel Machek
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值