Add a function to convert from cgroup v2 cpu.weight to
cgroup v1.

Signed-off-by: Tom Hromatka <tom.hroma...@oracle.com>
---
 src/abstraction-common.h |  3 +++
 src/ctrl-cpu.c           | 47 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/src/abstraction-common.h b/src/abstraction-common.h
index 4b0f3e8e87e5..bb43d9cefb74 100644
--- a/src/abstraction-common.h
+++ b/src/abstraction-common.h
@@ -77,6 +77,9 @@ int cgroup_convert_cpu(struct cgroup_controller * const 
out_cgc,
 #ifdef UNIT_TEST
 int v1_shares_to_v2(struct cgroup_controller * const dst_cgc,
                    const char * const shares_val);
+
+int v2_weight_to_v1(struct cgroup_controller * const dst_cgc,
+                   const char * const weight_val);
 #endif /* UNIT_TEST */
 
 __END_DECLS
diff --git a/src/ctrl-cpu.c b/src/ctrl-cpu.c
index 2b59312bc6b7..78c7bf130638 100644
--- a/src/ctrl-cpu.c
+++ b/src/ctrl-cpu.c
@@ -74,6 +74,46 @@ out:
        return ret;
 }
 
+STATIC int v2_weight_to_v1(struct cgroup_controller * const dst_cgc,
+                          const char * const weight_val)
+{
+#define SHARES_STR_LEN 20
+
+       long int shares;
+       char *shares_str = NULL;
+       int ret = 0;
+
+       if (!weight_val)
+               return ECGINVAL;
+
+       if (strlen(weight_val) > 0) {
+               ret = cgroup_strtol(weight_val, 10, &shares);
+               if (ret)
+                       goto out;
+
+               /* now scale from cpu.shares to cpu.weight */
+               shares = shares * DEFAULT_SHARES_VALUE / DEFAULT_WEIGHT_VALUE;
+
+               shares_str = calloc(sizeof(char), SHARES_STR_LEN);
+               ret = snprintf(shares_str, SHARES_STR_LEN, "%ld\n", shares);
+               if (ret == SHARES_STR_LEN) {
+                       /* we ran out of room in the string. throw an error */
+                       cgroup_err("Error: shares too large for string: %d\n",
+                                  shares);
+                       ret = ECGFAIL;
+                       goto out;
+               }
+       }
+
+       ret = cgroup_add_value_string(dst_cgc, cpu_shares, shares_str);
+
+out:
+       if (shares_str)
+               free(shares_str);
+
+       return ret;
+}
+
 static int v1_to_v2(struct cgroup_controller * const out_cgc,
                    const struct cgroup_controller * const in_cgc)
 {
@@ -119,7 +159,12 @@ static int v2_to_v1(struct cgroup_controller * const 
out_cgc,
         * the case, we can make this function smarter.
         */
        for (i = 0; i < in_cgc->index; i++) {
-               /* todo - add conversions here */
+               if (strcmp(in_cgc->values[i]->name, cpu_weight) == 0) {
+                       ret = v2_weight_to_v1(out_cgc,
+                               in_cgc->values[i]->value);
+               }
+               if (ret)
+                       goto out;
        }
 
        if (out_cgc->index == 0)
-- 
2.26.2



_______________________________________________
Libcg-devel mailing list
Libcg-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to