[ACCEPTED]-How to programmatically get the CPU cache page size in C++?-cpu
On Win32, GetLogicalProcessorInformation
will give you back a SYSTEM_LOGICAL_PROCESSOR_INFORMATION
which contains 1 a CACHE_DESCRIPTOR
, which has the information you need.
On Linux try the proccpuinfo library, an architecture independent 1 C API for reading /proc/cpuinfo
For x86, the CPUID instruction. A quick google 3 search reveals some libraries for win32 and c++. I 2 have used CPUID via inline assembler as 1 well.
Some more info:
Looks like at least SCO unix (http://uw714doc.sco.com/en/man/html.3C/sysconf.3C.html) has _SC_CACHE_LINE 2 for sysconf. Perhaps other platforms have 1 something similar?
On 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();
}
On Linux
http://linux.die.net/man/2/getpagesize
0
Here is sample code for those who wonder 2 how to to utilize the function in accepted 1 answer:
#include <new>
#include <iostream>
#include <Windows.h>
void ShowCacheSize()
{
using CPUInfo = SYSTEM_LOGICAL_PROCESSOR_INFORMATION;
DWORD len = 0;
CPUInfo* buffer = nullptr;
// Determine required length of a buffer
if ((GetLogicalProcessorInformation(buffer, &len) == FALSE) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
{
// Allocate buffer of required size
buffer = new (std::nothrow) CPUInfo[len]{ };
if (buffer == nullptr)
{
std::cout << "Buffer allocation of " << len << " bytes failed" << std::endl;
}
else if (GetLogicalProcessorInformation(buffer, &len) != FALSE)
{
for (DWORD i = 0; i < len; ++i)
{
// This will be true for multiple returned caches, we need just one
if (buffer[i].Relationship == RelationCache)
{
std::cout << "Cache line size is: " << buffer[i].Cache.LineSize << " bytes" << std::endl;
break;
}
}
}
else
{
std::cout << "ERROR: " << GetLastError() << std::endl;
}
delete[] buffer;
}
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.