Below is the C ++ code that determines the size of CPU caches L1, L2 and L3 on Windows using GetLogicalProcessorInformation :
typedef BOOL (WINAPI *LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
LPFN_GLPI glpi = (LPFN_GLPI) GetProcAddress(
GetModuleHandle(TEXT("kernel32")), "GetLogicalProcessorInformation");
if (glpi)
{
DWORD bytes = 0;
glpi(0, &bytes);
size_t size = bytes / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
vector<SYSTEM_LOGICAL_PROCESSOR_INFORMATION> info(size);
glpi(info.data(), &bytes);
for (size_t i = 0; i < size; i++)
{
if (info[i].Relationship == RelationCache)
{
if (info[i].Cache.Level == 1)
l1_cache_Size = info[i].Cache.Size;
if (info[i].Cache.Level == 2)
l2_cache_Size = info[i].Cache.Size;
if (info[i].Cache.Level == 3)
l3_cache_Size = info[i].Cache.Size;
}
}
}
As a next step, I would like to get the number of processor cores that share the cache. On an x64 processor with a hyperthread, two logical cores of the processor usually use the L2 cache, and all logical cores of the processor share the L3 cache.
After reading the MSDN, I thought that GetLogicalProcessorInformationExboth CACHE_RELATIONSHIP and GROUP_AFFINITY , where the data structures that I was looking for, but after checking these data structures seem useless for my purpose.
Question:
, Windows, C/++? ( cpuid)
:
, , GetLogicalProcessorInformationEx CACHE_RELATIONSHIP GROUP_AFFINITY. GROUP_AFFINITY.Mask , (RelationCache). Intel GROUP_AFFINITY.Mask 2 , L2, 8 L3 4 8 .
++:
#include <windows.h>
#include <vector>
#include <iostream>
using namespace std;
typedef BOOL (WINAPI *LPFN_GLPI)(LOGICAL_PROCESSOR_RELATIONSHIP,
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, PDWORD);
int main()
{
LPFN_GLPI glpi = (LPFN_GLPI) GetProcAddress(
GetModuleHandle(TEXT("kernel32")), "GetLogicalProcessorInformationEx");
if (!glpi)
return 1;
DWORD bytes = 0;
glpi(RelationAll, 0, &bytes);
vector<char> buffer(bytes);
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* info;
if (!glpi(RelationAll, (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*) &buffer[0], &bytes))
return 1;
for (size_t i = 0; i < bytes; i += info->Size)
{
info = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*) &buffer[i];
if (info->Relationship == RelationCache &&
(info->Cache.Type == CacheData ||
info->Cache.Type == CacheUnified))
{
cout << "info->Cache.Level: " << (int) info->Cache.Level << endl;
cout << "info->Cache.CacheSize: " << (int) info->Cache.CacheSize << endl;
cout << "info->Cache.GroupMask.Group: " << info->Cache.GroupMask.Group << endl;
cout << "info->Cache.GroupMask.Mask: " << info->Cache.GroupMask.Mask << endl << endl;
}
}
return 0;
}
:
, Windows , , . , , , L1, L2 L3.