lib: Move sprint_size() from tc here, add print_size()

When displaying sizes of various sorts, tc commonly uses the function
sprint_size() to format the size into a buffer as a human-readable string.
This string is then displayed either using print_string(), or in some code
even fprintf(). As a result, a typical sequence of code when formatting a
size is something like the following:

	SPRINT_BUF(b);
	print_uint(PRINT_JSON, "foo", NULL, foo);
	print_string(PRINT_FP, NULL, "foo %s ", sprint_size(foo, b));

For a concept as broadly useful as size, it would be better to have a
dedicated function in json_print.

To that end, move sprint_size() from tc_util to json_print. Add helpers
print_size() and print_color_size() that wrap arount sprint_size() and
provide the JSON dispatch as appropriate.

Since print_size() should be the preferred interface, convert vast majority
of uses of sprint_size() to print_size(). Two notable exceptions are:

- q_tbf, which does not show the size as such, but uses the string
  "$human_readable_size/$cell_size" even in JSON. There is simply no way to
  have print_size() emit the same text, because print_size() in JSON mode
  should of course just use the raw number, without human-readable frills.

- q_cake, which relies on the existence of sprint_size() in its macro-based
  formatting helpers. There might be ways to convert this particular case,
  but given q_tbf simply cannot be converted, leave it as is.

Signed-off-by: Petr Machata <me@pmachata.org>
Signed-off-by: David Ahern <dsahern@gmail.com>
This commit is contained in:
Petr Machata 2020-12-05 22:13:31 +01:00 committed by David Ahern
parent 60265cc226
commit adbe5de966
18 changed files with 91 additions and 161 deletions

View File

@ -69,6 +69,7 @@ _PRINT_FUNC(on_off, bool)
_PRINT_FUNC(null, const char*) _PRINT_FUNC(null, const char*)
_PRINT_FUNC(string, const char*) _PRINT_FUNC(string, const char*)
_PRINT_FUNC(uint, unsigned int) _PRINT_FUNC(uint, unsigned int)
_PRINT_FUNC(size, __u32)
_PRINT_FUNC(u64, uint64_t) _PRINT_FUNC(u64, uint64_t)
_PRINT_FUNC(hhu, unsigned char) _PRINT_FUNC(hhu, unsigned char)
_PRINT_FUNC(hu, unsigned short) _PRINT_FUNC(hu, unsigned short)
@ -96,4 +97,7 @@ static inline int print_rate(bool use_iec, enum output_type t,
return print_color_rate(use_iec, t, COLOR_NONE, key, fmt, rate); return print_color_rate(use_iec, t, COLOR_NONE, key, fmt, rate);
} }
/* A backdoor to the size formatter. Please use print_size() instead. */
char *sprint_size(__u32 sz, char *buf);
#endif /* _JSON_PRINT_H_ */ #endif /* _JSON_PRINT_H_ */

View File

@ -11,6 +11,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <math.h>
#include "utils.h" #include "utils.h"
#include "json_print.h" #include "json_print.h"
@ -340,3 +341,30 @@ int print_color_rate(bool use_iec, enum output_type type, enum color_attr color,
free(buf); free(buf);
return rc; return rc;
} }
char *sprint_size(__u32 sz, char *buf)
{
size_t len = SPRINT_BSIZE - 1;
double tmp = sz;
if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
snprintf(buf, len, "%gKb", rint(tmp/1024));
else
snprintf(buf, len, "%ub", sz);
return buf;
}
int print_color_size(enum output_type type, enum color_attr color,
const char *key, const char *fmt, __u32 sz)
{
SPRINT_BUF(buf);
if (_IS_JSON_CONTEXT(type))
return print_color_uint(type, color, key, "%u", sz);
sprint_size(sz, buf);
return print_color_string(type, color, key, fmt, buf);
}

View File

