How is CPU usage calculated?

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)