有谁知道如何获取应用程序的CPU使用率?这绝对是可能的,因为应用程序商店(Activity Monitor Touch)中有应用程序可以显示它.
我需要在OS X上获得目标C/C的总CPU空闲时间吗?
如果可能的话请提供代码示例.这是我用来获取这些指标的代码.结果百分比与我在Activity Monitor中的百分比不同.所以我假设CPU时间计算不正确:
#include <sys/sysctl.h>
#include <sys/types.h>
#include <mach/mach.h>
#include <mach/processor_info.h>
#include <mach/mach_host.h>
- (void)printCPUUsage
{
processor_cpu_load_info_t cpuLoad;
mach_msg_type_number_t processorMsgCount;
natural_t processorCount;
uint64_t totalSystemTime = 0, totalUserTime = 0, totalIdleTime = 0, totalCPUTime = 0;
kern_return_t err = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &processorCount, (processor_info_array_t *)&cpuLoad, &processorMsgCount);
for (natural_t i = 0; i < processorCount; i++) {
// Calc load types and totals, with guards against 32-bit overflow
// (values are natural_t)
uint64_t system = 0, user = 0, idle = 0, …Run Code Online (Sandbox Code Playgroud)