richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
c2dfb7
From 11df25536319a4c0f3276c2218054243d9ee213c Mon Sep 17 00:00:00 2001
c2dfb7
From: Filipe Brandenburger <filbranden@google.com>
c2dfb7
Date: Fri, 2 Nov 2018 09:21:57 -0700
c2dfb7
Subject: [PATCH] core: add CPUQuotaPeriodSec=
c2dfb7
c2dfb7
This new setting allows configuration of CFS period on the CPU cgroup, instead
c2dfb7
of using a hardcoded default of 100ms.
c2dfb7
c2dfb7
Tested:
c2dfb7
- Legacy cgroup + Unified cgroup
c2dfb7
- systemctl set-property
c2dfb7
- systemctl show
c2dfb7
- Confirmed that the cgroup settings (such as cpu.cfs_period_ns) were set
c2dfb7
  appropriately, including updating the CPU quota (cpu.cfs_quota_ns) when
c2dfb7
  CPUQuotaPeriodSec= is updated.
c2dfb7
- Checked that clamping works properly when either period or (quota * period)
c2dfb7
  are below the resolution of 1ms, or if period is above the max of 1s.
c2dfb7
c2dfb7
(cherry picked from commit 10f28641115733c61754342d5dcbe70b083bea4b)
c2dfb7
c2dfb7
Resolves: #1770379
c2dfb7
---
c2dfb7
 doc/TRANSIENT-SETTINGS.md             |  1 +
c2dfb7
 man/systemd.resource-control.xml      | 19 +++++++++
c2dfb7
 src/core/cgroup.c                     | 61 ++++++++++++++++++++++-----
c2dfb7
 src/core/cgroup.h                     |  3 ++
c2dfb7
 src/core/dbus-cgroup.c                | 23 ++++++++++
c2dfb7
 src/core/load-fragment-gperf.gperf.m4 |  1 +
c2dfb7
 src/core/load-fragment.c              |  1 +
c2dfb7
 src/shared/bus-unit-util.c            | 14 ++++++
c2dfb7
 src/test/meson.build                  |  5 +++
c2dfb7
 src/test/test-cgroup-cpu.c            | 38 +++++++++++++++++
c2dfb7
 10 files changed, 155 insertions(+), 11 deletions(-)
c2dfb7
 create mode 100644 src/test/test-cgroup-cpu.c
c2dfb7
c2dfb7
diff --git a/doc/TRANSIENT-SETTINGS.md b/doc/TRANSIENT-SETTINGS.md
c2dfb7
index 23fe84e4d1..0d2d3e9065 100644
c2dfb7
--- a/doc/TRANSIENT-SETTINGS.md
c2dfb7
+++ b/doc/TRANSIENT-SETTINGS.md
c2dfb7
@@ -218,6 +218,7 @@ All cgroup/resource control settings are available for transient units
c2dfb7
 ✓ CPUShares=
c2dfb7
 ✓ StartupCPUShares=
c2dfb7
 ✓ CPUQuota=
c2dfb7
+✓ CPUQuotaPeriodSec=
c2dfb7
 ✓ AllowedCPUs=
c2dfb7
 ✓ AllowedMemoryNodes=
c2dfb7
 ✓ MemoryAccounting=
c2dfb7
diff --git a/man/systemd.resource-control.xml b/man/systemd.resource-control.xml
c2dfb7
index b0064bf98f..cfe19a6574 100644
c2dfb7
--- a/man/systemd.resource-control.xml
c2dfb7
+++ b/man/systemd.resource-control.xml
c2dfb7
@@ -231,6 +231,25 @@
c2dfb7
         </listitem>
c2dfb7
       </varlistentry>
c2dfb7
 
