Logical CPU count return 16 instead of 4

若如初见. 提交于 2020-01-11 06:51:11

问题


I have a Intel Core i5-2450m (2 physical processors and 4 logical processors) and I want to find a way to count logical and physical cores on AMD and Intel CPUs. But, after some searches I noticed something strange. Instead of returning 4 logical units, my code give me 16.

static int8_t LogicalProcCount(void)
{
    if ( !Hyperthreading )
        return 1;

    uint32_t unused, ebx;
    CPUID(1, unused, ebx, unused, unused);

    return (int8_t) ( (ebx >> 16 ) & 0xFF );
}

回答1:


CPUID.1:EBX[23:16] represents the maximum number of addressable IDs (initial APIC ID) that can be assigned to logical processors in a physical package.

Source.

So 16 has nothing to do with the actual number of your logical CPUs. On my machine CPUID.1:EBX[23:16] also returns 16, though it has 8 logical CPUs.

Sample code to count actual logical CPUs can be also found in the linked whire paper.




回答2:


A more complete answer would be that for Intel processors made before 2010 that kind code was usually okay. (I'd like to hear about counter-examples though!) That kind of counting routine was even given in an older Intel presentation dating to 2007(?) or so https://software.intel.com/en-us/articles/hyper-threading-technology-and-multi-core-processor-detection (Don't let the posting date the fool you; when that presentation was made the Intel Core wasn't yet public and the Pentium Extreme Edition was Intel's top offering.) Rather shamefully, that algorithm is still given in the MSDN page about __cpuid, even for their 2013 edition! (http://msdn.microsoft.com/en-us/library/hskdteyh.aspx) The MSDN sample code hasn't been updated since 2008...

In early 2010 Intel put out Westmere processors that have gaps in their APIC id space. So these and later processors need a more refined method of counting their cores. These processors all have x2APIC and support leaf 0xB of cpuid as well... which can tell you about the gaps in the APIC id space, via EAX[4:0] and exactly how many logical processors are present at each level (package/core) via EBX. The link provided by n.m. is the authoritative one, albeit rather dry. Basically all you have to do is call cpuid with EAX=0xB, ECX=1 and you'll have the correct number of logical processors per package returned in EBX.

Also, the part of question about AMD was not answered. AMD has a different methodology because they don't support the same cpuid leaves as Intel. The most up to date info I could find about AMD (from 2013) is at http://developer.amd.com/resources/documentation-articles/articles-whitepapers/processor-and-core-enumeration-using-cpuid/

I dug up all this info while updating the Wikipedia page on CPUID today, by the way.



来源:https://stackoverflow.com/questions/24088837/logical-cpu-count-return-16-instead-of-4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!