static int memcg_initialize (xcgroup_ns_t *ns, xcgroup_t *cg, char *path, uint64_t mem_limit, uid_t uid, gid_t gid) { uint64_t mlb = mem_limit_in_bytes (mem_limit); uint64_t mls = swap_limit_in_bytes (mem_limit); if (xcgroup_create (ns, cg, path, uid, gid) != XCGROUP_SUCCESS) return -1; if (xcgroup_instanciate (cg) != XCGROUP_SUCCESS) { xcgroup_destroy (cg); return -1; } xcgroup_set_param (cg, "memory.use_hierarchy","1"); xcgroup_set_uint64_param (cg, "memory.limit_in_bytes", mlb); xcgroup_set_uint64_param (cg, "memory.memsw.limit_in_bytes", mls); info ("task/cgroup: %s: alloc=%luMB mem.limit=%luMB memsw.limit=%luMB", path, (unsigned long) mem_limit, (unsigned long) mlb/(1024*1024), (unsigned long) mls/(1024*1024)); return 0; }
/* * Return configured swap limit in bytes given a memory limit in MB. * * Swap limit is calculated as: * * mem_limit_in_bytes + (configured_swap_percent * allocated_mem_in_bytes) */ static uint64_t swap_limit_in_bytes (uint64_t mem) { uint64_t swap; /* * If mem == 0 assume "unlimited" and use totalram. */ swap = percent_in_bytes (mem ? mem : totalram, allowed_swap_space); mem = mem_limit_in_bytes (mem) + swap; if (mem < min_ram_space) return (min_ram_space); if (mem > max_swap) return (max_swap); return (mem); }
static int memcg_initialize (xcgroup_ns_t *ns, xcgroup_t *cg, char *path, uint64_t mem_limit, uid_t uid, gid_t gid, uint32_t notify) { uint64_t mlb = mem_limit_in_bytes (mem_limit); uint64_t mls = swap_limit_in_bytes (mem_limit); if (xcgroup_create (ns, cg, path, uid, gid) != XCGROUP_SUCCESS) return -1; cg->notify = notify; if (xcgroup_instantiate (cg) != XCGROUP_SUCCESS) { xcgroup_destroy (cg); return -1; } xcgroup_set_param (cg, "memory.use_hierarchy", "1"); /* when RAM space has not to be constrained and we are here, it * means that only Swap space has to be constrained. Thus set * RAM space limit to the mem+swap limit too */ if ( ! constrain_ram_space ) mlb = mls; xcgroup_set_uint64_param (cg, "memory.limit_in_bytes", mlb); /* * Also constrain kernel memory (if available). * See https://lwn.net/Articles/516529/ */ xcgroup_set_uint64_param (cg, "memory.kmem.limit_in_bytes", mlb); /* this limit has to be set only if ConstrainSwapSpace is set to yes */ if ( constrain_swap_space ) { xcgroup_set_uint64_param (cg, "memory.memsw.limit_in_bytes", mls); info ("task/cgroup: %s: alloc=%luMB mem.limit=%luMB " "memsw.limit=%luMB", path, (unsigned long) mem_limit, (unsigned long) mlb/(1024*1024), (unsigned long) mls/(1024*1024)); } else { info ("task/cgroup: %s: alloc=%luMB mem.limit=%luMB " "memsw.limit=unlimited", path, (unsigned long) mem_limit, (unsigned long) mlb/(1024*1024)); } return 0; }