summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarco Pfatschbacher <mpf@cvs.openbsd.org>2011-04-05 07:35:33 +0000
committerMarco Pfatschbacher <mpf@cvs.openbsd.org>2011-04-05 07:35:33 +0000
commit1bdd4092e5ce051a1a74be62d82d33a1862988be (patch)
tree7124d7c2a1b6b093788713139f84cb1c855bc539
parente52df9698db5ec3875f31e4a128cec228c7656e0 (diff)
Add support to display numbers with thousands separators
and to switch the ifstat view to Bits/s. This allows us to see live network throughput in units like MBit/s, which is more practical. OK sthen@, manpage OK jmc@, "put it in" deraadt@
-rw-r--r--usr.bin/systat/engine.c80
-rw-r--r--usr.bin/systat/engine.h4
-rw-r--r--usr.bin/systat/if.c28
-rw-r--r--usr.bin/systat/main.c6
-rw-r--r--usr.bin/systat/systat.19
5 files changed, 105 insertions, 22 deletions
diff --git a/usr.bin/systat/engine.c b/usr.bin/systat/engine.c
index 9b7aa3383b4..ed00c638976 100644
--- a/usr.bin/systat/engine.c
+++ b/usr.bin/systat/engine.c
@@ -1,4 +1,4 @@
-/* $Id: engine.c,v 1.13 2010/07/19 04:41:28 lum Exp $ */
+/* $Id: engine.c,v 1.14 2011/04/05 07:35:32 mpf Exp $ */
/*
* Copyright (c) 2001, 2007 Can Erkin Acar <canacar@openbsd.org>
*
@@ -69,6 +69,7 @@ volatile sig_atomic_t gotsig_resize = 0;
volatile sig_atomic_t gotsig_alarm = 0;
int need_update = 0;
int need_sort = 0;
+int separate_thousands = 0;
SCREEN *screen;
@@ -134,7 +135,60 @@ tbprintf(char *format, ...)
tb_ptr += len;
tb_len -= len;
}
-
+
+ return len;
+}
+
+int
+tbprintft(char *format, ...)
+ GCC_PRINTFLIKE(1,2) /* defined in curses.h */
+{
+ int len;
+ va_list arg;
+ char buf[MAX_LINE_BUF];
+
+ if (tb_ptr == NULL || tb_len <= 0)
+ return 0;
+
+ va_start(arg, format);
+ len = vsnprintf(buf, tb_len, format, arg);
+ va_end(arg);
+
+ if (len > tb_len)
+ tb_end();
+ else if (len > 0) {
+ int d, s;
+ int digits, curdigit;
+
+ if (!separate_thousands) {
+ strlcpy(tb_ptr, buf, tb_len);
+ return len;
+ }
+
+ /* count until we hit a non digit. (e.g. the prefix) */
+ for (digits = 0; digits < len; digits++)
+ if (!isdigit(buf[digits]))
+ break;
+
+ curdigit = digits;
+ d = s = 0;
+ /* insert thousands separators while copying */
+ while (curdigit && d < tb_len) {
+ if (curdigit < digits && curdigit % 3 == 0)
+ tb_ptr[d++] = ',';
+ tb_ptr[d++] = buf[s++];
+ curdigit--;
+ }
+ /* copy the remaining non-digits */
+ while (len > digits && d < tb_len) {
+ tb_ptr[d++] = buf[s++];
+ digits++;
+ }
+ tb_ptr[d] = '\0';
+ tb_ptr += d;
+ tb_len -= d;
+ len = d;
+ }
return len;
}
@@ -672,33 +726,33 @@ print_fld_sdiv(field_def *fld, u_int64_t size, int d)
return;
tb_start();
- if (tbprintf("%llu", size) <= len)
+ if (tbprintft("%llu", size) <= len)
goto ok;
tb_start();
size /= d;
- if (tbprintf("%lluK", size) <= len)
+ if (tbprintft("%lluK", size) <= len)
goto ok;
if (size == 0)
goto err;
tb_start();
size /= d;
- if (tbprintf("%lluM", size) <= len)
+ if (tbprintft("%lluM", size) <= len)
goto ok;
if (size == 0)
goto err;
tb_start();
size /= d;
- if (tbprintf("%lluG", size) <= len)
+ if (tbprintft("%lluG", size) <= len)
goto ok;
if (size == 0)
goto err;
tb_start();
size /= d;
- if (tbprintf("%lluT", size) <= len)
+ if (tbprintft("%lluT", size) <= len)
goto ok;
err:
@@ -729,33 +783,33 @@ print_fld_ssdiv(field_def *fld, int64_t size, int d)
return;
tb_start();
- if (tbprintf("%lld", size) <= len)
+ if (tbprintft("%lld", size) <= len)
goto ok;
tb_start();
size /= d;
- if (tbprintf("%lldK", size) <= len)
+ if (tbprintft("%lldK", size) <= len)
goto ok;
if (size == 0)
goto err;
tb_start();
size /= d;
- if (tbprintf("%lldM", size) <= len)
+ if (tbprintft("%lldM", size) <= len)
goto ok;
if (size == 0)
goto err;
tb_start();
size /= d;
- if (tbprintf("%lldG", size) <= len)
+ if (tbprintft("%lldG", size) <= len)
goto ok;
if (size == 0)
goto err;
tb_start();
size /= d;
- if (tbprintf("%lldT", size) <= len)
+ if (tbprintft("%lldT", size) <= len)
goto ok;
err:
@@ -806,7 +860,7 @@ print_fld_uint(field_def *fld, unsigned int size)
return;
tb_start();
- if (tbprintf("%u", size) > len)
+ if (tbprintft("%u", size) > len)
print_fld_str(fld, "*");
else
print_fld_tb(fld);
diff --git a/usr.bin/systat/engine.h b/usr.bin/systat/engine.h
index e4725ca1a0b..8ca071f8bcc 100644
--- a/usr.bin/systat/engine.h
+++ b/usr.bin/systat/engine.h
@@ -1,4 +1,4 @@
-/* $Id: engine.h,v 1.6 2010/07/16 05:22:48 lum Exp $ */
+/* $Id: engine.h,v 1.7 2011/04/05 07:35:32 mpf Exp $ */
/*
* Copyright (c) 2001, 2007 Can Erkin Acar <canacar@openbsd.org>
*
@@ -101,6 +101,7 @@ void tb_start(void);
void tb_end(void);
int tbprintf(char *format, ...) GCC_PRINTFLIKE(1,2);
+int tbprintft(char *format, ...) GCC_PRINTFLIKE(1,2);
void end_line(void);
void end_page(void);
@@ -155,6 +156,7 @@ extern int columns, lines;
extern int need_update;
extern int need_sort;
+extern int separate_thousands;
extern volatile sig_atomic_t gotsig_close;
extern volatile sig_atomic_t gotsig_resize;
diff --git a/usr.bin/systat/if.c b/usr.bin/systat/if.c
index 3dfe976bdee..74aedd035af 100644
--- a/usr.bin/systat/if.c
+++ b/usr.bin/systat/if.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: if.c,v 1.19 2011/03/02 06:48:17 jasper Exp $ */
+/* $OpenBSD: if.c,v 1.20 2011/04/05 07:35:32 mpf Exp $ */
/*
* Copyright (c) 2004 Markus Friedl <markus@openbsd.org>
*
@@ -44,6 +44,7 @@ struct ifstat {
static int nifs = 0;
static int num_ifs = 0;
+static int show_bits = 0;
void print_if(void);
int read_if(void);
@@ -288,6 +289,9 @@ fetchifstat(void)
static void
showifstat(struct ifstat *ifs)
{
+ int conv = show_bits ? 8 : 1;
+ int div = show_bits ? 1000 : 1024;
+
print_fld_str(FLD_IF_IFACE, ifs->ifs_name);
tb_start();
@@ -309,11 +313,11 @@ showifstat(struct ifstat *ifs)
print_fld_str(FLD_IF_DESC, ifs->ifs_description);
- print_fld_size(FLD_IF_IBYTES, ifs->ifs_cur.ifc_ib);
+ print_fld_sdiv(FLD_IF_IBYTES, ifs->ifs_cur.ifc_ib * conv, div);
print_fld_size(FLD_IF_IPKTS, ifs->ifs_cur.ifc_ip);
print_fld_size(FLD_IF_IERRS, ifs->ifs_cur.ifc_ie);
- print_fld_size(FLD_IF_OBYTES, ifs->ifs_cur.ifc_ob);
+ print_fld_sdiv(FLD_IF_OBYTES, ifs->ifs_cur.ifc_ob * conv, div);
print_fld_size(FLD_IF_OPKTS, ifs->ifs_cur.ifc_op);
print_fld_size(FLD_IF_OERRS, ifs->ifs_cur.ifc_oe);
@@ -325,13 +329,16 @@ showifstat(struct ifstat *ifs)
static void
showtotal(void)
{
+ int conv = show_bits ? 8 : 1;
+ int div = show_bits ? 1000 : 1024;
+
print_fld_str(FLD_IF_IFACE, "Totals");
- print_fld_size(FLD_IF_IBYTES, sum.ifc_ib);
+ print_fld_sdiv(FLD_IF_IBYTES, sum.ifc_ib * conv, div);
print_fld_size(FLD_IF_IPKTS, sum.ifc_ip);
print_fld_size(FLD_IF_IERRS, sum.ifc_ie);
- print_fld_size(FLD_IF_OBYTES, sum.ifc_ob);
+ print_fld_sdiv(FLD_IF_OBYTES, sum.ifc_ob * conv, div);
print_fld_size(FLD_IF_OPKTS, sum.ifc_op);
print_fld_size(FLD_IF_OERRS, sum.ifc_oe);
@@ -360,6 +367,17 @@ if_keyboard_callback(int ch)
bzero(&ifs->ifs_old, sizeof(ifs->ifs_old));
gotsig_alarm = 1;
break;
+ case 'B':
+ show_bits = !show_bits;
+ if (show_bits) {
+ FLD_IF_IBYTES->title = "IBITS";
+ FLD_IF_OBYTES->title = "OBITS";
+ } else {
+ FLD_IF_IBYTES->title = "IBYTES";
+ FLD_IF_OBYTES->title = "OBYTES";
+ }
+ gotsig_alarm = 1;
+ break;
case 't':
state = TIME;
gotsig_alarm = 1;
diff --git a/usr.bin/systat/main.c b/usr.bin/systat/main.c
index 4f6662d0c06..44461697171 100644
--- a/usr.bin/systat/main.c
+++ b/usr.bin/systat/main.c
@@ -1,4 +1,4 @@
-/* $Id: main.c,v 1.58 2011/03/31 06:12:34 lum Exp $ */
+/* $Id: main.c,v 1.59 2011/04/05 07:35:32 mpf Exp $ */
/*
* Copyright (c) 2001, 2007 Can Erkin Acar
* Copyright (c) 2001 Daniel Hartmeier
@@ -327,6 +327,10 @@ keyboard_callback(int ch)
case 's':
command_set(&cm_delay, NULL);
break;
+ case ',':
+ separate_thousands = !separate_thousands;
+ gotsig_alarm = 1;
+ break;
case ':':
command_set(&cm_compat, NULL);
break;
diff --git a/usr.bin/systat/systat.1 b/usr.bin/systat/systat.1
index c12edb0d5af..a983ce56a5d 100644
--- a/usr.bin/systat/systat.1
+++ b/usr.bin/systat/systat.1
@@ -1,4 +1,4 @@
-.\" $OpenBSD: systat.1,v 1.90 2011/01/18 03:38:05 lum Exp $
+.\" $OpenBSD: systat.1,v 1.91 2011/04/05 07:35:32 mpf Exp $
.\" $NetBSD: systat.1,v 1.6 1996/05/10 23:16:39 thorpej Exp $
.\"
.\" Copyright (c) 1985, 1990, 1993
@@ -30,7 +30,7 @@
.\"
.\" @(#)systat.1 8.2 (Berkeley) 12/30/93
.\"
-.Dd $Mdocdate: January 18 2011 $
+.Dd $Mdocdate: April 5 2011 $
.Dt SYSTAT 1
.Os
.Sh NAME
@@ -175,6 +175,8 @@ Quit
.Nm .
.It Ic r
Reverse the selected ordering if supported by the view.
+.It Ic \&,
+Print numbers with thousand separators, where applicable.
.It Ic ^A \*(Ba Aq Ic Home
Jump to the beginning of the current view.
.It Ic ^B \*(Ba Aq Ic right arrow
@@ -257,6 +259,9 @@ represent whether the interface is connected or not;
in the case of
.Xr carp 4
interfaces, whether the interface is in master or backup state, respectively.
+The character
+.Ic B
+changes the counter view between bytes and bits.
.\"See below for more options.
.It Ic iostat
Display statistics about disk throughput.