From 6e279e4fb13115f9fe7966ef4c16bd057acae91f Mon Sep 17 00:00:00 2001 From: Jan Kryl Date: Mon, 4 Jun 2018 09:33:21 +0200 Subject: [PATCH] US1644 configurable memory consumption limit (#56) The memory limit is configurable be means of setting cgroup mem limit when starting a container (-m docker run option) Signed-off-by: Jan Kryl --- lib/libzpool/kernel.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/libzpool/kernel.c b/lib/libzpool/kernel.c index bc91e8964b34..d9c406d10e54 100644 --- a/lib/libzpool/kernel.c +++ b/lib/libzpool/kernel.c @@ -1509,12 +1509,25 @@ void kernel_init(int mode) { extern uint_t rrw_tsd_key; + uint64_t cgroupmem; + FILE *f; umem_nofail_callback(umem_out_of_memory); physmem = sysconf(_SC_PHYS_PAGES); - dprintf("physmem = %llu pages (%.2f GB)\n", physmem, + /* If we run inside a container get the cgroup mem limit */ + f = fopen("/sys/fs/cgroup/memory/memory.limit_in_bytes", "r"); + if (f != NULL) { + if (fscanf(f, "%lu", &cgroupmem) == 1) { + cgroupmem /= sysconf(_SC_PAGE_SIZE); + if (physmem > cgroupmem) + physmem = cgroupmem; + } + fclose(f); + } + + printf("physmem = %lu pages (%.2f GB)\n", physmem, (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30)); (void) snprintf(hw_serial, sizeof (hw_serial), "%ld",