diff --git a/src/Common/AsynchronousMetrics.cpp b/src/Common/AsynchronousMetrics.cpp index 6c55bfe3d97..ac2180103c5 100644 --- a/src/Common/AsynchronousMetrics.cpp +++ b/src/Common/AsynchronousMetrics.cpp @@ -67,8 +67,15 @@ AsynchronousMetrics::AsynchronousMetrics( openFileIfExists("/proc/uptime", uptime); openFileIfExists("/proc/net/dev", net_dev); - openFileIfExists("/sys/fs/cgroup/memory/memory.limit_in_bytes", cgroupmem_limit_in_bytes); - openFileIfExists("/sys/fs/cgroup/memory/memory.usage_in_bytes", cgroupmem_usage_in_bytes); + /// CGroups v2 + openFileIfExists("/sys/fs/cgroup/memory.max", cgroupmem_limit_in_bytes); + openFileIfExists("/sys/fs/cgroup/memory.current", cgroupmem_usage_in_bytes); + + /// CGroups v1 + if (!cgroupmem_limit_in_bytes) + openFileIfExists("/sys/fs/cgroup/memory/memory.limit_in_bytes", cgroupmem_limit_in_bytes); + if (!cgroupmem_usage_in_bytes) + openFileIfExists("/sys/fs/cgroup/memory/memory.usage_in_bytes", cgroupmem_usage_in_bytes); openSensors(); openBlockDevices(); @@ -900,33 +907,25 @@ void AsynchronousMetrics::update(TimePoint update_time) if (cgroupmem_limit_in_bytes && cgroupmem_usage_in_bytes) { - try { + try + { cgroupmem_limit_in_bytes->rewind(); cgroupmem_usage_in_bytes->rewind(); - uint64_t cgroup_mem_limit_in_bytes = 0; - uint64_t cgroup_mem_usage_in_bytes = 0; + uint64_t limit = 0; + uint64_t usage = 0; - readText(cgroup_mem_limit_in_bytes, *cgroupmem_limit_in_bytes); - readText(cgroup_mem_usage_in_bytes, *cgroupmem_usage_in_bytes); + tryReadText(limit, *cgroupmem_limit_in_bytes); + tryReadText(usage, *cgroupmem_usage_in_bytes); - if (cgroup_mem_limit_in_bytes && cgroup_mem_usage_in_bytes) - { - new_values["CgroupMemoryTotal"] = { cgroup_mem_limit_in_bytes, "The total amount of memory in cgroup, in bytes." }; - new_values["CgroupMemoryUsed"] = { cgroup_mem_usage_in_bytes, "The amount of memory used in cgroup, in bytes." }; - } - else - { - LOG_DEBUG(log, "Cannot read statistics about the cgroup memory total and used. Total got '{}', Used got '{}'.", - cgroup_mem_limit_in_bytes, cgroup_mem_usage_in_bytes); - } + new_values["CGroupMemoryTotal"] = { limit, "The total amount of memory in cgroup, in bytes. If stated zero, the limit is the same as OSMemoryTotal." }; + new_values["CGroupMemoryUsed"] = { usage, "The amount of memory used in cgroup, in bytes." }; } catch (...) { tryLogCurrentException(__PRETTY_FUNCTION__); } } - if (meminfo) { try