96

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?

Chris Laplante
  • 29,338
  • 17
  • 103
  • 134
  • 1
    Are you asking about CPU power management, like the CPU detecting whether it's using a lot of electricity and making a lot of heat or not? Skylake for example has a microcontroller on die to do power management (separate from CPU cores), with inputs from temperature / power sensors and some workload info. (e.g. it will even downclock when running code with a lot of cache / TLB misses, even when in OS terms it's using 100% CPU)... – Peter Cordes Apr 15 '18 at 01:44
  • 2
    ... This answers part 2, and has nothing to do with part 1 (OS-level load average). See [Why does this delay-loop start to run faster after several iterations with no sleep?](//stackoverflow.com/a/38300014) for links. You were asking about how *the CPU* can examine *it's own* usage, rather than the OS tracking CPU usage. Maybe that's not what you meant, but that's the answer to that interpretation of the question :P – Peter Cordes Apr 15 '18 at 01:45
  • `delta mperf / delta tsc` is how power gadget and linux do it I believe. See https://community.intel.com/t5/Software-Archive/Intel-Xeon-Phi-Per-Core-Data/m-p/1162417/highlight/true – 1110101001 Jun 12 '23 at 07:35
  • To clarify above, reason why it works is that I believe mperf does not increment when idle, whereas tsc always increments (technically not at nominal frequency, but for intuition you can assume so). See https://patchwork.kernel.org/project/linux-pm/patch/52f711be59539723358bea1aa3c368910a68b46d.1459485198.git.len.brown@intel.com/ `APERF and MPERF will not overflow in the uptime of the machine. They are both 64-bit registers, and they tick at TSC rate or slower. (Indeed, they tick at 0 when idle)` – 1110101001 Jun 12 '23 at 08:41

8 Answers8

90

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
  • 12,634
  • 3
  • 20
  • 12
  • 17
    ++ 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 Sep 20 '10 at 20:40
  • 1
    +1 This explains everything concisely, thank you! It's good to know that it is actually possible for the CPU to "do nothing" by using the HLT instruction and interrupts. – tjklemz Apr 24 '13 at 02:45
  • @tjklemz: Fun fact: modern x86 enters sleep states using special args for `monitor` / `mwait` instructions. [`hlt` saves some power, but only the shallowest sleep state (C1)](//stackoverflow.com/questions/13465422/mwait-vs-halt-in-terms-of-efficiency). Related: https://software.intel.com/en-us/blogs/2008/03/27/update-c-states-c-states-and-even-more-c-states / https://software.intel.com/en-us/blogs/2008/04/29/theres-got-to-be-a-catch/. Note that Skylake finally lets the OS hand full control to the HW, at least for frequency scaling while active, but maybe OS still decides on sleep states. – Peter Cordes Apr 15 '18 at 01:57
36

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

djvg
  • 11,722
  • 5
  • 72
  • 103
In silico
  • 51,091
  • 10
  • 150
  • 143
  • 3
    "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 Sep 20 '10 at 01:28
  • 2
    @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 Sep 20 '10 at 01:31
  • @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 Sep 20 '10 at 01:36
  • 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. – Anycorn Sep 20 '10 at 01:48
  • 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 Sep 20 '10 at 06:32
  • 2
    @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 Sep 20 '10 at 06:53
21

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
  • 11,339
  • 6
  • 52
  • 58
9

One way to do it is as follows:

Pick a sampling interval, say every 5 min (300 seconds) of real elapsed time. You can get this from gettimeofday.

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

Your cpu percentage is then (process_time/elapsed_time)*100.0 You can set an alarm to signal you every 300 seconds to make these calculations.

I have a process that I do not want to use more than a certain target cpu percentage. This method works pretty good, and agrees well with my system monitor. If we're using too much cpu, we usleep for a little.

jeremy
  • 9,965
  • 4
  • 39
  • 59
user1725779
  • 148
  • 1
  • 7
2

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

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Anycorn
  • 50,217
  • 42
  • 167
  • 261
2

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.

AndyG
  • 39,700
  • 8
  • 109
  • 143
1

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
  • 16,621
  • 12
  • 64
  • 85
0
  1. The CPU does not get 'hung' up, it is simply operating at peak capacity, meaning it is processing as many instructions as it is physically capable every second. The process that is calculating CPU usage is some of those instructions. If applications try to do operations faster than the CPU is capable, then simply they will be delayed, hence the 'hung up'.

  2. The calculation of CPU utilization is based on the total available utilization. So if a CPU has two cores, and one core has 30% usage, and the other is 60%, the overall utilization is 45%. You can also see the usage of each individual core.

Aashishkebab
  • 295
  • 3
  • 10