【Darwin学习笔记】之获取系统处理器数量的方法

阅读Darwin源码的时候看到这个方法,感觉挺有用处,且考虑了多种平台下的实现方式,直接贴代码,以后说不定会用到~

单一种平台下的实现方法可能很容易,但是把这些个系统都收集在一起,在一个函数中实现还是极好的

【转载请注明出处】:http://blog.csdn.net/longlong530

  1.  
    UInt32 OS::GetNumProcessors()
  2.  
    {
  3.  
    #if (__Win32__)
  4.  
    SYSTEM_INFO theSystemInfo;
  5.  
    ::GetSystemInfo(&theSystemInfo);
  6.  
     
  7.  
    return (UInt32)theSystemInfo.dwNumberOfProcessors;
  8.  
    #endif
  9.  
     
  10.  
    #if (__MacOSX__ || __FreeBSD__)
  11.  
    int numCPUs = 1;
  12.  
    size_t len = sizeof(numCPUs);
  13.  
    int mib[2];
  14.  
    mib[ 0] = CTL_HW;
  15.  
    mib[ 1] = HW_NCPU;
  16.  
    ( void) ::sysctl(mib,2,&numCPUs,&len,NULL,0);
  17.  
    if (numCPUs < 1)
  18.  
    numCPUs = 1;
  19.  
    return (UInt32) numCPUs;
  20.  
    #endif
  21.  
     
  22.  
    #if(__linux__ || __linuxppc__)
  23.  
     
  24.  
    char cpuBuffer[8192] = "";
  25.  
    StrPtrLen cpuInfoBuf(cpuBuffer, sizeof(cpuBuffer));
  26.  
    FILE *cpuFile = ::fopen( "/proc/cpuinfo", "r" );
  27.  
    if (cpuFile)
  28.  
    { cpuInfoBuf.Len = ::fread(cpuInfoBuf.Ptr, sizeof(char), cpuInfoBuf.Len, cpuFile);
  29.  
    ::fclose(cpuFile);
  30.  
    }
  31.  
     
  32.  
    StringParser cpuInfoFileParser(&cpuInfoBuf);
  33.  
    StrPtrLen line;
  34.  
    StrPtrLen word;
  35.  
    UInt32 numCPUs = 0;
  36.  
     
  37.  
    while( cpuInfoFileParser.GetDataRemaining() != 0 )
  38.  
    {
  39.  
    cpuInfoFileParser.GetThruEOL(&line); // Read each line
  40.  
    StringParser lineParser(&line);
  41.  
    lineParser.ConsumeWhitespace(); //skip over leading whitespace
  42.  
     
  43.  
    if (lineParser.GetDataRemaining() == 0) // must be an empty line
  44.  
    continue;
  45.  
     
  46.  
    lineParser.ConsumeUntilWhitespace(&word);
  47.  
     
  48.  
    if ( word.Equal("processor") ) // found a processor as first word in line
  49.  
    { numCPUs ++;
  50.  
    }
  51.  
    }
  52.  
     
  53.  
    if (numCPUs == 0)
  54.  
    numCPUs = 1;
  55.  
     
  56.  
    return numCPUs;
  57.  
    #endif
  58.  
     
  59.  
    #if(__solaris__)
  60.  
    {
  61.  
    UInt32 numCPUs = 0;
  62.  
    char linebuff[512] = "";
  63.  
    StrPtrLen line(linebuff, sizeof(linebuff));
  64.  
    StrPtrLen word;
  65.  
     
  66.  
    FILE *p = ::popen( "uname -X","r");
  67.  
    while((::fgets(linebuff, sizeof(linebuff -1), p)) > 0)
  68.  
    {
  69.  
    StringParser lineParser(&line);
  70.  
    lineParser.ConsumeWhitespace(); //skip over leading whitespace
  71.  
     
  72.  
    if (lineParser.GetDataRemaining() == 0) // must be an empty line
  73.  
    continue;
  74.  
     
  75.  
    lineParser.ConsumeUntilWhitespace(&word);
  76.  
     
  77.  
    if ( word.Equal("NumCPU")) // found a tag as first word in line
  78.  
    {
  79.  
    lineParser.GetThru( NULL,'=');
  80.  
    lineParser.ConsumeWhitespace(); //skip over leading whitespace
  81.  
    lineParser.ConsumeUntilWhitespace(&word); //read the number of cpus
  82.  
    if (word.Len > 0)
  83.  
    :: sscanf(word.Ptr, "%"_U32BITARG_"", &numCPUs);
  84.  
     
  85.  
    break;
  86.  
    }
  87.  
    }
  88.  
    if (numCPUs == 0)
  89.  
    numCPUs = 1;
  90.  
     
  91.  
    ::pclose(p);
  92.  
     
  93.  
    return numCPUs;
  94.  
    }
  95.  
    #endif
  96.  
     
  97.  
    #if(__sgi__)
  98.  
    UInt32 numCPUs = 0;
  99.  
     
  100.  
    numCPUs = sysconf(_SC_NPROC_ONLN);
  101.  
     
  102.  
    return numCPUs;
  103.  
    #endif
  104.  
     
  105.  
     
  106.  
    return 1;
  107.  
    }


PS:  函数摘自Darwin源码:\StreamServer\CommonUtilitiesLib\OS.cpp

猜你喜欢

转载自www.cnblogs.com/lidabo/p/9466897.html
今日推荐