How is CPU usage calculated?

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

I always wondered how a computing processor calculates how much of its computing power is used. Also, if a processor freezes while doing some heavy computing, how does it (or something that processes this action) examine usage without hanging up?

+52
performance algorithm cpu cpu-architecture cpu-usage
Sep 20 '10 at 1:07
source share
7 answers

The CPU does not perform usage calculations on its own. It may have hardware features that facilitate this task, but it is mainly the task of the operating system. Thus, it is obvious that implementation details will be different (especially in the case of multi-core systems).

The general idea is to see how long the processor queue takes. The operating system can periodically look at the scheduler to determine the number of things that it needs to perform.

This is a Linux function (torn out from Wikipedia) that performs the specified 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 multitasking . This means that the OS is not going to allow programs to take up all the processing time and not have for themselves (if you do not) . In other words, even if the application is stuck, the OS can still steal some time for its work.

+25
Sep 20 '10 at 1:22
source share

There is a special task called the idle task, which starts when no other task can be started. Using% is just a percentage of the time when we are not starting a simple task. The OS will support the total amount of time spent on performing an unoccupied task:

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

If we take two segments of the run in one and a half seconds, we can calculate the percentage of those n seconds spent on the idle task, as (the second sample - the first sample) / n

Please note that this is what the OS does, not the processor. The concept of a task does not exist at the CPU level! (In practice, an idle task will cause the processor to sleep with the HLT instruction, so the CPU knows when it is not in use)

Regarding the second question, modern operating systems are proactive in multi-tasking, which means that the OS can abandon your task at any time. How does the OS actually steal the processor from your task? Interrupts: http://en.wikipedia.org/wiki/Interrupt

+48
Sep 20 '10 at 2:27
source share

To get CPU utilization, periodically try the total process time and find the difference.

For example, if this is the processor time for process 1:

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

And then you get them again after two seconds, and they:

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

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

Thus, over the past two seconds, 32% of the processor time was used in this process.

The specific system calls required to receive this information are (obviously) different on each platform. On Windows, you can use GetProcessTimes or GetSystemTimes if you want to use a shortcut for general use or inactivity of processor time.

+12
Sep 20 '10 at 1:38
source share

One way to do this:

Select a sampling interval, say every 5 minutes (300 seconds) of real time. You can get it from gettimeofday .

Get the process time that you used in these 300 seconds. You can use the times() call to get this. This will be new_process_time - old_process_time , where old_process_time is the time of the process that you saved from the last time interval.

The percentage of your processor then (process_time/elapsed_time)*100.0 You can set an alarm to call this calculation every thirty seconds.

I have a process that I do not want to use more than a certain target percentage of the processor. This method works very well and fits well with my system monitor. If we use too much CPU, we’ll get a little messy.

+6
06 Oct
source share

There are several ways to do this:

The processor supports several counters that measure performance, you can access them through the Papi interface. for example, here is a brief introduction: http://blogs.oracle.com/jonh/entry/performance_counter_generic_events

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

may require a PAPI_TOT_CYC counter, which is the number of cycles taken (if I remember correctly)

+1
Sep 20 '10 at 1:22
source share

Well, as far as I understand, there is a giant

 while(true){} 

that operating systems are spinning. Your process is controlled from this cycle. It allows you to execute external code directly on the processor in pieces. Not too exaggerating, this is a simplified simplification of what is actually happening.

+1
Sep 20 '10 at 1:24
source share

This is my basic understanding of a little impact on similar code. Programs such as the task manager or your system widget calls, such as NtQuerySystemInformation (), and use the information collected from the OS to make it easy to calculate the percentage of time that the processor has been idle or in use (at standard times). The CPU knows when it is idle, so it can determine when it is not idle. These programs can really get clogged ... my crummy laptop Task Manager freezes all the time when calculating CPU usage when it reaches 100%.

You can find sample code on the MSDN website that shows function calls to calculate 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 to the kernel code, which I believe ... that goes beyond my understanding.

+1
Sep 20 '10 at 1:28
source share



All Articles