From owner-svn-src-all@freebsd.org Wed Jun 13 08:52:14 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 646EB10150F8; Wed, 13 Jun 2018 08:52:14 +0000 (UTC) (envelope-from eadler@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 0680F752B0; Wed, 13 Jun 2018 08:52:11 +0000 (UTC) (envelope-from eadler@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 8AD1A172D2; Wed, 13 Jun 2018 08:52:10 +0000 (UTC) (envelope-from eadler@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w5D8qAi7093626; Wed, 13 Jun 2018 08:52:10 GMT (envelope-from eadler@FreeBSD.org) Received: (from eadler@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w5D8q9Qs093623; Wed, 13 Jun 2018 08:52:09 GMT (envelope-from eadler@FreeBSD.org) Message-Id: <201806130852.w5D8q9Qs093623@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: eadler set sender to eadler@FreeBSD.org using -f From: Eitan Adler Date: Wed, 13 Jun 2018 08:52:09 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r335038 - head/usr.bin/top X-SVN-Group: head X-SVN-Commit-Author: eadler X-SVN-Commit-Paths: head/usr.bin/top X-SVN-Commit-Revision: 335038 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 13 Jun 2018 08:52:14 -0000 Author: eadler Date: Wed Jun 13 08:52:09 2018 New Revision: 335038 URL: https://svnweb.freebsd.org/changeset/base/335038 Log: top(1): format_time, format_k, etc. - Use humanize_number for format_k and format_k2 - Fix some style nits in format_time Modified: head/usr.bin/top/Makefile head/usr.bin/top/machine.c head/usr.bin/top/utils.c head/usr.bin/top/utils.h Modified: head/usr.bin/top/Makefile ============================================================================== --- head/usr.bin/top/Makefile Wed Jun 13 08:52:06 2018 (r335037) +++ head/usr.bin/top/Makefile Wed Jun 13 08:52:09 2018 (r335038) @@ -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 Modified: head/usr.bin/top/machine.c ============================================================================== --- head/usr.bin/top/machine.c Wed Jun 13 08:52:06 2018 (r335037) +++ head/usr.bin/top/machine.c Wed Jun 13 08:52:09 2018 (r335038) @@ -1087,7 +1087,7 @@ format_next_process(void* xhandle, char *(*get_userid) 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) 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, Modified: head/usr.bin/top/utils.c ============================================================================== --- head/usr.bin/top/utils.c Wed Jun 13 08:52:06 2018 (r335037) +++ head/usr.bin/top/utils.c Wed Jun 13 08:52:09 2018 (r335038) @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -262,35 +263,34 @@ percentages(int cnt, int *out, long *new, long *old, l 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 Modified: head/usr.bin/top/utils.h ============================================================================== --- head/usr.bin/top/utils.h Wed Jun 13 08:52:06 2018 (r335037) +++ head/usr.bin/top/utils.h Wed Jun 13 08:52:09 2018 (r335038) @@ -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);