From 33557c3db463ac6efb337a67dd5f099609d62b30 Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Wed, 7 Jun 2017 15:16:49 +0200 Subject: [PATCH] Display a percent sign after the value when --human option used When option --human has been entered, display a percent sign (%) following every percentage value. Signed-off-by: Sebastien GODARD --- common.c | 28 +++++++++++++++++++++++++--- common.h | 3 +-- iostat.c | 8 ++++---- mpstat.c | 12 ++++++------ pidstat.c | 10 +++++----- pr_stats.c | 52 ++++++++++++++++++++++++++-------------------------- tapestat.c | 4 ++-- 7 files changed, 69 insertions(+), 48 deletions(-) diff --git a/common.c b/common.c index 373b0c2..b687b10 100644 --- a/common.c +++ b/common.c @@ -1276,17 +1276,38 @@ void cprintf_f(int unit, int num, int wi, int wd, ...) * Print "percent" statistics values using colors. * * IN: + * @human Set to > 0 if a percent sign (%) shall be displayed after + * the value. * @num Number of values to print. * @wi Output width. * @wd Number of decimal places. *************************************************************************** */ -void cprintf_pc(int num, int wi, int wd, ...) +void cprintf_pc(int human, int num, int wi, int wd, ...) { int i; - double val; + double val, lim = 0.005; + char u = '\0'; va_list args; + /* + * If a percent sign is to be displayed, then there will be only one decimal place. + * In this case, a value smaller than 0.05 shall be considered as 0. + */ + if (human > 0) { + lim = 0.05; + u = '%'; + if (wi < 4) { + /* E.g., 100% */ + wi = 4; + } + /* Keep one place for the percent sign */ + wi -= 1; + if (wd > 0) { + wd -= 1; + } + } + va_start(args, wd); for (i = 0; i < num; i++) { @@ -1297,7 +1318,7 @@ void cprintf_pc(int num, int wi, int wd, ...) else if (val >= PERCENT_LIMIT_LOW) { printf("%s", sc_percent_low); } - else if (val < 0.005) { + else if (val < lim) { printf("%s", sc_zero_int_stat); } else { @@ -1305,6 +1326,7 @@ void cprintf_pc(int num, int wi, int wd, ...) } printf(" %*.*f", wi, wd, val); printf("%s", sc_normal); + printf("%c", u); } va_end(args); diff --git a/common.h b/common.h index c871dc8..70ed486 100644 --- a/common.h +++ b/common.h @@ -42,7 +42,6 @@ #define NR_UNITS 8 - /* Timestamp buffer length */ #define TIMESTAMP_LEN 64 @@ -251,7 +250,7 @@ void cprintf_f void cprintf_in (int, char *, char *, int); void cprintf_pc - (int, int, int, ...); + (int, int, int, int, ...); void cprintf_s (int, char *, char *); void cprintf_u64 diff --git a/iostat.c b/iostat.c index 0c219cd..7b346e2 100644 --- a/iostat.c +++ b/iostat.c @@ -854,7 +854,7 @@ void write_plain_cpu_stat(int curr, unsigned long long itv) printf("avg-cpu: %%user %%nice %%system %%iowait %%steal %%idle\n"); printf(" "); - cprintf_pc(6, 7, 2, + cprintf_pc(DISPLAY_UNIT(flags), 6, 7, 2, ll_sp_value(st_cpu[!curr]->cpu_user, st_cpu[curr]->cpu_user, itv), ll_sp_value(st_cpu[!curr]->cpu_nice, st_cpu[curr]->cpu_nice, itv), /* @@ -1051,7 +1051,7 @@ void write_plain_ext_stat(unsigned long long itv, int fctr, * In the case of a device group (option -g), shi->used is the number of * devices in the group. Else shi->used equals 1. */ - cprintf_pc(1, 6, 2, + cprintf_pc(DISPLAY_UNIT(flags), 1, 6, 2, shi->used ? xds->util / 10.0 / (double) shi->used : xds->util / 10.0); /* shi->used should never be zero here */ printf("\n"); @@ -1073,7 +1073,7 @@ void write_plain_ext_stat(unsigned long long itv, int fctr, S_VALUE(ioj->rd_merges, ioi->rd_merges, itv), S_VALUE(ioj->wr_merges, ioi->wr_merges, itv)); /* %rrqm %wrqm */ - cprintf_pc(2, 6, 2, + cprintf_pc(DISPLAY_UNIT(flags), 2, 6, 2, xios->rrqm_pc, xios->wrqm_pc); /* r_await w_await */ cprintf_f(NO_UNIT, 2, 7, 2, @@ -1092,7 +1092,7 @@ void write_plain_ext_stat(unsigned long long itv, int fctr, * In the case of a device group (option -g), shi->used is the number of * devices in the group. Else shi->used equals 1. */ - cprintf_pc(1, 6, 2, + cprintf_pc(DISPLAY_UNIT(flags), 1, 6, 2, shi->used ? xds->util / 10.0 / (double) shi->used : xds->util / 10.0); /* shi->used should never be zero here */ printf("\n"); diff --git a/mpstat.c b/mpstat.c index 3171048..1c83ea2 100644 --- a/mpstat.c +++ b/mpstat.c @@ -394,7 +394,7 @@ void write_plain_cpu_stats(int dis, unsigned long long g_itv, int prev, int curr printf("%-11s", curr_string); cprintf_in(IS_STR, " %s", " all", 0); - cprintf_pc(10, 7, 2, + cprintf_pc(NO_UNIT, 10, 7, 2, (st_cpu[curr]->cpu_user - st_cpu[curr]->cpu_guest) < (st_cpu[prev]->cpu_user - st_cpu[prev]->cpu_guest) ? 0.0 : @@ -458,7 +458,7 @@ void write_plain_cpu_stats(int dis, unsigned long long g_itv, int prev, int curr if (!DISPLAY_ONLINE_CPU(flags)) { printf("%-11s", curr_string); cprintf_in(IS_INT, " %4d", "", cpu - 1); - cprintf_pc(10, 7, 2, + cprintf_pc(NO_UNIT, 10, 7, 2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); printf("\n"); } @@ -476,13 +476,13 @@ void write_plain_cpu_stats(int dis, unsigned long long g_itv, int prev, int curr * If the CPU is tickless then there is no change in CPU values * but the sum of values is not zero. */ - cprintf_pc(10, 7, 2, + cprintf_pc(NO_UNIT, 10, 7, 2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 100.0); printf("\n"); } else { - cprintf_pc(10, 7, 2, + cprintf_pc(NO_UNIT, 10, 7, 2, (scc->cpu_user - scc->cpu_guest) < (scp->cpu_user - scp->cpu_guest) ? 0.0 : ll_sp_value(scp->cpu_user - scp->cpu_guest, @@ -763,7 +763,7 @@ void write_plain_node_stats(int dis, unsigned long long g_itv, unsigned long lon printf("%-11s", curr_string); cprintf_in(IS_STR, " %s", " all", 0); - cprintf_pc(10, 7, 2, + cprintf_pc(NO_UNIT, 10, 7, 2, (st_cpu[curr]->cpu_user - st_cpu[curr]->cpu_guest) < (st_cpu[prev]->cpu_user - st_cpu[prev]->cpu_guest) ? 0.0 : @@ -821,7 +821,7 @@ void write_plain_node_stats(int dis, unsigned long long g_itv, unsigned long lon printf("%-11s", curr_string); cprintf_in(IS_INT, " %4d", "", node); - cprintf_pc(10, 7, 2, + cprintf_pc(NO_UNIT, 10, 7, 2, (snc->cpu_user - snc->cpu_guest) < (snp->cpu_user - snp->cpu_guest) ? 0.0 : ll_sp_value(snp->cpu_user - snp->cpu_guest, diff --git a/pidstat.c b/pidstat.c index 24268a1..990944e 100644 --- a/pidstat.c +++ b/pidstat.c @@ -1435,7 +1435,7 @@ int write_pid_task_all_stats(int prev, int curr, int dis, __print_line_id(pstc, '0'); if (DISPLAY_CPU(actflag)) { - cprintf_pc(5, 7, 2, + cprintf_pc(DISPLAY_UNIT(pidflag), 5, 7, 2, (pstc->utime - pstc->gtime) < (pstp->utime - pstp->gtime) ? 0.0 : SP_VALUE_100(pstp->utime - pstp->gtime, @@ -1460,7 +1460,7 @@ int write_pid_task_all_stats(int prev, int curr, int dis, cprintf_u64(DISPLAY_UNIT(pidflag) ? UNIT_KILOBYTE : NO_UNIT, 2, 7, (unsigned long long) pstc->vsz, (unsigned long long) pstc->rss); - cprintf_pc(1, 6, 2, + cprintf_pc(DISPLAY_UNIT(pidflag), 1, 6, 2, tlmkb ? SP_VALUE(0, pstc->rss, tlmkb) : 0.0); } @@ -1644,7 +1644,7 @@ int write_pid_task_cpu_stats(int prev, int curr, int dis, int disp_avg, continue; print_line_id(curr_string, pstc); - cprintf_pc(5, 7, 2, + cprintf_pc(DISPLAY_UNIT(pidflag), 5, 7, 2, (pstc->utime - pstc->gtime) < (pstp->utime - pstp->gtime) ? 0.0 : SP_VALUE_100(pstp->utime - pstp->gtime, @@ -1821,7 +1821,7 @@ int write_pid_task_memory_stats(int prev, int curr, int dis, int disp_avg, (double) pstc->total_vsz / pstc->rt_asum_count, (double) pstc->total_rss / pstc->rt_asum_count); - cprintf_pc(1, 6, 2, + cprintf_pc(DISPLAY_UNIT(pidflag), 1, 6, 2, tlmkb ? SP_VALUE(0, pstc->total_rss / pstc->rt_asum_count, tlmkb) : 0.0); @@ -1831,7 +1831,7 @@ int write_pid_task_memory_stats(int prev, int curr, int dis, int disp_avg, (unsigned long long) pstc->vsz, (unsigned long long) pstc->rss); - cprintf_pc(1, 6, 2, + cprintf_pc(DISPLAY_UNIT(pidflag), 1, 6, 2, tlmkb ? SP_VALUE(0, pstc->rss, tlmkb) : 0.0); } diff --git a/pr_stats.c b/pr_stats.c index 3c40e96..72dc641 100644 --- a/pr_stats.c +++ b/pr_stats.c @@ -179,7 +179,7 @@ __print_funct_t print_cpu_stats(struct activity *a, int prev, int curr, *scc = *scp; /* %user, %nice, %system, %iowait, %steal, ..., %idle */ - cprintf_pc(6, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 6, 9, 2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); if (DISPLAY_CPU_ALL(a->opt_flags)) { @@ -187,7 +187,7 @@ __print_funct_t print_cpu_stats(struct activity *a, int prev, int curr, * Four additional fields to display: * %irq, %soft, %guest, %gnice. */ - cprintf_pc(4, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 4, 9, 2, 0.0, 0.0, 0.0, 0.0); } printf("\n"); @@ -203,11 +203,11 @@ __print_funct_t print_cpu_stats(struct activity *a, int prev, int curr, * but the sum of values is not zero. * %user, %nice, %system, %iowait, %steal, ..., %idle */ - cprintf_pc(5, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 5, 9, 2, 0.0, 0.0, 0.0, 0.0, 0.0); if (DISPLAY_CPU_DEF(a->opt_flags)) { - cprintf_pc(1, 9, 2, 100.0); + cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, 100.0); printf("\n"); } /* @@ -215,7 +215,7 @@ __print_funct_t print_cpu_stats(struct activity *a, int prev, int curr, * %irq, %soft, %guest, %gnice. */ else if (DISPLAY_CPU_ALL(a->opt_flags)) { - cprintf_pc(4, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 4, 9, 2, 0.0, 0.0, 0.0, 100.0); printf("\n"); } @@ -224,7 +224,7 @@ __print_funct_t print_cpu_stats(struct activity *a, int prev, int curr, } if (DISPLAY_CPU_DEF(a->opt_flags)) { - cprintf_pc(6, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 6, 9, 2, ll_sp_value(scp->cpu_user, scc->cpu_user, g_itv), ll_sp_value(scp->cpu_nice, scc->cpu_nice, g_itv), ll_sp_value(scp->cpu_sys + scp->cpu_hardirq + scp->cpu_softirq, @@ -238,7 +238,7 @@ __print_funct_t print_cpu_stats(struct activity *a, int prev, int curr, printf("\n"); } else if (DISPLAY_CPU_ALL(a->opt_flags)) { - cprintf_pc(10, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 10, 9, 2, (scc->cpu_user - scc->cpu_guest) < (scp->cpu_user - scp->cpu_guest) ? 0.0 : ll_sp_value(scp->cpu_user - scp->cpu_guest, @@ -405,7 +405,7 @@ __print_funct_t print_paging_stats(struct activity *a, int prev, int curr, S_VALUE(spp->pgscan_kswapd, spc->pgscan_kswapd, itv), S_VALUE(spp->pgscan_direct, spc->pgscan_direct, itv), S_VALUE(spp->pgsteal, spc->pgsteal, itv)); - cprintf_pc(1, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, (spc->pgscan_kswapd + spc->pgscan_direct - spp->pgscan_kswapd - spp->pgscan_direct) ? SP_VALUE(spp->pgsteal, spc->pgsteal, @@ -513,7 +513,7 @@ void stub_print_memory_stats(struct activity *a, int prev, int curr, (unsigned long long) smc->frmkb, (unsigned long long) smc->availablekb, (unsigned long long) (smc->tlmkb - smc->frmkb)); - cprintf_pc(1, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, smc->tlmkb ? SP_VALUE(smc->frmkb, smc->tlmkb, smc->tlmkb) : 0.0); @@ -521,7 +521,7 @@ void stub_print_memory_stats(struct activity *a, int prev, int curr, (unsigned long long) smc->bufkb, (unsigned long long) smc->camkb, (unsigned long long) smc->comkb); - cprintf_pc(1, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, (smc->tlmkb + smc->tlskb) ? SP_VALUE(0, smc->comkb, smc->tlmkb + smc->tlskb) : 0.0); @@ -568,7 +568,7 @@ void stub_print_memory_stats(struct activity *a, int prev, int curr, (double) avg_frmkb / avg_count, (double) avg_availablekb / avg_count, (double) smc->tlmkb - ((double) avg_frmkb / avg_count)); - cprintf_pc(1, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, smc->tlmkb ? SP_VALUE((double) (avg_frmkb / avg_count), smc->tlmkb, smc->tlmkb) : 0.0); @@ -576,7 +576,7 @@ void stub_print_memory_stats(struct activity *a, int prev, int curr, (double) avg_bufkb / avg_count, (double) avg_camkb / avg_count, (double) avg_comkb / avg_count); - cprintf_pc(1, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, (smc->tlmkb + smc->tlskb) ? SP_VALUE(0.0, (double) (avg_comkb / avg_count), smc->tlmkb + smc->tlskb) : 0.0); @@ -615,13 +615,13 @@ void stub_print_memory_stats(struct activity *a, int prev, int curr, cprintf_u64(unit, 2, 9, (unsigned long long) smc->frskb, (unsigned long long) (smc->tlskb - smc->frskb)); - cprintf_pc(1, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, smc->tlskb ? SP_VALUE(smc->frskb, smc->tlskb, smc->tlskb) : 0.0); cprintf_u64(unit, 1, 9, (unsigned long long) smc->caskb); - cprintf_pc(1, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, (smc->tlskb - smc->frskb) ? SP_VALUE(0, smc->caskb, smc->tlskb - smc->frskb) : 0.0); @@ -643,7 +643,7 @@ void stub_print_memory_stats(struct activity *a, int prev, int curr, (double) avg_frskb / avg_count, ((double) avg_tlskb / avg_count) - ((double) avg_frskb / avg_count)); - cprintf_pc(1, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, avg_tlskb ? SP_VALUE((double) avg_frskb / avg_count, (double) avg_tlskb / avg_count, @@ -651,7 +651,7 @@ void stub_print_memory_stats(struct activity *a, int prev, int curr, : 0.0); cprintf_f(unit, 1, 9, 0, (double) avg_caskb / avg_count); - cprintf_pc(1, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, (avg_tlskb != avg_frskb) ? SP_VALUE(0.0, (double) avg_caskb / avg_count, ((double) avg_tlskb / avg_count) - @@ -1032,7 +1032,7 @@ __print_funct_t print_disk_stats(struct activity *a, int prev, int curr, S_VALUE(sdp->rq_ticks, sdc->rq_ticks, itv) / 1000.0, xds.await, xds.svctm); - cprintf_pc(1, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, xds.util / 10.0); printf("\n"); } @@ -1101,7 +1101,7 @@ __print_funct_t print_net_dev_stats(struct activity *a, int prev, int curr, S_VALUE(sndp->tx_compressed, sndc->tx_compressed, itv), S_VALUE(sndp->multicast, sndc->multicast, itv)); ifutil = compute_ifutil(sndc, rxkb, txkb); - cprintf_pc(1, 9, 2, ifutil); + cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, ifutil); printf("\n"); } } @@ -2143,7 +2143,7 @@ void stub_print_pwr_temp_stats(struct activity *a, int curr, int dispavg) if (dispavg) { /* Display average values */ cprintf_f(NO_UNIT, 1, 9, 2, (double) avg_temp[i] / avg_count); - cprintf_pc(1, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, (avg_temp_max[i] - avg_temp_min[i]) ? ((double) (avg_temp[i] / avg_count) - avg_temp_min[i]) / (avg_temp_max[i] - avg_temp_min[i]) * 100 : 0.0); @@ -2151,7 +2151,7 @@ void stub_print_pwr_temp_stats(struct activity *a, int curr, int dispavg) else { /* Display instantaneous values */ cprintf_f(NO_UNIT, 1, 9, 2, spc->temp); - cprintf_pc(1, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, (spc->temp_max - spc->temp_min) ? (spc->temp - spc->temp_min) / (spc->temp_max - spc->temp_min) * 100 : 0.0); @@ -2268,7 +2268,7 @@ void stub_print_pwr_in_stats(struct activity *a, int curr, int dispavg) if (dispavg) { /* Display average values */ cprintf_f(NO_UNIT, 1, 9, 2, (double) avg_in[i] / avg_count); - cprintf_pc(1, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, (avg_in_max[i] - avg_in_min[i]) ? ((double) (avg_in[i] / avg_count) - avg_in_min[i]) / (avg_in_max[i] - avg_in_min[i]) * 100 : 0.0); @@ -2276,7 +2276,7 @@ void stub_print_pwr_in_stats(struct activity *a, int curr, int dispavg) else { /* Display instantaneous values */ cprintf_f(NO_UNIT, 1, 9, 2, spc->in); - cprintf_pc(1, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, (spc->in_max - spc->in_min) ? (spc->in - spc->in_min) / (spc->in_max - spc->in_min) * 100 : 0.0); @@ -2374,7 +2374,7 @@ void stub_print_huge_stats(struct activity *a, int curr, int dispavg) cprintf_u64(unit, 2, 9, (unsigned long long) smc->frhkb, (unsigned long long) (smc->tlhkb - smc->frhkb)); - cprintf_pc(1, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, smc->tlhkb ? SP_VALUE(smc->frhkb, smc->tlhkb, smc->tlhkb) : 0.0); printf("\n"); @@ -2390,7 +2390,7 @@ void stub_print_huge_stats(struct activity *a, int curr, int dispavg) (double) avg_frhkb / avg_count, ((double) avg_tlhkb / avg_count) - ((double) avg_frhkb / avg_count)); - cprintf_pc(1, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, avg_tlhkb ? SP_VALUE((double) avg_frhkb / avg_count, (double) avg_tlhkb / avg_count, @@ -2669,7 +2669,7 @@ __print_funct_t stub_print_filesystem_stats(struct activity *a, int curr, int di unit < 0 ? (double) sfc->f_bfree / 1024 / 1024 : (double) sfc->f_bfree, unit < 0 ? (double) (sfc->f_blocks - sfc->f_bfree) / 1024 / 1024 : (double) (sfc->f_blocks - sfc->f_bfree)); - cprintf_pc(2, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 2, 9, 2, /* f_blocks is not zero. But test it anyway ;-) */ sfc->f_blocks ? SP_VALUE(sfc->f_bfree, sfc->f_blocks, sfc->f_blocks) : 0.0, @@ -2678,7 +2678,7 @@ __print_funct_t stub_print_filesystem_stats(struct activity *a, int curr, int di cprintf_u64(NO_UNIT, 2, 9, (unsigned long long) sfc->f_ffree, (unsigned long long) (sfc->f_files - sfc->f_ffree)); - cprintf_pc(1, 9, 2, + cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, sfc->f_files ? SP_VALUE(sfc->f_ffree, sfc->f_files, sfc->f_files) : 0.0); cprintf_in(IS_STR, " %s\n", diff --git a/tapestat.c b/tapestat.c index b03f1b8..268e4a5 100644 --- a/tapestat.c +++ b/tapestat.c @@ -369,7 +369,7 @@ void tape_write_headings(void) } else { printf("kB_read/s kB_wrtn/s"); } - printf(" %%Rd %%Wr %%Oa Rs/s Ot/s\n"); + printf(" %%Rd %%Wr %%Oa Rs/s Ot/s\n"); } /* @@ -454,7 +454,7 @@ void tape_write_stats(struct calc_stats *tape, int i) : tape->kbytes_read_per_second / divisor, DISPLAY_UNIT(flags) ? tape->kbytes_written_per_second : tape->kbytes_written_per_second / divisor); - cprintf_pc(3, 3, 0, + cprintf_pc(DISPLAY_UNIT(flags), 3, 4, 0, (double) tape->read_pct_wait, (double) tape->write_pct_wait, (double) tape->all_pct_wait); -- 2.40.0