Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 28 May 2014 17:03:05 GMT
From:      zkorchev@FreeBSD.org
To:        svn-soc-all@FreeBSD.org
Subject:   socsvn commit: r268778 - soc2014/zkorchev/freebsd_head/usr.bin/vmstat
Message-ID:  <201405281703.s4SH35Ja016145@socsvn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: zkorchev
Date: Wed May 28 17:03:05 2014
New Revision: 268778
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=268778

Log:
  implemented vmstat json output for several options

Modified:
  soc2014/zkorchev/freebsd_head/usr.bin/vmstat/Makefile
  soc2014/zkorchev/freebsd_head/usr.bin/vmstat/vmstat.8
  soc2014/zkorchev/freebsd_head/usr.bin/vmstat/vmstat.c

Modified: soc2014/zkorchev/freebsd_head/usr.bin/vmstat/Makefile
==============================================================================
--- soc2014/zkorchev/freebsd_head/usr.bin/vmstat/Makefile	Wed May 28 17:01:51 2014	(r268777)
+++ soc2014/zkorchev/freebsd_head/usr.bin/vmstat/Makefile	Wed May 28 17:03:05 2014	(r268778)
@@ -2,9 +2,11 @@
 # $FreeBSD$
 
 PROG=	vmstat
+SRCS=	vmstat.c sol.c
 MAN=	vmstat.8
 DPADD=	${LIBDEVSTAT} ${LIBKVM} ${LIBMEMSTAT} ${LIBUTIL}
-LDADD=	-ldevstat -lkvm -lmemstat -lutil
+LDADD=	-L/usr/local/lib -ldevstat -lkvm -lmemstat -lutil -lyajl
+CFLAGS+= -DSOL_ON -I/usr/local/include
 
 WARNS?=	1
 

Modified: soc2014/zkorchev/freebsd_head/usr.bin/vmstat/vmstat.8
==============================================================================
--- soc2014/zkorchev/freebsd_head/usr.bin/vmstat/vmstat.8	Wed May 28 17:01:51 2014	(r268777)
+++ soc2014/zkorchev/freebsd_head/usr.bin/vmstat/vmstat.8	Wed May 28 17:03:05 2014	(r268778)
@@ -116,6 +116,8 @@
 by type.
 .It Fl n
 Change the maximum number of disks to display from the default of 2.
+.It Fl O
+Output the results in JSON format.
 .It Fl P
 Report per-cpu system/user/idle cpu statistics.
 .It Fl p

Modified: soc2014/zkorchev/freebsd_head/usr.bin/vmstat/vmstat.c
==============================================================================
--- soc2014/zkorchev/freebsd_head/usr.bin/vmstat/vmstat.c	Wed May 28 17:01:51 2014	(r268777)
+++ soc2014/zkorchev/freebsd_head/usr.bin/vmstat/vmstat.c	Wed May 28 17:03:05 2014	(r268778)
@@ -76,6 +76,12 @@
 #include <unistd.h>
 #include <libutil.h>
 
