Skip to contentSkip to conversation

ss: add support for Gbit speeds in sprint_bw()

PATCH Applied Eric Dumazet 2 participants 2 messages May 5, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ss: add support for Gbit speeds in sprint_bw()

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
---
 misc/ss.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
misc/ss.c +6 -4
index 3ef151fbf1f1b3856e95a1baa751a1cdd27d10b7..ab206b2011ec92b899709d2c78ce7310e88ec80e 100644
@@ -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);
 
Signature · 1 line
2.26.2.526.g744177e7f7-goog
#1 lore raw
Quoted 10 lines · On Tue, 5 May 2020 08:37:41 -0700 Eric Dumazet <[email protected]> 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

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"