How to programmatically get the CPU cache page size in C++?

Mathieu Pagé picture Mathieu Pagé · Sep 29, 2008 · Viewed 13.5k times · Source

I'd like my program to read the cache line size of the CPU it's running on in C++.

I know that this can't be done portably, so I will need a solution for Linux and another for Windows (Solutions for other systems could be usefull to others, so post them if you know them).

For Linux I could read the content of /proc/cpuinfo and parse the line begining with cache_alignment. Maybe there is a better way involving a call to an API.

For Windows I simply have no idea.

Answer

Nick picture Nick · Sep 29, 2008

On Win32, GetLogicalProcessorInformation will give you back a SYSTEM_LOGICAL_PROCESSOR_INFORMATION which contains a CACHE_DESCRIPTOR, which has the information you need.