How do I detect a dual core CPU on iOS?

前端 未结 6 494
臣服心动
臣服心动 2020-12-08 14:48

My app uses an NSOperationQueue to cache thumbnail images in a background thread. On the iPad2 I can push the concurrent task count limit up to 5 or 6, but on s

相关标签:
6条回答
  • 2020-12-08 15:13

    I guess:

    sysctl(HW_NCPU) 
    

    or

    sysctlbyname("hw.ncpu", NULL, &size, NULL, 0);
    

    should work.

    you can modify code from: Detect the specific iPhone/iPod touch model

    0 讨论(0)
  • 2020-12-08 15:17

    In Swift you can detect / print the number of active processors with the following code:

    let processInfo = ProcessInfo()
    print(processInfo.activeProcessorCount)
    

    This code does not need any extra header files or frameworks and works completely natively.

    0 讨论(0)
  • 2020-12-08 15:22

    I guess this needs a separate answer rather than a comment:

    I wonder what does the number of cores means now that apple brought more asymmetric processing with A10 fusion? Do we have 2.2 cores or 4 cores? apple neglected to make activeProcessorCount float to account for it's fractal nature. note it was like so even before fusion cause they likely had thermal throttling back then. They need either fix overcomittal of nsoperationqueue or come up with a float equivalent for activeProcessorCount and deprecate the activeProcessorCount in its current form that is losing its utility in the face of recent hw advancements

    So the faster the Moore's law falls into oblivion or receives a shiny new constant the more meaningless core counting becomes. Unless you are writing some sort of a Geek Bench 42, multicore score edition.

    Living in the late 2016, to address the underlying issue you are facing rather than hacking through with max concurrent operation prop I assume you have adjusted the QoS for the NSOperationQueue to .Background?

    Me thinks this is a cleaner way to solve your problem with modern ios sdk then counting 'em cores using miles of rope courtesy of albertamg

    Also please take a look at NSProcessInfoThermalStateDidChangeNotification (macos) and NSProcessInfo.lowPowerModeEnabled (ios) (I guess an alternative to observing NSProcessInfoThermalStateDidChangeNotification is to KVo on activeProcessorCount new value)

    if you start to account for those new realities, the magic constants adjustments for multiplying core count would get interesting very fast... and rot as the new hardware rolls out of cupertino.

    And it will be just about as easy to get it right on the current zoo of Apple hw as to get socket-level networking working correctly: doable, but by select few in Cupetino with iSteve shadow looming over the shoulder checking quality ;-)

    0 讨论(0)
  • 2020-12-08 15:31

    Method 1

    [[NSProcessInfo processInfo] activeProcessorCount];
    

    NSProcessInfo also has a processorCount property. Learn the difference here.

    Method 2

    #include <mach/mach_host.h>
    
    unsigned int countCores()
    {
      host_basic_info_data_t hostInfo;
      mach_msg_type_number_t infoCount;
    
      infoCount = HOST_BASIC_INFO_COUNT;
      host_info( mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount ) ;
    
      return (unsigned int)(hostInfo.max_cpus);
    }
    

    Method 3

    #include <sys/sysctl.h>
    
    unsigned int countCores()
    {
      size_t len;
      unsigned int ncpu;
    
      len = sizeof(ncpu);
      sysctlbyname ("hw.ncpu",&ncpu,&len,NULL,0);
    
      return ncpu;
    }
    
    0 讨论(0)
  • 2020-12-08 15:36

    Among other things, you can get that information through a system call...

    NSDictionary dictionaryWithObjectsAndKeys:
        [self machineType],@"MachineType",
        [self humanMachineType],@"HumanMachineType",
        [self powerPCTypeString],@"ProcessorType",
        [NSNumber numberWithLong:
                         [self processorClockSpeed]],
                             @"ProcessorClockSpeed",
        [NSNumber numberWithLong:
                         [self processorClockSpeedInMHz]],
                             @"ProcessorClockSpeedInMHz",
        [NSNumber numberWithInt:[self countProcessors]],
                         @"CountProcessors",
        [self computerName],@"ComputerName",
        [self computerSerialNumber],@"ComputerSerialNumber",
        [self operatingSystemString],@"OperatingSystem",
        [self systemVersionString],@"SystemVersion",        
        nil];
    

    Here's the reference... http://cocoadev.com/HowToGetHardwareAndNetworkInfo

    0 讨论(0)
  • 2020-12-08 15:37

    Just use:

    [[NSProcessInfo processInfo] processorCount]

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