+#include "sol.h"
+
+#if defined(SOL_ON)
+static struct sol_stream sol_stream;
+#endif
+
 static char da[] = "da";
 
 static struct nlist namelist[] = {
@@ -133,6 +139,7 @@
 static int	nflag;
 static int	Pflag;
 static int	hflag;
+static int	Oflag;
 
 static kvm_t   *kd;
 
@@ -181,7 +188,7 @@
 	interval = reps = todo = 0;
 	maxshowdevs = 2;
 	hflag = isatty(1);
-	while ((c = getopt(argc, argv, "ac:fhHiM:mN:n:Pp:stw:z")) != -1) {
+	while ((c = getopt(argc, argv, "ac:fhHiM:mN:n:OPp:stw:z")) != -1) {
 		switch (c) {
 		case 'a':
 			aflag++;
@@ -220,6 +227,9 @@
 				errx(1, "number of devices %d is < 0",
 				     maxshowdevs);
 			break;
+		case 'O':
+			Oflag = 1;
+			break;
 		case 'p':
 			if (devstat_buildmatch(optarg, &matches, &num_matches) != 0)
 				errx(1, "%s", devstat_errbuf);
@@ -305,6 +315,10 @@
 	} else if (reps)
 		interval = 1 * 1000;
 
+#if defined(SOL_ON)
+	if (Oflag) sol_init(&sol_stream, SOL_JSON);
+#endif
+
 	if (todo & FORKSTAT)
 		doforkst();
 	if (todo & MEMSTAT)
@@ -321,6 +335,11 @@
 		dointr();
 	if (todo & VMSTAT)
 		dovmstat(interval, reps);
+
+#if defined(SOL_ON)
+	if (Oflag) sol_term(&sol_stream);
+#endif
+
 	exit(0);
 }
 
@@ -1012,18 +1031,65 @@
 doforkst(void)
 {
 	fill_vmmeter(&sum);
-	(void)printf("%u forks, %u pages, average %.2f\n",
-	    sum.v_forks, sum.v_forkpages,
-	    sum.v_forks == 0 ? 0.0 :
-	    (double)sum.v_forkpages / sum.v_forks);
-	(void)printf("%u vforks, %u pages, average %.2f\n",
-	    sum.v_vforks, sum.v_vforkpages,
-	    sum.v_vforks == 0 ? 0.0 :
-	    (double)sum.v_vforkpages / sum.v_vforks);
-	(void)printf("%u rforks, %u pages, average %.2f\n",
-	    sum.v_rforks, sum.v_rforkpages,
-	    sum.v_rforks == 0 ? 0.0 :
-	    (double)sum.v_rforkpages / sum.v_rforks);
+
+#if defined(SOL_ON)
+	if (Oflag) {
+		sol_map_start(&sol_stream);
+
+		sol_map_key(&sol_stream, "fork", 4);
+		sol_map_start(&sol_stream);
+		sol_map_key(&sol_stream, "count", 5);
+		sol_map_integer(&sol_stream, sum.v_forks);
+		sol_map_key(&sol_stream, "pages", 5);
+		sol_map_integer(&sol_stream, sum.v_forkpages);
+		sol_map_key(&sol_stream, "average", 7);
+		sol_map_float(&sol_stream, ((sum.v_forks == 0) ?
+			0.0 :
+			(double)sum.v_forkpages / sum.v_forks));
+		sol_map_end(&sol_stream);
+
+		sol_map_key(&sol_stream, "vfork", 5);
+		sol_map_start(&sol_stream);
+		sol_map_key(&sol_stream, "count", 5);
+		sol_map_integer(&sol_stream, sum.v_vforks);
+		sol_map_key(&sol_stream, "pages", 5);
+		sol_map_integer(&sol_stream, sum.v_vforkpages);
+		sol_map_key(&sol_stream, "average", 7);
+		sol_map_float(&sol_stream, ((sum.v_vforks == 0) ?
+			0.0 :
+			(double)sum.v_vforkpages / sum.v_vforks));
+		sol_map_end(&sol_stream);
+
+		sol_map_key(&sol_stream, "rfork", 5);
+		sol_map_start(&sol_stream);
+		sol_map_key(&sol_stream, "count", 5);
+		sol_map_integer(&sol_stream, sum.v_rforks);
+		sol_map_key(&sol_stream, "pages", 5);
+		sol_map_integer(&sol_stream, sum.v_rforkpages);
+		sol_map_key(&sol_stream, "average", 7);
+		sol_map_float(&sol_stream, ((sum.v_rforks == 0) ?
+			0.0 :
+			(double)sum.v_rforkpages / sum.v_rforks));
+		sol_map_end(&sol_stream);
+
+		sol_map_end(&sol_stream);
+	}
+	else
+#endif
+	{
+		(void)printf("%u forks, %u pages, average %.2f\n",
+			sum.v_forks, sum.v_forkpages,
+			sum.v_forks == 0 ? 0.0 :
+			(double)sum.v_forkpages / sum.v_forks);
+		(void)printf("%u vforks, %u pages, average %.2f\n",
+			sum.v_vforks, sum.v_vforkpages,
+			sum.v_vforks == 0 ? 0.0 :
+			(double)sum.v_vforkpages / sum.v_vforks);
+		(void)printf("%u rforks, %u pages, average %.2f\n",
+			sum.v_rforks, sum.v_rforkpages,
+			sum.v_rforks == 0 ? 0.0 :
+			(double)sum.v_rforkpages / sum.v_rforks);
+	}
 }
 
 static void
@@ -1226,29 +1292,71 @@
 				    memstat_strerror(error));
 		}
 	}