c2dfb7
+      <varlistentry>
c2dfb7
+        <term><varname>CPUQuotaPeriodSec=</varname></term>
c2dfb7
+
c2dfb7
+        <listitem>
c2dfb7
+          <para>Assign the duration over which the CPU time quota specified by <varname>CPUQuota=</varname> is measured.
c2dfb7
+          Takes a time duration value in seconds, with an optional suffix such as "ms" for milliseconds (or "s" for seconds.)
c2dfb7
+          The default setting is 100ms. The period is clamped to the range supported by the kernel, which is [1ms, 1000ms].
c2dfb7
+          Additionally, the period is adjusted up so that the quota interval is also at least 1ms.
c2dfb7
+          Setting <varname>CPUQuotaPeriodSec=</varname> to an empty value resets it to the default.</para>
c2dfb7
+
c2dfb7
+          <para>This controls the second field of <literal>cpu.max</literal> attribute on the unified control group hierarchy
c2dfb7
+          and <literal>cpu.cfs_period_us</literal> on legacy. For details about these control group attributes, see
c2dfb7
+          <ulink url="https://www.kernel.org/doc/Documentation/cgroup-v2.txt">cgroup-v2.txt</ulink> and
c2dfb7
+          <ulink url="https://www.kernel.org/doc/Documentation/scheduler/sched-design-CFS.txt">sched-design-CFS.txt</ulink>.</para>
c2dfb7
+
c2dfb7
+          <para>Example: <varname>CPUQuotaPeriodSec=10ms</varname> to request that the CPU quota is measured in periods of 10ms.</para>
c2dfb7
+        </listitem>
c2dfb7
+      </varlistentry>
c2dfb7
+
c2dfb7
       <varlistentry>
c2dfb7
         <term><varname>MemoryAccounting=</varname></term>
c2dfb7
 
c2dfb7
diff --git a/src/core/cgroup.c b/src/core/cgroup.c
c2dfb7
index 7aa7db9261..45fd64a394 100644
c2dfb7
--- a/src/core/cgroup.c
c2dfb7
+++ b/src/core/cgroup.c
c2dfb7
@@ -23,7 +23,7 @@
c2dfb7
 #include "string-util.h"
c2dfb7
 #include "virt.h"
c2dfb7
 
c2dfb7
-#define CGROUP_CPU_QUOTA_PERIOD_USEC ((usec_t) 100 * USEC_PER_MSEC)
c2dfb7
+#define CGROUP_CPU_QUOTA_DEFAULT_PERIOD_USEC ((usec_t) 100 * USEC_PER_MSEC)
c2dfb7
 
