]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
abstraction-map: Add memory controller abstractions
authorTom Hromatka <tom.hromatka@oracle.com>
Wed, 26 Feb 2025 19:48:13 +0000 (19:48 +0000)
committerKamalesh Babulal <kamalesh.babulal@oracle.com>
Fri, 25 Apr 2025 09:50:21 +0000 (15:20 +0530)
Add abstraction logic for:
memory.max <-> memory.limit_in_bytes
memory.high <-> memory.soft_limit_in_bytes

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
src/Makefile.am
src/abstraction-common.h
src/abstraction-map.c
src/abstraction-memory.c [new file with mode: 0644]

index 50065cf97f16f21926b07934141f19fcd07bf25a..c145d86e471cc82f9cd8027e5523382d43411363 100644 (file)
@@ -31,6 +31,7 @@ lib_LTLIBRARIES = libcgroup.la
 libcgroup_la_SOURCES = parse.h parse.y lex.l api.c config.c libcgroup-internal.h libcgroup.map \
                       wrapper.c log.c abstraction-common.c abstraction-common.h \
                       abstraction-map.c abstraction-map.h abstraction-cpu.c abstraction-cpuset.c \
+                      abstraction-memory.c \
                       systemd.c tools/cgxget.c tools/cgxset.c
 
 libcgroup_la_LIBADD = -lpthread $(CODE_COVERAGE_LIBS)
@@ -47,7 +48,8 @@ noinst_LTLIBRARIES = libcgroupfortesting.la
 libcgroupfortesting_la_SOURCES = parse.h parse.y lex.l api.c config.c libcgroup-internal.h \
                                 libcgroup.map wrapper.c log.c abstraction-common.c \
                                 abstraction-common.h abstraction-map.c abstraction-map.h \
-                                abstraction-cpu.c abstraction-cpuset.c systemd.c
+                                abstraction-cpu.c abstraction-cpuset.c abstraction-memory.c \
+                                systemd.c
 
 libcgroupfortesting_la_LIBADD = -lpthread $(CODE_COVERAGE_LIBS)
 libcgroupfortesting_la_CFLAGS = $(CODE_COVERAGE_CFLAGS) -DSTATIC= -DUNIT_TEST
index 9507e58359da9e507cd15ecef50eb5d95022f0ca..cb6c538e131a9ee450f0a25631af81f18c94fc22 100644 (file)
@@ -112,6 +112,23 @@ int cgroup_convert_cpuset_to_partition(struct cgroup_controller * const dst_cgc,
                                       const char * const in_value, const char * const out_setting,
                                       void *in_dflt, void *out_dflt);
 
