sys-cpu - Linux issues

djberg96 on 2003-02-03T05:05:32

One of my co-workers wanted a Ruby module that provided cpu information. So, I took a look at Sys::CPU, Unix::Processors and Linux::Cpuinfo (since I'll want to support Linux as well) for guidance. I start with Linux...

Seems easy enough - just read out of /proc/cpuinfo and assign values to the appropriate attributes, right?. The problem, as Jonathan Stowe apparently learned the hard way, is that the information stored in this file varies slightly from one chip architecture to the next. You can't hard code the attributes.

My next idea was to dynamically generate the class during the configuration process. Seems logical and it's not that difficult to do. The problem with *that* approach is that the hardware could be upgraded at some point, and if the chip is changed (from, say, Intel to Athlon), we suddenly have a module returning bogus information.

What about dynamically generating the information each time a method is called? Seems horribly inefficient, since the probability of a cpu upgrade is rather low (relatively speaking).

The best compromise solution I can think of is to dynamically generate the class *once*, when the module is require'd. The file is small enough that the one time read should be negligable from a performance standpoint.

PS - I wouldn't have known about the differences in /proc/cpuinfo if I hadn't read the README file for Linux::Cpuinfo. :)