anitazha / rpms / systemd

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