]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/cgroup: use helper macro for bfq conversion 20522/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 10 Nov 2021 10:37:15 +0000 (11:37 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 10 Nov 2021 19:55:04 +0000 (04:55 +0900)
As suggested in https://github.com/systemd/systemd/pull/20522#discussion_r696699984.

src/core/cgroup.c

index 7d4d56534327813b58f9aef6af8be72ee51d3351..fa397ba84983ffbf953cc41df5c5d1603b312826 100644 (file)
@@ -1206,9 +1206,21 @@ 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;
 
         assert(u);
 
@@ -1216,12 +1228,7 @@ static void set_io_weight(Unit *u, uint64_t weight) {
          * See also: https://github.com/systemd/systemd/pull/13335 and
          * https://github.com/torvalds/linux/commit/65752aef0a407e1ef17ec78a7fc31ba4e0b360f9.
          * The range is 1..1000 apparently, and the default is 100. */
-        if (weight <= CGROUP_WEIGHT_DEFAULT)
-                bfq_weight = CGROUP_BFQ_WEIGHT_DEFAULT - (CGROUP_WEIGHT_DEFAULT - weight) * (CGROUP_BFQ_WEIGHT_DEFAULT - CGROUP_BFQ_WEIGHT_MIN) / (CGROUP_WEIGHT_DEFAULT - CGROUP_WEIGHT_MIN);
-        else
-                bfq_weight = CGROUP_BFQ_WEIGHT_DEFAULT + (weight - CGROUP_WEIGHT_DEFAULT) * (CGROUP_BFQ_WEIGHT_MAX - CGROUP_BFQ_WEIGHT_DEFAULT) / (CGROUP_WEIGHT_MAX - CGROUP_WEIGHT_DEFAULT);
-
-        xsprintf(buf, "%" PRIu64 "\n", bfq_weight);
+        xsprintf(buf, "%" PRIu64 "\n", BFQ_WEIGHT(weight));
         (void) set_attribute_and_warn(u, "io", "io.bfq.weight", buf);
 
         xsprintf(buf, "default %" PRIu64 "\n", weight);
@@ -1230,20 +1237,11 @@ static void set_io_weight(Unit *u, uint64_t weight) {
 
 static void set_blkio_weight(Unit *u, uint64_t weight) {
         char buf[STRLEN("\n")+DECIMAL_STR_MAX(uint64_t)];
-        uint64_t bfq_weight;
 
         assert(u);
 
-        /* FIXME: drop this when distro kernels properly support BFQ through "io.weight"
-         * See also: https://github.com/systemd/systemd/pull/13335 and
-         * https://github.com/torvalds/linux/commit/65752aef0a407e1ef17ec78a7fc31ba4e0b360f9.
-         * The range is 1..1000 apparently, and the default is 100. */
-        if (weight <= CGROUP_BLKIO_WEIGHT_DEFAULT)
-                bfq_weight = CGROUP_BFQ_WEIGHT_DEFAULT - (CGROUP_BLKIO_WEIGHT_DEFAULT - weight) * (CGROUP_BFQ_WEIGHT_DEFAULT - CGROUP_BFQ_WEIGHT_MIN) / (CGROUP_BLKIO_WEIGHT_DEFAULT - CGROUP_BLKIO_WEIGHT_MIN);
-        else
-                bfq_weight = CGROUP_BFQ_WEIGHT_DEFAULT + (weight - CGROUP_BLKIO_WEIGHT_DEFAULT) * (CGROUP_BFQ_WEIGHT_MAX - CGROUP_BFQ_WEIGHT_DEFAULT) / (CGROUP_BLKIO_WEIGHT_MAX - CGROUP_BLKIO_WEIGHT_DEFAULT);
-
-        xsprintf(buf, "%" PRIu64 "\n", bfq_weight);
+        /* FIXME: see comment in set_io_weight(). */
+        xsprintf(buf, "%" PRIu64 "\n", BFQ_WEIGHT(weight));
         (void) set_attribute_and_warn(u, "blkio", "blkio.bfq.weight", buf);
 
         xsprintf(buf, "%" PRIu64 "\n", weight);