使用sysctl()的每个进程的iOS cpu使用率? [英] iOS cpu usage for each process using sysctl()?

查看:73
本文介绍了使用sysctl()的每个进程的iOS cpu使用率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用sysctl()获取每个进程的CPU使用率?

我正在尝试找到一种方法来检测特定应用程序的启动.似乎无法获取前台运行的应用程序信息.因此,我想如果我可以监视特定应用程序的cpu使用情况,则可以监视cpu使用情况的变化,并大致假定该应用程序何时启动.这有可能吗?

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?

我不打算将此应用发布到Apple Appstore.

这只是一项研究.因此,如果有任何方法可以做到这一点,我很高兴知道.

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

推荐答案

执行以下过程1.导入以下文件

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.添加ivars

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文件

-(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];
}

}

这篇关于使用sysctl()的每个进程的iOS cpu使用率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