cpu使用率 htop显示_以htop为例 看怎么做Android iOS app cpu使用率监控

本文通过分析proc_pidtaskinfo结构体和proc_pidinfo系统调用,展示了如何获取并监控Android和iOS应用的CPU使用率,包括线程的用户时间和系统时间、上下文切换次数等关键指标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#define PROC_PIDTASKINFO4

struct proc_taskinfo pti;

proc_pidinfo(proc->super.pid, PROC_PIDTASKINFO, 0, &pti, sizeof(pti));

//proc_info.hstruct proc_taskinfo {

uint64_tpti_virtual_size;/* virtual memory size (bytes) */

uint64_tpti_resident_size;/* resident memory size (bytes) */

//关键成员uint64_tpti_total_user;/* total time */

uint64_tpti_total_system;

uint64_tpti_threads_user;/* existing threads only */

uint64_tpti_threads_system;

int32_tpti_policy;/* default policy for new threads */

int32_tpti_faults;/* number of page faults */

int32_tpti_pageins;/* number of actual pageins */

int32_tpti_cow_faults;/* number of copy-on-write faults */

int32_tpti_messages_sent;/* number of messages sent */

int32_tpti_messages_received;/* number of messages received */

int32_tpti_syscalls_mach;/* number of mach system calls */

int32_tpti_syscalls_unix;/* number of unix system calls */

int32_tpti_csw; /* number of context switches */

int32_tpti_threadnum;/* number of threads in the task */

int32_tpti_numrunning;/* number of running threads */

int32_tpti_priority;/* task priority*/

};

//proc_info.cint

proc_pidinfo(int pid, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t * retval)

{

struct proc * p = PROC_NULL;

int error = ENOTSUP;

int gotref = 0;

int findzomb = 0;

int shortversion = 0;

uint32_t size;

int zombie = 0;

int thuniqueid = 0;

int uniqidversion = 0;

boolean_t check_same_user;

...

switch (flavor) {

...

case PROC_PIDTASKINFO: {

struct proc_taskinfo ptinfo;

error = proc_pidtaskinfo(p, &ptinfo);

if (error == 0) {

//copyout 类似linux的 copy_to_user , 数据从kernel 拷贝到userlanderror = copyout(&ptinfo, buffer, sizeof(struct proc_taskinfo));

if (error == 0)

*retval = sizeof(struct proc_taskinfo);

}

}

break;

...

}

int

proc_pidtaskinfo(proc_t p, struct proc_taskinfo * ptinfo)

{

task_t task;

task = p->task;

bzero(ptinfo, sizeof(struct proc_taskinfo));

fill_taskprocinfo(task, (struct proc_taskinfo_internal *)ptinfo); //helin

return(0);

}

struct proc_taskinfo_internal {

uint64_t pti_virtual_size; /* virtual memory size (bytes) */

uint64_t pti_resident_size; /* resident memory size (bytes) */

uint64_t pti_total_user; /* total time */

uint64_t pti_total_system;

uint64_t pti_threads_user; /* existing threads only */

uint64_t pti_threads_system;

int32_t pti_policy; /* default policy for new threads */

int32_t pti_faults; /* number of page faults */

int32_t pti_pageins; /* number of actual pageins */

int32_t pti_cow_faults; /* number of copy-on-write faults */

int32_t pti_messages_sent; /* number of messages sent */

int32_t pti_messages_received; /* number of messages received */

int32_t pti_syscalls_mach; /* number of mach system calls */

int32_t pti_syscalls_unix; /* number of unix system calls */

int32_t pti_csw; /* number of context switches */

int32_t pti_threadnum; /* number of threads in the task */

int32_t pti_numrunning; /* number of running threads */

int32_t pti_priority; /* task priority*/

};

//bsd_kern.cvoid

fill_taskprocinfo(task_t task, struct proc_taskinfo_internal * ptinfo) //helin{

vm_map_t map;

task_absolutetime_info_data_t tinfo;

thread_t thread;

uint32_t cswitch = 0, numrunning = 0;

uint32_t syscalls_unix = 0;

uint32_t syscalls_mach = 0;

task_lock(task);

map = (task == kernel_task)? kernel_map: task->map;

ptinfo->pti_virtual_size = map->size;

ptinfo->pti_resident_size =

(mach_vm_size_t)(pmap_resident_count(map->pmap))

* PAGE_SIZE_64;

ptinfo->pti_policy = ((task != kernel_task)?

POLICY_TIMESHARE: POLICY_RR);

tinfo.threads_user = tinfo.threads_system = 0;

//task总的utime 和 stimetinfo.total_user = task->total_user_time;

tinfo.total_system = task->total_system_time;

//遍历线程queue_iterate(&task->threads, thread, thread_t, task_threads) {

uint64_t tval;

spl_t x;

if (thread->options & TH_OPT_IDLE_THREAD)

continue;

x = splsched();

thread_lock(thread);

if ((thread->state & TH_RUN) == TH_RUN)

numrunning++;

cswitch += thread->c_switch; //上下文切换

//各个线程的utime和stimetval = timer_grab(&thread->user_timer);

tinfo.threads_user += tval;

tinfo.total_user += tval;

tval = timer_grab(&thread->system_timer);

if (thread->precise_user_kernel_time) {

tinfo.threads_system += tval;

tinfo.total_system += tval;

} else {

/* system_timer may represent either sys or user */

tinfo.threads_user += tval;

tinfo.total_user += tval;

}

syscalls_unix += thread->syscalls_unix;

syscalls_mach += thread->syscalls_mach;

thread_unlock(thread);

splx(x);

}

//task累计时间, 耗时统计使用该2项:ptinfo->pti_total_system = tinfo.total_system;

ptinfo->pti_total_user = tinfo.total_user;

//内核态或用户态线程单独时间ptinfo->pti_threads_system = tinfo.threads_system;

ptinfo->pti_threads_user = tinfo.threads_user;

ptinfo->pti_faults = task->faults;

ptinfo->pti_pageins = task->pageins;

ptinfo->pti_cow_faults = task->cow_faults;

ptinfo->pti_messages_sent = task->messages_sent;

ptinfo->pti_messages_received = task->messages_received;

ptinfo->pti_syscalls_mach = task->syscalls_mach + syscalls_mach;

ptinfo->pti_syscalls_unix = task->syscalls_unix + syscalls_unix;

ptinfo->pti_csw = task->c_switch + cswitch; //上下文切换ptinfo->pti_threadnum = task->thread_count;

ptinfo->pti_numrunning = numrunning;

ptinfo->pti_priority = task->priority;

task_unlock(task);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值