public inbox for netdev@vger.kernel.org 
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber•org>
To: netdev@vger•kernel.org
Cc: Stephen Hemminger <sthemmin@microsoft•com>,
	Stephen Hemminger <stephen@networkplumber•org>
Subject: [PATCH iproute2-next 8/9] tc/util: add print helpers for JSON
Date: Mon,  9 Jul 2018 12:48:55 -0700	[thread overview]
Message-ID: <20180709194856.18922-9-stephen@networkplumber.org> (raw)
In-Reply-To: <20180709194856.18922-1-stephen@networkplumber.org>

From: Stephen Hemminger <sthemmin@microsoft•com>

Add a helper to print rate and size in numeric or pretty format
based on JSON flag.

Signed-off-by: Stephen Hemminger <stephen@networkplumber•org>
---
 tc/tc_util.c | 59 ++++++++++++++++++++++++++--------------------------
 tc/tc_util.h |  2 ++
 2 files changed, 31 insertions(+), 30 deletions(-)

diff --git a/tc/tc_util.c b/tc/tc_util.c
index 95cb49b98612..787c3fc8a57a 100644
--- a/tc/tc_util.c
+++ b/tc/tc_util.c
@@ -330,6 +330,19 @@ char *sprint_rate(__u64 rate, char *buf)
 	return buf;
 }
 
+/*
+ * print rate as numeric in JSON
+ * or in human format otherwise.
+ */
+void print_rate(const char *key, const char *fmt, __u64 rate)
+{
+	SPRINT_BUF(b1);
+
+	print_u64(PRINT_JSON, key, NULL, rate);
+	print_string(PRINT_FP, NULL, fmt,
+		     sprint_rate(rate, b1));
+}
+
 int get_time(unsigned int *time, const char *str)
 {
 	double t;
@@ -462,6 +475,15 @@ char *sprint_size(__u32 sz, char *buf)
 	return buf;
 }
 
+void print_size(const char *key, const char *fmt, __u32 sz)
+{
+	SPRINT_BUF(b1);
+
+	print_uint(PRINT_JSON, key, NULL, sz);
+	print_string(PRINT_FP, NULL, fmt,
+		     sprint_size(sz, b1));
+}
+
 void print_qdisc_handle(char *buf, int len, __u32 h)
 {
 	snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
@@ -792,7 +814,6 @@ void print_tm(FILE *f, const struct tcf_t *tm)
 void print_tcstats2_attr(FILE *fp, struct rtattr *rta,
 			 const char *prefix, struct rtattr **xstats)
 {
-	SPRINT_BUF(b1);
 	struct rtattr *tbs[TCA_STATS_MAX + 1];
 
 	parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
@@ -823,21 +844,15 @@ void print_tcstats2_attr(FILE *fp, struct rtattr *rta,
 		       MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST64]),
 			   sizeof(re)));
 		print_string(PRINT_FP, NULL, "\n%s", prefix);
-		print_lluint(PRINT_JSON, "rate", NULL, re.bps);
-		print_string(PRINT_FP, NULL, "rate %s",
-			     sprint_rate(re.bps, b1));
+		print_rate("rate", "rate %s", re.bps);
 		print_lluint(PRINT_ANY, "pps", " %llupps", re.pps);
 	} else if (tbs[TCA_STATS_RATE_EST]) {
 		struct gnet_stats_rate_est re = {0};
 
 		memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]),
 		       MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re)));
-		fprintf(fp, "\n%srate %s %upps ",
-			prefix, sprint_rate(re.bps, b1), re.pps);
 		print_string(PRINT_FP, NULL, "\n%s", prefix);
-		print_uint(PRINT_JSON, "rate", NULL, re.bps);
-		print_string(PRINT_FP, NULL, "rate %s",
-			     sprint_rate(re.bps, b1));
+		print_rate("rate", "rate %s", re.bps);
 		print_uint(PRINT_ANY, "pps", " %upps", re.pps);
 	}
 
@@ -847,9 +862,7 @@ void print_tcstats2_attr(FILE *fp, struct rtattr *rta,
 		memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
 		if (!tbs[TCA_STATS_RATE_EST])
 			print_string(PRINT_FP, NULL, "\n%s", prefix);
-		print_uint(PRINT_JSON, "backlog", NULL, q.backlog);
-		print_string(PRINT_FP, NULL, "backlog %s",
-			     sprint_size(q.backlog, b1));
+		print_size("backlog",  "backlog %s", q.backlog);
 		print_uint(PRINT_ANY, "qlen", " %up", q.qlen);
 		print_uint(PRINT_FP, NULL, " requeues %u", q.requeues);
 	}
