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.hroma...@oracle.com> --- src/abstraction-common.c | 44 ++++++++++++++++++++++++++++++++++++++++ src/abstraction-common.h | 14 +++++++++++++ 2 files changed, 58 insertions(+) diff --git a/src/abstraction-common.c b/src/abstraction-common.c index b70a122b9f19..df5fa1b3a452 100644 --- a/src/abstraction-common.c +++ b/src/abstraction-common.c @@ -64,3 +64,47 @@ 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); + ret = snprintf(out_value_str, OUT_VALUE_STR_LEN, "%ld\n", 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; +} diff --git a/src/abstraction-common.h b/src/abstraction-common.h index 12807f4cddaf..02f7b9ff9541 100644 --- a/src/abstraction-common.h +++ b/src/abstraction-common.h @@ -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 -- 2.25.1 _______________________________________________ Libcg-devel mailing list Libcg-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/libcg-devel