ios android cpu占用率,iPhone-iOS-从应用程序获取CPU使用率

iPhone-iOS-从应用程序获取CPU使用率

有谁知道如何获取应用程序的CPU使用率? 绝对有可能,因为应用商店中有一些可以显示它的应用程序(活动监视器触摸)。

3个解决方案

74 votes

更新。 这段代码为我工作:

更新2。thread_list泄漏,因此添加了vm_deallocate

#import

#import

float cpu_usage()

{

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 -1;

}

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 -1;

}

if (thread_count > 0)

stat_thread += thread_count;

long tot_sec = 0;

long tot_usec = 0;

float tot_cpu = 0;

int j;

for (j = 0; j < (int)thread_count; j++)

{

thread_info_count = THREAD_INFO_MAX;

kr = thread_info(thread_list[j], THREAD_BASIC_INFO,

(thread_info_t)thinfo, &thread_info_count);

if (kr != KERN_SUCCESS) {

return -1;

}

basic_info_th = (thread_basic_info_t)thinfo;

if (!(basic_info_th->flags & TH_FLAGS_IDLE)) {

tot_sec = tot_sec + basic_info_th->user_time.seconds + basic_info_th->system_time.seconds;

tot_usec = tot_usec + basic_info_th->user_time.microseconds + basic_info_th->system_time.microseconds;

tot_cpu = tot_cpu + basic_info_th->cpu_usage / (float)TH_USAGE_SCALE * 100.0;

}

} // for each thread

kr = vm_deallocate(mach_task_self(), (vm_offset_t)thread_list, thread_count * sizeof(thread_t));

assert(kr == KERN_SUCCESS);

return tot_cpu;

}

ivanzoid answered 2020-07-09T18:06:54Z

8 votes

对于Swift 3:

fileprivate func cpuUsage() -> Double {

var kr: kern_return_t

var task_info_count: mach_msg_type_number_t

task_info_count = mach_msg_type_number_t(TASK_INFO_MAX)

var tinfo = [integer_t](repeating: 0, count: Int(task_info_count))

kr = task_info(mach_task_self_, task_flavor_t(TASK_BASIC_INFO), &tinfo, &task_info_count)

if kr != KERN_SUCCESS {

return -1

}

var thread_list: thread_act_array_t? = UnsafeMutablePointer(mutating: [thread_act_t]())

var thread_count: mach_msg_type_number_t = 0

defer {

if let thread_list = thread_list {

vm_deallocate(mach_task_self_, vm_address_t(UnsafePointer(thread_list).pointee), vm_size_t(thread_count))

}

}

kr = task_threads(mach_task_self_, &thread_list, &thread_count)

if kr != KERN_SUCCESS {

return -1

}

var tot_cpu: Double = 0

if let thread_list = thread_list {

for j in 0 ..< Int(thread_count) {

var thread_info_count = mach_msg_type_number_t(THREAD_INFO_MAX)

var thinfo = [integer_t](repeating: 0, count: Int(thread_info_count))

kr = thread_info(thread_list[j], thread_flavor_t(THREAD_BASIC_INFO),

&thinfo, &thread_info_count)

if kr != KERN_SUCCESS {

return -1

}

let threadBasicInfo = convertThreadInfoToThreadBasicInfo(thinfo)

if threadBasicInfo.flags != TH_FLAGS_IDLE {

tot_cpu += (Double(threadBasicInfo.cpu_usage) / Double(TH_USAGE_SCALE)) * 100.0

}

} // for each thread

}

return tot_cpu

}

fileprivate func convertThreadInfoToThreadBasicInfo(_ threadInfo: [integer_t]) -> thread_basic_info {

var result = thread_basic_info()

result.user_time = time_value_t(seconds: threadInfo[0], microseconds: threadInfo[1])

result.system_time = time_value_t(seconds: threadInfo[2], microseconds: threadInfo[3])

result.cpu_usage = threadInfo[4]

result.policy = threadInfo[5]

result.run_state = threadInfo[6]

result.flags = threadInfo[7]

result.suspend_count = threadInfo[8]

result.sleep_time = threadInfo[9]

return result

}

Lionking answered 2020-07-09T18:07:14Z

0 votes

试试这个:

- (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)

stat_thread += thread_count;

long tot_idle = 0;

long tot_user = 0;

long tot_kernel = 0;

int j;

for (j = 0; j < thread_count; j++)

