[genius_platform software platform development] lecture 15: routines for Linux and windows to obtain CPU cores

One concept

① Physical CPU

Number of CPU s in the actual Server slot
Number of physical CPUs. How many physical IDS can be counted without repetition

② Logical CPU

Linux users must be familiar with / proc/cpuinfo It is used to store cpu hardware information
The information content lists the specifications of processor 0 – n respectively. It should be noted that if you think n is the real number of CPUs, you are very wrong
In general, we believe that a cpu can have multiple cores, and intel's hyper threading technology (HT) can logically double the number of cpu core s
Number of logical CPUs = number of physical CPUs x specification value of CPU cores x 2 (if supported and enabled)
Note: the number of CPUs viewed by top under Linux is also the number of logical CPUs

③ Number of CPU cores

The number of chipsets that can process data on a CPU. For example, the i5 760 is a dual core four thread CPU, while the i5 2250 is a four core four thread CPU

Generally speaking, the number of physical CPUs × The number of each core should be equal to the number of logical CPUs. If it is not equal, it means that the CPU of the server supports hyper threading technology

II. View CPU Information

When we cat /proc/cpuinfo
CPU s with the same core id are hyper threads of the same core
CPUs with the same physical id are threads or cores encapsulated by the same CPU

Three examples are given below

① View the number of physical CPU s

#cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l
2

② View the number of logical CPU s

#cat /proc/cpuinfo |grep "processor"|wc -l
8

③ Check how many cores the CPU is

#cat /proc/cpuinfo |grep "cores"|uniq
4

I think there should be two CPUs here. Each Cpu has four core s, so the logical Cpu is 8

On the Windows platform, we can use * * GetSystemInfo() * * to obtain some software and hardware information of the current system. One of these is the number of processor cores in the current machine. The required information can be obtained by the following statement:

SYSTEM_INFO info;
GetSystemInfo(&info);
return info.dwNumberOfProcessors;

On the Linux platform, we can use * * sysconf() or get_nprocs() * * to get the number of processor cores. As follows:

sysconf() has unistd H. to use this function, you need * * #include < unistd h>**,

sysconf(_SC_NPROCESSORS_CONF) returns the number of cores available to the system, but its value will include the number of disabled cores in the system, so this value does not represent the number of cores available in the current system.
The return value of sysconf(_SC_NPROCESSORS_ONLN) truly represents the number of cores currently available in the system.

The GNU C library provides another way to obtain the number of available cores of the machine. Function intget_nprocs_conf (void),int get_nprocs (void) is in sys / Sysinfo As defined in H, these two functions can obtain the number of cores of the machine.
get_ nprocs_ The return value of conf (void) is similar to sysconf(_SC_NPROCESSORS_CONF), which does not really the current number of available cores of the table name;
And get_ The return value of nprocs (void) is similar to sysconf(_SC_NPROCESSORS_ONLN), which truly reflects the current number of available cores.

1, linux obtains the number of cpu cores

// linux gets the number of CPU s cpp : Defines the entry point for the console application.
//


#include<stdio.h>
#include<unistd.h>

#include<unistd.h>	// sysconf( )
#include<sys/sysinfo.h> // 
int main()
{
    //usleep(10 * 1000);

    int cpu_num;
    cpu_num = sysconf(_SC_NPROCESSORS_CONF);
    printf("_SC_NPROCESSORS_CONF=%d\n", cpu_num);

    cpu_num = sysconf(_SC_NPROCESSORS_ONLN);
    printf("_SC_NPROCESSORS_ONLN=%d\n", cpu_num);

    cpu_num = get_nprocs();
    printf("get_nprocs=%d\n", cpu_num);

    cpu_num = get_nprocs_conf();
    printf("get_nprocs_conf=%d\n", cpu_num);

    return 0;
}
/*
* - _SC_NPROCESSORS_CONF
* The number of processors configured.
*
* - _SC_NPROCESSORS_ONLN
* The number of processors currently online (available).
*/

2, Windows gets the number of CPU s

// Get the number of system CPU s cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>

typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
DWORD GetNumberOfProcessors()
{
    SYSTEM_INFO si;

    // Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.
    PGNSI pfnGNSI = (PGNSI)GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "GetNativeSystemInfo");
    if (pfnGNSI)
    {
        pfnGNSI(&si);
    }
    else
    {
        GetSystemInfo(&si);
    }
    return si.dwNumberOfProcessors;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int nCpuSum = GetNumberOfProcessors();
    printf("CPU Total not=[%d]\n", nCpuSum);

    system("pause");

	return 0;
}


Added by noguru on Fri, 21 Jan 2022 07:56:56 +0200