From a9def1b4dfdfe06c1f6dfba6299a59afd26301f9 Mon Sep 17 00:00:00 2001 From: Steve Kay Date: Fri, 13 Mar 2015 01:57:28 -0700 Subject: [PATCH] Column widths : revise style to match existing code, use snprintf, calculate widths once. --- mpstat.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/mpstat.c b/mpstat.c index 662d184..1b41b25 100644 --- a/mpstat.c +++ b/mpstat.c @@ -218,7 +218,7 @@ void write_irqcpu_stats(struct stats_irqcpu *st_ic[], int ic_nr, int dis, char *prev_string, char *curr_string) { struct stats_cpu *scc; - int j = 0, offset, cpu, colwidth; + int j = 0, offset, cpu, colwidth[NR_IRQS]; struct stats_irqcpu *p, *q, *p0, *q0; char fmtspec[MAX_IRQ_LEN]; @@ -254,6 +254,19 @@ void write_irqcpu_stats(struct stats_irqcpu *st_ic[], int ic_nr, int dis, printf("\n"); } + /* Calculate column widths */ + for (j = 0; j < ic_nr; j++) { + p0 = st_ic[curr] + j; + if (p0->irq_name[0] != '\0') { /* Nb of irq per proc may have varied... */ + /* Width is IRQ name + 2 for the trailing "/s" */ + colwidth[j] = strlen(p0->irq_name) + 2; + /* normal space for printing a number is 14 chars + * (space + 10 digits + period + mantissa) */ + if (colwidth[j] < 14) + colwidth[j] = 10; + } + } + for (cpu = 1; cpu <= cpu_nr; cpu++) { scc = st_cpu[curr] + cpu; @@ -306,13 +319,7 @@ void write_irqcpu_stats(struct stats_irqcpu *st_ic[], int ic_nr, int dis, if (!strcmp(p0->irq_name, q0->irq_name) || !interval) { p = st_ic[curr] + (cpu - 1) * ic_nr + j; q = st_ic[prev] + (cpu - 1) * ic_nr + offset; - /* Width is IRQ name + 2 for the /s */ - colwidth=strlen(p0->irq_name)+2; - /* normal space for printing a number is 14 chars - * (space + 10 digits + period + mantissa) */ - if(colwidth<14) - colwidth=10; - sprintf(fmtspec," %%%d.2f",colwidth); + snprintf(fmtspec, sizeof(fmtspec), " %%%d.2f", colwidth[j]); printf(fmtspec, S_VALUE(q->interrupt, p->interrupt, itv)); } -- 2.40.0