From: Sage Weil Date: Fri, 8 Mar 2019 17:27:20 +0000 (-0600) Subject: common/util: get_cgroup_memory_limit() helper X-Git-Tag: v14.2.0~66^2~4 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=6bc541f6f0864f62240474d5b1a8502a2c7a149f;p=ceph.git common/util: get_cgroup_memory_limit() helper Signed-off-by: Sage Weil --- diff --git a/src/common/util.cc b/src/common/util.cc index 03e7c878448c5..84d413590f994 100644 --- a/src/common/util.cc +++ b/src/common/util.cc @@ -135,6 +135,38 @@ static void distro_detect(map *m, CephContext *cct) } } +int get_cgroup_memory_limit(uint64_t *limit) +{ + // /sys/fs/cgroup/memory/memory.limit_in_bytes + + // the magic value 9223372036854771712 or 0x7ffffffffffff000 + // appears to mean no limit. + FILE *f = fopen(PROCPREFIX "/sys/fs/cgroup/memory/memory.limit_in_bytes", "r"); + if (!f) { + return -errno; + } + char buf[100]; + int ret = 0; + long long value; + char *line = fgets(buf, sizeof(buf), f); + if (!line) { + ret = -EINVAL; + goto out; + } + if (sscanf(line, "%lld", &value) != 1) { + ret = -EINVAL; + } + if (value == 0x7ffffffffffff000) { + *limit = 0; // no limit + } else { + *limit = value; + } +out: + fclose(f); + return ret; +} + + void collect_sys_info(map *m, CephContext *cct) { // version diff --git a/src/include/util.h b/src/include/util.h index 62d4b6f35d599..9c377aa1413da 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -73,6 +73,9 @@ WRITE_CLASS_ENCODER(ceph_data_stats) int get_fs_stats(ceph_data_stats_t &stats, const char *path); +/// get memory limit for the current cgroup +int get_cgroup_memory_limit(uint64_t *limit); + /// collect info from @p uname(2), @p /proc/meminfo and @p /proc/cpuinfo void collect_sys_info(map *m, CephContext *cct);