c2dfb7
 bool manager_owns_root_cgroup(Manager *m) {
c2dfb7
         assert(m);
c2dfb7
@@ -77,6 +77,7 @@ void cgroup_context_init(CGroupContext *c) {
c2dfb7
                 .cpu_weight = CGROUP_WEIGHT_INVALID,
c2dfb7
                 .startup_cpu_weight = CGROUP_WEIGHT_INVALID,
c2dfb7
                 .cpu_quota_per_sec_usec = USEC_INFINITY,
c2dfb7
+                .cpu_quota_period_usec = USEC_INFINITY,
c2dfb7
 
c2dfb7
                 .cpu_shares = CGROUP_CPU_SHARES_INVALID,
c2dfb7
                 .startup_cpu_shares = CGROUP_CPU_SHARES_INVALID,
c2dfb7
@@ -190,6 +191,7 @@ void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
c2dfb7
         CGroupDeviceAllow *a;
c2dfb7
         IPAddressAccessItem *iaai;
c2dfb7
         char u[FORMAT_TIMESPAN_MAX];
c2dfb7
+        char v[FORMAT_TIMESPAN_MAX];
c2dfb7
 
c2dfb7
         assert(c);
c2dfb7
         assert(f);
c2dfb7
@@ -211,6 +213,7 @@ void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
c2dfb7
                 "%sCPUShares=%" PRIu64 "\n"
c2dfb7
                 "%sStartupCPUShares=%" PRIu64 "\n"
c2dfb7
                 "%sCPUQuotaPerSecSec=%s\n"
c2dfb7
+                "%sCPUQuotaPeriodSec=%s\n"
c2dfb7
                 "%sAllowedCPUs=%s\n"
c2dfb7
                 "%sAllowedMemoryNodes=%s\n"
c2dfb7
                 "%sIOWeight=%" PRIu64 "\n"
c2dfb7
@@ -236,6 +239,7 @@ void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
c2dfb7
                 prefix, c->cpu_shares,
c2dfb7
                 prefix, c->startup_cpu_shares,
c2dfb7
                 prefix, format_timespan(u, sizeof(u), c->cpu_quota_per_sec_usec, 1),
c2dfb7
+                prefix, format_timespan(v, sizeof(v), c->cpu_quota_period_usec, 1),
c2dfb7
                 prefix, cpuset_cpus,
c2dfb7
                 prefix, cpuset_mems,
c2dfb7
                 prefix, c->io_weight,
c2dfb7
@@ -515,7 +519,40 @@ static uint64_t cgroup_context_cpu_shares(CGroupContext *c, ManagerState state)
c2dfb7
                 return CGROUP_CPU_SHARES_DEFAULT;
c2dfb7
 }
c2dfb7
 
c2dfb7
-static void cgroup_apply_unified_cpu_config(Unit *u, uint64_t weight, uint64_t quota) {
c2dfb7
+usec_t cgroup_cpu_adjust_period(usec_t period, usec_t quota, usec_t resolution, usec_t max_period) {
c2dfb7
+        /* kernel uses a minimum resolution of 1ms, so both period and (quota * period)
c2dfb7
+         * need to be higher than that boundary. quota is specified in USecPerSec.
c2dfb7
+         * Additionally, period must be at most max_period. */
c2dfb7
+        assert(quota > 0);
c2dfb7
+
c2dfb7
+        return MIN(MAX3(period, resolution, resolution * USEC_PER_SEC / quota), max_period);
c2dfb7
+}
c2dfb7
+
c2dfb7
+static usec_t cgroup_cpu_adjust_period_and_log(Unit *u, usec_t period, usec_t quota) {
c2dfb7
+        usec_t new_period;
c2dfb7
+
c2dfb7
+        if (quota == USEC_INFINITY)
c2dfb7
+                /* Always use default period for infinity quota. */
c2dfb7
+                return CGROUP_CPU_QUOTA_DEFAULT_PERIOD_USEC;
c2dfb7
+
c2dfb7
+        if (period == USEC_INFINITY)
c2dfb7
+                /* Default period was requested. */
c2dfb7
+                period = CGROUP_CPU_QUOTA_DEFAULT_PERIOD_USEC;
c2dfb7
+
c2dfb7
+        /* Clamp to interval [1ms, 1s] */
c2dfb7
+        new_period = cgroup_cpu_adjust_period(period, quota, USEC_PER_MSEC, USEC_PER_SEC);
c2dfb7
+
c2dfb7
+        if (new_period != period) {
c2dfb7
+                char v[FORMAT_TIMESPAN_MAX];
c2dfb7
+                log_unit_full(u, LOG_WARNING, 0,
c2dfb7
+                              "Clamping CPU interval for cpu.max: period is now %s",
c2dfb7
+                              format_timespan(v, sizeof(v), new_period, 1));
c2dfb7
+        }
c2dfb7
+
c2dfb7
+        return new_period;
c2dfb7
+}
c2dfb7
+
c2dfb7
+static void cgroup_apply_unified_cpu_config(Unit *u, uint64_t weight, uint64_t quota, usec_t period) {
c2dfb7
         char buf[MAX(DECIMAL_STR_MAX(uint64_t) + 1, (DECIMAL_STR_MAX(usec_t) + 1) * 2)];
c2dfb7
         int r;
c2dfb7
 
c2dfb7
@@ -525,11 +562,12 @@ static void cgroup_apply_unified_cpu_config(Unit *u, uint64_t weight, uint64_t q
c2dfb7
                 log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
c2dfb7
                               "Failed to set cpu.weight: %m");
c2dfb7
 
c2dfb7
+        period = cgroup_cpu_adjust_period_and_log(u, period, quota);
c2dfb7
         if (quota != USEC_INFINITY)
c2dfb7
                 xsprintf(buf, USEC_FMT " " USEC_FMT "\n",
c2dfb7
-                         quota * CGROUP_CPU_QUOTA_PERIOD_USEC / USEC_PER_SEC, CGROUP_CPU_QUOTA_PERIOD_USEC);
c2dfb7
+                         MAX(quota * period / USEC_PER_SEC, USEC_PER_MSEC), period);
c2dfb7
         else
c2dfb7
-                xsprintf(buf, "max " USEC_FMT "\n", CGROUP_CPU_QUOTA_PERIOD_USEC);
c2dfb7
+                xsprintf(buf, "max " USEC_FMT "\n", period);
c2dfb7
 
c2dfb7
         r = cg_set_attribute("cpu", u->cgroup_path, "cpu.max", buf);
c2dfb7
 
c2dfb7
@@ -538,7 +576,7 @@ static void cgroup_apply_unified_cpu_config(Unit *u, uint64_t weight, uint64_t q
c2dfb7
                               "Failed to set cpu.max: %m");
c2dfb7
 }
c2dfb7
 
c2dfb7
-static void cgroup_apply_legacy_cpu_config(Unit *u, uint64_t shares, uint64_t quota) {
c2dfb7
+static void cgroup_apply_legacy_cpu_config(Unit *u, uint64_t shares, uint64_t quota, usec_t period) {
c2dfb7
         char buf[MAX(DECIMAL_STR_MAX(uint64_t), DECIMAL_STR_MAX(usec_t)) + 1];
c2dfb7
         int r;
c2dfb7
 
c2dfb7
@@ -548,20 +586,21 @@ static void cgroup_apply_legacy_cpu_config(Unit *u, uint64_t shares, uint64_t qu
c2dfb7
                 log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
c2dfb7
                               "Failed to set cpu.shares: %m");
c2dfb7
 
c2dfb7
-        xsprintf(buf, USEC_FMT "\n", CGROUP_CPU_QUOTA_PERIOD_USEC);
c2dfb7
+        period = cgroup_cpu_adjust_period_and_log(u, period, quota);
c2dfb7
+
c2dfb7
+        xsprintf(buf, USEC_FMT "\n", period);
c2dfb7
         r = cg_set_attribute("cpu", u->cgroup_path, "cpu.cfs_period_us", buf);
c2dfb7
         if (r < 0)
c2dfb7
                 log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
c2dfb7
                               "Failed to set cpu.cfs_period_us: %m");
c2dfb7
 
c2dfb7
         if (quota != USEC_INFINITY) {
c2dfb7
-                xsprintf(buf, USEC_FMT "\n", quota * CGROUP_CPU_QUOTA_PERIOD_USEC / USEC_PER_SEC);
c2dfb7
-                r = cg_set_attribute("cpu", u->cgroup_path, "cpu.cfs_quota_us", buf);
c2dfb7
-        } else
c2dfb7
+                xsprintf(buf, USEC_FMT "\n", MAX(quota * period / USEC_PER_SEC, USEC_PER_MSEC));
c2dfb7
                 r = cg_set_attribute("cpu", u->cgroup_path, "cpu.cfs_quota_us", "-1");
c2dfb7
         if (r < 0)
c2dfb7
                 log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
c2dfb7
                               "Failed to set cpu.cfs_quota_us: %m");
c2dfb7
+        }
c2dfb7
 }
