- Fix types of "struct vmmeter" members so they are unsigned.

- Fix overflow bugs in sysctl(8), systat(1), and vmstat(8)
  when printing values of "struct vmmeter" in kilobytes as
  they don't necessarily fit into 32 bits.  (Fix sysctl(8)
  reporting of a total virtual memory; it's in pages too.)
This commit is contained in:
Ruslan Ermilov 2006-11-20 16:04:41 +00:00
parent 681efe0834
commit 5c88a11e6e
5 changed files with 130 additions and 56 deletions

View File

@ -96,13 +96,17 @@ main(int argc, char *argv[])
printf(" r w l s tot act tot act tot act tot act\n");
}
printf("%2hu%2hu%2hu%2hu",v.t_rq-1,v.t_dw+v.t_pw,v.t_sl,v.t_sw);
printf("%7ld %7ld %7ld%7ld",
(long)pgtok(v.t_vm),(long)pgtok(v.t_avm),
(long)pgtok(v.t_rm),(long)pgtok(v.t_arm));
printf("%7ld%7ld%7ld%7ld%7ld\n",
(long)pgtok(v.t_vmshr),(long)pgtok(v.t_avmshr),
(long)pgtok(v.t_rmshr),(long)pgtok(v.t_armshr),
(long)pgtok(v.t_free));
printf("%7lu %7lu %7lu%7lu",
(unsigned long)pgtok(v.t_vm),
(unsigned long)pgtok(v.t_avm),
(unsigned long)pgtok(v.t_rm),
(unsigned long)pgtok(v.t_arm));
printf("%7lu%7lu%7lu%7lu%7lu\n",
(unsigned long)pgtok(v.t_vmshr),
(unsigned long)pgtok(v.t_avmshr),
(unsigned long)pgtok(v.t_rmshr),
(unsigned long)pgtok(v.t_armshr),
(unsigned long)pgtok(v.t_free));
sleep(5);
i++;
if(i>22) i=0;

View File

@ -54,6 +54,7 @@ static const char rcsid[] =
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <inttypes.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
@ -395,18 +396,18 @@ S_vmtotal(int l2, void *p)
"%hu Sleep: %hu)\n",
v->t_rq, v->t_dw, v->t_pw, v->t_sl);
printf(
"Virtual Memory:\t\t(Total: %luK, Active %lldK)\n",
(unsigned long)v->t_vm / 1024,
(long long)v->t_avm * pageKilo);
printf("Real Memory:\t\t(Total: %lldK Active %lldK)\n",
(long long)v->t_rm * pageKilo, (long long)v->t_arm * pageKilo);
printf("Shared Virtual Memory:\t(Total: %lldK Active: %lldK)\n",
(long long)v->t_vmshr * pageKilo,
(long long)v->t_avmshr * pageKilo);
printf("Shared Real Memory:\t(Total: %lldK Active: %lldK)\n",
(long long)v->t_rmshr * pageKilo,
(long long)v->t_armshr * pageKilo);
printf("Free Memory Pages:\t%lldK\n", (long long)v->t_free * pageKilo);
"Virtual Memory:\t\t(Total: %zuK, Active %zuK)\n",
(uintmax_t)v->t_vm * pageKilo,
(uintmax_t)v->t_avm * pageKilo);
printf("Real Memory:\t\t(Total: %zuK Active %zuK)\n",
(uintmax_t)v->t_rm * pageKilo, (uintmax_t)v->t_arm * pageKilo);
printf("Shared Virtual Memory:\t(Total: %zuK Active: %zuK)\n",
(uintmax_t)v->t_vmshr * pageKilo,
(uintmax_t)v->t_avmshr * pageKilo);
printf("Shared Real Memory:\t(Total: %zuK Active: %zuK)\n",
(uintmax_t)v->t_rmshr * pageKilo,
(uintmax_t)v->t_armshr * pageKilo);
printf("Free Memory Pages:\t%zuK\n", (uintmax_t)v->t_free * pageKilo);
return (0);
}

View File

