Skip to content

Commit

Permalink
Fix for issue Atoptool#261: Offline CPUs not handled well.
Browse files Browse the repository at this point in the history
  • Loading branch information
githubbie committed Mar 24, 2024
1 parent eec97d5 commit 505c4bd
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 128 deletions.
5 changes: 4 additions & 1 deletion atopsar.c
Original file line number Diff line number Diff line change
Expand Up @@ -1273,8 +1273,10 @@ cpuline(struct sstat *ss, struct tstat *ts, struct tstat **ps, int nactproc,
*/
if (ss->cpu.nrcpu > 1)
{
for (i=0; i < ss->cpu.nrcpu; i++)
for (i=0; i <= ss->cpu.maxcpunr; i++)
{
if (ss->cpu.cpu[i].active)
{
cputot = ss->cpu.cpu[i].stime + ss->cpu.cpu[i].utime +
ss->cpu.cpu[i].ntime + ss->cpu.cpu[i].itime +
ss->cpu.cpu[i].wtime + ss->cpu.cpu[i].Itime +
Expand Down Expand Up @@ -1310,6 +1312,7 @@ cpuline(struct sstat *ss, struct tstat *ts, struct tstat **ps, int nactproc,
postprint(badness);

nlines++;
}
}
}

Expand Down
22 changes: 13 additions & 9 deletions cgroups.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,10 @@ walkcgroup(char *dirname, struct cgchainer *cparent, int parentseq,

closedir(dirp);

chdir("..");
// return to original directory
//
if ( chdir("..") == -1)
return -1;

ccp->cstat->gen.procsbelow = procsbelow;

Expand Down Expand Up @@ -671,15 +674,16 @@ gettotpressure(char *fname)

if ( (fp = fopen(fname, "r")) != NULL)
{
fgets(linebuf, sizeof(linebuf), fp); // skip 'some' line

// only handle second line: 'total' pressure
//
if ( fgets(linebuf, sizeof(linebuf), fp) != NULL)
if ( fgets(linebuf, sizeof(linebuf), fp) != NULL) // skip 'some' line
{
sscanf(linebuf, psiformat,
&psitype, &a10, &a60, &a300, &totpressure);
}
// only handle second line: 'total' pressure
//
if ( fgets(linebuf, sizeof(linebuf), fp) != NULL)
{
sscanf(linebuf, psiformat,
&psitype, &a10, &a60, &a300, &totpressure);
}
}

fclose(fp);
}
Expand Down
14 changes: 12 additions & 2 deletions deviate.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ deviatsyst(struct sstat *cur, struct sstat *pre, struct sstat *dev,
struct ifprop ifprop;

dev->cpu.nrcpu = cur->cpu.nrcpu;
dev->cpu.maxcpunr = cur->cpu.maxcpunr;
dev->cpu.devint = subcount(cur->cpu.devint, pre->cpu.devint);
dev->cpu.csw = subcount(cur->cpu.csw, pre->cpu.csw);
dev->cpu.nprocs = subcount(cur->cpu.nprocs, pre->cpu.nprocs);
Expand All @@ -651,11 +652,14 @@ deviatsyst(struct sstat *cur, struct sstat *pre, struct sstat *dev,
dev->cpu.all.instr = subcount(cur->cpu.all.instr, pre->cpu.all.instr);
dev->cpu.all.cycle = subcount(cur->cpu.all.cycle, pre->cpu.all.cycle);

for (i=0; i < dev->cpu.nrcpu; i++)
for (i=0; i <= dev->cpu.maxcpunr; i++)
{
if (cur->cpu.cpu[i].active)
{
count_t ticks;

dev->cpu.cpu[i].cpunr = cur->cpu.cpu[i].cpunr;
dev->cpu.cpu[i].active= cur->cpu.cpu[i].active;
dev->cpu.cpu[i].stime = subcount(cur->cpu.cpu[i].stime,
pre->cpu.cpu[i].stime);
dev->cpu.cpu[i].utime = subcount(cur->cpu.cpu[i].utime,
Expand Down Expand Up @@ -694,6 +698,7 @@ deviatsyst(struct sstat *cur, struct sstat *pre, struct sstat *dev,
subcount(cur->cpu.cpu[i].freqcnt.ticks,
pre->cpu.cpu[i].freqcnt.ticks)
: cur->cpu.cpu[i].freqcnt.ticks;
}
}

dev->cpu.lavg1 = cur->cpu.lavg1;
Expand Down Expand Up @@ -1502,6 +1507,7 @@ totalsyst(char category, struct sstat *new, struct sstat *tot)
{
case 'c': /* accumulate cpu-related counters */
tot->cpu.nrcpu = new->cpu.nrcpu;
tot->cpu.maxcpunr = new->cpu.maxcpunr;
tot->cpu.devint += new->cpu.devint;
tot->cpu.csw += new->cpu.csw;
tot->cpu.nprocs += new->cpu.nprocs;
Expand All @@ -1522,9 +1528,12 @@ totalsyst(char category, struct sstat *new, struct sstat *tot)
}
else
{
for (i=0; i < new->cpu.nrcpu; i++)
for (i=0; i <= new->cpu.maxcpunr; i++)
{
if (new->cpu.cpu[i].active)
{
tot->cpu.cpu[i].cpunr = new->cpu.cpu[i].cpunr;
tot->cpu.cpu[i].active = new->cpu.cpu[i].active;
tot->cpu.cpu[i].stime += new->cpu.cpu[i].stime;
tot->cpu.cpu[i].utime += new->cpu.cpu[i].utime;
tot->cpu.cpu[i].ntime += new->cpu.cpu[i].ntime;
Expand All @@ -1534,6 +1543,7 @@ totalsyst(char category, struct sstat *new, struct sstat *tot)
tot->cpu.cpu[i].Stime += new->cpu.cpu[i].Stime;
tot->cpu.cpu[i].steal += new->cpu.cpu[i].steal;
tot->cpu.cpu[i].guest += new->cpu.cpu[i].guest;
}
}
}

Expand Down
74 changes: 46 additions & 28 deletions drawbar.c
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ do_cpubars(struct sstat *sstat, int nsecs, char initlabels, char mono)
static struct vertval *vertvals;

count_t alltics;
int i;
int i, j;
char buf[16];

// check if the number of CPUs has been changed since
Expand All @@ -551,7 +551,7 @@ do_cpubars(struct sstat *sstat, int nsecs, char initlabels, char mono)

numlabs = numcpus > 1 ? numcpus + 1 : 1;

labellen = snprintf(buf, sizeof buf, "%d", numcpus);
labellen = snprintf(buf, sizeof buf, "%lld", sstat->cpu.maxcpunr);

vertvals = malloc(numlabs * sizeof(struct vertval));
ptrverify(vertvals, "Malloc failed for %d vertval structs\n", numlabs);
Expand All @@ -562,23 +562,30 @@ do_cpubars(struct sstat *sstat, int nsecs, char initlabels, char mono)
// create new X axis labels
//
if (numcpus == 1)
vertvals->barlab = "0";
{
snprintf(vertvals->barlab, labellen+1, "%lld", sstat->cpu.maxcpunr);
}
else
{
vertvals->barlab = "Avg ";

for (i=0, p=labarea; i < numcpus; i++)
i=0;
for (j=0, p=labarea; j <= sstat->cpu.maxcpunr; j++)
{
if (sstat->cpu.cpu[j].active)
{
(vertvals+i+1)->barlab = p;
snprintf(p, labellen+1, "%-*d", labellen,
sstat->cpu.cpu[i].cpunr);
sstat->cpu.cpu[j].cpunr);
p += labellen+1;
i++;
}
}
}
}

// calculate overall busy percentage and
// fill first busy value (average)
// calculate overall busy percentage and fill
// first busy value (average or the only cpu)
//
alltics = sstat->cpu.all.stime +
sstat->cpu.all.utime +
Expand Down Expand Up @@ -634,24 +641,32 @@ do_cpubars(struct sstat *sstat, int nsecs, char initlabels, char mono)
//
if (numcpus > 1)
{
// total ticks during last interval for CPU 0
//
alltics = sstat->cpu.cpu[0].stime +
sstat->cpu.cpu[0].utime +
sstat->cpu.cpu[0].ntime +
sstat->cpu.cpu[0].itime +
sstat->cpu.cpu[0].wtime +
sstat->cpu.cpu[0].Itime +
sstat->cpu.cpu[0].Stime +
sstat->cpu.cpu[0].steal;
alltics = 0;

// busy percentage per CPU
//
for (i=0; i < numcpus; i++)
i=0;
for (j=0; j <= sstat->cpu.maxcpunr; j++)
{
if (sstat->cpu.cpu[j].active)
{
// total ticks during last interval for first
// active CPU
if (alltics == 0)
{
alltics = sstat->cpu.cpu[j].stime +
sstat->cpu.cpu[j].utime +
sstat->cpu.cpu[j].ntime +
sstat->cpu.cpu[j].itime +
sstat->cpu.cpu[j].wtime +
sstat->cpu.cpu[j].Itime +
sstat->cpu.cpu[j].Stime +
sstat->cpu.cpu[j].steal;
}

(vertvals+i+1)->barval =
100 - (sstat->cpu.cpu[i].itime +
sstat->cpu.cpu[i].wtime ) *100/alltics;
100 - (sstat->cpu.cpu[j].itime +
sstat->cpu.cpu[j].wtime ) *100/alltics;

if ((vertvals+i+1)->barval < 0)
(vertvals+i+1)->barval = 0;
Expand All @@ -663,38 +678,41 @@ do_cpubars(struct sstat *sstat, int nsecs, char initlabels, char mono)
(vertvals+i+1)->category[0].ccol = COLORCPUSYS;
(vertvals+i+1)->category[0].clab = 'S';
(vertvals+i+1)->category[0].cval =
sstat->cpu.cpu[i].stime * 100 /
sstat->cpu.cpu[j].stime * 100 /
alltics;

(vertvals+i+1)->category[1].ccol = COLORCPUUSR;
(vertvals+i+1)->category[1].clab = 'U';
(vertvals+i+1)->category[1].cval =
(sstat->cpu.cpu[i].utime +
sstat->cpu.cpu[i].ntime -
sstat->cpu.cpu[i].guest) *100/alltics;
(sstat->cpu.cpu[j].utime +
sstat->cpu.cpu[j].ntime -
sstat->cpu.cpu[j].guest) *100/alltics;

(vertvals+i+1)->category[2].ccol = COLORCPUIDLE;
(vertvals+i+1)->category[2].clab = 'I';
(vertvals+i+1)->category[2].cval =
(sstat->cpu.cpu[i].Stime +
sstat->cpu.cpu[i].Itime) *100/alltics;
(sstat->cpu.cpu[j].Stime +
sstat->cpu.cpu[j].Itime) *100/alltics;

(vertvals+i+1)->category[3].ccol = COLORCPUSTEAL;
(vertvals+i+1)->category[3].clab = 's';
(vertvals+i+1)->category[3].cval =
sstat->cpu.cpu[i].steal *100/alltics;
sstat->cpu.cpu[j].steal *100/alltics;

(vertvals+i+1)->category[4].ccol = COLORCPUGUEST;
(vertvals+i+1)->category[4].clab = 'G';
(vertvals+i+1)->category[4].cval =
sstat->cpu.cpu[i].guest *100/alltics;
sstat->cpu.cpu[j].guest *100/alltics;

(vertvals+i+1)->numcat = 5;
}
else
{
(vertvals+i+1)->numcat = 0;
}

i++;
}
}
}

Expand Down
12 changes: 9 additions & 3 deletions json.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,12 @@ json_print_CPU(char *hp, struct sstat *ss,
int i;

// calculate average clock frequency
for (i = 0; i < ss->cpu.nrcpu; i++) {
for (i = 0; i <= ss->cpu.maxcpunr; i++) {
if (ss->cpu.cpu[i].active)
{
cnt += ss->cpu.cpu[i].freqcnt.cnt;
ticks += ss->cpu.cpu[i].freqcnt.ticks;
}
}
maxfreq = ss->cpu.cpu[0].freqcnt.maxfreq;
json_calc_freqscale(maxfreq, cnt, ticks, &freq, &freqperc);
Expand Down Expand Up @@ -388,7 +391,9 @@ json_print_cpu(char *hp, struct sstat *ss,

printf(", %s: [", hp);

for (i = 0; i < ss->cpu.nrcpu; i++) {
for (i = 0; i <= ss->cpu.maxcpunr; i++) {
if (ss->cpu.cpu[i].active)
{
if (i > 0) {
printf(", ");
}
Expand All @@ -412,7 +417,7 @@ json_print_cpu(char *hp, struct sstat *ss,
"\"freqperc\": %d, "
"\"instr\": %lld, "
"\"cycle\": %lld}",
i,
ss->cpu.cpu[i].cpunr,
ss->cpu.cpu[i].stime,
ss->cpu.cpu[i].utime,
ss->cpu.cpu[i].ntime,
Expand All @@ -426,6 +431,7 @@ json_print_cpu(char *hp, struct sstat *ss,
freqperc,
ss->cpu.cpu[i].instr,
ss->cpu.cpu[i].cycle);
}
}

printf("]");
Expand Down
13 changes: 10 additions & 3 deletions parseable.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,13 @@ print_CPU(char *hp, struct sstat *ss,
int i;

// calculate average clock frequency
for (i=0; i < ss->cpu.nrcpu; i++)
for (i=0; i <= ss->cpu.maxcpunr; i++)
{
if (ss->cpu.cpu[i].active)
{
cnt += ss->cpu.cpu[i].freqcnt.cnt;
ticks += ss->cpu.cpu[i].freqcnt.ticks;
}
}
maxfreq = ss->cpu.cpu[0].freqcnt.maxfreq;
calc_freqscale(maxfreq, cnt, ticks, &freq, &freqperc);
Expand Down Expand Up @@ -372,8 +375,10 @@ print_cpu(char *hp, struct sstat *ss,
count_t freq;
int freqperc;

for (i=0; i < ss->cpu.nrcpu; i++)
for (i=0; i <= ss->cpu.maxcpunr; i++)
{
if (ss->cpu.cpu[i].active)
{
cnt = ss->cpu.cpu[i].freqcnt.cnt;
ticks = ss->cpu.cpu[i].freqcnt.ticks;
maxfreq= ss->cpu.cpu[0].freqcnt.maxfreq;
Expand All @@ -382,7 +387,8 @@ print_cpu(char *hp, struct sstat *ss,

printf("%s %u %d %lld %lld %lld "
"%lld %lld %lld %lld %lld %lld %lld %d %lld %lld\n",
hp, hertz, i,
hp, hertz,
ss->cpu.cpu[i].cpunr,
ss->cpu.cpu[i].stime,
ss->cpu.cpu[i].utime,
ss->cpu.cpu[i].ntime,
Expand All @@ -397,6 +403,7 @@ print_cpu(char *hp, struct sstat *ss,
ss->cpu.cpu[i].instr,
ss->cpu.cpu[i].cycle
);
}
}
}

Expand Down
Loading

0 comments on commit 505c4bd

Please sign in to comment.