Part 4 of 6 · 2 min read
System files
Think of a school's records office, with one register of attendance for the whole school, and a ledger of how the days were spent. A report is made by adding up columns. The analogy stops in one place: a school adds each day by hand, and the kernel only counts forward, so a report needs two readings and the difference.
In plain words: The main system files are meminfo (memory, in kilobytes, with dozens of lines), loadavg (the three load averages, the number of running tasks and the newest process ID), stat (counters of CPU time, interrupts and context switches), interrupts (counts for each device, Level 8), mounts (the mounted file systems, Level 9) and cpuinfo. The first line of /proc/stat counts, for all processors together, the time since boot in ticks of 1/100 s (getconf CLK_TCK gives 100): user, nice, system, idle, iowait, irq, softirq and steal.
A worked example. One reading on the test machine:
cpu 1255 0 189 105827 70 0 30 32 0 0
The total of the ten fields is 1,255 + 0 + 189 + 105,827 + 70 + 0 + 30 + 32 = 107,403 ticks, which is 1,074 s of processor time over 2 processors. The idle share is 105,827 / 107,403 = 98.5 per cent, the system share is 189 / 107,403 = 0.18 per cent, and the user share 1,255 / 107,403 = 1.17 per cent. This matches the idle figure of about 98 to 100 that vmstat printed. Over a short interval, one reading is not enough: read twice, subtract, and divide, which is what top and vmstat do. In /proc/meminfo the same machine shows, in kB: MemTotal 8,216,648, MemFree 7,850,432, MemAvailable 7,829,376, Cached 190,552 and SwapTotal 0.
The key lines. Counters in /proc only grow. A rate is the difference between two readings divided by the time between them.
Tick (USER_HZ)
A tick in /proc/stat is a unit of CPU time of 1/USER_HZ second, which is 1/100 s on Linux as reported by getconf CLK_TCK. The counters give the ticks that all processors spent in each mode since boot.
Example: 105,827 idle ticks equal 1,058.27 s of idle time summed over the processors.
Check yourself:
1. What unit is the CPU time in /proc/stat?
2. Why do you need two readings for a rate?
3. Which field of meminfo tells how much memory new programs can use?