0

There has been a lot of the same question, but yet I cannot find great answers. I've looked at codes from this particular post which gives me usage per core, but it is inaccurate, always stuck at 14% or so even when Instrument tells me one core is under full load (primality test!).

I also read this question which I actually got the code from.

I have little knowledge of sysctl, which seems to be the key for these kind of things. Can someone explain to me how I could achieve that, code example will be even more appreciated. Thanks!

This is my current code, it was ran on a separate thread, which I just realized could be a problem, can anyone confirm? (I use NSThread and NSNotificationCenter to send usage information to main thread.)

- (void)updateCPUUsage
{
    NSMutableArray* arrayOfCPUUsage = [NSMutableArray array];
    while ( 1 )
    {
        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];
                }
                [arrayOfCPUUsage addObject:@(_inUse / _total)];
            }

            [_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;
        }
        [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"CPUUsageUpdate" object:[arrayOfCPUUsage copy]]];
        [arrayOfCPUUsage removeAllObjects];
        usleep(1000);
    }
}
Community
  • 1
  • 1
Shane Hsu
  • 7,937
  • 6
  • 39
  • 63
  • This may be better for [CodeReview.SE], as we usually only deal with broken code on SO. – Richard J. Ross III Mar 25 '13 at 15:48
  • @RichardJ.RossIII First of all, Code Review has less traffic, second of all, it is broken to me. LOL! I am just not sure why things are getting so wrong with this. And why did Apple make it so difficult. – Shane Hsu Mar 25 '13 at 15:51
  • Less traffic is not always bad - it can be good if you get the *right kind* of traffic. Here on SO, you're lucky to get more than 15 views per question. – Richard J. Ross III Mar 25 '13 at 15:51

0 Answers0