[systemd-devel] [PATCH] WIP: conf-parser: allow config_parse_iec_off to parse percentages

Lennart Poettering lennart at poettering.net
Fri May 22 04:03:00 PDT 2015

On Fri, 22.05.15 12:30, Jan Synacek (jsynacek at redhat.com) wrote:

> > What about this: introduce a new type:
> >
> >         typedef struct SizeParameter {
> >                 uint64_t value;
> >                 bool relative;
> >         } SizeParameter;
> >
> > When .relative is false, then .value is an absolute value, otherwise
> > it's a relative value normalized to let's say 0x100000000 (so that
> > this value equals 100%, and values below it < 100% and above it >
> > 100%).
> Would you mind explaining this a bit more? I'm not sure if I understand
> this correctly, especially the "< 100%" and "> %100" parts. It doesn't
> seem to be needed at all. You always need the info from statvfs anyway,
> if the value is a percentage. If not, the value can be used as-is.

Well, basically what I am saying is that you should not store a
*percentage*, but a value relative to UINT32_MAX+1.

e.g.    0% would map to value=0
       50% would map to (UINT32_MAX+1)/2
      100% would map to UINT32_MAX+1
      120% would map to ((UINT32_MAX+1)*6)/5
      200% would map to (UINT32_MAX+1)*2

Or to put this differently, consider .value a fixed point number with
32bit before and 32bit after the point...

The point is simply that 0..100 is a weird range on computers, that
calculate in binary. Hence expose 0..100 in the UI, but internally
calculate with something more appropriate.
> > Then add new helper calls:
> >
> >      int size_parameter_from_string(const char *s, SizeParameter *ret);
> >      uint64 size_parameter_evaluate(SizeParameter *p, uint64_t base);
> >
> >
> > The latter should return .value as-is if p->relative is false, and
> > (base * .value) >> 32 otherwise.
> Why is "base" needed here?

"base" is the value you get from statfs.

The idea is basically that instead of storing just an absolute value,
you store either a relative or an absolute value, and remember which
one in a boolean. And then, when you actually need to evaluate things
you pass the statvfs data in and it will translate the relative value
to an absolute value -- but only at the time you actually need it.


Lennart Poettering, Red Hat

More information about the systemd-devel mailing list