[Libct] Regarding Libct: Cgroup API
Monali Porob
Monali.Porob at huawei.com
Fri Dec 12 16:43:45 PST 2014
Hi ,
I was working on creating container with memory limits sing Cgroup APIs in libct . Attached is the test program .
While testing I figured out that the limits were not getting set and I debugged it further to find :
In src/cgroups.c , function cgroups_create() which is called in local_spawn_cb() when state is yet not set to CT_RUNNING calls local_config_controller() and that postpones the cgroup configuration .
Hence I changed cgroups_create() to call config_controller() instead of local_config_controller()
As follows ret = config_controller(ct, cfg->ctype, cfg->param, cfg->value);
With this change, the cgroup limits are getting set correctly for the process which spawns/enters in the container .
Qn : Does this change look correct or is there something missing in my test program.
Also In future, would libct have an API to support changing cgroup limits while container is running ?
Thanks and Regards ,
Monali.
Test program
/*
* Test creation of container with cgroup controller. Impose memory limits .
*/
#define _XOPEN_SOURCE
#include <libct.h>
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <libct.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
#include "test.h"
#define CPUS "0"
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
#define MEMLIMIT 134217728 //128 MB
#define MEMLIMIT_STR STR(MEMLIMIT)
#define LOWER_THAN_LIMIT 104857600 //100 MB
#define LOWER_THAN_LIMIT_STR STR(LOWER_THAN_LIMIT)
// For testing Define STRESS_MEM_STR to either LOWER_THAN_LIMIT_STR or MEMLIMIT_STR
//#define STRESS_MEM_STR LOWER_THAN_LIMIT_STR
#define STRESS_MEM_STR MEMLIMIT_STR
int is_memory_limit_correct(const char *expected_limit)
{
int ret = 0;
FILE *f = NULL;
char buf[1024] = {'\0'};
f = fopen("/sys/fs/cgroup/memory/test1/memory.limit_in_bytes", "r");
if (!f) {
fprintf(stderr, "Unable to open memory limit file !\n");
goto err;
}
if (fscanf(f, "%s", buf) != 1) {
fprintf(stderr, "fscanf failed!\n");
goto err;
}
printf(" /sys/fs/cgroup/memory/test1/memory.limit_in_bytes now contains %s \n",buf);
ret = !strcmp(buf, expected_limit);
err:
fclose(f);
return ret;
}
int main(int argc, char **argv)
{
libct_session_t s;
ct_handler_t ct;
ct_process_desc_t p;
int ret = 0;
int state = 0;
s = libct_session_open_local();
ct = libct_container_create(s, "test1");
p = libct_process_desc_create(s);
libct_controller_add(ct, CTL_MEMORY);
ret = libct_controller_configure(ct, CTL_MEMORY, "memory.limit_in_bytes", MEMLIMIT_STR);
ret = libct_controller_configure(ct, CTL_MEMORY, "memory.memsw.limit_in_bytes", MEMLIMIT_STR);
char *stress_a[8];
int ret_pid = 0;
stress_a[0] = "/usr/bin/stress";
stress_a[1] = "--vm";
stress_a[2] = "1";
stress_a[3] = "--vm-bytes";
stress_a[4] = STRESS_MEM_STR;
stress_a[5] = "--vm-hang";
stress_a[6] = "0";
stress_a[7]= NULL;
ret_pid = libct_container_spawn_execv(ct,p, "/usr/bin/stress",stress_a);
printf("\n libct_container_spawn_execv returned %d \n", ret_pid);
if (ret_pid <= 0 )
return fail("Can't spawn stress program in container");
state = libct_container_state(ct);
printf("\n After spawn_exec state of the container is %d \n",state);
if (!is_memory_limit_correct(MEMLIMIT_STR))
return fail("Memory settings dont match to the expected value");
if (libct_container_wait(ct) < 0)
goto err;
libct_container_destroy(ct);
libct_session_close(s);
return pass("Memory limited CT is OK");
err:
return fail("Something wrong");
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openvz.org/pipermail/libct/attachments/20141213/062b37a3/attachment.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ct_cgroup_memory.c
URL: <http://lists.openvz.org/pipermail/libct/attachments/20141213/062b37a3/attachment.c>
More information about the Libct
mailing list