Darwin获取系统处理器数量的方法

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

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

UInt32  OS::GetNumProcessors()
{
#if (__Win32__)
    SYSTEM_INFO theSystemInfo;
    ::GetSystemInfo(&theSystemInfo);

    return (UInt32)theSystemInfo.dwNumberOfProcessors;
#endif

#if (__MacOSX__ || __FreeBSD__)
    int numCPUs = 1;
    size_t len = sizeof(numCPUs);
	int mib[2];
    mib[0] = CTL_HW;
    mib[1] = HW_NCPU;
    (void) ::sysctl(mib,2,&numCPUs,&len,NULL,0);
    if (numCPUs < 1)
        numCPUs = 1;
    return (UInt32) numCPUs;
#endif

#if(__linux__ || __linuxppc__)

    char cpuBuffer[8192] = "";
    StrPtrLen cpuInfoBuf(cpuBuffer, sizeof(cpuBuffer));
    FILE    *cpuFile = ::fopen( "/proc/cpuinfo", "r" );
    if (cpuFile)
    {   cpuInfoBuf.Len = ::fread(cpuInfoBuf.Ptr, sizeof(char),  cpuInfoBuf.Len, cpuFile);
        ::fclose(cpuFile);
    }

    StringParser cpuInfoFileParser(&cpuInfoBuf);
    StrPtrLen line;
    StrPtrLen word;
    UInt32 numCPUs = 0;

    while( cpuInfoFileParser.GetDataRemaining() != 0 )
    {
        cpuInfoFileParser.GetThruEOL(&line);    // Read each line
        StringParser lineParser(&line);
        lineParser.ConsumeWhitespace();         //skip over leading whitespace

        if (lineParser.GetDataRemaining() == 0) // must be an empty line
            continue;

        lineParser.ConsumeUntilWhitespace(&word);

        if ( word.Equal("processor") ) // found a processor as first word in line
        {   numCPUs ++;
        }
    }

    if (numCPUs == 0)
        numCPUs = 1;

    return numCPUs;
#endif

#if(__solaris__)
{
    UInt32 numCPUs = 0;
    char linebuff[512] = "";
    StrPtrLen line(linebuff, sizeof(linebuff));
    StrPtrLen word;

    FILE *p = ::popen("uname -X","r");
    while((::fgets(linebuff, sizeof(linebuff -1), p)) > 0)
    {
        StringParser lineParser(&line);
        lineParser.ConsumeWhitespace(); //skip over leading whitespace

        if (lineParser.GetDataRemaining() == 0) // must be an empty line
            continue;

        lineParser.ConsumeUntilWhitespace(&word);

        if ( word.Equal("NumCPU")) // found a tag as first word in line
        {
            lineParser.GetThru(NULL,'=');
            lineParser.ConsumeWhitespace();  //skip over leading whitespace
            lineParser.ConsumeUntilWhitespace(&word); //read the number of cpus
            if (word.Len > 0)
                ::sscanf(word.Ptr, "%"_U32BITARG_"", &numCPUs);

            break;
        }
    }
    if (numCPUs == 0)
        numCPUs = 1;

    ::pclose(p);

	return numCPUs;
}
#endif

#if(__sgi__)
    UInt32 numCPUs = 0;

    numCPUs = sysconf(_SC_NPROC_ONLN);

	return numCPUs;
#endif		

    return 1;
}

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

时间: 2024-08-30 06:10:31

Darwin获取系统处理器数量的方法的相关文章

WPF 获取系统 DPI 的多种方法

原文:WPF 获取系统 DPI 的多种方法 由于 WPF 的尺寸单位和系统的 DPI 相关,我们有时需要获取 DPI 值来进行一些界面布局的调整,本文汇总了一些 WPF 程序中获取系统 DPI 的方法. 首先,定义如下结构体来分别保存 X 方向 和 Y 方向的分量值,通常情况下两个值是一致的. public struct Dpi { public double X { get; set; } public double Y { get; set; } public Dpi(double x, d

C#获取系统时间的所有方法

C# 获取时间,日期 //C# 里内置的 DateTime 基本上都可以实现这些功能,巧用 DateTime 会使你处理这些事来变轻松多了       // 今天                  DateTime.Now.Date.ToShortDateString();     // 昨天,就是今天的日期减一                  DateTime.Now.AddDays(-1).ToShortDateString();     // 明天,同理,加一              

oc之获取系统当前时间的方法

NSDate *date = [NSDate date];//这个是NSDate类型的日期,所要获取的年月日都放在这里: NSCalendar *cal = [NSCalendar currentCalendar]; unsigned int unitFlags = NSCalendarUnitYear|NSCalendarUnitMonth| NSCalendarUnitDay;//这句是说你要获取日期的元素有哪些.获取年就要写NSYearCalendarUnit,获取小时就要写NSHourC

android获取系统应用大小的方法

<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;"></span></span><pre name="code" class="java">List<PackageInfo> packages = getPackageManager()

IOS,中获取系统内存占有率的方法

#import <mach/mach.h> - (float)__getMemoryUsedPer1 { struct mach_task_basic_info info; mach_msg_type_number_t size = sizeof(info); kern_return_t kerr = task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &size); if (kerr ==

C#获取系统字体

C#获取系统字体使用的方法: InstalledFontCollection MyFont = new InstalledFontCollection(); 但是当上面的命令出错的时候,可以试试下面这种: System.Drawing.Text.InstalledFontCollection MyFont = new System.Drawing.Text.InstalledFontCollection(); 原文地址:https://www.cnblogs.com/kuangzhenxia-4

JavaScript获取客户端计算机硬件及系统等信息的方法

JavaScript获取客户端计算机硬件及系统等信息的方法 JavaScript 获取客户端计算机硬件及系统信息 通过WMI来实现获取客户端计算机硬件及系统信息: function getSysInfo(){ var locator = new ActiveXObject ("WbemScripting.SWbemLocator"); var service = locator.ConnectServer("."); //CPU信息 var cpu = new En

Android获取系统时间方法详解

Android获取系统时间方法的方法有很多种,常用的有Calendar.Date.currentTimeMills等方法. (1)Calendar Calendar获取系统时间首先要用Calendar.getInstance()函数获取一个实例,再为该实例设定时区(中国的时区为GMT+8:00),最后使用Calendar.get()函数获取时间的具体信息,如年,月,日,小时,分,秒,星期几. package com.hzhi.time_example; import java.util.Cale

C++11新特性,利用std::chrono精简传统获取系统时间的方法

一.传统的获取系统时间的方法 传统的C++获取时间的方法需要分平台来定义.相信百度代码也不少. 我自己写了下,如下. const std::string getCurrentSystemTime() { if (PLATFORM_ANDROID || PLATFORM_IOS) { struct timeval s_now; struct tm* p_tm; gettimeofday(&s_now,NULL); p_tm = localtime((const time_t*)&s_now.