/* ** remove all remaining entries in RESIDUE-list */ int pdb_cleanresidue(void) { register struct pinfo *pr; register int pid; char isproc; /* ** start at RESIDUE-list anchor and delete all entries */ pr = presidue.prnext; while (pr != &presidue) { pid = pr->tstat.gen.pid; isproc = pr->tstat.gen.isproc; pr = pr->prnext; /* MUST be done before deletion */ pdb_deltask(pid, isproc); } return 1; }
/* ** calculate the process-activity during the last sample */ int deviattask(struct tstat *curtpres, int ntaskpres, struct tstat *curpexit, int nprocexit, int deviatonly, struct tstat *devtstat, struct sstat *devsstat, unsigned int *nprocdev, int *nprocpres, int *totrun, int *totslpi, int *totslpu, int *totzombie) { register int c, d; register struct tstat *curstat, *devstat, *procstat = 0; struct tstat prestat; struct pinfo *pinfo; count_t totusedcpu; char procsaved = 1, hashtype = 'p'; /* ** needed for sanity check later on... */ totusedcpu = devsstat->cpu.all.stime + devsstat->cpu.all.utime + devsstat->cpu.all.ntime + devsstat->cpu.all.itime + devsstat->cpu.all.wtime + devsstat->cpu.all.Itime + devsstat->cpu.all.Stime + devsstat->cpu.all.steal; /* ** make new list of all tasks in the process-database; ** after handling all processes, the left-overs are processes ** that have disappeared since the previous sample */ pdb_makeresidue(); /* ** calculate deviations per present process */ *nprocpres = *totrun = *totslpi = *totslpu = *totzombie = 0; for (c=0, d=0, *nprocdev=0; c < ntaskpres; c++) { char newtask = 0; curstat = curtpres+c; if (curstat->gen.isproc) { (*nprocpres)++; if (curstat->gen.state == 'Z') { (*totzombie)++; } else { *totrun += curstat->gen.nthrrun; *totslpi += curstat->gen.nthrslpi; *totslpu += curstat->gen.nthrslpu; } } /* ** get previous figures from task-database */ if ( pdb_gettask(curstat->gen.pid, curstat->gen.isproc, curstat->gen.btime, &pinfo)) { /* ** task already present during the previous sample; ** if no differences with previous sample, skip task ** unless all tasks have to be shown ** ** it might be that a process appears to have no ** differences while one its threads has differences; ** than the process will be inserted as well */ if (deviatonly && memcmp(curstat, &pinfo->tstat, sizeof(struct tstat)) == EQ) { /* remember last unsaved process */ if (curstat->gen.isproc) { procstat = curstat; procsaved = 0; } continue; } /* ** differences detected, so the task was active, ** or its status or memory-occupation has changed; ** save stats from previous sample (to use for ** further calculations) and store new statistics ** in task-database */ prestat = pinfo->tstat; /* save old */ pinfo->tstat = *curstat; /* overwrite */ } else { /* ** new task which must have been started during ** last interval */ memset(&prestat, 0, sizeof(prestat)); /* ** create new task struct */ pinfo = calloc(1, sizeof(struct pinfo)); ptrverify(pinfo, "Malloc failed for new pinfo\n"); pinfo->tstat = *curstat; /* ** add new task to task-database */ pdb_addtask(curstat->gen.pid, pinfo); newtask = 1; } /* ** active task found; do the difference calculations */ if (curstat->gen.isproc) { procsaved = 1; (*nprocdev)++; } else { /* ** active thread: check if related process registered */ if (!procsaved) { devstat = devtstat+d; calcdiff(devstat, procstat, procstat, 0, totusedcpu); procsaved = 1; (*nprocdev)++; d++; } } devstat = devtstat+d; calcdiff(devstat, curstat, &prestat, newtask, totusedcpu); d++; } /* ** calculate deviations per exited process */ if (nprocexit > 0 && supportflags&NETATOPD) { if (curpexit->gen.pid) hashtype = 'p'; else hashtype = 'b'; netatop_exithash(hashtype); } for (c=0; c < nprocexit; c++) { /* ** check if this process has been started AND ** finished since previous sample; ** if so, it has no use to check if there is still ** existing info present in the process-database */ curstat = curpexit+c; if (curstat->gen.pid) /* acctrecord contains pid? */ { if ( pdb_gettask(curstat->gen.pid, 1, curstat->gen.btime, &pinfo)) prestat = pinfo->tstat; else memset(&prestat, 0, sizeof(prestat)); } else { if ( curstat->gen.btime > pretime.tv_sec ) { /* ** process-start and -finish in same interval */ memset(&prestat, 0, sizeof(prestat)); } else { /* ** process must be known in process-database; ** try to match one of the remaining processes ** against this exited one */ if ( pdb_srchresidue(curstat, &pinfo) ) prestat = pinfo->tstat; else memset(&prestat, 0, sizeof(prestat)); } } /* ** now do the calculations */ devstat = devtstat+d; memset(devstat, 0, sizeof *devstat); devstat->gen = curstat->gen; if ( curstat->gen.pid == 0 ) devstat->gen.pid = prestat.gen.pid; if (!prestat.gen.pid) devstat->gen.excode |= ~(INT_MAX); strcpy(devstat->gen.cmdline, prestat.gen.cmdline); /* ** due to the strange exponent-type storage of values ** in the process accounting record, the resource-value ** in the exit-record might have been smaller than the ** stored value of the last registered sample; in that ** case the deviation should be set to zero */ if (curstat->cpu.stime > prestat.cpu.stime) devstat->cpu.stime = curstat->cpu.stime - prestat.cpu.stime; if (curstat->cpu.utime > prestat.cpu.utime) devstat->cpu.utime = curstat->cpu.utime - prestat.cpu.utime; if (curstat->mem.minflt > prestat.mem.minflt) devstat->mem.minflt = curstat->mem.minflt - prestat.mem.minflt; if (curstat->mem.majflt > prestat.mem.majflt) devstat->mem.majflt = curstat->mem.majflt - prestat.mem.majflt; if (curstat->dsk.rio > (prestat.dsk.rio + prestat.dsk.wio)) devstat->dsk.rio = curstat->dsk.rio - prestat.dsk.rio - prestat.dsk.wio; /* ** try to match the network counters of netatop */ if (supportflags & NETATOPD) { unsigned long val = (hashtype == 'p' ? curstat->gen.pid : curstat->gen.btime); netatop_exitfind(val, devstat, &prestat); } d++; (*nprocdev)++; if (prestat.gen.pid > 0) pdb_deltask(prestat.gen.pid, prestat.gen.isproc); } /* ** remove unused entries from RESIDUE chain */ pdb_cleanresidue(); return d; }