Re: svn commit: r291090 - head/usr.bin/vmstat

2015-11-20 Thread Oliver Pinter
On 11/20/15, Craig Rodrigues  wrote:
> Author: rodrigc
> Date: Fri Nov 20 05:15:34 2015
> New Revision: 291090
> URL: https://svnweb.freebsd.org/changeset/base/291090
>
> Log:
>   Convert vmstat to use libxo.
>
>   This patch was based on this patch:
>   https://github.com/Juniper/libxo/blob/master/patches/vmstat.patch
>
>   by Phil Shafer at Juniper Networks, but updated to the latest
>   vmstat code.
>
>   Reviewed by:   allanjude
>   Differential Revision: https://reviews.freebsd.org/D3935
>
> Modified:
>   head/usr.bin/vmstat/Makefile
>   head/usr.bin/vmstat/vmstat.8
>   head/usr.bin/vmstat/vmstat.c
>
> Modified: head/usr.bin/vmstat/Makefile
> ==
> --- head/usr.bin/vmstat/Makefile  Fri Nov 20 03:24:04 2015
> (r291089)
> +++ head/usr.bin/vmstat/Makefile  Fri Nov 20 05:15:34 2015
> (r291090)
> @@ -3,7 +3,7 @@
>
>  PROG=vmstat
>  MAN= vmstat.8
> -LIBADD=  devstat kvm memstat util
> +LIBADD=  devstat kvm memstat xo util
>
>  WARNS?=  1
>
>
> Modified: head/usr.bin/vmstat/vmstat.8
> ==
> --- head/usr.bin/vmstat/vmstat.8  Fri Nov 20 03:24:04 2015
> (r291089)
> +++ head/usr.bin/vmstat/vmstat.8  Fri Nov 20 05:15:34 2015
> (r291090)
> @@ -28,7 +28,7 @@
>  .\"  @(#)vmstat.88.1 (Berkeley) 6/6/93
>  .\" $FreeBSD$
>  .\"
> -.Dd August 8, 2014
> +.Dd November 19, 2015
>  .Dt VMSTAT 8
>  .Os
>  .Sh NAME
> @@ -37,6 +37,7 @@
>  .Sh SYNOPSIS
>  .Nm
>  .\" .Op Fl fimst
> +.Op Fl -libxo
>  .Op Fl afHhimoPsz
>  .Op Fl M Ar core Op Fl N Ar system
>  .Op Fl c Ar count
> @@ -68,6 +69,13 @@ the default image).
>  .Pp
>  The options are as follows:
>  .Bl -tag -width indent
> +.It Fl -libxo
> +Generate output via
> +.Xr libxo 3
> +in a selection of different human and machine readable formats.
> +See
> +.Xr xo_parse_args 3
> +for details on command line arguments.
>  .It Fl a
>  When used with
>  .Fl i ,
> @@ -361,6 +369,8 @@ statistics every second.
>  .Xr ps 1 ,
>  .Xr systat 1 ,
>  .Xr libmemstat 3 ,
> +.Xr libxo 3 ,
> +.Xr xo_parse_args 3 ,
>  .Xr gstat 8 ,
>  .Xr iostat 8 ,
>  .Xr pstat 8 ,
>
> Modified: head/usr.bin/vmstat/vmstat.c
> ==
> --- head/usr.bin/vmstat/vmstat.c  Fri Nov 20 03:24:04 2015
> (r291089)
> +++ head/usr.bin/vmstat/vmstat.c  Fri Nov 20 05:15:34 2015
> (r291090)
> @@ -76,6 +76,9 @@ __FBSDID("$FreeBSD$");
>  #include 
>  #include 
>  #include 
> +#include 
> +
> +#define VMSTAT_XO_VERSION "1"
>
>  static char da[] = "da";
>
> @@ -184,6 +187,11 @@ main(int argc, char *argv[])
>   interval = reps = todo = 0;
>   maxshowdevs = 2;
>   hflag = isatty(1);
> +
> + argc = xo_parse_args(argc, argv);
> + if (argc < 0)
> + return argc;
> +
>   while ((c = getopt(argc, argv, "ac:fhHiM:mN:n:oPp:stw:z")) != -1) {
>   switch (c) {
>   case 'a':
> @@ -220,7 +228,7 @@ main(int argc, char *argv[])
>   nflag = 1;
>   maxshowdevs = atoi(optarg);
>   if (maxshowdevs < 0)
> - errx(1, "number of devices %d is < 0",
> + xo_errx(1, "number of devices %d is < 0",
>maxshowdevs);
>   break;
>   case 'o':
> @@ -228,7 +236,7 @@ main(int argc, char *argv[])
>   break;
>   case 'p':
>   if (devstat_buildmatch(optarg, , _matches) 
> != 0)
> - errx(1, "%s", devstat_errbuf);
> + xo_errx(1, "%s", devstat_errbuf);
>   break;
>   case 's':
>   todo |= SUMSTAT;
> @@ -237,7 +245,7 @@ main(int argc, char *argv[])
>  #ifdef notyet
>   todo |= TIMESTAT;
>  #else
> - errx(EX_USAGE, "sorry, -t is not (re)implemented yet");
> + xo_errx(EX_USAGE, "sorry, -t is not (re)implemented 
> yet");
>  #endif
>   break;
>   case 'w':
> @@ -256,18 +264,21 @@ main(int argc, char *argv[])
>   argc -= optind;
>   argv += optind;
>
> + xo_set_version(VMSTAT_XO_VERSION);
>   if (todo == 0)
>   todo = VMSTAT;
>
>   if (memf != NULL) {
>   kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
>   if (kd == NULL)
> - errx(1, "kvm_openfiles: %s", errbuf);
> + xo_errx(1, "kvm_openfiles: %s", errbuf);
>   }
>
>  retry_nlist:
>   if (kd != NULL && (c = kvm_nlist(kd, namelist)) != 0) {
>   if (c > 0) {
> + int bufsize = 0, len = 0;
> + char *buf, *bp;
>   /*
>  

Re: svn commit: r291090 - head/usr.bin/vmstat

2015-11-20 Thread Alexey Dokuchaev
On Fri, Nov 20, 2015 at 12:01:36PM +0100, Oliver Pinter wrote:
> On 11/20/15, Craig Rodrigues  wrote:
> > New Revision: 291090
> > URL: https://svnweb.freebsd.org/changeset/base/291090
> >
> > Log:
> >   Convert vmstat to use libxo.
> >
> > [...]
> > @@ -774,49 +804,64 @@ dovmstat(unsigned int interval, int reps
> >
> > fill_vmmeter();
> > fill_vmtotal();
> > -   (void)printf("%1d %1d %1d",
> > +   xo_open_container("processes");
> > +   xo_emit("{:runnable/%1d} {:waiting/%ld} "
> > +   "{:swapped-out/%ld}",
> 
> Are these %1d -> %ld conversions here intended or a typo?

Oliver, please learn how to quote email when replying properly, thank you.

./danfe
___
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"


Re: svn commit: r291090 - head/usr.bin/vmstat

2015-11-20 Thread John Baldwin
On Friday, November 20, 2015 05:15:35 AM Craig Rodrigues wrote:
> Author: rodrigc
> Date: Fri Nov 20 05:15:34 2015
> New Revision: 291090
> URL: https://svnweb.freebsd.org/changeset/base/291090
> 
> Log:
>   Convert vmstat to use libxo.
>   
>   This patch was based on this patch:
>   https://github.com/Juniper/libxo/blob/master/patches/vmstat.patch
>   
>   by Phil Shafer at Juniper Networks, but updated to the latest
>   vmstat code.
>   
>   Reviewed by:   allanjude
>   Differential Revision: https://reviews.freebsd.org/D3935
>
> ...
> 
> Modified: head/usr.bin/vmstat/vmstat.c
> ==
> --- head/usr.bin/vmstat/vmstat.c  Fri Nov 20 03:24:04 2015
> (r291089)
> +++ head/usr.bin/vmstat/vmstat.c  Fri Nov 20 05:15:34 2015
> (r291090)
> @@ -277,20 +288,34 @@ retry_nlist:
>   namelist[X_SUM].n_name = "_cnt";
>   goto retry_nlist;
>   }
> - warnx("undefined symbols:");
>   for (c = 0;
>c < (int)(sizeof(namelist)/sizeof(namelist[0]));
>c++)
>   if (namelist[c].n_type == 0)
> - (void)fprintf(stderr, " %s",
> + bufsize += strlen(namelist[c].n_name) + 
> 1;
> + bufsize += len + 1;
> + buf = bp = alloca(bufsize);
> +
> + for (c = 0;
> +  c < (int)(sizeof(namelist)/sizeof(namelist[0]));
> +  c++)
> + if (namelist[c].n_type == 0) {
> + xo_error(" %s",
>   namelist[c].n_name);
> - (void)fputc('\n', stderr);
> + len = strlen(namelist[c].n_name);
> + *bp++ = ' ';
> + memcpy(bp, namelist[c].n_name, len);
> + bp += len;
> + }
> + *bp = '\0';
> + xo_error("undefined symbols:\n", buf);

Please use some sort of string builder (sbuf or open_memstream())
instead of manual string assembly.  The former is easier to read
and the latter is more error-prone.

Suggested replacement:

FILE *fp;

fp = open_memstream(, );
for (c = 0; c < nitems(namelist); c++) {
if (namelist[c].n_type == 0) {
xo_error(" %s",
namelist[c].n_name);
(void)fprintf(fp, " %s",
namelist[c].n_name);
}
}
fclose(fp);
xo_error("undefined symbols:\n", buf);
free(buf);

-- 
John Baldwin
___
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"


svn commit: r291090 - head/usr.bin/vmstat

2015-11-19 Thread Craig Rodrigues
Author: rodrigc
Date: Fri Nov 20 05:15:34 2015
New Revision: 291090
URL: https://svnweb.freebsd.org/changeset/base/291090

Log:
  Convert vmstat to use libxo.
  
  This patch was based on this patch:
  https://github.com/Juniper/libxo/blob/master/patches/vmstat.patch
  
  by Phil Shafer at Juniper Networks, but updated to the latest
  vmstat code.
  
  Reviewed by:   allanjude
  Differential Revision: https://reviews.freebsd.org/D3935

Modified:
  head/usr.bin/vmstat/Makefile
  head/usr.bin/vmstat/vmstat.8
  head/usr.bin/vmstat/vmstat.c

Modified: head/usr.bin/vmstat/Makefile
==
--- head/usr.bin/vmstat/MakefileFri Nov 20 03:24:04 2015
(r291089)
+++ head/usr.bin/vmstat/MakefileFri Nov 20 05:15:34 2015
(r291090)
@@ -3,7 +3,7 @@
 
 PROG=  vmstat
 MAN=   vmstat.8
-LIBADD=devstat kvm memstat util
+LIBADD=devstat kvm memstat xo util
 
 WARNS?=1
 

Modified: head/usr.bin/vmstat/vmstat.8
==
--- head/usr.bin/vmstat/vmstat.8Fri Nov 20 03:24:04 2015
(r291089)
+++ head/usr.bin/vmstat/vmstat.8Fri Nov 20 05:15:34 2015
(r291090)
@@ -28,7 +28,7 @@
 .\"@(#)vmstat.88.1 (Berkeley) 6/6/93
 .\" $FreeBSD$
 .\"
-.Dd August 8, 2014
+.Dd November 19, 2015
 .Dt VMSTAT 8
 .Os
 .Sh NAME
@@ -37,6 +37,7 @@
 .Sh SYNOPSIS
 .Nm
 .\" .Op Fl fimst
+.Op Fl -libxo
 .Op Fl afHhimoPsz
 .Op Fl M Ar core Op Fl N Ar system
 .Op Fl c Ar count
@@ -68,6 +69,13 @@ the default image).
 .Pp
 The options are as follows:
 .Bl -tag -width indent
+.It Fl -libxo
+Generate output via
+.Xr libxo 3
+in a selection of different human and machine readable formats.
+See
+.Xr xo_parse_args 3
+for details on command line arguments.
 .It Fl a
 When used with
 .Fl i ,
@@ -361,6 +369,8 @@ statistics every second.
 .Xr ps 1 ,
 .Xr systat 1 ,
 .Xr libmemstat 3 ,
+.Xr libxo 3 ,
+.Xr xo_parse_args 3 ,
 .Xr gstat 8 ,
 .Xr iostat 8 ,
 .Xr pstat 8 ,

Modified: head/usr.bin/vmstat/vmstat.c
==
--- head/usr.bin/vmstat/vmstat.cFri Nov 20 03:24:04 2015
(r291089)
+++ head/usr.bin/vmstat/vmstat.cFri Nov 20 05:15:34 2015
(r291090)
@@ -76,6 +76,9 @@ __FBSDID("$FreeBSD$");
 #include 
 #include 
 #include 
+#include 
+
+#define VMSTAT_XO_VERSION "1"
 
 static char da[] = "da";
 
@@ -184,6 +187,11 @@ main(int argc, char *argv[])
interval = reps = todo = 0;
maxshowdevs = 2;
hflag = isatty(1);
+
+   argc = xo_parse_args(argc, argv);
+   if (argc < 0)
+   return argc;
+
while ((c = getopt(argc, argv, "ac:fhHiM:mN:n:oPp:stw:z")) != -1) {
switch (c) {
case 'a':
@@ -220,7 +228,7 @@ main(int argc, char *argv[])
nflag = 1;
maxshowdevs = atoi(optarg);
if (maxshowdevs < 0)
-   errx(1, "number of devices %d is < 0",
+   xo_errx(1, "number of devices %d is < 0",
 maxshowdevs);
break;
case 'o':
@@ -228,7 +236,7 @@ main(int argc, char *argv[])
break;
case 'p':
if (devstat_buildmatch(optarg, , _matches) 
!= 0)
-   errx(1, "%s", devstat_errbuf);
+   xo_errx(1, "%s", devstat_errbuf);
break;
case 's':
todo |= SUMSTAT;
@@ -237,7 +245,7 @@ main(int argc, char *argv[])
 #ifdef notyet
todo |= TIMESTAT;
 #else
-   errx(EX_USAGE, "sorry, -t is not (re)implemented yet");
+   xo_errx(EX_USAGE, "sorry, -t is not (re)implemented 
yet");
 #endif
break;
case 'w':
@@ -256,18 +264,21 @@ main(int argc, char *argv[])
argc -= optind;
argv += optind;
 
+   xo_set_version(VMSTAT_XO_VERSION);
if (todo == 0)
todo = VMSTAT;
 
if (memf != NULL) {
kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
if (kd == NULL)
-   errx(1, "kvm_openfiles: %s", errbuf);
+   xo_errx(1, "kvm_openfiles: %s", errbuf);
}
 
 retry_nlist:
if (kd != NULL && (c = kvm_nlist(kd, namelist)) != 0) {
if (c > 0) {
+   int bufsize = 0, len = 0;
+   char *buf, *bp;
/*
 * 'cnt' was renamed to 'vm_cnt'. If 'vm_cnt' is not
 * found try looking up older 'cnt' symbol.
@@ -277,20 +288,34 @@ retry_nlist:
namelist[X_SUM].n_name = "_cnt";