diff mbox series

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

Message ID 20200505153741.223354-1-edumazet@google.com
State Accepted
Delegated to: stephen hemminger
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(-)

Comments

Stephen Hemminger May 5, 2020, 5:05 p.m. UTC | #1
On Tue,  5 May 2020 08:37:41 -0700
Eric Dumazet <edumazet@google.com> wrote:

> 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>

Applied, and add terrabit as well.

It looks like tc and ss are not the same in suffix here.

https://en.wikipedia.org/wiki/Data-rate_units

kilobit per sec  = kbit/sec or kb/s = kbps
kilobyte per sec = kB/s
megabit per sec  = Mbit/sec or Mb/s = Mbps

For now, lets get ss to follow the standard. 
That means "kbps" instead of "Kbps"
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);