{

thread_info_count = THREAD_INFO_MAX;

kr = thread_info(thread_list[j], THREAD_BASIC_INFO,

(thread_info_t)thinfo, &thread_info_count);

if (kr != KERN_SUCCESS)

{

return nil;

}

basic_info_th = (thread_basic_info_t)thinfo;

if (basic_info_th->flags & TH_FLAGS_IDLE)

{

//This is idle

tot_idle = tot_idle + basic_info_th->user_time.microseconds + basic_info_th->system_time.microseconds;

} else {

//This is user

tot_user = tot_user + basic_info_th->user_time.microseconds;

//This is kernel

tot_kernel = tot_kernel + basic_info_th->system_time.microseconds;

}

} // for each thread

kr = vm_deallocate(mach_task_self(), (vm_offset_t)thread_list, thread_count * sizeof(thread_t));

assert(kr == KERN_SUCCESS);

long tot_cpu = tot_idle + tot_user + tot_kernel

return [NSString stringWithFormat:@"Idle: %.2f, User: %.2f, Kernel: %.2f", tot_idle/tot_cpu, tot_user/tot_cpu, tot_kernel/tot_cpu];

}

但是,该方法会根据每个过程的起点来计算百分比。 如果您正在寻找更传统的方式来计算这些数字,请参见Petesh的答案。

Coder256 answered 2020-07-09T18:07:39Z

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/261891.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java 编程:如何提高性能?(简单总结篇)

2019独角兽企业重金招聘Python工程师标准>>> 开发者在编程中除了要有编程规范&#xff0c;还要注意性能&#xff0c;在 Java 编程中有什么提高性能的好办法呢&#xff1f; 本文转自国内 ITOM 行业领军企业 OneAPM Cloud Insight&#xff08;一款能够优雅监控多种操作…

ActiviteX 服务控件

ActiviteX 控件和ActiviteX 服务(dll ,exe) ActiveX 控件是由软件提供商开发的可重用的软件组件 我们可以使用vb自己编写控件,他们三个都是对过程 ,类,等功能块的封装. 区别就是在vb中使用ActiviteX控件的时候要先实例化..比如画出控件. 而使用ActiviteX时是先注册.然后再引用中…

mysql没有开启binlog能恢复数据吗_神了!一个妹子 rm -rf 把公司整个数据库删没了......

经历了两天不懈努力&#xff0c;终于恢复了一次误操作删除的生产服务器数据。对本次事故过程和解决办法记录在此&#xff0c;警醒自己&#xff0c;也提示别人莫犯此错。也希望遇到问题的朋友能找到一丝灵感解决问题。01事故背景安排一个妹子在一台生产服务器上安装 Oracle&…

PeerJS 0.1.7:一个用于浏览器内P2P的WebRTC封装器

Michelle Bu与Eric Zhang在3月6日发布了PeerJS 0.1.7&#xff0c;它封装了WebRTC。后者是W3C倡议的旨在促进浏览器内P2P通信的一种技术。 尽管WebSocket的作用发展迅速&#xff0c;但PeerJS代表的是之前由服务器组织数据传输的方式有了根本性转变。 Bu说&#xff1a;“WebSocke…

python 命名实体识别_使用Python和Keras的有关命名实体识别(NER)的完整教程

假设您是报纸行业的编辑&#xff0c;每天都会收到数千个故事。您将如何找到与体育&#xff0c;政治等特定领域相关的故事&#xff1f;您会讲完所有这些故事吗&#xff1f;无权利&#xff1f;一个可以帮助您划分为不同类别的系统怎么样&#xff1f;该系统还可以执行复杂的任务&a…

android手机活跃度,微信Android机型活跃度曝光,这个结果你满意吗?

原标题&#xff1a;微信Android机型活跃度曝光&#xff0c;这个结果你满意吗&#xff1f;在本周的第二届前端开发者大会上&#xff0c;由腾讯微信工程师公布了微信Android客户端机型前十的发布图&#xff0c;说的也是某款手机的存量跟用户活跃度的统计&#xff0c;在这张微信分…

postgresql9.4.4中文手册笔记-9.10 支持枚举函数

2019独角兽企业重金招聘Python工程师标准>>> 创建枚举类型 create type name as ENUM(zhao,qian,sun,li,zhou,wu, zheng,wang,cheng); 支持枚举函数 select enum_first(null::testenum); --查询枚举类型第一个元素 enum_first ------------zhao (1 row) select e…

.NET Framework源码研究系列之---Delegate

前言 曾几何时能看到微软产品的源码简直是天方夜谭,不过现在这却成了现实,微软终于对外开放了它的产品的源代码.抛去开源运动与微软之间的世代情仇,抛去微软这一做法的初衷,这总归是件好事,能够让我们拨开云雾,一窥优秀产品的秘密. 前两天看到有位仁兄在随笔中的留言,说他以为&…

