获取共享缓存的逻辑CPU核心数(L1,L2,L3)

时间:2017-08-01 17:16:21

标签: c++ windows visual-c++

下面是一些使用GetLogicalProcessorInformation在Windows上检测L1,L2和L3 CPU缓存大小的C ++代码:

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;
        }
    }
}

作为下一步,我希望获得共享缓存的逻辑CPU核心数量。在具有超线程的x64 CPU上,两个逻辑CPU内核通常共享一个L2缓存,所有逻辑CPU内核共享L3缓存。

通过MSDN阅读后,我认为GetLogicalProcessorInformationExCACHE_RELATIONSHIP以及GROUP_AFFINITY我正在寻找的数据结构,但在尝试了这些数据结构后,对我来说似乎毫无用处。 / p>

问题:

有没有办法让Windows上使用C / C ++共享缓存的逻辑CPU核心数量? (理想情况下不直接使用cpuid

解决方案:

可以使用GetLogicalProcessorInformationEx以及CACHE_RELATIONSHIPGROUP_AFFINITY数据结构获取共享缓存的逻辑CPU核心数。 GROUP_AFFINITY.Mask值包含为每个共享当前缓存(RelationCache)的CPU核心设置的一个位。作为大多数具有超线程{1}的英特尔CPU的示例,将包含为L2高速缓存设置的2位和为具有4个物理CPU核心和8个逻辑CPU核心的CPU设置的用于L3高速缓存的8位。

这是C ++代码:

GROUP_AFFINITY.Mask

注意事项:

我发现在虚拟机内部运行Windows时,上面的代码无法正确检测共享缓存的CPU核心数量,例如:在具有2个虚拟CPU核心的VM上,上面的代码报告每个逻辑CPU核心都有一个专用的L1,L2和L3缓存。

3 个答案:

答案 0 :(得分:1)

  

@RbMm:但CACHE_RELATIONSHIP包含所需的所有信息。逻辑CPU核心数= Cache-&gt; GroupMask.Mask

中设置的位数

我在AppVeyor CI上测试了这个(甚至在发布到stackoverflow之前)。这是x64 CPU的输出:

info->Cache.Level: 1
info->Cache.CacheSize: 32768
info->Cache.GroupMask.Group: 0
info->Cache.GroupMask.Mask: 1

info->Cache.Level: 1
info->Cache.CacheSize: 32768
info->Cache.GroupMask.Group: 0
info->Cache.GroupMask.Mask: 1

info->Cache.Level: 2
info->Cache.CacheSize: 262144
info->Cache.GroupMask.Group: 0
info->Cache.GroupMask.Mask: 1

info->Cache.Level: 3
info->Cache.CacheSize: 31457280
info->Cache.GroupMask.Group: 0
info->Cache.GroupMask.Mask: 1

info->Cache.Level: 1
info->Cache.CacheSize: 32768
info->Cache.GroupMask.Group: 0
info->Cache.GroupMask.Mask: 2

info->Cache.Level: 1
info->Cache.CacheSize: 32768
info->Cache.GroupMask.Group: 0
info->Cache.GroupMask.Mask: 2

info->Cache.Level: 2
info->Cache.CacheSize: 262144
info->Cache.GroupMask.Group: 0
info->Cache.GroupMask.Mask: 2

info->Cache.Level: 3
info->Cache.CacheSize: 31457280
info->Cache.GroupMask.Group: 0
info->Cache.GroupMask.Mask: 2

根据MSDN文档:&#34; GroupMask.Mask - 指定指定组内零个或多个处理器的亲和性的位图。&#34;。基于此文档,我预计L3缓存会有不同的GroupMask.Mask,但上面的输出并未显示此信息。对我来说,GroupMask.Mask中的数据毫无意义!

这是link to the code,它产生上面的数据

答案 1 :(得分:0)

可能是特定处理器和Windows版本的结果,但我在2个处理器上测试(win10)并得到了正确的结果:

i5(2核,4个线程):

ProcessorPackage
    [G0 000000000000000F { #3, #2, #1, #0}]
ProcessorCore HP=1 0
    [G0 0000000000000003 { #1, #0}]
Cache L1     8000 40 [G0 0000000000000003 { #1, #0}] A=8 Data
Cache L1     8000 40 [G0 0000000000000003 { #1, #0}] A=8 Instruction
Cache L2    40000 40 [G0 0000000000000003 { #1, #0}] A=8 Unified
Cache L3   300000 40 [G0 000000000000000F { #3, #2, #1, #0}] A=c Unified
ProcessorCore HP=1 0
    [G0 000000000000000C { #3, #2}]
Cache L1     8000 40 [G0 000000000000000C { #3, #2}] A=8 Data
Cache L1     8000 40 [G0 000000000000000C { #3, #2}] A=8 Instruction
Cache L2    40000 40 [G0 000000000000000C { #3, #2}] A=8 Unified
NumaNode #0 [G0 000000000000000F { #3, #2, #1, #0}]
Group:1/1
    4/4 [000000000000000F { #3, #2, #1, #0}]

i7(4核,8个线程):

ProcessorPackage
    [G0 00000000000000FF { #7, #6, #5, #4, #3, #2, #1, #0}]
ProcessorCore HP=1 0
    [G0 0000000000000003 { #1, #0}]
Cache L1     8000 40 [G0 0000000000000003 { #1, #0}] A=8 Data
Cache L1     8000 40 [G0 0000000000000003 { #1, #0}] A=8 Instruction
Cache L2    40000 40 [G0 0000000000000003 { #1, #0}] A=4 Unified
Cache L3   800000 40 [G0 00000000000000FF { #7, #6, #5, #4, #3, #2, #1, #0}] A=10 Unified
ProcessorCore HP=1 0
    [G0 000000000000000C { #3, #2}]
Cache L1     8000 40 [G0 000000000000000C { #3, #2}] A=8 Data
Cache L1     8000 40 [G0 000000000000000C { #3, #2}] A=8 Instruction
Cache L2    40000 40 [G0 000000000000000C { #3, #2}] A=4 Unified
ProcessorCore HP=1 0
    [G0 0000000000000030 { #5, #4}]
Cache L1     8000 40 [G0 0000000000000030 { #5, #4}] A=8 Data
Cache L1     8000 40 [G0 0000000000000030 { #5, #4}] A=8 Instruction
Cache L2    40000 40 [G0 0000000000000030 { #5, #4}] A=4 Unified
ProcessorCore HP=1 0
    [G0 00000000000000C0 { #7, #6}]
Cache L1     8000 40 [G0 00000000000000C0 { #7, #6}] A=8 Data
Cache L1     8000 40 [G0 00000000000000C0 { #7, #6}] A=8 Instruction
Cache L2    40000 40 [G0 00000000000000C0 { #7, #6}] A=4 Unified
NumaNode #0 [G0 00000000000000FF { #7, #6, #5, #4, #3, #2, #1, #0}]
Group:1/1
    8/8 [00000000000000FF { #7, #6, #5, #4, #3, #2, #1, #0}]

代码:

void FormatMask(KAFFINITY Mask, PSTR sz)
{
    sz += sprintf(sz, "%p {", (PVOID)Mask);

    ULONG i = sizeof(KAFFINITY) * 8;
    do
    {
        if (_bittest((PLONG)&Mask, --i))
        {
            sz += sprintf(sz, " #%u,", i);
        }
    } while (i);

    *--sz = '}';
}

void DumpCpuInfo()
{
    static PCSTR szCacheType[] = {
        "Unified",
        "Instruction",
        "Data",
        "Trace"
    };

    char szMask[64 * 5 + 19];
    ULONG cb = 0, rcb = 0x400;
    static volatile UCHAR guz;
    PVOID stack = alloca(guz);
    union {
        PVOID Buffer;
        PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX plpi;
    };

    do
    {
        if (cb < rcb) rcb = cb = RtlPointerToOffset(Buffer = alloca(rcb - cb), stack);

        if (GetLogicalProcessorInformationEx(::RelationAll, plpi, &rcb))
        {
            DWORD Size;
            do
            {
                Size = plpi->Size;

                union {
                    PPROCESSOR_RELATIONSHIP Processor;
                    PNUMA_NODE_RELATIONSHIP NumaNode;
                    PCACHE_RELATIONSHIP     Cache;
                    PGROUP_RELATIONSHIP     Group;
                    PVOID pv;
                };

                pv = &plpi->Processor;

                switch (plpi->Relationship)
                {
                case RelationProcessorPackage:
                    DbgPrint("ProcessorPackage\n");
                    goto __0;
                case RelationProcessorCore:
                    DbgPrint("ProcessorCore HP=%x %x\n",
                        Processor->Flags & LTP_PC_SMT ? 1 : 0, Processor->EfficiencyClass);
__0:
                    if (WORD GroupCount = Processor->GroupCount)
                    {
                        PGROUP_AFFINITY GroupMask = Processor->GroupMask;
                        do
                        {
                            FormatMask(GroupMask->Mask, szMask);
                            DbgPrint("\t[G%u %s]\n", GroupMask->Group, szMask);
                        } while (GroupMask++, --GroupCount);
                    }
                    break;

                case RelationNumaNode:
                    FormatMask(NumaNode->GroupMask.Mask, szMask);
                    DbgPrint("NumaNode #%u [G%u %s]\n",
                        NumaNode->NodeNumber, NumaNode->GroupMask.Group, szMask);
                    break;

                case RelationGroup:
                    DbgPrint("Group:%u/%u\n", Group->ActiveGroupCount, Group->MaximumGroupCount);
                    if (WORD ActiveGroupCount = Group->ActiveGroupCount)
                    {
                        PPROCESSOR_GROUP_INFO GroupInfo = Group->GroupInfo;
                        do
                        {
                            FormatMask(GroupInfo->ActiveProcessorMask, szMask);
                            DbgPrint("\t%u/%u [%s]\n",
                                GroupInfo->ActiveProcessorCount,
                                GroupInfo->MaximumProcessorCount, szMask);
                        } while (GroupInfo, --ActiveGroupCount);
                    }
                    break;

                case RelationCache:
                    FormatMask(Cache->GroupMask.Mask, szMask);
                    DbgPrint("Cache L%u %8x %2x [G%u %s] A=%x %s\n",
                        Cache->Level,
                        Cache->CacheSize, Cache->LineSize,
                        Cache->GroupMask.Group, szMask,
                        Cache->Associativity,
                        szCacheType[Cache->Type % RTL_NUMBER_OF(szCacheType)]
                    );
                    break;
                }
                Buffer = RtlOffsetToPointer(plpi, Size);
            } while (rcb -= Size);
            break;
        }
    } while (GetLastError() == ERROR_INSUFFICIENT_BUFFER);
}

答案 2 :(得分:-1)

还有一个来自boost库的替代解决方案。

// number of logical cores
auto logical = boost::thread::hardware_concurrency();

// number of physical cores
auto physical = boost::thread::physical_concurrency();

但不考虑缓存。