]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cgroup-util: Move macros to macros and tests to tests
authorMichal Koutný <mkoutny@suse.com>
Fri, 10 Dec 2021 18:00:45 +0000 (19:00 +0100)
committerMichal Koutný <mkoutny@suse.com>
Mon, 10 Jan 2022 17:22:41 +0000 (18:22 +0100)
Friends to friends to be consistent with the other macros and make tests
separate from main code.

src/basic/cgroup-util.h
src/core/cgroup.c
src/test/test-cgroup-util.c

index 21275e9eaa6f6f146607b33f32ed57563621e840..461c01b3c209080c2b112a038453e82964a975b2 100644 (file)
@@ -127,6 +127,20 @@ static inline bool CGROUP_CPU_SHARES_IS_OK(uint64_t x) {
             (x >= CGROUP_CPU_SHARES_MIN && x <= CGROUP_CPU_SHARES_MAX);
 }
 
+/* Special values for the special {blkio,io}.bfq.weight attribute */
+#define CGROUP_BFQ_WEIGHT_INVALID UINT64_MAX
+#define CGROUP_BFQ_WEIGHT_MIN UINT64_C(1)
+#define CGROUP_BFQ_WEIGHT_MAX UINT64_C(1000)
+#define CGROUP_BFQ_WEIGHT_DEFAULT UINT64_C(100)
+
+/* Convert the normal io.weight value to io.bfq.weight */
+static inline uint64_t BFQ_WEIGHT(uint64_t io_weight) {
+        return
+            io_weight <= CGROUP_WEIGHT_DEFAULT ?
+            CGROUP_BFQ_WEIGHT_DEFAULT - (CGROUP_WEIGHT_DEFAULT - io_weight) * (CGROUP_BFQ_WEIGHT_DEFAULT - CGROUP_BFQ_WEIGHT_MIN) / (CGROUP_WEIGHT_DEFAULT - CGROUP_WEIGHT_MIN) :
+            CGROUP_BFQ_WEIGHT_DEFAULT + (io_weight - CGROUP_WEIGHT_DEFAULT) * (CGROUP_BFQ_WEIGHT_MAX - CGROUP_BFQ_WEIGHT_DEFAULT) / (CGROUP_WEIGHT_MAX - CGROUP_WEIGHT_DEFAULT);
+}
+
 /* Special values for the blkio.weight attribute */
 #define CGROUP_BLKIO_WEIGHT_INVALID UINT64_MAX
 #define CGROUP_BLKIO_WEIGHT_MIN UINT64_C(10)
index 07d53b2fb69f49675c85a7de356caac85a282173..81147733a39cae522e9dae9d6058111748f65b0f 100644 (file)
 
 #define CGROUP_CPU_QUOTA_DEFAULT_PERIOD_USEC ((usec_t) 100 * USEC_PER_MSEC)
 
-/* Special values for the bfq.weight attribute */
-#define CGROUP_BFQ_WEIGHT_INVALID UINT64_MAX
-#define CGROUP_BFQ_WEIGHT_MIN UINT64_C(1)
-#define CGROUP_BFQ_WEIGHT_MAX UINT64_C(1000)
-#define CGROUP_BFQ_WEIGHT_DEFAULT UINT64_C(100)
-
 /* Returns the log level to use when cgroup attribute writes fail. When an attribute is missing or we have access
  * problems we downgrade to LOG_DEBUG. This is supposed to be nice to container managers and kernels which want to mask
  * out specific attributes from us. */
@@ -1264,19 +1258,6 @@ static int cgroup_apply_devices(Unit *u) {
         return r;
 }
 
-/* Convert the normal io.weight value to io.bfq.weight */
-#define BFQ_WEIGHT(weight) \
-        (weight <= CGROUP_WEIGHT_DEFAULT ? \
-         CGROUP_BFQ_WEIGHT_DEFAULT - (CGROUP_WEIGHT_DEFAULT - weight) * (CGROUP_BFQ_WEIGHT_DEFAULT - CGROUP_BFQ_WEIGHT_MIN) / (CGROUP_WEIGHT_DEFAULT - CGROUP_WEIGHT_MIN) : \
-         CGROUP_BFQ_WEIGHT_DEFAULT + (weight - CGROUP_WEIGHT_DEFAULT) * (CGROUP_BFQ_WEIGHT_MAX - CGROUP_BFQ_WEIGHT_DEFAULT) / (CGROUP_WEIGHT_MAX - CGROUP_WEIGHT_DEFAULT))
-
-assert_cc(BFQ_WEIGHT(1) == 1);
-assert_cc(BFQ_WEIGHT(50) == 50);
-assert_cc(BFQ_WEIGHT(100) == 100);
-assert_cc(BFQ_WEIGHT(500) == 136);
-assert_cc(BFQ_WEIGHT(5000) == 545);
-assert_cc(BFQ_WEIGHT(10000) == 1000);
-
 static void set_io_weight(Unit *u, uint64_t weight) {
         char buf[STRLEN("default \n")+DECIMAL_STR_MAX(uint64_t)];
         uint64_t bfq_weight;
index 93b114b3fd8e553c4597999e1cbc2bca3867f405..7113b07a9525fbae30690eea443de308cccc88e5 100644 (file)
@@ -426,4 +426,13 @@ TEST(cg_get_keyed_attribute) {
         }
 }
 
+TEST(bfq_weight_conversion) {
+        assert_se(BFQ_WEIGHT(1) == 1);
+        assert_se(BFQ_WEIGHT(50) == 50);
+        assert_se(BFQ_WEIGHT(100) == 100);
+        assert_se(BFQ_WEIGHT(500) == 136);
+        assert_se(BFQ_WEIGHT(5000) == 545);
+        assert_se(BFQ_WEIGHT(10000) == 1000);
+}
+
 DEFINE_TEST_MAIN(LOG_DEBUG);