+/* memory */
+int cgroup_convert_memory_limit_to_max(struct cgroup_controller * const dst_cgc,
+                               const char * const in_value, const char * const out_setting,
+                               void *in_dflt, void *out_dflt);
+
+int cgroup_convert_memory_soft_limit_to_max(struct cgroup_controller * const dst_cgc,
+                               const char * const in_value, const char * const out_setting,
+                               void *in_dflt, void *out_dflt);
+
+int cgroup_convert_memory_max_to_limit(struct cgroup_controller * const dst_cgc,
+                               const char * const in_value, const char * const out_setting,
+                               void *in_dflt, void *out_dflt);
+
+int cgroup_convert_memory_high_to_soft_limit(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
index 3e63b9d5f8a720c9ee7d2733ac5eaa614d1ac2a0..3b1758bb468994f57ab6c12fc216d68cf20587ec 100644 (file)
@@ -41,6 +41,12 @@ const struct cgroup_abstraction_map cgroup_v1_to_v2_map[] = {
        {cgroup_convert_unmappable, "cpuset.memory_spread_slab", NULL, NULL, NULL},
        {cgroup_convert_unmappable, "cpuset.sched_load_balance", NULL, NULL, NULL},
        {cgroup_convert_unmappable, "cpuset.sched_relax_domain_level", NULL, NULL, NULL},
+
+       /* memory controller */
+       {cgroup_convert_memory_limit_to_max, "memory.limit_in_bytes", NULL,
+               "memory.max", NULL},
+       {cgroup_convert_memory_soft_limit_to_max, "memory.soft_limit_in_bytes", NULL,
+               "memory.high", NULL},
 };
 const int cgroup_v1_to_v2_map_sz = ARRAY_SIZE(cgroup_v1_to_v2_map);
 
@@ -58,5 +64,11 @@ const struct cgroup_abstraction_map cgroup_v2_to_v1_map[] = {
        {cgroup_convert_passthrough, "cpuset.mems", NULL, "cpuset.mems", NULL},
        {cgroup_convert_cpuset_to_exclusive, "cpuset.cpus.partition", NULL,
                "cpuset.cpu_exclusive", NULL},
+
+       /* memory controller */
+       {cgroup_convert_memory_max_to_limit, "memory.max", NULL,
+               "memory.limit_in_bytes", NULL},
+       {cgroup_convert_memory_high_to_soft_limit, "memory.high", NULL,
+               "memory.soft_limit_in_bytes", NULL},
 };
 const int cgroup_v2_to_v1_map_sz = ARRAY_SIZE(cgroup_v2_to_v1_map);
diff --git a/src/abstraction-memory.c b/src/abstraction-memory.c
new file mode 100644 (file)
index 0000000..d508f4d
--- /dev/null
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: LGPL-2.1-only
+/**
+ * Libcgroup abstraction layer for the memory controller
+ *
+ * Copyright (c) 2021-2025 Oracle and/or its affiliates.
+ * Author: Tom Hromatka <tom.hromatka@oracle.com>
+ */
+
+#include "abstraction-common.h"
+#include "abstraction-map.h"
+
+#include <libcgroup.h>
+#include <libcgroup-internal.h>
+
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdio.h>
+
+#define V1_NEG1_STR "-1"
+#define V1_MAX_STR "9223372036854771712"
+#define V2_MAX_STR "max"
+
+#define LL_MAX 21
+
+static int convert_limit_to_max(struct cgroup_controller * const dst_cgc,
+               const char * const in_setting, const char * const in_value,
+               const char * const out_setting)
+{
+       char max_line[LL_MAX] = {0};
+       int ret;
+
+       if (strlen(in_value) == 0) {
+               ret = cgroup_add_value_string(dst_cgc, out_setting, NULL);
+       } else {
+               if (strcmp(in_value, V1_NEG1_STR) == 0)
+                       snprintf(max_line, LL_MAX, "%s", V2_MAX_STR);
+               else if (strcmp(in_value, V1_MAX_STR) == 0)
+                       snprintf(max_line, LL_MAX, "%s", V2_MAX_STR);
+               else
+                       snprintf(max_line, LL_MAX, "%s", in_value);
+
+               ret = cgroup_add_value_string(dst_cgc, out_setting, max_line);
+       }
+
+       return ret;
+}
+
+int cgroup_convert_memory_limit_to_max(struct cgroup_controller * const dst_cgc,
+                               const char * const in_value, const char * const out_setting,
+                               void *in_dflt, void *out_dflt)
+{
+       return convert_limit_to_max(dst_cgc, "memory.limit_in_bytes", in_value, out_setting);
+}
+
+int cgroup_convert_memory_soft_limit_to_max(struct cgroup_controller * const dst_cgc,
+                               const char * const in_value, const char * const out_setting,
+                               void *in_dflt, void *out_dflt)
+{
+       return convert_limit_to_max(dst_cgc, "memory.soft_limit_in_bytes", in_value, out_setting);
+}
+
+static int convert_max_to_limit(struct cgroup_controller * const dst_cgc,
+               const char * const in_setting,  const char * const in_value,
+               const char * const out_setting)
+{
+       char limit_line[LL_MAX] = {0};
+       int ret;
+
+       if (strlen(in_value) == 0) {
+               ret = cgroup_add_value_string(dst_cgc, out_setting, NULL);
+       } else {
+               if (strcmp(in_value, V2_MAX_STR) == 0)
+                       snprintf(limit_line, LL_MAX, "%s", V1_MAX_STR);
+               else
+                       snprintf(limit_line, LL_MAX, "%s", in_value);
+
+               ret = cgroup_add_value_string(dst_cgc, out_setting, limit_line);
+       }
+
+       return ret;
+}
+
+int cgroup_convert_memory_max_to_limit(struct cgroup_controller * const dst_cgc,
+                               const char * const in_value, const char * const out_setting,
+                               void *in_dflt, void *out_dflt)
+{
+       return convert_max_to_limit(dst_cgc, "memory.max", in_value, out_setting);
+}
+
+int cgroup_convert_memory_high_to_soft_limit(struct cgroup_controller * const dst_cgc,
+                               const char * const in_value, const char * const out_setting,
+                               void *in_dflt, void *out_dflt)
+{
+       return convert_max_to_limit(dst_cgc, "memory.high", in_value, out_setting);
+}