From 1a48d8549f5a0281ba73558e31490f7ec98f81cc Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 3 Jun 2024 10:40:35 +0200 Subject: [PATCH] core: Fix CPUQuotaPerSecUSec unit file serialization CPUQuota= can deal with float percentages perfectly fine these days (up to two places after the dot), so let's take that into account when serializing the value to the transient unit file so we don't lose precision when specifying e.g. "CPUQuota=0.5%". --- src/core/dbus-cgroup.c | 7 +++---- test/units/TEST-26-SYSTEMCTL.sh | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c index 6cb335608b..49e84b44e3 100644 --- a/src/core/dbus-cgroup.c +++ b/src/core/dbus-cgroup.c @@ -1312,11 +1312,10 @@ int bus_cgroup_set_property( if (c->cpu_quota_per_sec_usec == USEC_INFINITY) unit_write_setting(u, flags, "CPUQuota", "CPUQuota="); else - /* config_parse_cpu_quota() requires an integer, so truncating division is used on - * purpose here. */ unit_write_settingf(u, flags, "CPUQuota", - "CPUQuota=%0.f%%", - (double) (c->cpu_quota_per_sec_usec / 10000)); + "CPUQuota=" USEC_FMT ".%02" PRI_USEC "%%", + c->cpu_quota_per_sec_usec / 10000, + (c->cpu_quota_per_sec_usec % 10000) / 100); } return 1; diff --git a/test/units/TEST-26-SYSTEMCTL.sh b/test/units/TEST-26-SYSTEMCTL.sh index 117ffb81f3..ae7a5d6eb6 100755 --- a/test/units/TEST-26-SYSTEMCTL.sh +++ b/test/units/TEST-26-SYSTEMCTL.sh @@ -241,7 +241,7 @@ systemctl revert "$UNIT_NAME" systemctl set-property --runtime "$UNIT_NAME" CPUAccounting=no CPUQuota=10% systemctl cat "$UNIT_NAME" grep -r "CPUAccounting=no" "/run/systemd/system.control/${UNIT_NAME}.d/" -grep -r "CPUQuota=10%" "/run/systemd/system.control/${UNIT_NAME}.d/" +grep -r "CPUQuota=10.00%" "/run/systemd/system.control/${UNIT_NAME}.d/" systemctl revert "$UNIT_NAME" (! grep -r "CPUAccounting=" "/run/systemd/system.control/${UNIT_NAME}.d/") (! grep -r "CPUQuota=" "/run/systemd/system.control/${UNIT_NAME}.d/")