[LC++] Welcome to the "tuxCPProgramming" mailing list

Paul Gearon gearon at ieee.org
Mon Dec 3 19:24:35 UTC 2007


On Dec 3, 2007 11:21 AM, Saurabh Sehgal <saurabh.r.s at gmail.com> wrote:
> Hi,
>
> I had a question regarding C/C++ programming. What would be the best way to
> write platform independant code to determine the L2 cache size of a CPU ? Is
> there anyway that I can do this programatically , by reading the BIOS ? or
> timing memory accesses ?
>
> Any help would be greatly appreciated.

Well, this list is about Linux programming, so the quick answer is to
look at /proc/cpuinfo and grep for "cache size" (remembering that you
will probably see one of these lines per CPU core).

OTOH, I acknowledge that when you're writing code on Linux it is a
desirable thing to stay platform independent.

Unfortunately, I'm not sure you're going to have much luck with a
portable method for this.  I believe that the info you want comes from
a port IO on x86, and probably from memory access on non-x86 chips.
You don't usually have permission to do port accesses, or to read
specified memory ranges, unless you are in the kernel.  That means a
kernel module, which means non-portable code.

To my knowledge, most operating systems create *some* user-level
access to this info, in the same way that Linux does with
/proc/cpuinfo.  However, you'll have to look this up, depending on the
operating systems you are trying to support.  The advantage of using
an OS supplied mechanism is that you don't have to worry about
supporting different CPU types for that OS.  The disadvantage is that
it differs for every OS.

The only ones I have access to are:
Linux:  grep 'cache size' /proc/cpuinfo
OS X:  sysctl hw.l2cachesize

You can probably find the others on Google.

As for programming, Linux does allow a root process to get access to
ports, but this is specific to Linux, and won't help you in the
general case:
http://www.faqs.org/docs/Linux-mini/IO-Port-Programming.html#s2

Hope this was useful.

Regards,
Paul Gearon



More information about the tuxCPProgramming mailing list