Re: [RFC] Printing numbers in SI units

From: Andy Shevchenko
Date: Fri Apr 26 2024 - 11:43:49 EST


On Fri, Apr 26, 2024 at 05:27:08PM +0200, Geert Uytterhoeven wrote:
> On Wed, Jan 24, 2024 at 11:43 PM Rasmus Villemoes
> <linux@xxxxxxxxxxxxxxxxxx> wrote:
> > On 24/01/2024 19.58, Matthew Wilcox wrote:
> > > I was looking at hugetlbfs and it has several snippets of code like
> > > this:
> > >
> > > string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
> > > pr_warn("HugeTLB: allocating %u of page size %s failed node%d. Only allocated %lu hugepages.\n",
> > > h->max_huge_pages_node[nid], buf, nid, i);
> > >
> > > That's not terribly ergonomic, so I wondered if I could do better.
> > > Unfortunately, I decided to do it using the SPECIAL flag which GCC
> > > warns about. But I've written the code now, so I'm sending it out in
> > > case anybody has a better idea for how to incorporate it.
> >
> > Well, something that gcc will warn about with Wformat isn't gonna fly,
> > obviously. But my man page also mentions ' as a possible flag for d
> > conversions:
> >
> > ' For decimal conversion (i, d, u, f, F, g, G) the output is
> > to be grouped with thousands'
> > grouping characters if the locale information indicates any.
>
> > Obviously, our printf wouldn't implement that, [...]
>
> Why not? ;-)

:-)

> Old Gmail-white-space-damaged patch below, which I wrote when I got
> fed up with meticulously counting zeros in GHz-range clock
> frequencies...

..

> lib/vsprintf.c: Add support for thousands' grouping
>
> Use an underscore as the grouping character.
>
> TODO:
> - Documentation
> - Self test

Definitely won't fly without these two.

> - Do we want to use this in /sys/kernel/debug/clk/clk_summary ?
> RFC patch, compatibility was already broken by commit
> e55a839a7a1c561b ("clk: add clock protection mechanism to clk
> core")
>
> Signed-off-by: Geert Uytterhoeven <geert+renesas@xxxxxxxxx>
> ---
> Originally I wanted to use grouping by 4 for octal and hexadecimal
> numbers.

Not sure about octals, but for the hexadecimal we have hex_dump_to_buffer()
and %ph. The latter one can be modified to support grouping.

--
With Best Regards,
Andy Shevchenko