diff mbox series

[iproute2] ss: add support for Gbit speeds in sprint_bw()

Message ID 20200505153741.223354-1-edumazet@google.com
State New
Headers show
Series [iproute2] ss: add support for Gbit speeds in sprint_bw() | expand

Commit Message

Eric Dumazet May 5, 2020, 3:37 p.m. UTC
Also use 'g' specifier instead of 'f' to remove trailing zeros,
and increase precision.

Examples of output :
 Before        After
 8.0Kbps       8Kbps
 9.9Mbps       9.92Mbps
 55001Mbps     55Gbps

Signed-off-by: Eric Dumazet <edumazet@google.com>
---
 misc/ss.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/misc/ss.c b/misc/ss.c
index 3ef151fbf1f1b3856e95a1baa751a1cdd27d10b7..ab206b2011ec92b899709d2c78ce7310e88ec80e 100644
--- a/misc/ss.c
+++ b/misc/ss.c
@@ -2382,10 +2382,12 @@  static char *sprint_bw(char *buf, double bw)
 {
 	if (numeric)
 		sprintf(buf, "%.0f", bw);
-	else if (bw > 1000000.)
-		sprintf(buf, "%.1fM", bw / 1000000.);
-	else if (bw > 1000.)
-		sprintf(buf, "%.1fK", bw / 1000.);
+	else if (bw >= 1e9)
+		sprintf(buf, "%.3gG", bw / 1e9);
+	else if (bw >= 1e6)
+		sprintf(buf, "%.3gM", bw / 1e6);
+	else if (bw >= 1e3)
+		sprintf(buf, "%.3gK", bw / 1e3);
 	else
 		sprintf(buf, "%g", bw);