@ -191,20 +191,20 @@ vm_paging_needed(void)
/* systemwide totals computed every five seconds */
struct vmtotal {
int16_t t_rq; /* length of the run queue */
int16_t t_dw; /* jobs in ``disk wait'' (neg priority) */
int16_t t_pw; /* jobs in page wait */
int16_t t_sl; /* jobs sleeping in core */
int16_t t_sw; /* swapped out runnable/short block jobs */
int32_t t_vm; /* total virtual memory */
int32_t t_avm; /* active virtual memory */
int32_t t_rm; /* total real memory in use */
int32_t t_arm; /* active real memory */
int32_t t_vmshr; /* shared virtual memory */
int32_t t_avmshr; /* active shared virtual memory */
int32_t t_rmshr; /* shared real memory */
int32_t t_armshr; /* active shared real memory */
int32_t t_free; /* free memory pages */
uint16_t t_rq; /* length of the run queue */
uint16_t t_dw; /* jobs in ``disk wait'' (neg priority) */
uint16_t t_pw; /* jobs in page wait */
uint16_t t_sl; /* jobs sleeping in core */
uint16_t t_sw; /* swapped out runnable/short block jobs */
uint32_t t_vm; /* total virtual memory */
uint32_t t_avm; /* active virtual memory */
uint32_t t_rm; /* total real memory in use */
uint32_t t_arm; /* active real memory */
uint32_t t_vmshr; /* shared virtual memory */
uint32_t t_avmshr; /* active shared virtual memory */
uint32_t t_rmshr; /* shared real memory */
uint32_t t_armshr; /* active shared real memory */
uint32_t t_free; /* free memory pages */
};
#endif

View File

