This patch introduces the layer 2 API for the memory controller API. To start with, we just set and get the limit of the cgroup. As we go ahead, plans are to add APIs which can access the statistics and perform other functions.
For now, this is a candidate for merging. Signed-off-by: Dhaval Giani <[email protected]> --- include/libcgroup.h | 9 ++++ src/Makefile.am | 2 - src/api.c | 1 src/libcgroup.map | 2 + src/memory.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 1 deletion(-) Index: libcg/src/memory.c =================================================================== --- /dev/null +++ libcg/src/memory.c @@ -0,0 +1,98 @@ +/* + * Copyright IBM Corporation. 2010 + * + * Author: Dhaval Giani <[email protected]> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2.1 of the GNU Lesser General Public License + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it would be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * Code initiated and designed by Dhaval Giani. All faults are most likely + * his mistake. + * + */ + +#include <errno.h> +#include <libcgroup.h> +#include <libcgroup-internal.h> + +#define MEMORY "memory" + +extern int cgroup_initialized; + +int cgroup_memory_set_limit(char *name, u_int64_t limit) +{ + struct cgroup *cgroup; + struct cgroup_controller *cgc; + int ret = 0; + + if (!cgroup_initialized) + return ECGROUPNOTINITIALIZED; + + cgroup = cgroup_new_cgroup(name); + + if (!cgroup) + return ECGOTHER; + + cgc = cgroup_add_controller(cgroup, MEMORY); + + if (!cgc) { + /* + * The only reason cgroup_add_controller can fail is due to + * calloc failing. There is no other reason for it to fail. + */ + ret = ECGOTHER; + goto out; + } + + ret = cgroup_add_value_uint64(cgc, "memory.limit_in_bytes", limit); + + if (ret) + goto out; + + ret = cgroup_modify_cgroup(cgroup); + +out: + cgroup_free(&cgroup); + return ret; +} + +int cgroup_memory_get_limit(char *name, u_int64_t *limit) +{ + struct cgroup *cgroup; + struct cgroup_controller *cgc; + int ret = 0; + + if (!cgroup_initialized) + return ECGROUPNOTINITIALIZED; + + if (!limit) + return ECGINVAL; + + cgroup = cgroup_new_cgroup(name); + + if (!cgroup) { + last_errno = errno; + return ECGOTHER; + } + + ret = cgroup_get_cgroup(cgroup); + + if (ret) + goto out; + + cgc = cgroup_get_controller(cgroup, MEMORY); + + if (!cgc) + return ECGMEMNOTEXIST; + + ret = cgroup_get_value_uint64(cgc, "memory.limit_in_bytes", limit); + +out: + cgroup_free(&cgroup); + return ret; +} Index: libcg/include/libcgroup.h =================================================================== --- libcg.orig/include/libcgroup.h +++ libcg/include/libcgroup.h @@ -90,6 +90,7 @@ enum cgroup_errors { ECGEOF, /* End of file, iterator */ ECGCONFIGPARSEFAIL,/* Failed to parse config file (cgconfig.conf). */ ECGCPUNOTEXIST, /* This group dos not have the CPU controller for it */ + ECGMEMNOTEXIST, /* This group does not have the memory controller for it */ }; #define ECGRULESPARSEFAIL ECGROUPPARSEFAIL @@ -476,11 +477,19 @@ int cgroup_unload_cgroups(void); /* * Controller specific APIs */ + +/* + * cpu and cpuacct + */ int cgroup_cpu_set_shares(char *name, u_int64_t shares); int cgroup_cpu_get_shares(char *name, u_int64_t *shares); int cgroup_cpuacct_get_usage(char *name, u_int64_t *usage); int cgroup_cpuacct_reset_usage(char *name); +/* memory */ +int cgroup_memory_set_limit(char *name, u_int64_t limit); +int cgroup_memory_get_limit(char *name, u_int64_t *limit); + __END_DECLS #endif /* _LIBCG_H */ Index: libcg/src/Makefile.am =================================================================== --- libcg.orig/src/Makefile.am +++ libcg/src/Makefile.am @@ -5,7 +5,7 @@ AM_YFLAGS = -d INCLUDES = -I$(top_srcdir)/include lib_LTLIBRARIES = libcgroup.la -libcgroup_la_SOURCES = parse.y lex.l api.c config.c libcgroup-internal.h libcgroup.map wrapper.c cpu.c +libcgroup_la_SOURCES = parse.y lex.l api.c config.c libcgroup-internal.h libcgroup.map wrapper.c cpu.c memory.c libcgroup_la_LIBADD = -lpthread libcgroup_la_LDFLAGS = -Wl,--version-script,$(srcdir)/libcgroup.map \ -version-number $(LIBRARY_VERSION_MAJOR):$(LIBRARY_VERSION_MINOR):$(LIBRARY_VERSION_RELEASE) Index: libcg/src/api.c =================================================================== --- libcg.orig/src/api.c +++ libcg/src/api.c @@ -109,6 +109,7 @@ char *cgroup_strerror_codes[] = { "Sentinel" "End of File or iterator", "Cgroup does not exist for the CPU controller", + "Cgroup does not exist for memory controller", }; static int cg_chown_file(FTS *fts, FTSENT *ent, uid_t owner, gid_t group) Index: libcg/src/libcgroup.map =================================================================== --- libcg.orig/src/libcgroup.map +++ libcg/src/libcgroup.map @@ -86,4 +86,6 @@ global: cgroup_cpu_get_shares; cgroup_cpuacct_get_usage; cgroup_cpuacct_reset_usage; + cgroup_memory_set_limit; + cgroup_memory_get_limit; } CGROUP_0.34; ------------------------------------------------------------------------------ This SF.Net email is sponsored by the Verizon Developer Community Take advantage of Verizon's best-in-class app development support A streamlined, 14 day to market process makes app distribution fast and easy Join now and get one step closer to millions of Verizon customers http://p.sf.net/sfu/verizon-dev2dev _______________________________________________ Libcg-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/libcg-devel