@@ -861,8 +874,6 @@ void print_tcstats2_attr(FILE *fp, struct rtattr *rta,
 void print_tcstats_attr(FILE *fp, struct rtattr *tb[],
 			const char *prefix, struct rtattr **xstats)
 {
-	SPRINT_BUF(b1);
-
 	if (tb[TCA_STATS2]) {
 		print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
 		if (xstats && NULL == *xstats)
@@ -887,26 +898,14 @@ void print_tcstats_attr(FILE *fp, struct rtattr *tb[],
 
 			if (st.bps || st.pps) {
 				print_string(PRINT_FP, NULL, "%s", "rate ");
-				print_uint(PRINT_JSON, "rate", NULL, st.bps);
-				if (st.bps)
-					print_string(PRINT_FP, NULL, "%s ",
-						     sprint_rate(st.bps, b1));
-
-				print_uint(PRINT_JSON, "pps", NULL, st.pps);
-				if (st.pps)
-					print_uint(PRINT_FP, NULL, "%upps ", st.pps);
+				print_rate("rate", "%s ", st.bps);
+				print_uint(PRINT_JSON, "pps", "%upps ", st.pps);
 			}
 			if (st.qlen || st.backlog) {
 				print_string(PRINT_FP, NULL, "%s", "backlog ");
 
-				print_uint(PRINT_JSON, "backlog", NULL, st.backlog);
-				print_uint(PRINT_JSON, "qlen", NULL, st.qlen);
-				if (st.backlog)
-					print_string(PRINT_FP, NULL,
-						     "%s ", sprint_size(st.backlog, b1));
-				if (st.qlen)
-					print_uint(PRINT_FP, NULL,
-						     "%up ", st.qlen);
+				print_size("backlog", "%s ", st.backlog);
+				print_uint(PRINT_ANY, "qlen", "%up ", st.qlen);
 			}
 		}
 	}
diff --git a/tc/tc_util.h b/tc/tc_util.h
index 16babd21b473..d6bdfe035050 100644
--- a/tc/tc_util.h
+++ b/tc/tc_util.h
@@ -88,6 +88,8 @@ void print_qdisc_handle(char *buf, int len, __u32 h);
 void print_linklayer(char *buf, int len, unsigned int linklayer);
 void print_devname(enum output_type type, int ifindex);
 
+void print_rate(const char *key, const char *fmt, __u64 rate);
+void print_size(const char *key, const char *fmt, __u32 sz);
 char *sprint_rate(__u64 rate, char *buf);
 char *sprint_size(__u32 size, char *buf);
 char *sprint_qdisc_handle(__u32 h, char *buf);
-- 
2.18.0

  parent reply	other threads:[~2018-07-09 19:49 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-09 19:48 [PATCH iproute2-next 0/9] TC more JSON support Stephen Hemminger
2018-07-09 19:48 ` [PATCH iproute2-next 1/9] tc: use JSON in error handling Stephen Hemminger
2018-07-09 19:48 ` [PATCH iproute2-next 2/9] tc: use const char in util Stephen Hemminger
2018-07-09 19:48 ` [PATCH iproute2-next 3/9] tc: convert stats print to json Stephen Hemminger
2018-07-09 19:48 ` [PATCH iproute2-next 4/9] tc/cbq: use sprint_rate Stephen Hemminger
2018-07-09 19:48 ` [PATCH iproute2-next 5/9] tc/util: remove print_rate Stephen Hemminger
2018-07-09 19:48 ` [PATCH iproute2-next 6/9] tc/util: remove unused print_size Stephen Hemminger
2018-07-09 19:48 ` [PATCH iproute2-next 7/9] tc/util: remove unused print_time Stephen Hemminger
2018-07-09 19:48 ` Stephen Hemminger [this message]
2018-07-09 19:48 ` [PATCH iproute2-next 9/9] tc/sfq: add json support Stephen Hemminger
2018-07-13 20:48 ` [PATCH iproute2-next 0/9] TC more JSON support David Ahern
2018-07-13 21:29   ` Jakub Kicinski
2018-07-13 22:22     ` Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180709194856.18922-9-stephen@networkplumber.org \
    --to=stephen@networkplumber$(echo .)org \
    --cc=netdev@vger$(echo .)kernel.org \
    --cc=sthemmin@microsoft$(echo .)com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox