Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to parse out the data from <code>/proc/&lt;PID&gt;/stat</code>. These are the first few fields (from <code>Documentation/filesystems/proc.txt</code> in your kernel source):</p> <pre><code>Table 1-3: Contents of the stat files (as of 2.6.22-rc3) .............................................................................. Field Content pid process id tcomm filename of the executable state state (R is running, S is sleeping, D is sleeping in an uninterruptible wait, Z is zombie, T is traced or stopped) ppid process id of the parent process pgrp pgrp of the process sid session id tty_nr tty the process uses tty_pgrp pgrp of the tty flags task flags min_flt number of minor faults cmin_flt number of minor faults with child's maj_flt number of major faults cmaj_flt number of major faults with child's utime user mode jiffies stime kernel mode jiffies cutime user mode jiffies with child's cstime kernel mode jiffies with child's </code></pre> <p>You're probably after <code>utime</code> and/or <code>stime</code>. You'll also need to read the <code>cpu</code> line from <code>/proc/stat</code>, which looks like:</p> <pre><code>cpu 192369 7119 480152 122044337 14142 9937 26747 0 0 </code></pre> <p>This tells you the cumulative CPU time that's been used in various categories, in units of jiffies. You need to take the sum of the values on this line to get a <code>time_total</code> measure.</p> <p>Read both <code>utime</code> and <code>stime</code> for the process you're interested in, and read <code>time_total</code> from <code>/proc/stat</code>. Then sleep for a second or so, and read them all again. You can now calculate the CPU usage of the process over the sampling time, with:</p> <pre><code>user_util = 100 * (utime_after - utime_before) / (time_total_after - time_total_before); sys_util = 100 * (stime_after - stime_before) / (time_total_after - time_total_before); </code></pre> <p>Make sense?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload