本文介绍了时间进程使用的CPU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我已设法在此列表中实施代码获取所有正在运行的进程及其ID的列表。我现在需要的是提取每个进程使用CPU多少时间。 我已经尝试引用代码中的键,但是当我尝试打印Ticks of CPU Time时,所有进程的值都为零。此外,即使我没有得到一个值,我不知道如果'Ticks的CPU时间'是我正在寻找的。 struct vmspace * p_vmspace; / *地址空间。 * / struct sigacts * p_sigacts; / *信号动作,状态(PROC ONLY)。 * / int p_flag; / * P_ * flags。 * / char p_stat; / * S *进程状态。 * / pid_t p_pid; / *进程标识符。 * / pid_t p_oppid; / *在ptrace期间保存父pid。 XXX * / int p_dupfd; / *从fdopen横向返回值。 XXX * / / * Mach相关* / caddr_t user_stack; / * where user stack was allocated * / void * exit_thread; / * XXX哪个线程退出? * / int p_debugger; / * allow to debug * / boolean_t sigwait; / *挂起指示* / / *调度* / u_int p_estcpu; / * p_cpticks的时间平均值。 * / int p_cpticks; / * cpu时间戳。 * / fixpt_t p_pctcpu; / *%cpu,用于p_swtime期间的此进程* / void * p_wchan; / *休眠地址。 * / char * p_wmesg; / *睡眠原因。 * / u_int p_swtime; / *时间交换或交换。 * / u_int p_slptime; / *自上次阻塞以来的时间。 * / struct itimerval p_realtimer; / *报警定时器。 * / struct timeval p_rtime; /* 即时的。 * / u_quad_t p_uticks; / * Statclock在用户模式下命中。 * / u_quad_t p_sticks; / *系统模式下的时钟命中。 * / u_quad_t p_iticks; / * Statclock hits processing intr。 * / int p_traceflag; / *内核跟踪点。 * / struct vnode * p_tracep; / *跟踪到vnode。 * / int p_siglist; / * DEPRECATED * / struct vnode * p_textvp; / *可执行文件的Vnode。 * / int p_holdcnt; / *如果非零,不交换。 * / sigset_t p_sigmask; / * DEPRECATED。 * / sigset_t p_sigignore; / *忽略信号。 * / sigset_t p_sigcatch; / *用户捕获的信号。 * / u_char p_priority; / *进程优先级。 * / u_char p_usrpri; / *基于p_cpu和p_nice的用户优先级。 * / char p_nice; / *处理nice值。 * / char p_comm [MAXCOMLEN + 1]; struct pgrp * p_pgrp; / *指向进程组的指针。 * / struct user * p_addr; / * u-area的内核虚拟地址(PROC ONLY)。 * / u_short p_xstat; / *退出状态等待;也停止信号。 * / u_short p_acflag; / *会计标志。 * / struct rusage * p_ru; / *退出信息。 XXX * / 其实我也尝试打印p_cpticks和其他几个人的时间平均值并从来没有有趣的价值观。这是我的代码,它是打印检索的信息(我从cocoabuilder.com获得): - (NSDictionary *)getProcessList { NSMutableDictionary * ProcList = [[NSMutableDictionary alloc] init]; kinfo_proc * mylist; size_t mycount = 0; mylist =(kinfo_proc *)malloc(sizeof(kinfo_proc)); GetBSDProcessList(& mylist,& mycount); printf(There are%d processes.\\\,(int)mycount); NSLog(@= = = = = = = = = = = = = = =); int k; for(k = 0; k kinfo_proc * proc = NULL; proc =& mylist [k]; // NSString * processName = [NSString stringWithFormat:@%s,proc-> kp_proc.p_comm]; // [ProcList setObject:processName forKey:processName]; // [ProcList setObject:proc-> kp_proc.p_pid forKey:processName]; // printf(ID:%d - NAME:%s\\\,proc-> kp_proc.p_pid,proc-> kp_proc.p_comm); printf(ID:%d - NAME:%s CPU TIME:%d \\\,proc-> kp_proc.p_pid,proc-> kp_proc.p_comm,proc-> kp_proc.p_pid) ; //右键单击p_comm并选择跳转到定义以查找其他值。 } free(mylist); return [ProcList autorelease]; } 谢谢! EDIT :我刚刚为此问题提供了奖励。我正在寻找的是获得每个进程花在CPU中的时间量。 如果,除此之外,你可以给%CPU使用一个过程,这将是太棒了。 代码应该是最优的,因为它将被调用每秒,该方法将在所有运行的进程上被调用。 Objective-C首选。 再次感谢! 编辑2 此外,问题也很有帮助:)解决方案查看Darwin的 libtop.c ,特别是libtop_pinfo_update_cpu_usage()函数。请注意: 您需要对Mach编程基础知识有一个基本了解,才能理解此代码,因为它使用任务端口等 如果您只想使用libtop,则必须下载源代码并自行编译。 您的进程需要权限 如果这一切听起来相当令人生畏,那么...有... 一种使用较少秘密的API的方法:只产生一个顶级进程并解析其标准输出。快速浏览顶部(1)man页面打开了这个小宝石: $ top -s 1 -l 3600 - stats pid,cpu,time 也就是说,每秒采样3600秒并以日志形式只输出pid,cpu用法和时间的统计信息输出到stdout。 生成和管理子进程,然后解析其输出都是直接的编程练习。 I've managed to implement the code on this listing to get a list of all the processes running and their IDs. What I need now is to extract how much time each process uses the CPU.I've tried referring to the keys in the code, but when I try to print 'Ticks of CPU Time' I get a zero value for all of the processes. Plus, even if I did get a value I'm not sure if 'Ticks of CPU Time' is exactly what I'm looking for. struct vmspace *p_vmspace; /* Address space. */struct sigacts *p_sigacts; /* Signal actions, state (PROC ONLY). */int p_flag; /* P_* flags. */char p_stat; /* S* process status. */pid_t p_pid; /* Process identifier. */pid_t p_oppid; /* Save parent pid during ptrace. XXX */int p_dupfd; /* Sideways return value from fdopen. XXX *//* Mach related */caddr_t user_stack; /* where user stack was allocated */void *exit_thread; /* XXX Which thread is exiting? */int p_debugger; /* allow to debug */boolean_t sigwait; /* indication to suspend *//* scheduling */u_int p_estcpu; /* Time averaged value of p_cpticks. */int p_cpticks; /* Ticks of cpu time. */fixpt_t p_pctcpu; /* %cpu for this process during p_swtime */void *p_wchan; /* Sleep address. */char *p_wmesg; /* Reason for sleep. */u_int p_swtime; /* Time swapped in or out. */u_int p_slptime; /* Time since last blocked. */struct itimerval p_realtimer; /* Alarm timer. */struct timeval p_rtime; /* Real time. */u_quad_t p_uticks; /* Statclock hits in user mode. */u_quad_t p_sticks; /* Statclock hits in system mode. */u_quad_t p_iticks; /* Statclock hits processing intr. */int p_traceflag; /* Kernel trace points. */struct vnode *p_tracep; /* Trace to vnode. */int p_siglist; /* DEPRECATED */struct vnode *p_textvp; /* Vnode of executable. */int p_holdcnt; /* If non-zero, don't swap. */sigset_t p_sigmask; /* DEPRECATED. */sigset_t p_sigignore; /* Signals being ignored. */sigset_t p_sigcatch; /* Signals being caught by user. */u_char p_priority; /* Process priority. */u_char p_usrpri; /* User-priority based on p_cpu and p_nice. */char p_nice; /* Process "nice" value. */char p_comm[MAXCOMLEN+1];struct pgrp *p_pgrp; /* Pointer to process group. */struct user *p_addr; /* Kernel virtual addr of u-area (PROC ONLY). */u_short p_xstat; /* Exit status for wait; also stop signal. */u_short p_acflag; /* Accounting flags. */struct rusage *p_ru; /* Exit information. XXX */In fact I've also tried to print Time averaged value of p_cpticks and a few others and never got interesting values. Here is my code which is printing the information retrieved (I got it from cocoabuilder.com) :- (NSDictionary *) getProcessList { NSMutableDictionary *ProcList = [[NSMutableDictionary alloc] init]; kinfo_proc *mylist; size_t mycount = 0; mylist = (kinfo_proc *)malloc(sizeof(kinfo_proc)); GetBSDProcessList(&mylist, &mycount); printf("There are %d processes.\n", (int)mycount);NSLog(@" = = = = = = = = = = = = = = ="); int k; for(k = 0; k < mycount; k++) { kinfo_proc *proc = NULL; proc = &mylist[k]; // NSString *processName = [NSString stringWithFormat: @"%s",proc->kp_proc.p_comm]; //[ ProcList setObject: processName forKey: processName ]; // [ ProcList setObject: proc->kp_proc.p_pid forKey: processName]; // printf("ID: %d - NAME: %s\n", proc->kp_proc.p_pid, proc->kp_proc.p_comm); printf("ID: %d - NAME: %s CPU TIME: %d \n", proc->kp_proc.p_pid, proc->kp_proc.p_comm, proc->kp_proc.p_pid ); // Right click on p_comm and select 'jump to definition' to find other values. } free(mylist); return [ProcList autorelease];}Thanks!EDIT: I've just offered a bounty for this question. What I'm looking for specifically is to get the amount of time each process spends in CPU.If, in addition to this, you can give %CPU being used by a process, that would be fantastic.The code should be optimal in that it will be called every second and the method will be called on all running processes. Objective-C preferable.Thanks again!EDIT 2Also, any comments as to why people are ignoring this question would also be helpful :) 解决方案 Have a look at the Darwin source for libtop.c and particularly the libtop_pinfo_update_cpu_usage() function. Note that:You'll need a basic understanding of Mach programming fundamentals to make sense of this code, as it uses task ports, etc.If you want to simply use libtop, you'll have to download the source and compile it yourself.Your process will need privileges to get at the task ports for other processes.If all this sounds rather daunting, well… There is a way that uses less esoteric APIs: Just spawn a top process and parse its standard output. A quick glimpse over the top(1) man page turned up this little gem:$ top -s 1 -l 3600 -stats pid,cpu,timeThat is, sample once per second for 3600 seconds (one hour), and output to stdout in log form only the statistics for pid, cpu usage, and time.Spawning and managing the child top process and then parsing its output are all straightforward Unix programming exercises. 这篇关于时间进程使用的CPU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-28 06:01