/* * Test creation of container with cgroup controller. Impose memory limits . */ #define _XOPEN_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #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); printf("\n libct_controller_configure returned %d \n", ret); ret = libct_controller_configure(ct, CTL_MEMORY, "memory.memsw.limit_in_bytes", MEMLIMIT_STR); printf("\n libct_controller_configure returned %d \n", ret); state = libct_container_state(ct); printf(" Before spawn_exec state of the container is %d \n",state); 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"); }