Changeset View
Standalone View
usr.bin/systat/vmstat.c
Show First 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | |||||
#include <sys/sysctl.h> | #include <sys/sysctl.h> | ||||
#include <sys/vmmeter.h> | #include <sys/vmmeter.h> | ||||
#include <vm/vm_param.h> | #include <vm/vm_param.h> | ||||
#include <ctype.h> | #include <ctype.h> | ||||
#include <err.h> | #include <err.h> | ||||
#include <errno.h> | #include <errno.h> | ||||
#include <inttypes.h> | |||||
#include <langinfo.h> | #include <langinfo.h> | ||||
#include <libutil.h> | |||||
#include <nlist.h> | #include <nlist.h> | ||||
#include <paths.h> | #include <paths.h> | ||||
#include <signal.h> | #include <signal.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <time.h> | #include <time.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
#include <utmpx.h> | #include <utmpx.h> | ||||
▲ Show 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | |||||
static enum state { BOOT, TIME, RUN } state = TIME; | static enum state { BOOT, TIME, RUN } state = TIME; | ||||
static void allocinfo(struct Info *); | static void allocinfo(struct Info *); | ||||
static void copyinfo(struct Info *, struct Info *); | static void copyinfo(struct Info *, struct Info *); | ||||
static float cputime(int); | static float cputime(int); | ||||
static void dinfo(int, int, struct statinfo *, struct statinfo *); | static void dinfo(int, int, struct statinfo *, struct statinfo *); | ||||
static void getinfo(struct Info *); | static void getinfo(struct Info *); | ||||
static void putint(int, int, int, int); | static void putint(int64_t, int, int, int); | ||||
static void putfloat(double, int, int, int, int, int); | static void putfloat(double, int, int, int, int, int); | ||||
static void putlongdouble(long double, int, int, int, int, int); | static void putlongdouble(long double, int, int, int, int, int); | ||||
static int ucount(void); | static int ucount(void); | ||||
static int ncpu; | static int ncpu; | ||||
static char buf[26]; | static char buf[26]; | ||||
static time_t t; | static time_t t; | ||||
static double etime; | static double etime; | ||||
▲ Show 20 Lines • Show All 510 Lines • ▼ Show 20 Lines | cputime(int indx) | ||||
for (i = 0; i < CPUSTATES; i++) | for (i = 0; i < CPUSTATES; i++) | ||||
lt += s.time[i]; | lt += s.time[i]; | ||||
if (lt == 0.0) | if (lt == 0.0) | ||||
lt = 1.0; | lt = 1.0; | ||||
return (s.time[indx] * 100.0 / lt); | return (s.time[indx] * 100.0 / lt); | ||||
} | } | ||||
static void | static void | ||||
putint(int n, int l, int lc, int w) | putint(int64_t n, int l, int lc, int w) | ||||
{ | { | ||||
int snr; | int snr; | ||||
kib: Blank line is required by style(9) after '{' if no local vars are defined. | |||||
char b[128]; | char b[128]; | ||||
char buf[128]; | |||||
move(l, lc); | move(l, lc); | ||||
#ifdef DEBUG | #ifdef DEBUG | ||||
while (w-- > 0) | while (w-- > 0) | ||||
addch('*'); | addch('*'); | ||||
return; | return; | ||||
#endif | #endif | ||||
if (n == 0) { | if (n == 0) { | ||||
Not Done Inline ActionsPerhaps name this function do_putuint64(). kib: Perhaps name this function do_putuint64(). | |||||
while (w-- > 0) | while (w-- > 0) | ||||
addch(' '); | addch(' '); | ||||
return; | return; | ||||
} | } | ||||
Done Inline ActionsThe format specifier is wrong, type of n is not uintmax_t. kib: The format specifier is wrong, type of n is not uintmax_t. | |||||
snr = snprintf(b, sizeof(b), "%*d", w, n); | snr = snprintf(b, sizeof(b), "%*" PRIi64, w, n); | ||||
kibUnsubmitted Not Done Inline ActionsThe cast of n to uintmax_t is what lot of places in the FreeBSD tree use. I believe there is almost no uses of PRIi64 in the non-contributed code, and some people dislike this feature loudly. kib: The cast of n to uintmax_t is what lot of places in the FreeBSD tree use. I believe there is… | |||||
if (snr != w) | |||||
snr = snprintf(b, sizeof(b), "%*dk", w - 1, n / 1000); | |||||
if (snr != w) | |||||
snr = snprintf(b, sizeof(b), "%*dM", w - 1, n / 1000000); | |||||
if (snr != w) { | if (snr != w) { | ||||
humanize_number(buf, w, n, "", HN_AUTOSCALE, | |||||
Not Done Inline ActionsWhy use DIV_1000 ? It is completely unexpected to have decimal suffix for the memory size. We do not sell harddrives. kib: Why use DIV_1000 ? It is completely unexpected to have decimal suffix for the memory size. We… | |||||
Not Done Inline ActionsI kept what the old version did, which was dividing bo 1000. kaktus: I kept what the old version did, which was dividing bo 1000. | |||||
Not Done Inline ActionsI think it is reasonable to use natural suffixes, instead of decimals. Especially because we do not stop at k. kib: I think it is reasonable to use natural suffixes, instead of decimals. Especially because we… | |||||
HN_NOSPACE | HN_DECIMAL | HN_DIVISOR_1000); | |||||
Not Done Inline ActionsWhy do you ever need this snprintf() ? To copy the string, use strlcpy(). kib: Why do you ever need this snprintf() ? To copy the string, use strlcpy(). | |||||
Not Done Inline ActionsBecause it pads the code to the length of w, returning the whole length that is used later. kaktus: Because it pads the code to the length of `w`, returning the whole length that is used later. | |||||
Not Done Inline Actionss/code/string/ kaktus: s/code/string/ | |||||
snr = snprintf(b, sizeof(b), "%*s", w, buf); | |||||
} | |||||
if (snr != w) { | |||||
while (w-- > 0) | while (w-- > 0) | ||||
addch('*'); | addch('*'); | ||||
return; | return; | ||||
} | } | ||||
addstr(b); | addstr(b); | ||||
} | } | ||||
static void | static void | ||||
putfloat(double f, int l, int lc, int w, int d, int nz) | putfloat(double f, int l, int lc, int w, int d, int nz) | ||||
{ | { | ||||
int snr; | int snr; | ||||
char b[128]; | char b[128]; | ||||
char buf[128]; | |||||
move(l, lc); | move(l, lc); | ||||
#ifdef DEBUG | #ifdef DEBUG | ||||
while (--w >= 0) | while (--w >= 0) | ||||
addch('*'); | addch('*'); | ||||
return; | return; | ||||
#endif | #endif | ||||
if (nz && f == 0.0) { | if (nz && f == 0.0) { | ||||
while (--w >= 0) | while (--w >= 0) | ||||
addch(' '); | addch(' '); | ||||
return; | return; | ||||
} | } | ||||
snr = snprintf(b, sizeof(b), "%*.*f", w, d, f); | snr = snprintf(b, sizeof(b), "%*.*f", w, d, f); | ||||
if (snr != w) | |||||
snr = snprintf(b, sizeof(b), "%*.0f", w, f); | |||||
if (snr != w) | |||||
snr = snprintf(b, sizeof(b), "%*.0fk", w - 1, f / 1000); | |||||
if (snr != w) | |||||
snr = snprintf(b, sizeof(b), "%*.0fM", w - 1, f / 1000000); | |||||
if (snr != w) { | if (snr != w) { | ||||
humanize_number(buf, w, (int64_t)f, "", HN_AUTOSCALE, | |||||
kibUnsubmitted Done Inline ActionsWhy the explicit cast for f is needed ? That said, use of humanize_number for floating-point value is weird. Why do you change this code, for which field it matters ? kib: Why the explicit cast for f is needed ?
That said, use of humanize_number for floating-point… | |||||
kaktusAuthorUnsubmitted Not Done Inline ActionsI did the change for consistency with the putint() function. As you see in case of number larger enough the decimal part is dropped. Anyway, I think that the code is dead anyway because I wasn't able to cause any of the % field to grow beyond 100%. kaktus: I did the change for consistency with the putint() function. As you see in case of number… | |||||
HN_NOSPACE | HN_DECIMAL | HN_DIVISOR_1000); | |||||
snr = snprintf(b, sizeof(b), "%*s", w, buf); | |||||
} | |||||
if (snr != w) { | |||||
while (--w >= 0) | while (--w >= 0) | ||||
addch('*'); | addch('*'); | ||||
return; | return; | ||||
} | } | ||||
addstr(b); | addstr(b); | ||||
} | } | ||||
static void | static void | ||||
putlongdouble(long double f, int l, int lc, int w, int d, int nz) | putlongdouble(long double f, int l, int lc, int w, int d, int nz) | ||||
{ | { | ||||
int snr; | int snr; | ||||
char b[128]; | char b[128]; | ||||
char buf[128]; | |||||
move(l, lc); | move(l, lc); | ||||
#ifdef DEBUG | #ifdef DEBUG | ||||
while (--w >= 0) | while (--w >= 0) | ||||
addch('*'); | addch('*'); | ||||
return; | return; | ||||
#endif | #endif | ||||
if (nz && f == 0.0) { | if (nz && f == 0.0) { | ||||
while (--w >= 0) | while (--w >= 0) | ||||
addch(' '); | addch(' '); | ||||
return; | return; | ||||
} | } | ||||
snr = snprintf(b, sizeof(b), "%*.*Lf", w, d, f); | snr = snprintf(b, sizeof(b), "%*.*Lf", w, d, f); | ||||
if (snr != w) | if (snr != w) { | ||||
snr = snprintf(b, sizeof(b), "%*.0Lf", w, f); | humanize_number(buf, w, (int64_t)f, "", HN_AUTOSCALE, | ||||
kibUnsubmitted Not Done Inline ActionsSame as above. kib: Same as above. | |||||
if (snr != w) | HN_NOSPACE | HN_DECIMAL | HN_DIVISOR_1000); | ||||
snr = snprintf(b, sizeof(b), "%*.0Lfk", w - 1, f / 1000); | snr = snprintf(b, sizeof(b), "%*s", w, buf); | ||||
if (snr != w) | } | ||||
snr = snprintf(b, sizeof(b), "%*.0LfM", w - 1, f / 1000000); | |||||
if (snr != w) { | if (snr != w) { | ||||
while (--w >= 0) | while (--w >= 0) | ||||
addch('*'); | addch('*'); | ||||
return; | return; | ||||
} | } | ||||
addstr(b); | addstr(b); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 139 Lines • Show Last 20 Lines |
Blank line is required by style(9) after '{' if no local vars are defined.