]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: Fix CPUQuotaPerSecUSec unit file serialization
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 3 Jun 2024 08:40:35 +0000 (10:40 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 5 Jun 2024 14:50:36 +0000 (16:50 +0200)
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
test/units/TEST-26-SYSTEMCTL.sh

index 6cb335608b00477bc1a026bf387cbb623b1b52f6..49e84b44e3266693cbf9bc67c0f6a0fb40e71eae 100644 (file)
@@ -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;
index 117ffb81f31b74e9c920b9c86612f619a5122946..ae7a5d6eb6637b06bb2efef673af0af29e95c522 100755 (executable)
@@ -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/")