top(1): format_time, format_k, etc.
- Use humanize_number for format_k and format_k2 - Fix some style nits in format_time
This commit is contained in:
parent
e7cb1c0776
commit
8e4b205ea5
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=335038
@ -16,5 +16,5 @@ NO_WERROR=
|
|||||||
.endif
|
.endif
|
||||||
CFLAGS.clang=-Wno-error=incompatible-pointer-types-discards-qualifiers -Wno-error=cast-qual
|
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 <bsd.prog.mk>
|
.include <bsd.prog.mk>
|
||||||
|
@ -1087,7 +1087,7 @@ format_next_process(void* xhandle, char *(*get_userid)(int), int flags)
|
|||||||
else
|
else
|
||||||
snprintf(swap_buf, sizeof(swap_buf), "%*s",
|
snprintf(swap_buf, sizeof(swap_buf), "%*s",
|
||||||
swaplength - 1,
|
swaplength - 1,
|
||||||
format_k2(pagetok(ki_swap(pp)))); /* XXX */
|
format_k(pagetok(ki_swap(pp)))); /* XXX */
|
||||||
|
|
||||||
if (displaymode == DISP_IO) {
|
if (displaymode == DISP_IO) {
|
||||||
oldp = get_old_proc(pp);
|
oldp = get_old_proc(pp);
|
||||||
@ -1148,8 +1148,8 @@ format_next_process(void* xhandle, char *(*get_userid)(int), int flags)
|
|||||||
thr_buf,
|
thr_buf,
|
||||||
pp->ki_pri.pri_level - PZERO,
|
pp->ki_pri.pri_level - PZERO,
|
||||||
format_nice(pp),
|
format_nice(pp),
|
||||||
format_k2(PROCSIZE(pp)),
|
format_k(PROCSIZE(pp)),
|
||||||
format_k2(pagetok(pp->ki_rssize)),
|
format_k(pagetok(pp->ki_rssize)),
|
||||||
swaplength, swaplength, swap_buf,
|
swaplength, swaplength, swap_buf,
|
||||||
status,
|
status,
|
||||||
cpu,
|
cpu,
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
#include <sys/sysctl.h>
|
#include <sys/sysctl.h>
|
||||||
#include <sys/user.h>
|
#include <sys/user.h>
|
||||||
|
|
||||||
|
#include <libutil.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -262,7 +263,7 @@ percentages(int cnt, int *out, long *new, long *old, long *diffs)
|
|||||||
exceed 9999.9, we use "???".
|
exceed 9999.9, we use "???".
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *
|
const char *
|
||||||
format_time(long seconds)
|
format_time(long seconds)
|
||||||
{
|
{
|
||||||
static char result[10];
|
static char result[10];
|
||||||
@ -286,9 +287,8 @@ format_time(long seconds)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* standard method produces MMM:SS */
|
/* standard method produces MMM:SS */
|
||||||
/* we avoid printf as must as possible to make this quick */
|
|
||||||
sprintf(result, "%3ld:%02ld",
|
sprintf(result, "%3ld:%02ld",
|
||||||
(long)(seconds / 60), (long)(seconds % 60));
|
seconds / 60l, seconds % 60l);
|
||||||
}
|
}
|
||||||
return(result);
|
return(result);
|
||||||
}
|
}
|
||||||
@ -319,63 +319,16 @@ format_time(long seconds)
|
|||||||
#define NUM_STRINGS 8
|
#define NUM_STRINGS 8
|
||||||
|
|
||||||
char *
|
char *
|
||||||
format_k(long amt)
|
format_k(int64_t amt)
|
||||||
{
|
{
|
||||||
static char retarray[NUM_STRINGS][16];
|
static char retarray[NUM_STRINGS][16];
|
||||||
static int index = 0;
|
static int index = 0;
|
||||||
char *p;
|
|
||||||
char *ret;
|
char *ret;
|
||||||
char tag = 'K';
|
|
||||||
|
|
||||||
p = ret = retarray[index];
|
ret = retarray[index];
|
||||||
index = (index + 1) % NUM_STRINGS;
|
index = (index + 1) % NUM_STRINGS;
|
||||||
|
humanize_number(ret, 6, amt * 1024, "", HN_AUTOSCALE, HN_NOSPACE);
|
||||||
if (amt >= 10000)
|
return (ret);
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -18,9 +18,8 @@ char *itoa7(int);
|
|||||||
int digits(int);
|
int digits(int);
|
||||||
const char * const *argparse(char *, int *);
|
const char * const *argparse(char *, int *);
|
||||||
long percentages(int, int *, long *, long *, long *);
|
long percentages(int, int *, long *, long *, long *);
|
||||||
char *format_time(long);
|
const char *format_time(long);
|
||||||
char *format_k(long);
|
char *format_k(int64_t);
|
||||||
char *format_k2(unsigned long long);
|
|
||||||
int string_index(const char *string, const char * const *array);
|
int string_index(const char *string, const char * const *array);
|
||||||
int find_pid(pid_t pid);
|
int find_pid(pid_t pid);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user