views:

376

answers:

6

I hope this is the right place to ask this question, as I think it is related to algorithms and performance.

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.

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?

+1  A: 

there is a number of ways you can do it:

Processor maintains several counters which measure performance, you can access them using Papi interface. for example here is a brief introduction: http://blogs.sun.com/jonh/entry/performance_counter_generic_events

also: http://www.drdobbs.com/tools/184406109

the counter you may want is PAPI_TOT_CYC which is number of busy cycles (if I remember correctly)

aaa
+6  A: 

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).

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.

This is a function Linux in (ripped from Wikipedia) that performs said calculation:

#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.

In silico
"The CPU itself actually doesn't do any CPU usage calculations." - Where does the OS code that samples the workload on the processor execute? :)
Ani
@Ani: Well, the CPU can't do anything by itself without code, no? :-) What I meant was that there isn't an opcode or machine instruction that will tell you CPU usage, AFAIK.
In silico
http://en.wikipedia.org/wiki/Hardware_performance_counter
aaa
@aaa carp: Ah, that's interesting, I stand corrected. Although I'm pretty sure the OS still have to calculate (overall) CPU usage based on the performance counters.
In silico
sure, you still have to process them, they are just numbers in register. performance counters give pretty good information that operating system may not, for example cache miss, etc.
aaa
That's load average, rater than CPU usage (load average is the average length of the run queue, somewhat related to, but not identical to, CPU usage).
Vatine
@Vatine: I interpreted the OP's question as having to do with CPU load because the question mentions performance. If CPU A has 2 pending tasks while CPU B has 20 pending tasks (both performing similar things), both may be using 100% of the CPU time, but CPU A is performing better.
In silico
Very interesting, thank you all for explaining.
SimpleCoder
A: 

Well, as far as I understand it there's a giant

while(true){}

loop that the operating systems spins up. Your process is managed from within that loop. It allows external code to be executed directly on the processor in chunks. Without exaggerating too much, this is an uber-simplification of what is actually going on.

Gleno
+1  A: 

This is my basic understanding from having a little exposure to similar code. Programs like Task Manager or your widget access system calls like NtQuerySystemInformation() and use the information gathered from the OS to make the simple calculation of the percent of time a CPU is idle or being used (in a standard amount of time). A CPU knows when it is idle so it can therefore determine when it's not idle. These programs can indeed get clogged up...my crummy laptop's Task Manager freezes all the time when calculating CPU usage when it's topping out at 100%.

A cool bit of example code can be found on MSDNs website that shows function calls for calculating CPU usage for a set of instructions: http://msdn.microsoft.com/en-us/library/aa364157(VS.85).aspx

What these system calls do is access kernel code I believe... which is beyond the scope of my understanding.

SauceMaster
+3  A: 

To get CPU usage, periodically sample the total process time, and find the difference.

For example, if these are the CPU times for process 1:

kernel: 1:00:00.0000
user:   9:00:00.0000

And then you obtain them again two seconds later, and they are:

kernel: 1:00:00.0300
user:   9:00:00.6100

You subtract the kernel times (for a difference of 0.03) and the user times (0.61), add them together (0.64), and divide by the sample time of 2 seconds (0.32).

So over the past two seconds, the process used an average of 32% CPU time.

The specific system calls needed to get this info are (obviously) different on every platform. On Windows, you can use GetProcessTimes, or GetSystemTimes if you want a shortcut to total used or idle CPU time.

zildjohn01
+5  A: 

There's a special task called the idle task that runs when no other task can be run. The % usage is just the percentage of the time we're not running the idle task. The OS will keep a running total of the time spent running the idle task:

  • when we switch to the idle task, set t = current time
  • when we switch away from the idle task, add (current time - t) to the running total

If we take two samples of the running total n seconds apart, we can calculate the percentage of those n seconds spent running the idle task as (second sample - first sample)/n

Note that this is something the OS does, not the CPU. The concept of a task doesn't exist at the CPU level! (In practice, the idle task will put the processor to sleep with a HLT instruction, so the CPU does know when it isn't being used)

As for the second question, modern operating systems are preemptively multi-tasked, which means the OS can switch away from your task at any time. How does the OS actually steal the CPU away from your task? Interrupts: http://en.wikipedia.org/wiki/Interrupt

dave
++ Excellent answer. I also find myself telling people that programs are either running or waiting, and using 100% or 0% respectively, of the core. The "% utilization" they see is just a short running average. You'd think that would be obvious, but sometimes it's not.
Mike Dunlavey