Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bandwidth2: use binary and decimal prefixes as appropriate #518

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions bandwidth2/bandwidth2.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void get_values(char **const ifaces, int num_ifaces, time_t * const s, ulli * co
}
}

void display(int const unit, int const divisor,
void display(int const unit, int const divisor, char **const prefixes,
double b, int const warning, int const critical)
{
char fmtstr[7];
Expand All @@ -96,18 +96,13 @@ void display(int const unit, int const divisor,

snprintf(fmtstr, sizeof (fmtstr), "%%%d.1lf", divisor > 1000 ? 6 : 5);

if (b < divisor) {
printf(fmtstr, b);
printf(" %c/s", unit);
} else if (b < divisor * divisor) {
printf(fmtstr, b / divisor);
printf("K%c/s", unit);
} else if (b < divisor * divisor * divisor) {
printf(fmtstr, b / (divisor * divisor));
printf("M%c/s", unit);
} else {
printf(fmtstr, b / (divisor * divisor * divisor));
printf("G%c/s", unit);
int d = 1;
for (int i = 0; prefixes[i] != NULL; d *= divisor, i++) {
if (b < d*divisor) {
printf(fmtstr, b/d);
printf(" %s%c/s", prefixes[i], unit);
break;
}
}
printf("</span>");
}
Expand Down Expand Up @@ -136,6 +131,10 @@ int main(int argc, char *argv[])
char *envvar = NULL;
char *label = "";

char *prefixes_SI[] = { " ", "K", "M", "G", 0 };
char *prefixes_BI[] = { " ", "Ki", "Mi", "Gi", 0 };
char **prefixes = prefixes_BI;

envvar = getenv("USE_BITS");
if (envvar && *envvar == '1')
unit = 'b';
Expand Down Expand Up @@ -164,8 +163,10 @@ int main(int argc, char *argv[])
if (envvar)
criticaltx = atoi(envvar);
envvar = getenv("USE_SI");
if (envvar && *envvar == '1')
if (envvar && *envvar == '1') {
divisor = 1000;
prefixes = prefixes_SI;
}
envvar = getenv("LABEL");
if (envvar)
label = envvar;
Expand All @@ -190,6 +191,7 @@ int main(int argc, char *argv[])
break;
case 's':
divisor = 1000;
prefixes = prefixes_SI;
break;
case 'h':
usage(argv);
Expand All @@ -212,9 +214,9 @@ int main(int argc, char *argv[])
rx = (received - received_old) / (float)(s - s_old);
tx = (sent - sent_old) / (float)(s - s_old);
printf("%s", label);
display(unit, divisor, rx, warningrx, criticalrx);
display(unit, divisor, prefixes, rx, warningrx, criticalrx);
printf(" ");
display(unit, divisor, tx, warningtx, criticaltx);
display(unit, divisor, prefixes, tx, warningtx, criticaltx);
printf("\n");
fflush(stdout);
s_old = s;
Expand Down