如何CPU使用率计算出来的? [英] How is CPU usage calculated?

查看:152
本文介绍了如何CPU使用率计算出来的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的桌面上,我有一个小部件,告诉我我现在的CPU使用率。它也显示了用法为每次我的两个核心。

On my desktop, I have a little widget that tells me my current CPU usage. It also shows the usage for each of my two cores.

我一直在想,如何在CPU计算出其处理能力的多正在使用?此外,如果CPU挂了做一些激烈的计算,如何能(或任何处理此活动)检查使用情况,而不会挂了呢?

I always wondered, how does the CPU calculate how much of its processing power is being used? Also, if the CPU is hung up doing some intense calculations, how can it (or whatever handles this activity) examine the usage, without getting hung up as well?

推荐答案

该CPU本身并不做使用率计算。它可能有硬件的功能,使这项工作更容易,但它主要是操作系统的工作。所以很明显的实现的细节会有所不同(特别是在多核系统的情况下)。

The CPU doesn't do the usage calculations by itself. It may have hardware features to make that task easier, but it's mostly the job of the operating system. So obviously the details of implementations will vary (especially in the case of multicore systems).

总的想法是,看看有多长是事物的CPU需要做的队列。操作系统可以看看调度定期确定的事情是必须做的数额。

The general idea is to see how long is the queue of things the CPU needs to do. The operating system may take a look at the scheduler periodically to determine the amount of things it has to do.

这是一个Linux的功能(维基百科撕开)执行所述计算

#define FSHIFT   11  /* nr of bits of precision */
#define FIXED_1  (1<<FSHIFT) /* 1.0 as fixed-point */
#define LOAD_FREQ (5*HZ) /* 5 sec intervals */
#define EXP_1  1884  /* 1/exp(5sec/1min) as fixed-point */
#define EXP_5  2014  /* 1/exp(5sec/5min) */
#define EXP_15 2037  /* 1/exp(5sec/15min) */

#define CALC_LOAD(load,exp,n) \
    load *= exp; \
    load += n*(FIXED_1-exp); \
    load >>= FSHIFT;

unsigned long avenrun[3];

static inline void calc_load(unsigned long ticks)
{
    unsigned long active_tasks; /* fixed-point */
    static int count = LOAD_FREQ;

    count -= ticks;
    if (count < 0) {
        count += LOAD_FREQ;
        active_tasks = count_active_tasks();
        CALC_LOAD(avenrun[0], EXP_1, active_tasks);
        CALC_LOAD(avenrun[1], EXP_5, active_tasks);
        CALC_LOAD(avenrun[2], EXP_15, active_tasks);
    }
}

关于你问题的第二部分,最先进的操作系统是多任务。这意味着操作系统不会让程序占用所有的处理时间,没有任何为自己的(除非你把它做到这一点)。换句话说,即使应用程序似乎挂起,操作系统可以的还是的偷一些时间离开自己的工作。

As for the second part of your question, most modern operating systems are multi-tasked. That means the OS is not going to let programs take up all the processing time and not have any for itself (unless you make it do that). In other words, even if an application appears hung, the OS can still steal some time away for its own work.

这篇关于如何CPU使用率计算出来的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