How is CPU usage calculated?

后端 未结 8 1654
故里飘歌
故里飘歌 2020-12-12 13:43

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 calc

8条回答
  •  粉色の甜心
    2020-12-12 14:08

    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

提交回复
热议问题