/** * Fill data in the process tree by recusively walking through it * @param pt process tree * @param i process index * @return TRUE if succeeded otherwise FALSE. */ void fillprocesstree(ProcessTree_T *pt, int index) { int i; ProcessTree_T *parent_pt; ASSERT(pt); if (pt[index].visited == 1) return; pt[index].visited = 1; pt[index].children_sum = pt[index].children_num; pt[index].mem_kbyte_sum = pt[index].mem_kbyte; pt[index].cpu_percent_sum = pt[index].cpu_percent; for (i = 0; i < pt[index].children_num; i++) fillprocesstree(pt, pt[index].children[i]); if (pt[index].parent != -1 && pt[index].parent != index) { parent_pt = &pt[pt[index].parent]; parent_pt->children_sum += pt[index].children_sum; parent_pt->mem_kbyte_sum += pt[index].mem_kbyte_sum; parent_pt->cpu_percent_sum += pt[index].cpu_percent_sum; parent_pt->cpu_percent_sum = (pt[index].cpu_percent_sum > 1000) ? 1000 : parent_pt->cpu_percent_sum; } }
/** * Initialize the process tree * @return treesize >= 0 if succeeded otherwise < 0 */ int initprocesstree(ProcessTree_T **pt_r, int *size_r, ProcessTree_T **oldpt_r, int *oldsize_r) { int i; int oldentry; ProcessTree_T *pt; ProcessTree_T *oldpt; int root = -1; if (*pt_r != NULL) { if (oldpt_r && *oldpt_r != NULL) delprocesstree(oldpt_r, oldsize_r); *oldpt_r = *pt_r; *oldsize_r = *size_r; } if ((*size_r = initprocesstree_sysdep(pt_r)) <= 0) { DEBUG("system statistic error -- cannot initialize the process tree => process resource monitoring disabled\n"); Run.doprocess = FALSE; return -1; } else if (Run.doprocess == FALSE) { DEBUG("system statistic -- initialization of the process tree succeeded => process resource monitoring enabled\n"); Run.doprocess = TRUE; } pt = *pt_r; oldpt = *oldpt_r; if (pt == NULL) return 0; for (i = 0; i < (volatile int)*size_r; i ++) { if (oldpt && ((oldentry = findprocess(pt[i].pid, oldpt, *oldsize_r)) != -1)) { pt[i].cputime_prev = oldpt[oldentry].cputime; pt[i].time_prev = oldpt[oldentry].time; /* The cpu_percent may be set already (for example by HPUX module) */ if (pt[i].cpu_percent == 0 && pt[i].cputime_prev != 0 && pt[i].cputime != 0 && pt[i].cputime > pt[i].cputime_prev) { pt[i].cpu_percent = (int)((1000 * (double)(pt[i].cputime - pt[i].cputime_prev) / (pt[i].time - pt[i].time_prev)) / systeminfo.cpus); if (pt[i].cpu_percent > 1000 / systeminfo.cpus) pt[i].cpu_percent = 1000 / systeminfo.cpus; } } else { pt[i].cputime_prev = 0; pt[i].time_prev = 0.0; pt[i].cpu_percent = 0; } if (pt[i].pid == pt[i].ppid) { pt[i].parent = i; continue; } if ((pt[i].parent = findprocess(pt[i].ppid, pt, *size_r)) == -1) { /* Parent process wasn't found - on Linux this is normal: main process with PID 0 is not listed, similarly in FreeBSD jail. * We create virtual process entry for missing parent so we can have full tree-like structure with root. */ int j = (*size_r)++; pt = *pt_r = xresize(*pt_r, *size_r * sizeof(ProcessTree_T)); memset(&pt[j], 0, sizeof(ProcessTree_T)); pt[j].ppid = pt[j].pid = pt[i].ppid; pt[i].parent = j; } if (! connectchild(pt, pt[i].parent, i)) { /* connection to parent process has failed, this is usually caused in the part above */ DEBUG("system statistic error -- cannot connect process id %d to its parent %d\n", pt[i].pid, pt[i].ppid); pt[i].pid = 0; continue; } } /* The main process in Solaris zones and FreeBSD host doesn't have pid 1, so try to find process which is parent of itself */ for (i = 0; i < *size_r; i++) { if (pt[i].pid == pt[i].ppid) { root = i; break; } } if (root == -1) { DEBUG("system statistic error -- cannot find root process id\n"); return -1; } fillprocesstree(pt, root); update_system_load(*pt_r, *size_r); return *size_r; }