C++ 处理器数量与CPUZ不匹配

C++ 处理器数量与CPUZ不匹配,c++,cpu-cores,C++,Cpu Cores,我想知道cpu核心的数量,所以,我需要这个代码 SYSTEM_INFO siSysInfo; DWORD getProcessorNum() { GetSystemInfo(&siSysInfo); return siSysInfo.dwNumberOfProcessors; } 它返回4。但当我用CPUZ检查结果时,它返回2个Cores 4Thraeds 操作系统我尝试使用此代码\uuuCPUID SYSTEM_INFO siSysInfo; DWORD getPro

我想知道cpu核心的数量,所以,我需要这个代码

SYSTEM_INFO siSysInfo;
DWORD getProcessorNum()
{
    GetSystemInfo(&siSysInfo);
    return siSysInfo.dwNumberOfProcessors;
}
它返回4。但当我用CPUZ检查结果时,它返回2个Cores 4Thraeds

操作系统我尝试使用此代码\uuuCPUID

SYSTEM_INFO siSysInfo;
DWORD getProcessorNum()
{
    GetSystemInfo(&siSysInfo);
    return siSysInfo.dwNumberOfProcessors;
}
未签署的regs[4]

__cpuid(regs,4)
核心=((regs[0]>>26)和0x3f)+1


cout检查CPU的数量一点也不简单(主要是由于多核/套接字/CPU设计)。
GetSystemInfo
调用返回系统看到的执行线程数量,CPUID的值根据CPU拓扑结构的不同而不同(请参阅《英特尔开发人员手册》)

我构建了一些东西来捕捉所有情况,您可能会发现它很有用:

static void GetProcessorTopology(size_t& nPhysical, size_t& nCores, size_t& nSystemThreads, size_t& nHWThreads, size_t& nThreadsPerCore)
{
    int nInfo[4];
    SYSTEM_INFO pSystemInfo;
    IsWOW64() ? GetNativeSystemInfo(&pSystemInfo) : GetSystemInfo(&pSystemInfo);
    nSystemThreads = pSystemInfo.dwNumberOfProcessors;
    if(TestTopologyLevel(nInfo,0x0,1) || TestTopologyLevel(nInfo,0x1,1))
    {
        nThreadsPerCore = nInfo[1] & 0xFFFF;
        if(TestTopologyLevel(nInfo,0x0,2) || TestTopologyLevel(nInfo,0x1,2))
        {
            nHWThreads = nInfo[1] & 0xFFFF;
            nCores = (nThreadsPerCore == 0) ? GetCoreCount(nInfo) : nHWThreads / nThreadsPerCore;
        }
        else
        {
            nHWThreads = nSystemThreads;
            nCores = GetCoreCount(nInfo);       
        }       
    }
    else
    {
        nThreadsPerCore = 1;
        nHWThreads = nSystemThreads;
        nCores = GetCoreCount(nInfo);
    }

    nPhysical = (nCores == 0) ? 1 : (nSystemThreads / nThreadsPerCore) / nCores;
}

static inline bool TestTopologyLevel(int* pInfo, int nMode, int nLevel)
{
    __cpuidex(pInfo,0xB,nMode);
    return ((pInfo[2] >> 8) & 0xFF) == nLevel;
}

static inline size_t GetCoreCount(int* nInfo)
{
    __cpuid(nInfo,0);
    if(nInfo[0] >= 4)
    {
        __cpuidex(nInfo,0x4,0x0);
        return (nInfo[0] >> 26) + 1;
    }
    else
    {
        __cpuid(nInfo,0x80000000);
        int nCPUID = nInfo[0];
        __cpuid(nInfo,1);                   
        if(nInfo[3] & 0x10000000 && nCPUID >= 0x80000008)
        {
            __cpuid(nInfo,0x80000008);
            return (nInfo[2] & 0xFF) + 1;
            }
        }

    return 1;
}

检查CPU的数量一点也不简单(主要是由于多核/套接字/CPU设计)。
GetSystemInfo
调用返回系统看到的执行线程数量,CPUID的值根据CPU拓扑结构的不同而不同(请参阅《英特尔开发人员手册》)

