如何以编程方式获得CPU缓存页大小在C + +?

问题描述:

我希望我的程序读取它在C ++中运行的CPU的缓存行大小。

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

我知道这不能移植,所以我需要一个解决方案为Linux和另一个为Windows(其他系统的解决方案可能对其他人有用,所以如果你知道他们发布他们)。

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).

对于Linux我可以读取/ proc / cpuinfo的内容,并解析用cache_alignment开头的行。也许有一个更好的方法涉及调用API。

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.

对于Windows我根本不知道。

For Windows I simply have no idea.

在Windows上

#include <Windows.h>
#include <iostream>

using std::cout; using std::endl;

int main()
{
    SYSTEM_INFO systemInfo;
    GetSystemInfo(&systemInfo);
    cout << "Page Size Is: " << systemInfo.dwPageSize;
    getchar();
}

在Linux上

http://linux.die.net/man/2/getpagesize