tc: more user friendly rates

Display more user friendly rates.

10Mbit is more readable than 10000Kbit

Before :
class htb 1:2 root prio 0 rate 10000Kbit ceil 10000Kbit ...

After:
class htb 1:2 root prio 0 rate 10Mbit ceil 10Mbit ...

Signed-off-by: Eric Dumazet <edumazet@google.com>
This commit is contained in:
Eric Dumazet 2013-11-21 22:37:17 -08:00 committed by Stephen Hemminger
parent 4a79c7a2dc
commit 8cecdc2837

View File

@ -173,28 +173,22 @@ int get_rate(unsigned *rate, const char *str)
void print_rate(char *buf, int len, __u64 rate) void print_rate(char *buf, int len, __u64 rate)
{ {
double tmp = (double)rate*8;
extern int use_iec; extern int use_iec;
unsigned long kilo = use_iec ? 1024 : 1000;
const char *str = use_iec ? "i" : "";
int i = 0;
static char *units[5] = {"", "K", "M", "G", "T"};
if (use_iec) { rate <<= 3; /* bytes/sec -> bits/sec */
if (tmp >= 1000.0*1024.0*1024.0*1024.0)
snprintf(buf, len, "%.0fGibit", tmp/(1024.0*1024.0*1024.0)); for (i = 0; i < ARRAY_SIZE(units); i++) {
else if (tmp >= 1000.0*1024.0*1024.0) if (rate < kilo)
snprintf(buf, len, "%.0fMibit", tmp/(1024.0*1024.0)); break;
else if (tmp >= 1000.0*1024) if (((rate % kilo) != 0) && rate < 1000*kilo)
snprintf(buf, len, "%.0fKibit", tmp/1024); break;
else rate /= kilo;
snprintf(buf, len, "%.0fbit", tmp);
} else {
if (tmp >= 1000.0*1000000000.0)
snprintf(buf, len, "%.0fGbit", tmp/1000000000.0);
else if (tmp >= 1000.0*1000000.0)
snprintf(buf, len, "%.0fMbit", tmp/1000000.0);
else if (tmp >= 1000.0 * 1000.0)
snprintf(buf, len, "%.0fKbit", tmp/1000.0);
else
snprintf(buf, len, "%.0fbit", tmp);
} }
snprintf(buf, len, "%.0f%s%sbit", (double)rate, units[i], str);
} }
char * sprint_rate(__u64 rate, char *buf) char * sprint_rate(__u64 rate, char *buf)