9

/proc/stat shows ticks for user, nice, sys, idle, iowait, irq and sirq like this:

cpu 6214713 286 1216407 121074379 260283 253506 197368 0 0 0

How can I calculate the individual utilizations (in %) for user, nice etc with these values? Like the values that shows in 'top' or 'vmstat'.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
jgr
  • 3,914
  • 2
  • 24
  • 28

2 Answers2

11

This code calculates user utilization spread over all cores.

import os
import time
import multiprocessing

def main():
    jiffy = os.sysconf(os.sysconf_names['SC_CLK_TCK'])
    num_cpu = multiprocessing.cpu_count()

    stat_fd = open('/proc/stat')
    stat_buf = stat_fd.readlines()[0].split()
    user, nice, sys, idle, iowait, irq, sirq = ( float(stat_buf[1]), float(stat_buf[2]),
                                            float(stat_buf[3]), float(stat_buf[4]),
                                            float(stat_buf[5]), float(stat_buf[6]),
                                            float(stat_buf[7]) )

    stat_fd.close()

    time.sleep(1)

    stat_fd = open('/proc/stat')
    stat_buf = stat_fd.readlines()[0].split()
    user_n, nice_n, sys_n, idle_n, iowait_n, irq_n, sirq_n = ( float(stat_buf[1]), float(stat_buf[2]),.
                                                            float(stat_buf[3]), float(stat_buf[4]),
                                                            float(stat_buf[5]), float(stat_buf[6]),
                                                            float(stat_buf[7]) )

    stat_fd.close()

    print ((user_n - user) * 100 / jiffy) / num_cpu

if __name__ == '__main__':
    main()
vdmit
  • 45
  • 5
jgr
  • 3,914
  • 2
  • 24
  • 28
4

From Documentation/filesystems/proc.txt:

(...) These numbers identify the amount of time the CPU has spent performing different kinds of work. Time units are in USER_HZ (typically hundredths of a second).

So to figure out utilization in terms of percentages you need to:

  • Find out what USER_HZ is on the machine
  • Find out how long it's been since the system booted.

The second one is easy: there is a btime line in that same file which you can use for that. For USER_HZ, check out How to get number of mili seconds per jiffy.

Community
  • 1
  • 1
Mat
  • 202,337
  • 40
  • 393
  • 406
  • This will get me the total amount of % spent in user, nice etc, right? I'm interrested in the current amount, i.e at the present moment. – jgr Sep 04 '11 at 11:15
  • You need to track that yourself by reading that file for example every second and "diff"ing the values. – Mat Sep 04 '11 at 11:21
  • 2
    Each column measures the time spent in a given mode since boot. If you take two readings at 1s interval, and subtract the values in, say, the USER column from each other, you'll know how many ticks were spent in user mode during one second. Once you know that, and how many ticks there are in a second, you can calculate your percentage. – Mat Sep 04 '11 at 11:42
  • "Time units are in USER_HZ" which is a frequency (ticks/second) – Mat Sep 04 '11 at 11:56