进程使用的时间CPU

Eri*_*tto 6 macos cocoa process objective-c

我已设法在此列表上实现代码,以获取所有正在运行的进程及其ID的列表.我现在需要的是提取每个进程使用CPU的时间.

我已经尝试过引用代码中的键,但是当我尝试打印'Ticks of CPU Time'时,我得到了所有进程的零值.另外,即使我确实得到了一个值,我也不确定"Ticks of CPU Time"是否正是我正在寻找的.

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 */
Run Code Online (Sandbox Code Playgroud)

事实上,我也尝试打印时间平均值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.\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];
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

编辑:我刚刚为这个问题提供了赏金.我特别想要的是获得每个进程在CPU中花费的时间.

除此之外,如果您可以为进程使用%CPU,那将是非常棒的.

代码应该是最佳的,因为它将每秒调用一次,并且将在所有正在运行的进程上调用该方法.Objective-C更可取.

再次感谢!

编辑2

此外,任何关于为什么人们忽略这个问题的评论也会有所帮助:)

Kae*_*ure 4

查看libtop.c的 Darwin 源代码,特别是 libtop_pinfo_update_cpu_usage() 函数。注意:

\n\n
    \n
  1. 您需要对 Mach 编程基础有基本的了解才能理解此代码,因为它使用任务端口等。
  2. \n
  3. 如果您只想使用 libtop,则必须下载源代码并自行编译。
  4. \n
  5. 您的进程将需要权限才能访问其他进程的任务端口。
  6. \n
\n\n

如果这一切听起来相当令人畏惧,那么 \xe2\x80\xa6 有一种方法可以使用不太深奥的 API:只需生成一个顶级进程并解析其标准输出。快速浏览一下 top(1) 手册页就会发现这个小宝石:

\n\n
$ top -s 1 -l 3600 -stats pid,cpu,time\n
Run Code Online (Sandbox Code Playgroud)\n\n

即每秒采样一次,持续 3600 秒(一小时),仅将 pid、cpu 使用率和时间的统计信息以日志形式输出到 stdout。

\n\n

生成和管理子顶级进程,然后解析其输出都是简单的 Unix 编程练习。

\n