]> git.proxmox.com Git - mirror_iproute2.git/commit
lib: Move sprint_size() from tc here, add print_size()
authorPetr Machata <me@pmachata.org>
Sat, 5 Dec 2020 21:13:31 +0000 (22:13 +0100)
committerDavid Ahern <dsahern@gmail.com>
Wed, 9 Dec 2020 02:30:25 +0000 (02:30 +0000)
commitadbe5de966626782c8059ac997527842ed8ff0e0
treebd9ebe343b5cfb0e28874231dc2359b5fc653485
parent60265cc2264109ce832f17d502dce5965ed670d8
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>
18 files changed:
include/json_print.h
lib/json_print.c
tc/m_gate.c
tc/m_police.c
tc/q_cake.c
tc/q_drr.c
tc/q_fifo.c
tc/q_fq.c
tc/q_fq_codel.c
tc/q_fq_pie.c
tc/q_gred.c
tc/q_hhf.c
tc/q_htb.c
tc/q_red.c
tc/q_sfq.c
tc/q_tbf.c
tc/tc_util.c
tc/tc_util.h