@ -465,10 +465,8 @@ static int print_gate_list(struct rtattr *list)
} }
if (maxoctets != -1) { if (maxoctets != -1) {
memset(buf, 0, sizeof(buf)); print_size(PRINT_ANY, "max_octets", "\t max-octets %s",
print_uint(PRINT_JSON, "max_octets", NULL, maxoctets); maxoctets);
print_string(PRINT_FP, NULL, "\t max-octets %s",
sprint_size(maxoctets, buf));
} else { } else {
print_string(PRINT_FP, NULL, print_string(PRINT_FP, NULL,
"\t max-octets %s", "wildcard"); "\t max-octets %s", "wildcard");

View File

@ -238,7 +238,6 @@ int parse_police(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
static int print_police(struct action_util *a, FILE *f, struct rtattr *arg) static int print_police(struct action_util *a, FILE *f, struct rtattr *arg)
{ {
SPRINT_BUF(b1);
SPRINT_BUF(b2); SPRINT_BUF(b2);
struct tc_police *p; struct tc_police *p;
struct rtattr *tb[TCA_POLICE_MAX+1]; struct rtattr *tb[TCA_POLICE_MAX+1];
@ -271,8 +270,8 @@ static int print_police(struct action_util *a, FILE *f, struct rtattr *arg)
fprintf(f, " police 0x%x ", p->index); fprintf(f, " police 0x%x ", p->index);
tc_print_rate(PRINT_FP, NULL, "rate %s ", rate64); tc_print_rate(PRINT_FP, NULL, "rate %s ", rate64);
buffer = tc_calc_xmitsize(rate64, p->burst); buffer = tc_calc_xmitsize(rate64, p->burst);
fprintf(f, "burst %s ", sprint_size(buffer, b1)); print_size(PRINT_FP, NULL, "burst %s ", buffer);
fprintf(f, "mtu %s ", sprint_size(p->mtu, b1)); print_size(PRINT_FP, NULL, "mtu %s ", p->mtu);
if (show_raw) if (show_raw)
fprintf(f, "[%08x] ", p->burst); fprintf(f, "[%08x] ", p->burst);

View File

@ -434,7 +434,6 @@ static int cake_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
int atm = 0; int atm = 0;
int nat = 0; int nat = 0;
SPRINT_BUF(b1);
SPRINT_BUF(b2); SPRINT_BUF(b2);
if (opt == NULL) if (opt == NULL)
@ -573,11 +572,8 @@ static int cake_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
if (mpu) if (mpu)
print_uint(PRINT_ANY, "mpu", "mpu %u ", mpu); print_uint(PRINT_ANY, "mpu", "mpu %u ", mpu);
if (memlimit) { if (memlimit)
print_uint(PRINT_JSON, "memlimit", NULL, memlimit); print_size(PRINT_ANY, "memlimit", "memlimit %s ", memlimit);
print_string(PRINT_FP, NULL, "memlimit %s ",
sprint_size(memlimit, b1));
}
if (fwmark) if (fwmark)
print_uint(PRINT_FP, NULL, "fwmark 0x%x ", fwmark); print_uint(PRINT_FP, NULL, "fwmark 0x%x ", fwmark);
@ -637,11 +633,11 @@ static int cake_print_xstats(struct qdisc_util *qu, FILE *f,
if (st[TCA_CAKE_STATS_MEMORY_USED] && if (st[TCA_CAKE_STATS_MEMORY_USED] &&
st[TCA_CAKE_STATS_MEMORY_LIMIT]) { st[TCA_CAKE_STATS_MEMORY_LIMIT]) {
print_string(PRINT_FP, NULL, " memory used: %s", print_size(PRINT_FP, NULL, " memory used: %s",
sprint_size(GET_STAT_U32(MEMORY_USED), b1)); GET_STAT_U32(MEMORY_USED));
print_string(PRINT_FP, NULL, " of %s\n", print_size(PRINT_FP, NULL, " of %s\n",
sprint_size(GET_STAT_U32(MEMORY_LIMIT), b1)); GET_STAT_U32(MEMORY_LIMIT));
print_uint(PRINT_JSON, "memory_used", NULL, print_uint(PRINT_JSON, "memory_used", NULL,
GET_STAT_U32(MEMORY_USED)); GET_STAT_U32(MEMORY_USED));

View File

@ -84,16 +84,14 @@ static int drr_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
{ {
struct rtattr *tb[TCA_DRR_MAX + 1]; struct rtattr *tb[TCA_DRR_MAX + 1];
SPRINT_BUF(b1);
if (opt == NULL) if (opt == NULL)
return 0; return 0;
parse_rtattr_nested(tb, TCA_DRR_MAX, opt); parse_rtattr_nested(tb, TCA_DRR_MAX, opt);
if (tb[TCA_DRR_QUANTUM]) if (tb[TCA_DRR_QUANTUM])
fprintf(f, "quantum %s ", print_size(PRINT_FP, NULL, "quantum %s ",
sprint_size(rta_getattr_u32(tb[TCA_DRR_QUANTUM]), b1)); rta_getattr_u32(tb[TCA_DRR_QUANTUM]));
return 0; return 0;
} }
@ -101,15 +99,13 @@ static int drr_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstat
{ {
struct tc_drr_stats *x; struct tc_drr_stats *x;
SPRINT_BUF(b1);
if (xstats == NULL) if (xstats == NULL)
return 0; return 0;
if (RTA_PAYLOAD(xstats) < sizeof(*x)) if (RTA_PAYLOAD(xstats) < sizeof(*x))
return -1; return -1;
x = RTA_DATA(xstats); x = RTA_DATA(xstats);
fprintf(f, " deficit %s ", sprint_size(x->deficit, b1)); print_size(PRINT_FP, NULL, " deficit %s ", x->deficit);
return 0; return 0;
} }

View File

@ -67,14 +67,10 @@ static int fifo_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
if (RTA_PAYLOAD(opt) < sizeof(*qopt)) if (RTA_PAYLOAD(opt) < sizeof(*qopt))
return -1; return -1;
qopt = RTA_DATA(opt); qopt = RTA_DATA(opt);
if (strcmp(qu->id, "bfifo") == 0) { if (strcmp(qu->id, "bfifo") == 0)
SPRINT_BUF(b1); print_size(PRINT_ANY, "limit", "limit %s", qopt->limit);
print_uint(PRINT_JSON, "limit", NULL, qopt->limit); else
print_string(PRINT_FP, NULL, "limit %s",
sprint_size(qopt->limit, b1));
} else {
print_uint(PRINT_ANY, "limit", "limit %up", qopt->limit); print_uint(PRINT_ANY, "limit", "limit %up", qopt->limit);
}
return 0; return 0;
} }

View File

@ -315,16 +315,13 @@ static int fq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
if (tb[TCA_FQ_QUANTUM] && if (tb[TCA_FQ_QUANTUM] &&
RTA_PAYLOAD(tb[TCA_FQ_QUANTUM]) >= sizeof(__u32)) { RTA_PAYLOAD(tb[TCA_FQ_QUANTUM]) >= sizeof(__u32)) {
quantum = rta_getattr_u32(tb[TCA_FQ_QUANTUM]); quantum = rta_getattr_u32(tb[TCA_FQ_QUANTUM]);
print_uint(PRINT_JSON, "quantum", NULL, quantum); print_size(PRINT_ANY, "quantum", "quantum %s ", quantum);
print_string(PRINT_FP, NULL, "quantum %s ",
sprint_size(quantum, b1));
} }
if (tb[TCA_FQ_INITIAL_QUANTUM] && if (tb[TCA_FQ_INITIAL_QUANTUM] &&
RTA_PAYLOAD(tb[TCA_FQ_INITIAL_QUANTUM]) >= sizeof(__u32)) { RTA_PAYLOAD(tb[TCA_FQ_INITIAL_QUANTUM]) >= sizeof(__u32)) {
quantum = rta_getattr_u32(tb[TCA_FQ_INITIAL_QUANTUM]); quantum = rta_getattr_u32(tb[TCA_FQ_INITIAL_QUANTUM]);
print_uint(PRINT_JSON, "initial_quantum", NULL, quantum); print_size(PRINT_ANY, "initial_quantum", "initial_quantum %s ",
print_string(PRINT_FP, NULL, "initial_quantum %s ", quantum);
sprint_size(quantum, b1));
} }
if (tb[TCA_FQ_FLOW_MAX_RATE] && if (tb[TCA_FQ_FLOW_MAX_RATE] &&
RTA_PAYLOAD(tb[TCA_FQ_FLOW_MAX_RATE]) >= sizeof(__u32)) { RTA_PAYLOAD(tb[TCA_FQ_FLOW_MAX_RATE]) >= sizeof(__u32)) {

View File

@ -221,9 +221,8 @@ static int fq_codel_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt
if (tb[TCA_FQ_CODEL_MEMORY_LIMIT] && if (tb[TCA_FQ_CODEL_MEMORY_LIMIT] &&
RTA_PAYLOAD(tb[TCA_FQ_CODEL_MEMORY_LIMIT]) >= sizeof(__u32)) { RTA_PAYLOAD(tb[TCA_FQ_CODEL_MEMORY_LIMIT]) >= sizeof(__u32)) {
memory_limit = rta_getattr_u32(tb[TCA_FQ_CODEL_MEMORY_LIMIT]); memory_limit = rta_getattr_u32(tb[TCA_FQ_CODEL_MEMORY_LIMIT]);
print_uint(PRINT_JSON, "memory_limit", NULL, memory_limit); print_size(PRINT_ANY, "memory_limit", "memory_limit %s ",
print_string(PRINT_FP, NULL, "memory_limit %s ", memory_limit);
sprint_size(memory_limit, b1));
} }
if (tb[TCA_FQ_CODEL_ECN] && if (tb[TCA_FQ_CODEL_ECN] &&
RTA_PAYLOAD(tb[TCA_FQ_CODEL_ECN]) >= sizeof(__u32)) { RTA_PAYLOAD(tb[TCA_FQ_CODEL_ECN]) >= sizeof(__u32)) {

View File

@ -232,16 +232,13 @@ static int fq_pie_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
if (tb[TCA_FQ_PIE_QUANTUM] && if (tb[TCA_FQ_PIE_QUANTUM] &&
RTA_PAYLOAD(tb[TCA_FQ_PIE_QUANTUM]) >= sizeof(__u32)) { RTA_PAYLOAD(tb[TCA_FQ_PIE_QUANTUM]) >= sizeof(__u32)) {
quantum = rta_getattr_u32(tb[TCA_FQ_PIE_QUANTUM]); quantum = rta_getattr_u32(tb[TCA_FQ_PIE_QUANTUM]);
print_uint(PRINT_JSON, "quantum", NULL, quantum); print_size(PRINT_ANY, "quantum", "quantum %s ", quantum);
print_string(PRINT_FP, NULL, "quantum %s ",
sprint_size(quantum, b1));
} }
if (tb[TCA_FQ_PIE_MEMORY_LIMIT] && if (tb[TCA_FQ_PIE_MEMORY_LIMIT] &&
RTA_PAYLOAD(tb[TCA_FQ_PIE_MEMORY_LIMIT]) >= sizeof(__u32)) { RTA_PAYLOAD(tb[TCA_FQ_PIE_MEMORY_LIMIT]) >= sizeof(__u32)) {
memory_limit = rta_getattr_u32(tb[TCA_FQ_PIE_MEMORY_LIMIT]); memory_limit = rta_getattr_u32(tb[TCA_FQ_PIE_MEMORY_LIMIT]);
print_uint(PRINT_JSON, "memory_limit", NULL, memory_limit); print_size(PRINT_ANY, "memory_limit", "memory_limit %s ",
print_string(PRINT_FP, NULL, "memory_limit %s ", memory_limit);
sprint_size(memory_limit, b1));
} }
if (tb[TCA_FQ_PIE_ECN_PROB] && if (tb[TCA_FQ_PIE_ECN_PROB] &&
RTA_PAYLOAD(tb[TCA_FQ_PIE_ECN_PROB]) >= sizeof(__u32)) { RTA_PAYLOAD(tb[TCA_FQ_PIE_ECN_PROB]) >= sizeof(__u32)) {

View File

@ -373,18 +373,11 @@ gred_print_stats(struct tc_gred_info *info, struct tc_gred_qopt *qopt)
{ {
__u64 bytes = info ? info->bytes : qopt->bytesin; __u64 bytes = info ? info->bytes : qopt->bytesin;
SPRINT_BUF(b1);
if (!is_json_context()) if (!is_json_context())
printf("\n Queue size: "); printf("\n Queue size: ");
print_uint(PRINT_JSON, "qave", NULL, qopt->qave); print_size(PRINT_ANY, "qave", "average %s ", qopt->qave);
print_string(PRINT_FP, NULL, "average %s ", print_size(PRINT_ANY, "backlog", "current %s ", qopt->backlog);
sprint_size(qopt->qave, b1));
print_uint(PRINT_JSON, "backlog", NULL, qopt->backlog);
print_string(PRINT_FP, NULL, "current %s ",
sprint_size(qopt->backlog, b1));
if (!is_json_context()) if (!is_json_context())
printf("\n Dropped packets: "); printf("\n Dropped packets: ");
@ -415,9 +408,7 @@ gred_print_stats(struct tc_gred_info *info, struct tc_gred_qopt *qopt)
printf("\n Total packets: "); printf("\n Total packets: ");
print_uint(PRINT_ANY, "packets", "%u ", qopt->packets); print_uint(PRINT_ANY, "packets", "%u ", qopt->packets);
print_size(PRINT_ANY, "bytes", "(%s) ", bytes);
print_uint(PRINT_JSON, "bytes", NULL, bytes);
print_string(PRINT_FP, NULL, "(%s) ", sprint_size(bytes, b1));
} }
static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
@ -431,8 +422,6 @@ static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
__u32 *limit = NULL; __u32 *limit = NULL;
unsigned int i; unsigned int i;
SPRINT_BUF(b1);
if (opt == NULL) if (opt == NULL)
return 0; return 0;
@ -470,11 +459,8 @@ static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
else else
print_bool(PRINT_ANY, "grio", NULL, false); print_bool(PRINT_ANY, "grio", NULL, false);
if (limit) { if (limit)
print_uint(PRINT_JSON, "limit", NULL, *limit); print_size(PRINT_ANY, "limit", "limit %s ", *limit);
print_string(PRINT_FP, NULL, "limit %s ",
sprint_size(*limit, b1));
}
tc_red_print_flags(sopt->flags); tc_red_print_flags(sopt->flags);
@ -487,18 +473,9 @@ static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
print_uint(PRINT_ANY, "vq", "\n vq %u ", qopt->DP); print_uint(PRINT_ANY, "vq", "\n vq %u ", qopt->DP);
print_hhu(PRINT_ANY, "prio", "prio %hhu ", qopt->prio); print_hhu(PRINT_ANY, "prio", "prio %hhu ", qopt->prio);
print_size(PRINT_ANY, "limit", "limit %s ", qopt->limit);
print_uint(PRINT_JSON, "limit", NULL, qopt->limit); print_size(PRINT_ANY, "min", "min %s ", qopt->qth_min);
print_string(PRINT_FP, NULL, "limit %s ", print_size(PRINT_ANY, "max", "max %s ", qopt->qth_max);
sprint_size(qopt->limit, b1));
print_uint(PRINT_JSON, "min", NULL, qopt->qth_min);
print_string(PRINT_FP, NULL, "min %s ",
sprint_size(qopt->qth_min, b1));
print_uint(PRINT_JSON, "max", NULL, qopt->qth_max);
print_string(PRINT_FP, NULL, "max %s ",
sprint_size(qopt->qth_max, b1));
if (infos[i].flags_present) if (infos[i].flags_present)
tc_red_print_flags(infos[i].flags); tc_red_print_flags(infos[i].flags);

View File

@ -143,9 +143,7 @@ static int hhf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
if (tb[TCA_HHF_QUANTUM] && if (tb[TCA_HHF_QUANTUM] &&
RTA_PAYLOAD(tb[TCA_HHF_QUANTUM]) >= sizeof(__u32)) { RTA_PAYLOAD(tb[TCA_HHF_QUANTUM]) >= sizeof(__u32)) {
quantum = rta_getattr_u32(tb[TCA_HHF_QUANTUM]); quantum = rta_getattr_u32(tb[TCA_HHF_QUANTUM]);
print_uint(PRINT_JSON, "quantum", NULL, quantum); print_size(PRINT_ANY, "quantum", "quantum %s ", quantum);
print_string(PRINT_FP, NULL, "quantum %s ",
sprint_size(quantum, b1));
} }
if (tb[TCA_HHF_HH_FLOWS_LIMIT] && if (tb[TCA_HHF_HH_FLOWS_LIMIT] &&
RTA_PAYLOAD(tb[TCA_HHF_HH_FLOWS_LIMIT]) >= sizeof(__u32)) { RTA_PAYLOAD(tb[TCA_HHF_HH_FLOWS_LIMIT]) >= sizeof(__u32)) {
@ -162,9 +160,8 @@ static int hhf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
if (tb[TCA_HHF_ADMIT_BYTES] && if (tb[TCA_HHF_ADMIT_BYTES] &&
RTA_PAYLOAD(tb[TCA_HHF_ADMIT_BYTES]) >= sizeof(__u32)) { RTA_PAYLOAD(tb[TCA_HHF_ADMIT_BYTES]) >= sizeof(__u32)) {
admit_bytes = rta_getattr_u32(tb[TCA_HHF_ADMIT_BYTES]); admit_bytes = rta_getattr_u32(tb[TCA_HHF_ADMIT_BYTES]);
print_uint(PRINT_JSON, "admit_bytes", NULL, admit_bytes); print_size(PRINT_ANY, "admit_bytes", "admit_bytes %s ",
print_string(PRINT_FP, NULL, "admit_bytes %s ", admit_bytes);
sprint_size(admit_bytes, b1));
} }
if (tb[TCA_HHF_EVICT_TIMEOUT] && if (tb[TCA_HHF_EVICT_TIMEOUT] &&
RTA_PAYLOAD(tb[TCA_HHF_EVICT_TIMEOUT]) >= sizeof(__u32)) { RTA_PAYLOAD(tb[TCA_HHF_EVICT_TIMEOUT]) >= sizeof(__u32)) {

View File

@ -269,7 +269,6 @@ static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
__u64 rate64, ceil64; __u64 rate64, ceil64;
SPRINT_BUF(b1); SPRINT_BUF(b1);
SPRINT_BUF(b2);
SPRINT_BUF(b3); SPRINT_BUF(b3);
if (opt == NULL) if (opt == NULL)
@ -310,18 +309,16 @@ static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
if (linklayer > TC_LINKLAYER_ETHERNET || show_details) if (linklayer > TC_LINKLAYER_ETHERNET || show_details)
fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b3)); fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b3));
if (show_details) { if (show_details) {
fprintf(f, "burst %s/%u mpu %s ", print_size(PRINT_FP, NULL, "burst %s/", buffer);
sprint_size(buffer, b1), fprintf(f, "%u ", 1<<hopt->rate.cell_log);
1<<hopt->rate.cell_log, print_size(PRINT_FP, NULL, "mpu %s ", hopt->rate.mpu);
sprint_size(hopt->rate.mpu, b2)); print_size(PRINT_FP, NULL, "cburst %s/", cbuffer);
fprintf(f, "cburst %s/%u mpu %s ", fprintf(f, "%u ", 1<<hopt->ceil.cell_log);
sprint_size(cbuffer, b1), print_size(PRINT_FP, NULL, "mpu %s ", hopt->ceil.mpu);
1<<hopt->ceil.cell_log,
sprint_size(hopt->ceil.mpu, b2));
fprintf(f, "level %d ", (int)hopt->level); fprintf(f, "level %d ", (int)hopt->level);
} else { } else {
fprintf(f, "burst %s ", sprint_size(buffer, b1)); print_size(PRINT_FP, NULL, "burst %s ", buffer);
fprintf(f, "cburst %s ", sprint_size(cbuffer, b1)); print_size(PRINT_FP, NULL, "cburst %s ", cbuffer);
} }
if (show_raw) if (show_raw)
fprintf(f, "buffer [%08x] cbuffer [%08x] ", fprintf(f, "buffer [%08x] cbuffer [%08x] ",

View File

@ -192,10 +192,6 @@ static int red_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
struct tc_red_qopt *qopt; struct tc_red_qopt *qopt;
__u32 max_P = 0; __u32 max_P = 0;
SPRINT_BUF(b1);
SPRINT_BUF(b2);
SPRINT_BUF(b3);
if (opt == NULL) if (opt == NULL)
return 0; return 0;
@ -217,12 +213,9 @@ static int red_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
qopt->flags = flags_bf->value; qopt->flags = flags_bf->value;
} }
print_uint(PRINT_JSON, "limit", NULL, qopt->limit); print_size(PRINT_ANY, "limit", "limit %s ", qopt->limit);
print_string(PRINT_FP, NULL, "limit %s ", sprint_size(qopt->limit, b1)); print_size(PRINT_ANY, "min", "min %s ", qopt->qth_min);
print_uint(PRINT_JSON, "min", NULL, qopt->qth_min); print_size(PRINT_ANY, "max", "max %s ", qopt->qth_max);
print_string(PRINT_FP, NULL, "min %s ", sprint_size(qopt->qth_min, b2));
print_uint(PRINT_JSON, "max", NULL, qopt->qth_max);
print_string(PRINT_FP, NULL, "max %s ", sprint_size(qopt->qth_max, b3));
tc_red_print_flags(qopt->flags); tc_red_print_flags(qopt->flags);

View File

@ -206,9 +206,6 @@ static int sfq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
struct tc_sfq_qopt *qopt; struct tc_sfq_qopt *qopt;
struct tc_sfq_qopt_v1 *qopt_ext = NULL; struct tc_sfq_qopt_v1 *qopt_ext = NULL;
SPRINT_BUF(b1);
SPRINT_BUF(b2);
SPRINT_BUF(b3);
if (opt == NULL) if (opt == NULL)
return 0; return 0;
@ -219,9 +216,7 @@ static int sfq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
qopt = RTA_DATA(opt); qopt = RTA_DATA(opt);
print_uint(PRINT_ANY, "limit", "limit %up ", qopt->limit); print_uint(PRINT_ANY, "limit", "limit %up ", qopt->limit);
print_uint(PRINT_JSON, "quantum", NULL, qopt->quantum); print_size(PRINT_ANY, "quantum", "quantum %s ", qopt->quantum);
print_string(PRINT_FP, NULL, "quantum %s ",
sprint_size(qopt->quantum, b1));
if (qopt_ext && qopt_ext->depth) if (qopt_ext && qopt_ext->depth)
print_uint(PRINT_ANY, "depth", "depth %u ", qopt_ext->depth); print_uint(PRINT_ANY, "depth", "depth %u ", qopt_ext->depth);
@ -237,12 +232,8 @@ static int sfq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
qopt->perturb_period); qopt->perturb_period);
if (qopt_ext && qopt_ext->qth_min) { if (qopt_ext && qopt_ext->qth_min) {
print_uint(PRINT_ANY, "ewma", "ewma %u ", qopt_ext->Wlog); print_uint(PRINT_ANY, "ewma", "ewma %u ", qopt_ext->Wlog);
print_uint(PRINT_JSON, "min", NULL, qopt_ext->qth_min); print_size(PRINT_ANY, "min", "min %s ", qopt_ext->qth_min);
print_string(PRINT_FP, NULL, "min %s ", print_size(PRINT_ANY, "max", "max %s ", qopt_ext->qth_max);
sprint_size(qopt_ext->qth_min, b2));
print_uint(PRINT_JSON, "max", NULL, qopt_ext->qth_max);
print_string(PRINT_FP, NULL, "max %s ",
sprint_size(qopt_ext->qth_max, b3));
print_float(PRINT_ANY, "probability", "probability %lg ", print_float(PRINT_ANY, "probability", "probability %lg ",
qopt_ext->max_P / pow(2, 32)); qopt_ext->max_P / pow(2, 32));
tc_red_print_flags(qopt_ext->flags); tc_red_print_flags(qopt_ext->flags);

View File

@ -292,13 +292,9 @@ static int tbf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
sprintf(b1, "%s/%u", sprint_size(buffer, b2), sprintf(b1, "%s/%u", sprint_size(buffer, b2),
1 << qopt->rate.cell_log); 1 << qopt->rate.cell_log);
print_string(PRINT_ANY, "burst", "burst %s ", b1); print_string(PRINT_ANY, "burst", "burst %s ", b1);
print_uint(PRINT_JSON, "mpu", NULL, qopt->rate.mpu); print_size(PRINT_ANY, "mpu", "mpu %s ", qopt->rate.mpu);
print_string(PRINT_FP, NULL, "mpu %s ",
sprint_size(qopt->rate.mpu, b1));
} else { } else {
print_u64(PRINT_JSON, "burst", NULL, buffer); print_size(PRINT_ANY, "burst", "burst %s ", buffer);
print_string(PRINT_FP, NULL, "burst %s ",
sprint_size(buffer, b1));
} }
if (show_raw) if (show_raw)
print_hex(PRINT_ANY, "burst_raw", "[%08x] ", qopt->buffer); print_hex(PRINT_ANY, "burst_raw", "[%08x] ", qopt->buffer);
@ -314,15 +310,11 @@ static int tbf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
sprintf(b1, "%s/%u", sprint_size(mtu, b2), sprintf(b1, "%s/%u", sprint_size(mtu, b2),
1 << qopt->peakrate.cell_log); 1 << qopt->peakrate.cell_log);
print_string(PRINT_ANY, "mtu", "mtu %s ", b1); print_string(PRINT_ANY, "mtu", "mtu %s ", b1);
print_uint(PRINT_JSON, "mpu", NULL, print_size(PRINT_ANY, "mpu", "mpu %s ",
qopt->peakrate.mpu); qopt->peakrate.mpu);
print_string(PRINT_FP, NULL, "mpu %s ",
sprint_size(qopt->peakrate.mpu,
b1));
} else { } else {
print_u64(PRINT_JSON, "minburst", NULL, mtu); print_size(PRINT_ANY, "minburst",
print_string(PRINT_FP, NULL, "minburst %s ", "minburst %s ", mtu);
sprint_size(mtu, b1));
} }
if (show_raw) if (show_raw)
print_hex(PRINT_ANY, "mtu_raw", "[%08x] ", print_hex(PRINT_ANY, "mtu_raw", "[%08x] ",
@ -344,11 +336,8 @@ static int tbf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
print_string(PRINT_FP, NULL, "lat %s ", print_string(PRINT_FP, NULL, "lat %s ",
sprint_time(latency, b1)); sprint_time(latency, b1));
} }
if (show_raw || latency < 0.0) { if (show_raw || latency < 0.0)
print_uint(PRINT_JSON, "limit", NULL, qopt->limit); print_size(PRINT_ANY, "limit", "limit %s ", qopt->limit);
print_string(PRINT_FP, NULL, "limit %s ",
sprint_size(qopt->limit, b1));
}
if (qopt->rate.overhead) if (qopt->rate.overhead)
print_int(PRINT_ANY, "overhead", "overhead %d ", print_int(PRINT_ANY, "overhead", "overhead %d ",
qopt->rate.overhead); qopt->rate.overhead);

View File

@ -412,24 +412,6 @@ void print_devname(enum output_type type, int ifindex)
"dev", "%s ", ifname); "dev", "%s ", ifname);
} }
static void print_size(char *buf, int len, __u32 sz)
{
double tmp = sz;
if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
snprintf(buf, len, "%gKb", rint(tmp/1024));
else
snprintf(buf, len, "%ub", sz);
}
char *sprint_size(__u32 size, char *buf)
{
print_size(buf, SPRINT_BSIZE-1, size);
return buf;
}
static const char *action_n2a(int action) static const char *action_n2a(int action)
{ {
static char buf[64]; static char buf[64];
@ -786,7 +768,6 @@ static void print_tcstats_basic_hw(struct rtattr **tbs, char *prefix)
void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats) void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats)
{ {
SPRINT_BUF(b1);
struct rtattr *tbs[TCA_STATS_MAX + 1]; struct rtattr *tbs[TCA_STATS_MAX + 1];
parse_rtattr_nested(tbs, TCA_STATS_MAX, rta); parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
@ -852,10 +833,8 @@ void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtat
MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q))); MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
if (!tbs[TCA_STATS_RATE_EST]) if (!tbs[TCA_STATS_RATE_EST])
print_nl(); print_nl();
print_uint(PRINT_JSON, "backlog", NULL, q.backlog);
print_string(PRINT_FP, NULL, "%s", prefix); print_string(PRINT_FP, NULL, "%s", prefix);
print_string(PRINT_FP, NULL, "backlog %s", print_size(PRINT_ANY, "backlog", "backlog %s", q.backlog);
sprint_size(q.backlog, b1));
print_uint(PRINT_ANY, "qlen", " %up", q.qlen); print_uint(PRINT_ANY, "qlen", " %up", q.qlen);
print_uint(PRINT_FP, NULL, " requeues %u", q.requeues); print_uint(PRINT_FP, NULL, " requeues %u", q.requeues);
} }
@ -867,8 +846,6 @@ void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtat
void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix,
struct rtattr **xstats) struct rtattr **xstats)
{ {
SPRINT_BUF(b1);
if (tb[TCA_STATS2]) { if (tb[TCA_STATS2]) {
print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats); print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
if (xstats && !*xstats) if (xstats && !*xstats)
@ -901,8 +878,8 @@ void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix,
if (st.qlen || st.backlog) { if (st.qlen || st.backlog) {
fprintf(fp, "backlog "); fprintf(fp, "backlog ");
if (st.backlog) if (st.backlog)
fprintf(fp, "%s ", print_size(PRINT_FP, NULL, "%s ",
sprint_size(st.backlog, b1)); st.backlog);
if (st.qlen) if (st.qlen)
fprintf(fp, "%up ", st.qlen); fprintf(fp, "%up ", st.qlen);
} }

View File

@ -88,7 +88,6 @@ void tc_print_rate(enum output_type t, const char *key, const char *fmt,
unsigned long long rate); unsigned long long rate);
void print_devname(enum output_type type, int ifindex); void print_devname(enum output_type type, int ifindex);
char *sprint_size(__u32 size, char *buf);
char *sprint_tc_classid(__u32 h, char *buf); char *sprint_tc_classid(__u32 h, char *buf);
char *sprint_ticks(__u32 ticks, char *buf); char *sprint_ticks(__u32 ticks, char *buf);
char *sprint_linklayer(unsigned int linklayer, char *buf); char *sprint_linklayer(unsigned int linklayer, char *buf);