我构建了一些东西来捕捉所有情况,您可能会发现它很有用:

static void GetProcessorTopology(size_t& nPhysical, size_t& nCores, size_t& nSystemThreads, size_t& nHWThreads, size_t& nThreadsPerCore)
{
    int nInfo[4];
    SYSTEM_INFO pSystemInfo;
    IsWOW64() ? GetNativeSystemInfo(&pSystemInfo) : GetSystemInfo(&pSystemInfo);
    nSystemThreads = pSystemInfo.dwNumberOfProcessors;
    if(TestTopologyLevel(nInfo,0x0,1) || TestTopologyLevel(nInfo,0x1,1))
    {
        nThreadsPerCore = nInfo[1] & 0xFFFF;
        if(TestTopologyLevel(nInfo,0x0,2) || TestTopologyLevel(nInfo,0x1,2))
        {
            nHWThreads = nInfo[1] & 0xFFFF;
            nCores = (nThreadsPerCore == 0) ? GetCoreCount(nInfo) : nHWThreads / nThreadsPerCore;
        }
        else
        {
            nHWThreads = nSystemThreads;
            nCores = GetCoreCount(nInfo);       
        }       
    }
    else
    {
        nThreadsPerCore = 1;
        nHWThreads = nSystemThreads;
        nCores = GetCoreCount(nInfo);
    }

    nPhysical = (nCores == 0) ? 1 : (nSystemThreads / nThreadsPerCore) / nCores;
}

static inline bool TestTopologyLevel(int* pInfo, int nMode, int nLevel)
{
    __cpuidex(pInfo,0xB,nMode);
    return ((pInfo[2] >> 8) & 0xFF) == nLevel;
}

static inline size_t GetCoreCount(int* nInfo)
{
    __cpuid(nInfo,0);
    if(nInfo[0] >= 4)
    {
        __cpuidex(nInfo,0x4,0x0);
        return (nInfo[0] >> 26) + 1;
    }
    else
    {
        __cpuid(nInfo,0x80000000);
        int nCPUID = nInfo[0];
        __cpuid(nInfo,1);                   
        if(nInfo[3] & 0x10000000 && nCPUID >= 0x80000008)
        {
            __cpuid(nInfo,0x80000008);
            return (nInfo[2] & 0xFF) + 1;
            }
        }

    return 1;
}

CPUZ报告硬件核心和逻辑核心。似乎,你的机器上的“超线程”已打开,它将使内核数增加一倍(2物理->4逻辑)。@osgx:不要相信那个页面,它有许多不准确/模糊的语句。当使用cpuid时,只信任从
\uuuuCPUID(regs,0)
返回的计数和英特尔/amd开发人员手册(编辑:我看到你更改了注释/删除了注释)中的数据。Necrolis,你能说说为什么cpuid 4返回8作为核心计数吗?@osgx:你误解了我的注释,它指的是你现在删除的关于4是一个有效的cpuid索引的部分(
\uuuCPUID(regs,0)
返回与CPU id一起使用的有效索引),至于为什么他得到8,i3有4个执行线程,它的执行线程数是HT的两倍(如你所说)。但是CPUz说:“2Cores4thraeds”CPUz报告硬件核心和逻辑核心。似乎,你的机器上的“超线程”已打开,它将使内核数增加一倍(2物理->4逻辑)。@osgx:不要相信那个页面,它有许多不准确/模糊的语句。当使用cpuid时,只信任从
\uuuuCPUID(regs,0)
返回的计数和英特尔/amd开发人员手册(编辑:我看到你更改了注释/删除了注释)中的数据。Necrolis,你能说说为什么cpuid 4返回8作为核心计数吗?@osgx:你误解了我的注释,它指的是你现在删除的关于4是一个有效的cpuid索引的部分(
\uuuCPUID(regs,0)
返回与CPU id一起使用的有效索引),至于为什么他得到8,i3有4个执行线程,它的执行线程数是HT的两倍(如你所说)。但是CPUz说:“2cores4thrads”