How is CPU usage calculated?

后端 未结 8 1606
故里飘歌
故里飘歌 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:05

    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.

    0 讨论(0)
  • 2020-12-12 14:06

    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)

    0 讨论(0)
  • 2020-12-12 14:07

    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.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-12 14:21
    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.

    0 讨论(0)
  • 2020-12-12 14:23

    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.

    0 讨论(0)
提交回复
热议问题