CPU time used by the process

I managed to implement the code on this listing to get a list of all running processes and their identifiers. Now I need to find out how long each processor uses the processor.

I tried to refer to the keys in the code, but when I try to print "Ticks of CPU Time", I get a zero value for all processes. Plus, even if I got the value, I'm not sure that "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 also tried to print the average time value of p_cpticks and several others and never got interesting values. Here is my code that prints the received information (I received 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 just offered generosity for this question. What I am looking for specifically is the time spent by each process on the processor.

If in addition to this you can give% CPU used by the process, it will be fantastic.

The code should be optimal, because it will be called every second, and the method will be called for all running processes. Objective-C is preferred.

Thanks again!

EDIT 2

Also, any comments regarding why people ignore this question will also be helpful :)

+6
objective-c process cocoa macos
source share
2 answers

Check out Darwin's source on libtop.c and, in particular, the libtop_pinfo_update_cpu_usage () function. Note:

  • You will need a basic understanding of the basics of Mach programming in order to understand this code as it uses task ports, etc.
  • If you just want to use libtop, you will have to download the source code and compile it yourself.
  • Your process will require privileges to access task ports for other processes.

If all this sounds pretty complicated, well ... There is a way that uses less esoteric APIs: just create the top process and parse its standard output. A quick look at the top (1) page of man raised this little stone:

 $ top -s 1 -l 3600 -stats pid,cpu,time 

That is, the sample once per second for 3600 seconds (one hour) and output to stdout in the form of a log only statistics for pid, CPU usage and time.

Creating and managing a child process and then analyzing its output are simple Unix programming exercises.

+3
source share

Have you looked at the rusage structure? You listed it and commented on it as "Exiting Information", but I know that it contains the resources actually used by the process. Check out this page . I remember that I used getrusage() to calculate the exact amount of processor time used in my scientific calculation for my current process, so you just need to know how to query this structure for each process in your list. I think

+1
source share

All Articles