-	printf("%13s %5s %6s %7s %8s  Size(s)\n", "Type", "InUse", "MemUse",
-	    "HighUse", "Requests");
+
+#if defined(SOL_ON)
+	if (Oflag)
+		sol_map_start(&sol_stream);
+	else
+#endif
+		printf("%13s %5s %6s %7s %8s  Size(s)\n", "Type", "InUse", "MemUse",
+			"HighUse", "Requests");
+
 	for (mtp = memstat_mtl_first(mtlp); mtp != NULL;
 	    mtp = memstat_mtl_next(mtp)) {
 		if (memstat_get_numallocs(mtp) == 0 &&
 		    memstat_get_count(mtp) == 0)
 			continue;
-		printf("%13s %5" PRIu64 " %5" PRIu64 "K %7s %8" PRIu64 "  ",
-		    memstat_get_name(mtp), memstat_get_count(mtp),
-		    (memstat_get_bytes(mtp) + 1023) / 1024, "-",
-		    memstat_get_numallocs(mtp));
-		first = 1;
-		for (i = 0; i < 32; i++) {
-			if (memstat_get_sizemask(mtp) & (1 << i)) {
-				if (!first)
-					printf(",");
-				printf("%d", 1 << (i + 4));
-				first = 0;
+
+#if defined(SOL_ON)
+		if (Oflag)
+		{
+			const char *name = memstat_get_name(mtp);
+			sol_map_key(&sol_stream, name, strlen(name));
+			sol_map_start(&sol_stream);
+
+			sol_map_key(&sol_stream, "inuse", 5);
+			sol_map_integer(&sol_stream, memstat_get_count(mtp));
+
+			sol_map_key(&sol_stream, "memuse", 6);
+			sol_map_integer(&sol_stream, (memstat_get_bytes(mtp) + 1023) / 1024);
+
+			sol_map_key(&sol_stream, "requests", 8);
+			sol_map_integer(&sol_stream, memstat_get_numallocs(mtp));
+
+			sol_map_key(&sol_stream, "size", 4);
+			sol_array_start(&sol_stream);
+			for (i = 0; i < 32; i++)
+				if (memstat_get_sizemask(mtp) & (1 << i))
+					sol_integer(&sol_stream, 1 << (i + 4));
+			sol_array_end(&sol_stream);
+
+			sol_map_end(&sol_stream);
+		}
+		else
+#endif
+		{
+			printf("%13s %5" PRIu64 " %5" PRIu64 "K %7s %8" PRIu64 "  ",
+				memstat_get_name(mtp), memstat_get_count(mtp),
+				(memstat_get_bytes(mtp) + 1023) / 1024, "-",
+				memstat_get_numallocs(mtp));
+
+			first = 1;
+			for (i = 0; i < 32; i++) {
+				if (memstat_get_sizemask(mtp) & (1 << i)) {
+					if (!first)
+						printf(",");
+					printf("%d", 1 << (i + 4));
+					first = 0;
+				}
 			}
+			printf("\n");
 		}
-		printf("\n");
 	}
 	memstat_mtl_free(mtlp);
+
+#if defined(SOL_ON)
+	if (Oflag) sol_map_end(&sol_stream);
+#endif
 }
 
 static void
@@ -1350,7 +1458,7 @@
 usage(void)
 {
 	(void)fprintf(stderr, "%s%s",
-		"usage: vmstat [-afHhimPsz] [-c count] [-M core [-N system]] [-w wait]\n",
+		"usage: vmstat [-afHhimPsz] [-c count] [-M core [-N system]] [-w wait] [-O]\n",
 		"              [-n devs] [-p type,if,pass] [disks]\n");
 	exit(1);
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201405281703.s4SH35Ja016145>