c2dfb7
 
c2dfb7
 static uint64_t cgroup_cpu_shares_to_weight(uint64_t shares) {
c2dfb7
@@ -815,7 +854,7 @@ static void cgroup_context_apply(
c2dfb7
                         } else
c2dfb7
                                 weight = CGROUP_WEIGHT_DEFAULT;
c2dfb7
 
c2dfb7
-                        cgroup_apply_unified_cpu_config(u, weight, c->cpu_quota_per_sec_usec);
c2dfb7
+                        cgroup_apply_unified_cpu_config(u, weight, c->cpu_quota_per_sec_usec, c->cpu_quota_period_usec);
c2dfb7
                 } else {
c2dfb7
                         uint64_t shares;
c2dfb7
 
c2dfb7
@@ -831,7 +870,7 @@ static void cgroup_context_apply(
c2dfb7
                         else
c2dfb7
                                 shares = CGROUP_CPU_SHARES_DEFAULT;
c2dfb7
 
c2dfb7
-                        cgroup_apply_legacy_cpu_config(u, shares, c->cpu_quota_per_sec_usec);
c2dfb7
+                        cgroup_apply_legacy_cpu_config(u, shares, c->cpu_quota_per_sec_usec, c->cpu_quota_period_usec);
c2dfb7
                 }
c2dfb7
         }
c2dfb7
 
