我是靠谱客的博主 开朗灰狼,最近开发中收集的这篇文章主要介绍cpu使用率 htop显示_以htop为例 看怎么做Android iOS app cpu使用率监控,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#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);

}

最后

以上就是开朗灰狼为你收集整理的cpu使用率 htop显示_以htop为例 看怎么做Android iOS app cpu使用率监控的全部内容,希望文章能够帮你解决cpu使用率 htop显示_以htop为例 看怎么做Android iOS app cpu使用率监控所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(100)

评论列表共有 0 条评论

立即
投稿
返回
顶部