2

Is there any way I can the get overall CPU usage of an iPhone. I have seen a few apps like Battery Doctor and System Activity Monitor for iPhones which display overall CPU usage.

I found a solution, (link to the answer) but it only gives me the CPU usage of my app and not of all the overall apps.

Community
  • 1
  • 1
Manish Ahuja
  • 4,509
  • 3
  • 28
  • 35

1 Answers1

6

please, try this one, it might a good start for you. it has been tested on real device only.

processor_info_array_t _cpuInfo, _prevCPUInfo = nil;
mach_msg_type_number_t _numCPUInfo, _numPrevCPUInfo = 0;
unsigned _numCPUs;
NSLock *_cpuUsageLock;

int _mib[2U] = { CTL_HW, HW_NCPU };
size_t _sizeOfNumCPUs = sizeof(_numCPUs);
int _status = sysctl(_mib, 2U, &_numCPUs, &_sizeOfNumCPUs, NULL, 0U);
if(_status)
    _numCPUs = 1;

_cpuUsageLock = [[NSLock alloc] init];

natural_t _numCPUsU = 0U;
kern_return_t err = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &_numCPUsU, &_cpuInfo, &_numCPUInfo);
if(err == KERN_SUCCESS) {
    [_cpuUsageLock lock];

    for(unsigned i = 0U; i < _numCPUs; ++i) {
        Float32 _inUse, _total;
        if(_prevCPUInfo) {
            _inUse = (
                     (_cpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_USER]   - _prevCPUInfo[(CPU_STATE_MAX * i) + CPU_STATE_USER])
                     + (_cpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_SYSTEM] - _prevCPUInfo[(CPU_STATE_MAX * i) + CPU_STATE_SYSTEM])
                     + (_cpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_NICE]   - _prevCPUInfo[(CPU_STATE_MAX * i) + CPU_STATE_NICE])
                     );
            _total = _inUse + (_cpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_IDLE] - _prevCPUInfo[(CPU_STATE_MAX * i) + CPU_STATE_IDLE]);
        } else {
            _inUse = _cpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_USER] + _cpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_SYSTEM] + _cpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_NICE];
            _total = _inUse + _cpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_IDLE];
        }

        NSLog(@"Core : %u, Usage: %.2f%%", i, _inUse / _total * 100.f);
    }

    [_cpuUsageLock unlock];

    if(_prevCPUInfo) {
        size_t prevCpuInfoSize = sizeof(integer_t) * _numPrevCPUInfo;
        vm_deallocate(mach_task_self(), (vm_address_t)_prevCPUInfo, prevCpuInfoSize);
    }

    _prevCPUInfo = _cpuInfo;
    _numPrevCPUInfo = _numCPUInfo;

    _cpuInfo = nil;
    _numCPUInfo = 0U;
} else {
    NSLog(@"Error!");
}

you also need the following headers as well:

#import <sys/sysctl.h>
#import <sys/types.h>
#import <sys/param.h>
#import <sys/mount.h>
#import <mach/mach.h>
#import <mach/processor_info.h>
#import <mach/mach_host.h>
holex
  • 23,961
  • 7
  • 62
  • 76
  • I tried with what you said, and I get some value for CPU usage. If I go and start some other apps which run in background or start playing music in the background CPU usage should go up right? But I get the same values i was getting earlier. – Manish Ahuja Aug 07 '12 at 10:02
  • theoretically yes, it should happen. – holex Aug 07 '12 at 10:06
  • @ManishAhuja The problem with the code from above it's that he declared the _prevCPUInfo variable locally, and because of that the "if(_prevCPUInfo)" condition will not be evaluated properly. – arturgrigor Aug 24 '13 at 18:09
  • This method does not work at all. At fully loaded CPU, this always returns 6.89% for core 1 and 7.38% for core 0. – Legoless Nov 11 '14 at 09:16
  • @Legoless, could be more specific? on simulator? on device? how do you know it is fully loaded? who told you CPU is fully loaded? etc... – holex Nov 11 '14 at 09:19
  • Of course @holex. The device is iPhone 6+ running on iOS 8.1. And how do I know it is fully loaded? I've loaded it fully myself and I have another function which returns correct values, but it only measures current application. – Legoless Nov 11 '14 at 09:22
  • okay, I ask you again, how do you know the CPU is fully loaded? it does not count if you _think_ you's fully loaded. the OS usually has a professional task-balancer, which works based on the tasks' priorities and always request the less busier core to commit a task. it is very rare nowadays to 'fully' load all cores at the same time... I think that confuses you a bit. :) and yes, it measures only _your_ application's usage as the OP requested exactly this. – holex Nov 11 '14 at 09:28
  • @ManishAhuja I know this is many months later, but did you find a proper way to get the total cpu usage? This answer doesn't seem to work. Please reply. thanks. – john Mar 26 '15 at 12:30