]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
abstraction: Add integer conversion function
authorTom Hromatka <tom.hromatka@oracle.com>
Wed, 15 Dec 2021 20:14:11 +0000 (20:14 +0000)
committerTom Hromatka <tom.hromatka@oracle.com>
Thu, 3 Feb 2022 21:42:03 +0000 (14:42 -0700)
Add a function to the abstraction layer that can convert from
one integer setting to another.  For example, this can be used
for simple conversions like cpu.shares to cpu.weight and vice
versa.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
src/abstraction-common.c
src/abstraction-common.h

index 04fec848d21d4283ba1850353c1e19dec38a8696..7616aab539b2878852ae233beec999769782c7d7 100644 (file)
@@ -65,3 +65,52 @@ int cgroup_strtol(const char * const in_str, int base,
 out:
        return ret;
 }
+
+int cgroup_convert_int(struct cgroup_controller * const dst_cgc,
+                      const char * const in_value,
+                      const char * const out_setting,
+                      void *in_dflt, void *out_dflt)
+{
+#define OUT_VALUE_STR_LEN 20
+
+       long int in_dflt_int = (long int)in_dflt;
+       long int out_dflt_int = (long int)out_dflt;
+       char *out_value_str = NULL;
+       long int out_value;
+       int ret;
+
+       if (!in_value)
+               return ECGINVAL;
+
+       if (strlen(in_value) > 0) {
+               ret = cgroup_strtol(in_value, 10, &out_value);
+               if (ret)
+                       goto out;
+
+               /* now scale from the input range to the output range */
+               out_value = out_value * out_dflt_int / in_dflt_int;
+
+               out_value_str = calloc(sizeof(char), OUT_VALUE_STR_LEN);
+               if (!out_value_str) {
+                       ret = ECGOTHER;
+                       goto out;
+               }
+
+               ret = snprintf(out_value_str, OUT_VALUE_STR_LEN, "%ld", out_value);
+               if (ret == OUT_VALUE_STR_LEN) {
+                       /* we ran out of room in the string. throw an error */
+                       cgroup_err("Error: output value too large for string: %d\n",
+                                  out_value);
+                       ret = ECGFAIL;
+                       goto out;
+               }
+       }
+
+       ret = cgroup_add_value_string(dst_cgc, out_setting, out_value_str);
+
+out:
+       if (out_value_str)
+               free(out_value_str);
+
+       return ret;
+}
index dc44bb675d7024d167ce3a29c094c0f7a385ea15..bbd25d735c62eb6c42eb7c1ad47923a41019fc35 100644 (file)
@@ -43,6 +43,20 @@ extern "C" {
 int cgroup_strtol(const char * const in_str, int base,
                  long int * const out_value);
 
+/**
+ * Convert an integer setting to another integer setting
+ *
+ * @param dst_cgc Destination cgroup controller
+ * @param in_value Contents of the input setting
+ * @param out_setting Destination cgroup setting
+ * @param in_dflt Default value of the input setting (used to scale the value)
+ * @param out_dflt Default value of the output setting (used to scale the value)
+ */
+int cgroup_convert_int(struct cgroup_controller * const dst_cgc,
+                      const char * const in_value,
+                      const char * const out_setting,
+                      void *in_dflt, void *out_dflt);
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif