From: Punit Agrawal <punit1.agrawal@xxxxxxxxxxxxx> As part of latency evaluation, cyclictest reports the average latencies observed for the threads. To calculate the average, the aggregate latency of a thread across all samples is tracked and the value calcualted at the end when it is reported. In preparation for adding support to report standard deviation, update the average calculation to track the streaming average, i.e., averages of the samples observed so far. Streaming average can be calculated using - avg = prev_avg + (sample - prev_avg) / #samples The patch implements this formula and updates the usage of average in the rest of the code. Signed-off-by: Punit Agrawal <punit1.agrawal@xxxxxxxxxxxxx> --- src/cyclictest/cyclictest.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/cyclictest/cyclictest.c b/src/cyclictest/cyclictest.c index 48a782fe167d..dde8b1625c62 100644 --- a/src/cyclictest/cyclictest.c +++ b/src/cyclictest/cyclictest.c @@ -616,6 +616,7 @@ static void *timerthread(void *param) uint64_t diff; unsigned long diff_smi = 0; int sigs, ret; + double prev_avg; /* Wait for next period */ switch (par->mode) { @@ -702,7 +703,6 @@ static void *timerthread(void *param) if (refresh_on_max) pthread_cond_signal(&refresh_on_max_cond); } - stat->avg += (double) diff; if (trigger && (diff > trigger)) trigger_update(par, diff, calctime(now)); @@ -742,6 +742,14 @@ static void *timerthread(void *param) stat->cycles++; + /* + * Calculate the streaming average + * + * avg = avg' + (x - avg') / n + */ + prev_avg = stat->avg; + stat->avg = prev_avg + ((diff - prev_avg) / stat->cycles); + next.tv_sec += interval.tv_sec; next.tv_nsec += interval.tv_nsec; if (par->mode == MODE_CYCLIC) { @@ -1429,8 +1437,7 @@ static void print_stat(FILE *fp, struct thread_param *par, int index, int verbos fprintf(fp, fmt, index, stat->tid, par->prio, par->interval, stat->cycles, stat->min, - stat->act, stat->cycles ? - (long)(stat->avg/stat->cycles) : 0, stat->max); + stat->act, (long)stat->avg, stat->max); if (smi) fprintf(fp, " SMI:%8ld", stat->smi_count); @@ -1485,8 +1492,7 @@ static void rstat_print_stat(struct thread_param *par, int index, int verbose, i dprintf(fd, fmt, index, stat->tid, par->prio, par->interval, stat->cycles, stat->min, - stat->act, stat->cycles ? - (long)(stat->avg/stat->cycles) : 0, stat->max); + stat->act, (long)stat->avg, stat->max); if (smi) dprintf(fd, " SMI:%8ld", stat->smi_count); @@ -1774,7 +1780,7 @@ static void write_stats(FILE *f, void *data) fprintf(f, " \"cycles\": %ld,\n", s->cycles); fprintf(f, " \"min\": %ld,\n", s->min); fprintf(f, " \"max\": %ld,\n", s->max); - fprintf(f, " \"avg\": %.2f,\n", s->avg/s->cycles); + fprintf(f, " \"avg\": %.2f,\n", s->avg); fprintf(f, " \"cpu\": %d,\n", par[i]->cpu); fprintf(f, " \"node\": %d\n", par[i]->node); fprintf(f, " }%s\n", i == num_threads - 1 ? "" : ","); -- 2.32.0