我是Objective-C的初学者,我决定尝试编写一些简单的应用程序,所以我正在尝试制作一个可以衡量CPU使用率的应用程序.有没有一种简单的方法可以在Cocoa应用程序中使用Objective-C获取诸如CPU百分比使用率等信息?
我发现这个问题在Darwin/OSX中以编程方式确定过程信息类似但不完全相同.主要是,我想要整个系统的CPU百分比使用,而不仅仅是我的过程,我实际上更喜欢Objective-C解决方案,而在那个问题中,海报需要其他东西.
例如,如何在IOS中以不同状态获取CPU使用率
1.Idle
2.运行用户空间
3.运行内核/系统
CPU使用率例如,如本 提供全面的CPU使用率只喜欢below.How我可以检查使用中的不同状态?有帮助吗?
一般使用示例主要如下所示:
- (NSString *)cpuUsage
{
kern_return_t kr;
task_info_data_t tinfo;
mach_msg_type_number_t task_info_count;
task_info_count = TASK_INFO_MAX;
kr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)tinfo, &task_info_count);
if (kr != KERN_SUCCESS)
{
return @"NA";
}
task_basic_info_t basic_info;
thread_array_t thread_list;
mach_msg_type_number_t thread_count;
thread_info_data_t thinfo;
mach_msg_type_number_t thread_info_count;
thread_basic_info_t basic_info_th;
uint32_t stat_thread = 0; // Mach threads
basic_info = (task_basic_info_t)tinfo;
// get threads in the task
kr = task_threads(mach_task_self(), &thread_list, &thread_count);
if (kr != KERN_SUCCESS)
{
return @"NA";
}
if (thread_count > 0) …Run Code Online (Sandbox Code Playgroud) 我需要在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) 我正在寻找一个API来监控普通iPhone上运行的任务(没有越狱).这些是关于:
我想我可以查看系统C库(sigint等)但我不确定是否能够检索此信息,除了当前正在运行的应用程序.我知道一些监视器应用程序在全局使用上运行,但我希望能够按流程查找信息流程.
如果有人可以提供某些链接或有用的东西,那么我将开始进行更深入的调查.
我显然导致设备(iPad)耗尽内存,所以它放弃了我的应用程序.我正在试图理解正在发生的事情,因为Instruments正在告诉我,我使用的是大约80Mb,并且设备上没有运行其他应用程序.
我发现这段代码片段要求iOS下的Mach系统获取内存统计信息:
#import <mach/mach.h>
#import <mach/mach_host.h>
static void print_free_memory () {
mach_port_t host_port;
mach_msg_type_number_t host_size;
vm_size_t pagesize;
host_port = mach_host_self();
host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
host_page_size(host_port, &pagesize);
vm_statistics_data_t vm_stat;
if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
NSLog(@"Failed to fetch vm statistics");
/* Stats in bytes */
natural_t mem_used = (vm_stat.active_count +
vm_stat.inactive_count +
vm_stat.wire_count) * pagesize;
natural_t mem_free = vm_stat.free_count * pagesize;
natural_t mem_total = mem_used + mem_free;
NSLog(@"used: %u free: %u total: %u", mem_used, mem_free, mem_total);
}
Run Code Online (Sandbox Code Playgroud)
当我使用此函数获取三个内存值时,我发现即使mem_used总数没有发生太大变化,mem_total值也会下降.这是两个连续的输出行:
<Warning>: …Run Code Online (Sandbox Code Playgroud) 有没有办法访问并打印出设备当前的 cpu 信息?我找不到任何东西。
如何以编程方式让活动进程在后台运行,iOS 的 CPU 和 RAM 使用情况?
有什么方法可以获得iPhone的整体CPU使用率.我见过一些应用程序,如电池医生和iPhone的系统活动监视器,显示整体CPU使用率.
我找到了一个解决方案,(链接到答案)但它只给了我应用程序的CPU使用率而不是所有整体应用程序.