【原】P2P应用的探究

什么是P2P&#xff08;peer to peer&#xff09;? 跟P2P对应的是传统的客户端-服务器&#xff08;C/S或B/S)体系结构,这种体系结构就是客户端发送请求&#xff0c;服务器端给予响应。但是随着客户端不断的增加&#xff0c;成了服务器端崩溃的主要原因。增加服务器的功能或者资…

如何设置多个图层层叠关系_如何玩转 XMind 中的多种思维结构?

熟悉 XMind 的用户都知道&#xff0c;XMind 支持多种思维结构&#xff0c;并且不同思维结构可以混用。每一个分支都可以是一个不同的结构&#xff0c;让你不受限制、自由地进行思维的发散和整理。这个是目前其他思维导图工具少有的。在 XMind 中&#xff0c;你可以用思维导图、…

python修改html表格,使用styles和css更改pandas dataframe html表python中...

这需要几个步骤&#xff1a;首先导入HTML并重新输入from IPython.display import HTMLimport re你可以通过to_html方法得到html pandas.df_html df.to_html()接下来,我们将为html表和我们要创建的样式生成随机标识符.random_id id%d % np.random.choice(np.arange(1000000))因…

PHP关于VC11,VC9,VC6以及Thread Safe和Non Thread Safe版本选择

2019独角兽企业重金招聘Python工程师标准>>> 这里是我在搭建php环境时收集的资料供大家参考&#xff1a; 现在PHP官网上下载PHP安装包都有VC11或VC9的字样&#xff0c;这是什么含义&#xff0c;我们应该下载哪种安装包更好呢&#xff1f;其实PHP官网给出了答案&…

Silverlight与WCF之间的通信(5)silverlight应用和wcf服务的发布方法

上一篇博文中有朋友问到关于silverlight程序发布的问题&#xff0c;上一篇写的是silverlight访问host在console上的wcf&#xff0c;其实关于wcf和silverlihgt通信的问题有好几种方式&#xff0c;这里列举了一下 客户端和服务端采用http协议通信&#xff08;分两种&#xff0c;同…

常用函数(字符和字符串)

Pascal常用的字符处理标准函数有5个 &#xff08;1&#xff09;ord(ch) 求字符ch对应的ASCII代码值&#xff1b;如 ord (A)结果为65&#xff0c;ord(true)结果为1&#xff0c;ord(false)结果为0&#xff08;2&#xff09;chr(x) 求x&#xff08;x为1…255…

python搜索文件内容_python实现搜索文本文件内容

{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],"search_count":[{"count_phone":4,"count":4}]},"card":[{"des":"阿里云文件存储NAS是一个可共享访问&#xf…

html单张图片效果,jquery+html5实现单张图片上传预览

js&#xff1a;if (window.File && window.FileReader && window.FileList && window.Blob){//Blob是计算机界通用术语之一&#xff0c;全称写作&#xff1a;BLOB (binary large object)&#xff0c;表示二进制大对象。//全部支持function handleFileS…

通向成功的23个方法

1.设立大目标和小目标。总要保持向前看&#xff0c;但不要忘记你今天的成绩。 2.每天都设立新的目标。因为成功是一个过程&#xff0c;所以要确保总是有新的事情要完成。 3.逐一完成每周的事情。将时间表分成容易做到的几块&#xff0c;并且单独完成每一块。 4.每天至少完成一件…

科讯SQL标签调用文章技巧

http://qdh68.blog.163.com/blog/static/13756126201241584835332/转载于:https://www.cnblogs.com/zhangzhu/archive/2013/03/31/2991754.html

小程序引用其他页面js_来聊聊小程序页面之间如何通信

小程序页面之间如何通信?首先将通信的模型列举出来, 分为以下几种兄弟页面间通信父路径页面向子路径页面通信子路径页面向父路径页面通信通信的方式localStorage 本地存储globalData 全局对象eventBus 发布订阅PageModel 缓存整个pageModel至globalDataLocalStorage利用onShow…

html meta页面自适应,【转载·收藏】 html5手机网站自适应需要加的meta标签

webapp开发初期,会碰到在pc端开发好的页面在移动端显示过大的问题,这里需要在html head中加入meta标签来控制缩放下面是对于这个标签的具体说明:viewport 语法介绍&#xff1a;content包含有以下几个属性:height [pixel_value | device-height] ,width [pixel_value | device-w…