Re: [PATCH v13 12/32] x86,fs/resctrl: Support binary fixed point event counters

Luck, Tony
2025-11-06T02:27:59Z

On Wed, Nov 05, 2025 at 03:31:07PM -0800, Luck, Tony wrote:
> > > +
> > > + if (!binary_bits) {
> > > + seq_printf(m, "%llu.0\n", val);
> > > + return;
> > > + }

I can't completely escape a test for !binary_bits. Most of the
flow works ok (doing nothing, working towards frac == 0 when
it comes time for the snprintf()).

But the round-up code:

frac += 1ull << (binary_bits - 1);

goes badly wrong if binary_bits == 0.

I could write it like this:

static void print_event_value(struct seq_file *m, unsigned int binary_bits, u64 val)
{
char buf[decplaces[MAX_BINARY_BITS] + 1];
unsigned long long frac = 0;

if (binary_bits) {
/* Mask off the integer part of the fixed-point value. */
frac = val & GENMASK_ULL(binary_bits - 1, 0);

/*
* Multiply by 10^{desired decimal places}. The integer part of
* the fixed point value is now almost what is needed.
*/
frac *= int_pow(10ull, decplaces[binary_bits]);

/*
* Round to nearest by adding a value that would be a "1" in the
* binary_bits + 1 place. Integer part of fixed point value is
* now the needed value.
*/
frac += 1ull << (binary_bits - 1);

/*
* Extract the integer part of the value. This is the decimal
* representation of the original fixed-point fractional value.
*/
frac >>= binary_bits;
}

/*
* "frac" is now in the range [0 .. 10^decplaces). I.e. string
* representation will fit into chosen number of decimal places.
*/
snprintf(buf, sizeof(buf), "%0*llu", decplaces[binary_bits], frac);

seq_printf(m, "%llu.%s\n", val >> binary_bits, buf);
}

-Tony