diff mbox series

[iproute2-next,01/10] iplink: Fix formatting of MPLS stats

Message ID dfded37d94dc8db764f00fae062d02504885c858.1652104101.git.petrm@nvidia.com (mailing list archive)
State Accepted
Commit 72623b73c4eaceb0267f110e55c7bc1c342267b1
Delegated to: David Ahern
Headers show
Series ip stats: Support for xstats and afstats | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Petr Machata May 9, 2022, 1:59 p.m. UTC
Currently, MPLS stats are formatted this way:

 # ip -n ns0-DBZpxj8I link afstats dev veth01
 3: veth01
     mpls:
         RX: bytes  packets  errors  dropped  noroute
                  0        0       0        0       0
         TX: bytes  packets  errors  dropped
                216        2       0       0

Note how most numbers are not aligned properly under their column headers.
Fix by converting the code to use size_columns() to dynamically determine
the necessary width of individual columns, which also takes care of
formatting the table properly in case the counter values are high.
After the fix, the formatting looks as follows:

 # ip -n ns0-Y1PyEc55 link afstats dev veth01
 3: veth01
     mpls:
         RX: bytes packets errors dropped noroute
                 0       0      0       0       0
         TX: bytes packets errors dropped
               108       1      0       0

Signed-off-by: Petr Machata <petrm@nvidia.com>
Reviewed-by: Ido Schimmel <idosch@nvidia.com>
---
 ip/iplink.c | 44 ++++++++++++++++++++++++++++++++------------
 1 file changed, 32 insertions(+), 12 deletions(-)
diff mbox series

Patch

diff --git a/ip/iplink.c b/ip/iplink.c
index 23eb6c6e..b87d9bcd 100644
--- a/ip/iplink.c
+++ b/ip/iplink.c
@@ -1521,6 +1521,13 @@  static void print_mpls_stats(FILE *fp, struct rtattr *attr)
 {
 	struct rtattr *mrtb[MPLS_STATS_MAX+1];
 	struct mpls_link_stats *stats;
+	unsigned int cols[] = {
+		strlen("*X: bytes"),
+		strlen("packets"),
+		strlen("errors"),
+		strlen("dropped"),
+		strlen("noroute"),
+	};
 
 	parse_rtattr(mrtb, MPLS_STATS_MAX, RTA_DATA(attr),
 		     RTA_PAYLOAD(attr));
@@ -1528,22 +1535,35 @@  static void print_mpls_stats(FILE *fp, struct rtattr *attr)
 		return;
 
 	stats = RTA_DATA(mrtb[MPLS_STATS_LINK]);
-
 	fprintf(fp, "    mpls:\n");
-	fprintf(fp, "        RX: bytes  packets  errors  dropped  noroute\n");
+
+	size_columns(cols, ARRAY_SIZE(cols),
+		     stats->rx_bytes, stats->rx_packets, stats->rx_errors,
+		     stats->rx_dropped, stats->rx_noroute);
+	size_columns(cols, ARRAY_SIZE(cols),
+		     stats->tx_bytes, stats->tx_packets, stats->tx_errors,
+		     stats->tx_dropped, 0);
+
+	fprintf(fp, "        RX: %*s %*s %*s %*s %*s%s",
+		cols[0] - 4, "bytes", cols[1], "packets",
+		cols[2], "errors", cols[3], "dropped",
+		cols[4], "noroute", _SL_);
 	fprintf(fp, "        ");
-	print_num(fp, 10, stats->rx_bytes);
-	print_num(fp, 8, stats->rx_packets);
-	print_num(fp, 7, stats->rx_errors);
-	print_num(fp, 8, stats->rx_dropped);
-	print_num(fp, 7, stats->rx_noroute);
+	print_num(fp, cols[0], stats->rx_bytes);
+	print_num(fp, cols[1], stats->rx_packets);
+	print_num(fp, cols[2], stats->rx_errors);
+	print_num(fp, cols[3], stats->rx_dropped);
+	print_num(fp, cols[4], stats->rx_noroute);
 	fprintf(fp, "\n");
-	fprintf(fp, "        TX: bytes  packets  errors  dropped\n");
+
+	fprintf(fp, "        TX: %*s %*s %*s %*s%s",
+		cols[0] - 4, "bytes", cols[1], "packets",
+		cols[2], "errors", cols[3], "dropped", _SL_);
 	fprintf(fp, "        ");
-	print_num(fp, 10, stats->tx_bytes);
-	print_num(fp, 8, stats->tx_packets);
-	print_num(fp, 7, stats->tx_errors);
-	print_num(fp, 7, stats->tx_dropped);
+	print_num(fp, cols[0], stats->tx_bytes);
+	print_num(fp, cols[1], stats->tx_packets);
+	print_num(fp, cols[2], stats->tx_errors);
+	print_num(fp, cols[3], stats->tx_dropped);
 	fprintf(fp, "\n");
 }