title: Accurate calculation of CPU usage given in percentage in Linux?
body: <p>It's a question which has been asked many times, however there is no well supported answer I could find.</p>
  
  <p>Many people suggest the use of top command, but if you run top once (because you have a script for example collecting Cpu usage every 1 second) it will always give the same Cpu usage result (<a href="https://unix.stackexchange.com/questions/69185/getting-cpu-usage-same-every-time">example 1</a>, <a href="https://unix.stackexchange.com/questions/27076/how-can-i-receive-top-like-cpu-statistics-from-the-shell">example 2</a>).</p>
  
  <p>A more accurate way to calculate CPU usage, is by reading the values from <code>/proc/stat</code>, but most of the answers use only the first 4 fields from <code>/proc/stat</code> to calculate it (one example <a href="https://unix.stackexchange.com/questions/27076/how-can-i-receive-top-like-cpu-statistics-from-the-shell">here</a>).</p>
  
  <p><code>/proc/stat/</code> has 10 fields per CPU core as of Linux kernel 2.6.33!</p>
  
  <p>I also found this <a href="https://stackoverflow.com/questions/5514119/accurately-calculating-cpu-utilization-in-linux-using-proc-stat">Accurately Calculating CPU Utilization in Linux using /proc/stat</a> question which is pointing out the same issue, -that most other questions only take into consideration 4 out of the many fields- but still the answer given here starts with "I think" (not certain), and except that, it is only concerned about the first 7 fields (out of 10 in <code>/proc/stat/</code>)</p>
  
  <p><a href="https://gist.github.com/creaktive/781248" rel="noreferrer">This</a> perl script uses all of the fields to calculate the CPU usage, which again I do not think is correct after some further investigation.</p>
  
  <p>After taking a quick look into the kernel code <a href="http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/kernel/sched/cputime.c?h=v4.8#n169" rel="noreferrer">here</a>, it looks like, for example, <code>guest_nice</code> and <code>guest fields</code> are always increasing together with <code>nice</code> and <code>user</code> (so they should not be included in the cpu usage calculation, since they are included in <code>nice</code> and <code>user</code> fields already)</p>
  
  <pre><code>/*
   * Account guest cpu time to a process.
   * @p: the process that the cpu time gets accounted to
   * @cputime: the cpu time spent in virtual machine since the last update
   * @cputime_scaled: cputime scaled by cpu frequency
   */
  static void account_guest_time(struct task_struct *p, cputime_t cputime,
                     cputime_t cputime_scaled)
  {
      u64 *cpustat = kcpustat_this_cpu-&gt;cpustat;
  
      /* Add guest time to process. */
      p-&gt;utime += cputime;
      p-&gt;utimescaled += cputime_scaled;
      account_group_user_time(p, cputime);
      p-&gt;gtime += cputime;
  
      /* Add guest time to cpustat. */
      if (task_nice(p) &gt; 0) {
          cpustat[CPUTIME_NICE] += (__force u64) cputime;
          cpustat[CPUTIME_GUEST_NICE] += (__force u64) cputime;
      } else {
          cpustat[CPUTIME_USER] += (__force u64) cputime;
          cpustat[CPUTIME_GUEST] += (__force u64) cputime;
      }
  }
  </code></pre>
  
  <p>So to sum up, what is an accurate way to calculate the CPU usage in Linux and which fields should be considered in the calculations and how (which fields are attributed to the idle time and which fields to non-idle time)?</p>