c2dfb7
diff --git a/src/core/cgroup.h b/src/core/cgroup.h
c2dfb7
index f7365b4c46..2ba57d3ded 100644
c2dfb7
--- a/src/core/cgroup.h
c2dfb7
+++ b/src/core/cgroup.h
c2dfb7
@@ -84,6 +84,7 @@ struct CGroupContext {
c2dfb7
         uint64_t cpu_weight;
c2dfb7
         uint64_t startup_cpu_weight;
c2dfb7
         usec_t cpu_quota_per_sec_usec;
c2dfb7
+        usec_t cpu_quota_period_usec;
c2dfb7
 
c2dfb7
         CPUSet cpuset_cpus;
c2dfb7
         CPUSet cpuset_mems;
c2dfb7
@@ -136,6 +137,8 @@ typedef enum CGroupIPAccountingMetric {
c2dfb7
 typedef struct Unit Unit;
c2dfb7
 typedef struct Manager Manager;
c2dfb7
 
c2dfb7
+usec_t cgroup_cpu_adjust_period(usec_t period, usec_t quota, usec_t resolution, usec_t max_period);
c2dfb7
+
c2dfb7
 void cgroup_context_init(CGroupContext *c);
c2dfb7
 void cgroup_context_done(CGroupContext *c);
c2dfb7
 void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix);
c2dfb7
diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c
c2dfb7
index a1d3014d61..c8b918e45d 100644
c2dfb7
--- a/src/core/dbus-cgroup.c
c2dfb7
+++ b/src/core/dbus-cgroup.c
c2dfb7
@@ -334,6 +334,7 @@ const sd_bus_vtable bus_cgroup_vtable[] = {
c2dfb7
         SD_BUS_PROPERTY("CPUShares", "t", NULL, offsetof(CGroupContext, cpu_shares), 0),
c2dfb7
         SD_BUS_PROPERTY("StartupCPUShares", "t", NULL, offsetof(CGroupContext, startup_cpu_shares), 0),
c2dfb7
         SD_BUS_PROPERTY("CPUQuotaPerSecUSec", "t", bus_property_get_usec, offsetof(CGroupContext, cpu_quota_per_sec_usec), 0),
c2dfb7
+        SD_BUS_PROPERTY("CPUQuotaPeriodUSec", "t", bus_property_get_usec, offsetof(CGroupContext, cpu_quota_period_usec), 0),
c2dfb7
         SD_BUS_PROPERTY("AllowedCPUs", "ay", property_get_cpuset, offsetof(CGroupContext, cpuset_cpus), 0),
c2dfb7
         SD_BUS_PROPERTY("AllowedMemoryNodes", "ay", property_get_cpuset, offsetof(CGroupContext, cpuset_mems), 0),
c2dfb7
         SD_BUS_PROPERTY("IOAccounting", "b", bus_property_get_bool, offsetof(CGroupContext, io_accounting), 0),
c2dfb7
@@ -725,6 +726,28 @@ int bus_cgroup_set_property(
c2dfb7
 
c2dfb7
                 return 1;
c2dfb7
 
c2dfb7
+        } else if (streq(name, "CPUQuotaPeriodUSec")) {
c2dfb7
+                uint64_t u64;
c2dfb7
+
c2dfb7
+                r = sd_bus_message_read(message, "t", &u64);
c2dfb7
+                if (r < 0)
c2dfb7
+                        return r;
c2dfb7
+
c2dfb7
+                if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
c2dfb7
+                        c->cpu_quota_period_usec = u64;
c2dfb7
+                        unit_invalidate_cgroup(u, CGROUP_MASK_CPU);
c2dfb7
+                        if (c->cpu_quota_period_usec == USEC_INFINITY)
c2dfb7
+                                unit_write_setting(u, flags, "CPUQuotaPeriodSec", "CPUQuotaPeriodSec=");
c2dfb7
+                        else {
c2dfb7
+                                char v[FORMAT_TIMESPAN_MAX];
c2dfb7
+                                unit_write_settingf(u, flags, "CPUQuotaPeriodSec",
c2dfb7
+                                                    "CPUQuotaPeriodSec=%s",
c2dfb7
+                                                    format_timespan(v, sizeof(v), c->cpu_quota_period_usec, 1));
c2dfb7
+                        }
c2dfb7
+                }
c2dfb7
+
c2dfb7
+                return 1;
c2dfb7
+
c2dfb7
         } else if (STR_IN_SET(name, "AllowedCPUs", "AllowedMemoryNodes")) {
c2dfb7
                 const void *a;
c2dfb7
                 size_t n;
c2dfb7
diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
c2dfb7
index 23879c001f..4defa82ac1 100644
c2dfb7
--- a/src/core/load-fragment-gperf.gperf.m4
c2dfb7
+++ b/src/core/load-fragment-gperf.gperf.m4
c2dfb7
@@ -169,6 +169,7 @@ $1.StartupCPUWeight,             config_parse_cg_weight,             0,
c2dfb7
 $1.CPUShares,                    config_parse_cpu_shares,            0,                             offsetof($1, cgroup_context.cpu_shares)
c2dfb7
 $1.StartupCPUShares,             config_parse_cpu_shares,            0,                             offsetof($1, cgroup_context.startup_cpu_shares)
c2dfb7
 $1.CPUQuota,                     config_parse_cpu_quota,             0,                             offsetof($1, cgroup_context)
c2dfb7
+$1.CPUQuotaPeriodSec,            config_parse_sec_def_infinity,      0,                             offsetof($1, cgroup_context.cpu_quota_period_usec)
c2dfb7
 $1.MemoryAccounting,             config_parse_bool,                  0,                             offsetof($1, cgroup_context.memory_accounting)
c2dfb7
 $1.MemoryLow,                    config_parse_memory_limit,          0,                             offsetof($1, cgroup_context)
c2dfb7
 $1.MemoryHigh,                   config_parse_memory_limit,          0,                             offsetof($1, cgroup_context)
c2dfb7
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
c2dfb7
index 1e22013b75..762b106007 100644
c2dfb7
--- a/src/core/load-fragment.c
c2dfb7
+++ b/src/core/load-fragment.c
c2dfb7
@@ -55,6 +55,7 @@
c2dfb7
 #include "unit-name.h"
c2dfb7
 #include "unit-printf.h"
c2dfb7
 #include "user-util.h"
c2dfb7
+#include "time-util.h"
c2dfb7
 #include "web-util.h"
c2dfb7
 
c2dfb7
 static int supported_socket_protocol_from_string(const char *s) {
c2dfb7
diff --git a/src/shared/bus-unit-util.c b/src/shared/bus-unit-util.c
c2dfb7
index 3c1ecf2027..ec45d6f86d 100644
c2dfb7
--- a/src/shared/bus-unit-util.c
c2dfb7
+++ b/src/shared/bus-unit-util.c
c2dfb7
@@ -480,6 +480,20 @@ static int bus_append_cgroup_property(sd_bus_message *m, const char *field, cons
c2dfb7
                 return 1;
c2dfb7
         }
c2dfb7
 
c2dfb7
+        if (streq(field, "CPUQuotaPeriodSec")) {
c2dfb7
+                usec_t u = USEC_INFINITY;
c2dfb7
+
c2dfb7
+                r = parse_sec_def_infinity(eq, &u);
c2dfb7
+                if (r < 0)
c2dfb7
+                        return log_error_errno(r, "CPU quota period '%s' invalid.", eq);
c2dfb7
+
c2dfb7
+                r = sd_bus_message_append(m, "(sv)", "CPUQuotaPeriodUSec", "t", u);
c2dfb7
+                if (r < 0)
c2dfb7
+                        return bus_log_create_error(r);
c2dfb7
+
c2dfb7
+                return 1;
c2dfb7
+        }
c2dfb7
+
c2dfb7
         if (streq(field, "DeviceAllow")) {
c2dfb7
 
c2dfb7
                 if (isempty(eq))
c2dfb7
diff --git a/src/test/meson.build b/src/test/meson.build
c2dfb7
index ead000e30c..22264d034c 100644
c2dfb7
--- a/src/test/meson.build
c2dfb7
+++ b/src/test/meson.build
c2dfb7
@@ -513,6 +513,11 @@ tests += [
c2dfb7
          [],
c2dfb7
          '', 'manual'],
c2dfb7
 
c2dfb7
+        [['src/test/test-cgroup-cpu.c'],
c2dfb7
+         [libcore,
c2dfb7
+          libshared],
c2dfb7
+         []],
c2dfb7
+
c2dfb7
         [['src/test/test-cgroup-mask.c',
c2dfb7
           'src/test/test-helper.c'],
c2dfb7
          [libcore,
c2dfb7
diff --git a/src/test/test-cgroup-cpu.c b/src/test/test-cgroup-cpu.c
c2dfb7
new file mode 100644
c2dfb7
index 0000000000..a445acc955
c2dfb7
--- /dev/null
c2dfb7
+++ b/src/test/test-cgroup-cpu.c
c2dfb7
@@ -0,0 +1,38 @@
c2dfb7
+/* SPDX-License-Identifier: LGPL-2.1+ */
c2dfb7
+
c2dfb7
+#include "cgroup.h"
c2dfb7
+#include "log.h"
c2dfb7
+
c2dfb7
+static void test_cgroup_cpu_adjust_period(void) {
c2dfb7
+        log_info("/* %s */", __func__);
c2dfb7
+
c2dfb7
+        /* Period 1ms, quota 40% -> Period 2.5ms */
c2dfb7
+        assert_se(2500 == cgroup_cpu_adjust_period(USEC_PER_MSEC, 400 * USEC_PER_MSEC, USEC_PER_MSEC, USEC_PER_SEC));
c2dfb7
+        /* Period 10ms, quota 10% -> keep. */
c2dfb7
+        assert_se(10 * USEC_PER_MSEC == cgroup_cpu_adjust_period(10 * USEC_PER_MSEC, 100 * USEC_PER_MSEC, USEC_PER_MSEC, USEC_PER_SEC));
c2dfb7
+        /* Period 1ms, quota 1000% -> keep. */
c2dfb7
+        assert_se(USEC_PER_MSEC == cgroup_cpu_adjust_period(USEC_PER_MSEC, 10000 * USEC_PER_MSEC, USEC_PER_MSEC, USEC_PER_SEC));
c2dfb7
+        /* Period 100ms, quota 30% -> keep. */
c2dfb7
+        assert_se(100 * USEC_PER_MSEC == cgroup_cpu_adjust_period(100 * USEC_PER_MSEC, 300 * USEC_PER_MSEC, USEC_PER_MSEC, USEC_PER_SEC));
c2dfb7
+        /* Period 5s, quota 40% -> adjust to 1s. */
c2dfb7
+        assert_se(USEC_PER_SEC == cgroup_cpu_adjust_period(5 * USEC_PER_SEC, 400 * USEC_PER_MSEC, USEC_PER_MSEC, USEC_PER_SEC));
c2dfb7
+        /* Period 2s, quota 250% -> adjust to 1s. */
c2dfb7
+        assert_se(USEC_PER_SEC == cgroup_cpu_adjust_period(2 * USEC_PER_SEC, 2500 * USEC_PER_MSEC, USEC_PER_MSEC, USEC_PER_SEC));
c2dfb7
+        /* Period 10us, quota 5,000,000% -> adjust to 1ms. */
c2dfb7
+        assert_se(USEC_PER_MSEC == cgroup_cpu_adjust_period(10, 50000000 * USEC_PER_MSEC, USEC_PER_MSEC, USEC_PER_SEC));
c2dfb7
+        /* Period 10ms, quota 50,000% -> keep. */
c2dfb7
+        assert_se(10 * USEC_PER_MSEC == cgroup_cpu_adjust_period(10 * USEC_PER_MSEC, 500000 * USEC_PER_MSEC, USEC_PER_MSEC, USEC_PER_SEC));
c2dfb7
+        /* Period 10ms, quota 1% -> adjust to 100ms. */
c2dfb7
+        assert_se(100 * USEC_PER_MSEC == cgroup_cpu_adjust_period(10 * USEC_PER_MSEC, 10 * USEC_PER_MSEC, USEC_PER_MSEC, USEC_PER_SEC));
c2dfb7
+        /* Period 10ms, quota .001% -> adjust to 1s. */
c2dfb7
+        assert_se(1 * USEC_PER_SEC == cgroup_cpu_adjust_period(10 * USEC_PER_MSEC, 10, USEC_PER_MSEC, USEC_PER_SEC));
c2dfb7
+        /* Period 0ms, quota 200% -> adjust to 1ms. */
c2dfb7
+        assert_se(1 * USEC_PER_MSEC == cgroup_cpu_adjust_period(0, 2 * USEC_PER_SEC, USEC_PER_MSEC, USEC_PER_SEC));
c2dfb7
+        /* Period 0ms, quota 40% -> adjust to 2.5ms. */
c2dfb7
+        assert_se(2500 == cgroup_cpu_adjust_period(0, 400 * USEC_PER_MSEC, USEC_PER_MSEC, USEC_PER_SEC));
c2dfb7
+}
c2dfb7
+
c2dfb7
+int main(int argc, char *argv[]) {
c2dfb7
+        test_cgroup_cpu_adjust_period();
c2dfb7
+        return 0;
c2dfb7
+}