Josh Stone, Blog

Josh’s projects and security nerdery

Measuring Processor Utilization

I just built a new computer, and I’m having fun with it. I’ve gone from a Core 2 Duo to an AMD FX 8350, so from two cores to eight. I’m playing around with lots of things trying to figure out how hard I can push this thing. I’ve been unable, as yet, to make it seem slow.

I decided I wanted to get a different visualization of the processor utilization. The classic top and htop give me numbers or a little ASCII bar graph. Interesting, but not what I was looking for. The Gnome system-monitor graph looks nice – but it’s cluttered up with other things I don’t care about, and it doesn’t let me configure it.

I decided to make my own. The first step was to figure out how to track the CPU usage. It turns out that there’s a really handy pseudo-file in /proc, called /proc/stat. Here’s a sample from my machine:

cpu  324629 808258 103163 76914167 28804 0 1830 0 0 0
cpu0 56785 98585 18448 9593582 5237 0 873 0 0 0
cpu1 20507 105063 9047 9633487 3084 0 156 0 0 0
cpu2 62386 91911 15888 9598734 4454 0 191 0 0 0
cpu3 19742 106883 9919 9631595 3438 0 102 0 0 0
cpu4 59074 94476 15884 9601288 3803 0 153 0 0 0
cpu5 30218 112291 9456 9618554 1121 0 85 0 0 0
cpu6 55968 96145 15387 9601175 4143 0 164 0 0 0
cpu7 19944 102901 9131 9635749 3521 0 103 0 0 0
intr 36545629 131 2 0 0 0 0 0 0 39 0 0 0 4 0 0 0 2429 0 2 204576 217223 ... LOTS HERE ...
ctxt 39652434
btime 1367432955
processes 25119
procs_running 1
procs_blocked 0
softirq 22450716 0 13874463 16300 157581 285408 0 813471 4918540 54174 2330779

Of note are the rows that start with cpuN. There is one line for each CPU, and the following list of numbers are “jiffy” counts. What’s a “jiffy”? It’s a fake unit used by the kernel to measure timings of various things. I’ve read in some places that it’s about a hundredth of a second, sometimes, but could vary. Regardless, what we’re interested in are ratios, so units don’t matter.

The numbers always go up. So how do we calculate the utilization of the processor? Simple – take two samples of the file’s output and calculate how much the numbers changed. By doing the right math, we can calculate how the processor’s time was spent during the period between our samples. To do this, we need to know the fields in these lines:

cpu3 19742 106883 9919 9631595 3438 0 102 0 0 0

  19742   --  jiffies spent in user time
  106883  --  jiffies spent in nice time
  9919    --  jiffies spent in system time
  9631595 --  jiffies spent in idle time

The rest of the fields don’t really matter for now (IRQs and IO wait stats – but not relevant to CPU utilization). We can calculate the utilization by doing a little addition and division:

utilization = (user + nice + system) / (user + nice + system + idle)

Do this a lot of times and update your visual presentation, and you have a nice tool for watching your CPUs. I decided to write my CPU usage monitor (source here) in Haskell, using OpenGL for the visualization. It takes less than 2% of a core, and flutters nicely off to the side so I can see what I’m looking for. Here’s a sample image:

CPU Utilization