iOS cpu usage for each process using sysctl()?
Asked Answered
N

1

7

Is there a way to get cpu usage for each process using sysctl() ?

I'm trying to find a way to detect the launch of a specific application. It seems there's no way to get foreground running app information. So I guess if I can monitor cpu usage for that specific app I can monitor the cpu usage changes and roughly assume when the app launches. Is this possible at all?

I'm not planning to publish this app to apple appstore.

This is only a research. So if there is ANY way to do this I'm glad to know.

Narayan answered 15/10, 2012 at 4:45 Comment(0)
P
6

Go through the following process 1. Import Following Files

#include <sys/sysctl.h>
#include <sys/types.h>
#include <mach/mach.h>
#include <mach/processor_info.h>
#include <mach/mach_host.h>

2. Add ivars

processor_info_array_t cpuInfo, prevCpuInfo;
mach_msg_type_number_t numCpuInfo, numPrevCpuInfo;
unsigned numCPUs;
NSTimer *updateTimer;
NSLock *CPUUsageLock;

3.IN .m file

-(void)voidDidLoad
{
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];

updateTimer = [[NSTimer scheduledTimerWithTimeInterval:3
                                                target:self
                                              selector:@selector(updateInfo:)
                                              userInfo:nil
                                               repeats:YES] retain];    
}
- (void)updateInfo:(NSTimer *)timer
{
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) {
        float 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: %f",i,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 = NULL;
    numCpuInfo = 0U;
} else {
    NSLog(@"Error!");
    [NSApp terminate:nil];
}

}

Periodic answered 15/10, 2012 at 5:22 Comment(3)
Hi Ankit, thank you verymuch for the answer. But I still couldn't figure out a way to get cpu usage by process id. if you have a sample code snippet it would be extremely helpful. thank you!Narayan
Hi thank you very much for the answer. However , does this code log cpu usage for each process? I would like to know the cpu usage for 'Youtube' applicaiton or 'Camera' application by passing their PIDs to a function. If you can give me a clue it's highly appreciated.Narayan
Thank you for answer. How to get total idle cpu time? (NOT for each process).Hamlett

© 2022 - 2024 — McMap. All rights reserved.