From 1ead0b2a7973782f4bacaab2d9deed94ac69d867 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 17 Feb 2021 17:29:43 +0100 Subject: [PATCH] parse-util: add format string macro for outputting permyriad Let's define a set of macros for making output of permyriad values easy. They are printed in pure ASCII, i.e. without the permille/permyriad suffix, using just percent and two places after the dot. --- src/basic/percent-util.h | 3 +++ src/core/cgroup.c | 1 + src/core/dbus-cgroup.c | 4 ++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/basic/percent-util.h b/src/basic/percent-util.h index d806ec007a..24f4c3bdef 100644 --- a/src/basic/percent-util.h +++ b/src/basic/percent-util.h @@ -59,3 +59,6 @@ static inline int UINT32_SCALE_TO_PERMYRIAD(uint32_t scale) { return (int) u; } + +#define PERMYRIAD_AS_PERCENT_FORMAT_STR "%i.%02i%%" +#define PERMYRIAD_AS_PERCENT_FORMAT_VAL(x) ((x)/100), ((x)%100) diff --git a/src/core/cgroup.c b/src/core/cgroup.c index b0fc67a125..644bf50e7d 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -21,6 +21,7 @@ #include "nulstr-util.h" #include "parse-util.h" #include "path-util.h" +#include "percent-util.h" #include "process-util.h" #include "procfs-util.h" #include "special.h" diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c index e22233d6ff..5bf9d7f4a8 100644 --- a/src/core/dbus-cgroup.c +++ b/src/core/dbus-cgroup.c @@ -706,9 +706,9 @@ static int bus_cgroup_set_boolean( assert_se(endswith(name, "Scale")); \ \ int scaled = UINT32_SCALE_TO_PERMYRIAD(raw); \ - unit_write_settingf(u, flags, name, "%.*s=%i.%02i%%", \ + unit_write_settingf(u, flags, name, "%.*s=" PERMYRIAD_AS_PERCENT_FORMAT_STR, \ (int)(strlen(name) - strlen("Scale")), name, \ - scaled / 100, scaled % 100); \ + PERMYRIAD_AS_PERCENT_FORMAT_VAL(scaled)); \ } \ \ return 1; \