int get_process_info_sysdep(ProcInfo_T p) { int nproc; int i,rv; struct userinfo user; memset(&user,0,sizeof(struct userinfo)); nproc=getproc(procs,NPROCS,sizeof(struct procinfo)); for(i=0;i<nproc;i++) { if(p->pid==procs[i].pi_pid) { if(procs[i].pi_stat==SZOMB) { empty: p->cputime_prev= p->cputime = 0; p->mem_kbyte= 0; p->mem_percent= 0.0; p->status_flag |= PROCESS_ZOMBIE; } else { rv=getuser(&(procs[i]),sizeof(struct procinfo), &user,sizeof(struct userinfo)); if(rv== -1) { goto empty; } p->mem_percent = user.ui_prm*10; p->mem_kbyte = (user.ui_drss+user.ui_trss)*4; p->cputime_prev= p->cputime; p->cputime= ( timestruc_to_tseconds(user.ui_ru.ru_utime) + timestruc_to_tseconds(user.ui_ru.ru_stime)); if( include_children ) { p->cputime+= ( timestruc_to_tseconds(user.ui_cru.ru_utime)+ timestruc_to_tseconds(user.ui_cru.ru_stime)); } /* first run ? */ if ( p->time_prev == 0.0 ) { p->cputime_prev= p->cputime; } } return TRUE; } } return FALSE; }
/** * Read all processes of the proc files system to initialize * the process tree (sysdep version... but should work for * all procfs based unices) * @param reference reference of ProcessTree * @return treesize>0 if succeeded otherwise =0. */ int initprocesstree_sysdep(ProcessTree_T ** reference) { int i; int rv; int pid; int treesize; char buf[4096]; glob_t globbuf; pstatus_t pstatus; psinfo_t *psinfo = (psinfo_t *)&buf; ProcessTree_T *pt; ASSERT(reference); /* Find all processes in the /proc directory */ if ((rv = glob("/proc/[0-9]*", NULL, NULL, &globbuf)) != 0) { LogError("system statistic error -- glob failed: %d (%s)\n", rv, STRERROR); return 0; } treesize = globbuf.gl_pathc; /* Allocate the tree */ pt = xcalloc(sizeof(ProcessTree_T), treesize); /* Insert data from /proc directory */ for (i = 0; i < treesize; i++) { pid = atoi(globbuf.gl_pathv[i] + strlen("/proc/")); pt[i].pid = pid; /* get the actual time */ pt[i].time = get_float_time(); if (! read_proc_file(buf, sizeof(buf), "psinfo", pt[i].pid, NULL)) { pt[i].cputime = 0; pt[i].cpu_percent = 0; pt[i].mem_kbyte = 0; continue; } pt[i].ppid = psinfo->pr_ppid; pt[i].starttime = psinfo->pr_start.tv_sec; /* If we don't have any light-weight processes (LWP) then we are definitely a zombie */ if (psinfo->pr_nlwp == 0) { pt[i].status_flag = PROCESS_ZOMBIE; pt[i].cputime = 0; pt[i].cpu_percent = 0; pt[i].mem_kbyte = 0; continue; } pt[i].mem_kbyte = psinfo->pr_rssize; pt[i].cmdline = xstrdup(psinfo->pr_psargs); if (! pt[i].cmdline || ! *pt[i].cmdline) pt[i].cmdline = xstrdup(psinfo->pr_fname); if (! read_proc_file(buf, sizeof(buf), "status", pt[i].pid, NULL)) { pt[i].cputime = 0; pt[i].cpu_percent = 0; } else { memcpy(&pstatus, buf, sizeof(pstatus_t)); pt[i].cputime = (timestruc_to_tseconds(pstatus.pr_utime) + timestruc_to_tseconds(pstatus.pr_stime)); pt[i].cpu_percent = 0; } } *reference = pt; /* Free globbing buffer */ globfree(&globbuf); return treesize; }