From 8e4b205ea5c6a58189c6e82c27c61a694b1d7182 Mon Sep 17 00:00:00 2001 From: Eitan Adler Date: Wed, 13 Jun 2018 08:52:09 +0000 Subject: [PATCH] top(1): format_time, format_k, etc. - Use humanize_number for format_k and format_k2 - Fix some style nits in format_time --- usr.bin/top/Makefile | 2 +- usr.bin/top/machine.c | 6 +-- usr.bin/top/utils.c | 107 ++++++++++++------------------------------ usr.bin/top/utils.h | 5 +- 4 files changed, 36 insertions(+), 84 deletions(-) diff --git a/usr.bin/top/Makefile b/usr.bin/top/Makefile index 34be3e38b157..ccf2c02d96fe 100644 --- a/usr.bin/top/Makefile +++ b/usr.bin/top/Makefile @@ -16,5 +16,5 @@ NO_WERROR= .endif CFLAGS.clang=-Wno-error=incompatible-pointer-types-discards-qualifiers -Wno-error=cast-qual -LIBADD= ncursesw m kvm jail +LIBADD= ncursesw m kvm jail util .include diff --git a/usr.bin/top/machine.c b/usr.bin/top/machine.c index 5cf8a216af6d..88d533d3e37a 100644 --- a/usr.bin/top/machine.c +++ b/usr.bin/top/machine.c @@ -1087,7 +1087,7 @@ format_next_process(void* xhandle, char *(*get_userid)(int), int flags) else snprintf(swap_buf, sizeof(swap_buf), "%*s", swaplength - 1, - format_k2(pagetok(ki_swap(pp)))); /* XXX */ + format_k(pagetok(ki_swap(pp)))); /* XXX */ if (displaymode == DISP_IO) { oldp = get_old_proc(pp); @@ -1148,8 +1148,8 @@ format_next_process(void* xhandle, char *(*get_userid)(int), int flags) thr_buf, pp->ki_pri.pri_level - PZERO, format_nice(pp), - format_k2(PROCSIZE(pp)), - format_k2(pagetok(pp->ki_rssize)), + format_k(PROCSIZE(pp)), + format_k(pagetok(pp->ki_rssize)), swaplength, swaplength, swap_buf, status, cpu, diff --git a/usr.bin/top/utils.c b/usr.bin/top/utils.c index d4aeb8476b2c..953361d527b3 100644 --- a/usr.bin/top/utils.c +++ b/usr.bin/top/utils.c @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -262,35 +263,34 @@ percentages(int cnt, int *out, long *new, long *old, long *diffs) exceed 9999.9, we use "???". */ -char * +const char * format_time(long seconds) { - static char result[10]; + static char result[10]; - /* sanity protection */ - if (seconds < 0 || seconds > (99999l * 360l)) - { - strcpy(result, " ???"); - } - else if (seconds >= (1000l * 60l)) - { - /* alternate (slow) method displaying hours and tenths */ - sprintf(result, "%5.1fH", (double)seconds / (double)(60l * 60l)); + /* sanity protection */ + if (seconds < 0 || seconds > (99999l * 360l)) + { + strcpy(result, " ???"); + } + else if (seconds >= (1000l * 60l)) + { + /* alternate (slow) method displaying hours and tenths */ + sprintf(result, "%5.1fH", (double)seconds / (double)(60l * 60l)); - /* It is possible that the sprintf took more than 6 characters. - If so, then the "H" appears as result[6]. If not, then there - is a \0 in result[6]. Either way, it is safe to step on. - */ - result[6] = '\0'; - } - else - { - /* standard method produces MMM:SS */ - /* we avoid printf as must as possible to make this quick */ - sprintf(result, "%3ld:%02ld", - (long)(seconds / 60), (long)(seconds % 60)); - } - return(result); + /* It is possible that the sprintf took more than 6 characters. + If so, then the "H" appears as result[6]. If not, then there + is a \0 in result[6]. Either way, it is safe to step on. + */ + result[6] = '\0'; + } + else + { + /* standard method produces MMM:SS */ + sprintf(result, "%3ld:%02ld", + seconds / 60l, seconds % 60l); + } + return(result); } /* @@ -319,63 +319,16 @@ format_time(long seconds) #define NUM_STRINGS 8 char * -format_k(long amt) +format_k(int64_t amt) { static char retarray[NUM_STRINGS][16]; static int index = 0; - char *p; char *ret; - char tag = 'K'; - p = ret = retarray[index]; - index = (index + 1) % NUM_STRINGS; - - if (amt >= 10000) - { - amt = (amt + 512) / 1024; - tag = 'M'; - if (amt >= 10000) - { - amt = (amt + 512) / 1024; - tag = 'G'; - } - } - - p = stpcpy(p, itoa(amt)); - *p++ = tag; - *p = '\0'; - - return(ret); -} - -char * -format_k2(unsigned long long amt) -{ - static char retarray[NUM_STRINGS][16]; - static int index = 0; - char *p; - char *ret; - char tag = 'K'; - - p = ret = retarray[index]; - index = (index + 1) % NUM_STRINGS; - - if (amt >= 100000) - { - amt = (amt + 512) / 1024; - tag = 'M'; - if (amt >= 100000) - { - amt = (amt + 512) / 1024; - tag = 'G'; - } - } - - p = stpcpy(p, itoa((int)amt)); - *p++ = tag; - *p = '\0'; - - return(ret); + ret = retarray[index]; + index = (index + 1) % NUM_STRINGS; + humanize_number(ret, 6, amt * 1024, "", HN_AUTOSCALE, HN_NOSPACE); + return (ret); } int diff --git a/usr.bin/top/utils.h b/usr.bin/top/utils.h index 67f23b433bbb..106e1da08963 100644 --- a/usr.bin/top/utils.h +++ b/usr.bin/top/utils.h @@ -18,9 +18,8 @@ char *itoa7(int); int digits(int); const char * const *argparse(char *, int *); long percentages(int, int *, long *, long *, long *); -char *format_time(long); -char *format_k(long); -char *format_k2(unsigned long long); +const char *format_time(long); +char *format_k(int64_t); int string_index(const char *string, const char * const *array); int find_pid(pid_t pid);