Programmatically find the number of cores on a machine

前端 未结 19 2138
刺人心
刺人心 2020-11-22 16:38

Is there a way to determine how many cores a machine has from C/C++ in a platform-independent way? If no such thing exists, what about determining it per-platform (Windows/*

相关标签:
19条回答
  • 2020-11-22 17:12

    hwloc (http://www.open-mpi.org/projects/hwloc/) is worth looking at. Though requires another library integration into your code but it can provide all the information about your processor (number of cores, the topology, etc.)

    0 讨论(0)
  • 2020-11-22 17:16

    Windows Server 2003 and later lets you leverage the GetLogicalProcessorInformation function

    http://msdn.microsoft.com/en-us/library/ms683194.aspx

    0 讨论(0)
  • 2020-11-22 17:16

    On Linux, it's may not be safe to to use _SC_NPROCESSORS_ONLN as it's not part of POSIX standard and the sysconf manual states as much. So there's a possibility that _SC_NPROCESSORS_ONLN may not be present:

     These values also exist, but may not be standard.
    
         [...]     
    
         - _SC_NPROCESSORS_CONF
                  The number of processors configured.   
         - _SC_NPROCESSORS_ONLN
                  The number of processors currently online (available).
    

    A simple approach would be to read /proc/stat or /proc/cpuinfo and count them:

    #include<unistd.h>
    #include<stdio.h>
    
    int main(void)
    {
    char str[256];
    int procCount = -1; // to offset for the first entry
    FILE *fp;
    
    if( (fp = fopen("/proc/stat", "r")) )
    {
      while(fgets(str, sizeof str, fp))
      if( !memcmp(str, "cpu", 3) ) procCount++;
    }
    
    if ( procCount == -1) 
    { 
    printf("Unable to get proc count. Defaulting to 2");
    procCount=2;
    }
    
    printf("Proc Count:%d\n", procCount);
    return 0;
    }
    

    Using /proc/cpuinfo:

    #include<unistd.h>
    #include<stdio.h>
    
    int main(void)
    {
    char str[256];
    int procCount = 0;
    FILE *fp;
    
    if( (fp = fopen("/proc/cpuinfo", "r")) )
    {
      while(fgets(str, sizeof str, fp))
      if( !memcmp(str, "processor", 9) ) procCount++;
    }
    
    if ( !procCount ) 
    { 
    printf("Unable to get proc count. Defaulting to 2");
    procCount=2;
    }
    
    printf("Proc Count:%d\n", procCount);
    return 0;
    }
    

    The same approach in shell using grep:

    grep -c ^processor /proc/cpuinfo
    

    Or

    grep -c ^cpu /proc/stat # subtract 1 from the result
    
    0 讨论(0)
  • 2020-11-22 17:18

    Unrelated to C++, but on Linux I usually do:

    grep processor /proc/cpuinfo | wc -l
    

    Handy for scripting languages like bash/perl/python/ruby.

    0 讨论(0)
  • 2020-11-22 17:19

    On linux the best programmatic way as far as I know is to use

    sysconf(_SC_NPROCESSORS_CONF)
    

    or

    sysconf(_SC_NPROCESSORS_ONLN)
    

    These aren't standard, but are in my man page for Linux.

    0 讨论(0)
  • 2020-11-22 17:20

    Note that "number of cores" might not be a particularly useful number, you might have to qualify it a bit more. How do you want to count multi-threaded CPUs such as Intel HT, IBM Power5 and Power6, and most famously, Sun's Niagara/UltraSparc T1 and T2? Or even more interesting, the MIPS 1004k with its two levels of hardware threading (supervisor AND user-level)... Not to mention what happens when you move into hypervisor-supported systems where the hardware might have tens of CPUs but your particular OS only sees a few.

    The best you can hope for is to tell the number of logical processing units that you have in your local OS partition. Forget about seeing the true machine unless you are a hypervisor. The only exception to this rule today is in x86 land, but the end of non-virtual machines is coming fast...

    0 讨论(0)
提交回复
热议问题