@ -58,6 +58,7 @@ static const char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 1/12/94";
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <inttypes.h>
#include <langinfo.h>
#include <nlist.h>
#include <paths.h>
@ -136,6 +137,8 @@ static float cputime(int);
static void dinfo(int, int, struct statinfo *, struct statinfo *);
static void getinfo(struct Info *);
static void putint(int, int, int, int);
static void putuint(unsigned, int, int, int);
static void putuintmax(uintmax_t, int, int, int);
static void putfloat(double, int, int, int, int, int);
static void putlongdouble(long double, int, int, int, int, int);
static int ucount(void);
@ -489,20 +492,20 @@ showkre()
putfloat(avenrun[2], STATROW, STATCOL + 32, 5, 2, 0);
mvaddstr(STATROW, STATCOL + 55, buf);
#define pgtokb(pg) ((pg) * (s.v_page_size / 1024))
putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 4, 7);
putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 12, 7);
putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 20, 8);
putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 29, 8);
putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 4, 7);
putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 12, 7);
putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 20, 8);
putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 29, 8);
putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 38, 7);
putint(total.t_rq - 1, PROCSROW + 2, PROCSCOL, 3);
putint(total.t_pw, PROCSROW + 2, PROCSCOL + 4, 3);
putint(total.t_dw, PROCSROW + 2, PROCSCOL + 8, 3);
putint(total.t_sl, PROCSROW + 2, PROCSCOL + 12, 3);
putint(total.t_sw, PROCSROW + 2, PROCSCOL + 16, 3);
putuintmax(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 4, 7);
putuintmax(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 12, 7);
putuintmax(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 20, 8);
putuintmax(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 29, 8);
putuintmax(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 4, 7);
putuintmax(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 12, 7);
putuintmax(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 20, 8);
putuintmax(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 29, 8);
putuintmax(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 38, 7);
putuint(total.t_rq - 1, PROCSROW + 2, PROCSCOL, 3);
putuint(total.t_pw, PROCSROW + 2, PROCSCOL + 4, 3);
putuint(total.t_dw, PROCSROW + 2, PROCSCOL + 8, 3);
putuint(total.t_sl, PROCSROW + 2, PROCSCOL + 12, 3);
putuint(total.t_sw, PROCSROW + 2, PROCSCOL + 16, 3);
PUTRATE(v_cow_faults, VMSTATROW, VMSTATCOL + 2, 8 - 2);
PUTRATE(v_zfod, VMSTATROW + 1, VMSTATCOL + 2, 8 - 2);
PUTRATE(v_ozfod, VMSTATROW + 2, VMSTATCOL, 8);
@ -515,11 +518,11 @@ showkre()
PUTRATE(v_pdwakeups, VMSTATROW + 8, VMSTATCOL, 8);
PUTRATE(v_pdpages, VMSTATROW + 9, VMSTATCOL, 8);
PUTRATE(v_intrans, VMSTATROW + 10, VMSTATCOL, 8);
putint(pgtokb(s.v_wire_count), VMSTATROW + 11, VMSTATCOL, 8);
putint(pgtokb(s.v_active_count), VMSTATROW + 12, VMSTATCOL, 8);
putint(pgtokb(s.v_inactive_count), VMSTATROW + 13, VMSTATCOL, 8);
putint(pgtokb(s.v_cache_count), VMSTATROW + 14, VMSTATCOL, 8);
putint(pgtokb(s.v_free_count), VMSTATROW + 15, VMSTATCOL, 8);
putuintmax(pgtokb(s.v_wire_count), VMSTATROW + 11, VMSTATCOL, 8);
putuintmax(pgtokb(s.v_active_count), VMSTATROW + 12, VMSTATCOL, 8);
putuintmax(pgtokb(s.v_inactive_count), VMSTATROW + 13, VMSTATCOL, 8);
putuintmax(pgtokb(s.v_cache_count), VMSTATROW + 14, VMSTATCOL, 8);
putuintmax(pgtokb(s.v_free_count), VMSTATROW + 15, VMSTATCOL, 8);
if (LINES - 1 > VMSTATROW + 16)
putint(s.bufspace / 1024, VMSTATROW + 16, VMSTATCOL, 8);
PUTRATE(v_vnodein, PAGEROW + 2, PAGECOL + 6, 5);
@ -693,6 +696,70 @@ putint(n, l, lc, w)
addstr(b);
}
static void
putuint(n, l, lc, w)
unsigned n;
int l, lc, w;
{
int snr;
char b[128];
move(l, lc);
#ifdef DEBUG
while (w-- > 0)
addch('*');
return;
#endif
if (n == 0) {
while (w-- > 0)
addch(' ');
return;
}
snr = snprintf(b, sizeof(b), "%*u", w, n);
if (snr != w)
snr = snprintf(b, sizeof(b), "%*uk", w - 1, n / 1000);
if (snr != w)
snr = snprintf(b, sizeof(b), "%*uM", w - 1, n / 1000000);
if (snr != w) {
while (w-- > 0)
addch('*');
return;
}
addstr(b);
}
static void
putuintmax(n, l, lc, w)
uintmax_t n;
int l, lc, w;
{
int snr;
char b[128];
move(l, lc);
#ifdef DEBUG
while (w-- > 0)
addch('*');
return;
#endif
if (n == 0) {
while (w-- > 0)
addch(' ');
return;
}
snr = snprintf(b, sizeof(b), "%*zu", w, n);
if (snr != w)
snr = snprintf(b, sizeof(b), "%*zuk", w - 1, n / 1000);
if (snr != w)
snr = snprintf(b, sizeof(b), "%*zuM", w - 1, n / 1000000);
if (snr != w) {
while (w-- > 0)
addch('*');
return;
}
addstr(b);
}
static void
putfloat(f, l, lc, w, d, nz)
double f;

View File

@ -65,6 +65,7 @@ __FBSDID("$FreeBSD$");
#include <devstat.h>
#include <err.h>
#include <errno.h>
#include <inttypes.h>
#include <kvm.h>
#include <limits.h>
#include <memstat.h>
@ -574,12 +575,13 @@ dovmstat(unsigned int interval, int reps)
fill_vmmeter(&sum);
fill_vmtotal(&total);
(void)printf("%2d %1d %1d",
(void)printf("%2u %1u %1u",
total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
#define vmstat_pgtok(a) ((a) * (sum.v_page_size >> 10))
#define vmstat_pgtok(a) ((uintmax_t)(a) * (sum.v_page_size >> 10))
#define rate(x) (((x) + halfuptime) / uptime) /* round */
(void)printf(" %7ld %6ld ", (long)vmstat_pgtok(total.t_avm),
(long)vmstat_pgtok(total.t_free));
(void)printf(" %7zu %6zu ",
vmstat_pgtok(total.t_avm),
vmstat_pgtok(total.t_free));
(void)printf("%5lu ",
(unsigned long)rate(sum.v_vm_faults - osum.v_vm_faults));
(void)printf("%3lu ",