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

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

diff --git a/src/abstraction-common.h b/src/abstraction-common.h
index 054c4bea61b1..4b0f3e8e87e5 100644
--- a/src/abstraction-common.h
+++ b/src/abstraction-common.h
@@ -69,6 +69,16 @@ int cgroup_convert_cgroup(struct cgroup * const out_cgroup,
 int cgroup_convert_cpu(struct cgroup_controller * const out_cgc,
                       const struct cgroup_controller * const in_cgc);
 
+/**
+ * Functions that are defined as STATIC can be placed within the UNIT_TEST
+ * ifdef.  This will allow them to be included in the unit tests while
+ * remaining static in a normal libcgroup library build.
+ */
+#ifdef UNIT_TEST
+int v1_shares_to_v2(struct cgroup_controller * const dst_cgc,
+                   const char * const shares_val);
+#endif /* UNIT_TEST */
+
 __END_DECLS
 
 #endif /* __ABSTRACTION_COMMON */
diff --git a/src/ctrl-cpu.c b/src/ctrl-cpu.c
index 2fab490c7b21..2b59312bc6b7 100644
--- a/src/ctrl-cpu.c
+++ b/src/ctrl-cpu.c
@@ -28,6 +28,52 @@
 
 #include "abstraction-common.h"
 
+#define DEFAULT_SHARES_VALUE 1024
+#define DEFAULT_WEIGHT_VALUE 100
+
+static const char * const cpu_shares = "cpu.shares";
+static const char * const cpu_weight = "cpu.weight";
+
+STATIC int v1_shares_to_v2(struct cgroup_controller * const dst_cgc,
+                          const char * const shares_val)
+{
+#define WEIGHT_STR_LEN 20
+
+       long int weight;
+       char *weight_str = NULL;
+       int ret = 0;
+
+       if (!shares_val)
+               return ECGINVAL;
+
+       if (strlen(shares_val) > 0) {
+               ret = cgroup_strtol(shares_val, 10, &weight);
+               if (ret)
+                       goto out;
+
+               /* now scale from cpu.shares to cpu.weight */
+               weight = weight * DEFAULT_WEIGHT_VALUE / DEFAULT_SHARES_VALUE;
+
+               weight_str = calloc(sizeof(char), WEIGHT_STR_LEN);
+               ret = snprintf(weight_str, WEIGHT_STR_LEN, "%ld\n", weight);
+               if (ret == WEIGHT_STR_LEN) {
+                       /* we ran out of room in the string. throw an error */
+                       cgroup_err("Error: weight too large for string: %d\n",
+                                  weight);
+                       ret = ECGFAIL;
+                       goto out;
+               }
+       }
+
+       ret = cgroup_add_value_string(dst_cgc, cpu_weight, weight_str);
+
+out:
+       if (weight_str)
+               free(weight_str);
+
+       return ret;
+}
+
 static int v1_to_v2(struct cgroup_controller * const out_cgc,
                    const struct cgroup_controller * const in_cgc)
 {
@@ -43,7 +89,11 @@ static int v1_to_v2(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_shares) == 0)
+                       ret = v1_shares_to_v2(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