Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 486
Mathieu Pagé Avatar asked Sep 29 '08 19:09

Mathieu Pagé


People also ask

How do I check my cache size?

Right-click on the Start button and click on Task Manager. 2. On the Task Manager screen, click on the Performance tab > click on CPU in the left pane. In the right-pane, you will see L1, L2 and L3 Cache sizes listed under “Virtualization” section.

What is the size of L1 cache?

There are 4 cores reported, then the total size of L1 cache = 4 X 64 KB = 256 KB.

How large is a cache line?

The cache line is generally fixed in size, typically ranging from 16 to 256 bytes. The effectiveness of the line size depends on the application, and cache circuits may be configurable to a different line size by the system designer.


1 Answers

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

like image 153
Nick Avatar answered Sep 21 '22 07:09

Nick