Skip to content

Container Memory Support - #1

Open
luochenglcs wants to merge 7 commits into
procps-ng:masterfrom
luochenglcs:master
Open

Container Memory Support#1
luochenglcs wants to merge 7 commits into
procps-ng:masterfrom
luochenglcs:master

Conversation

@luochenglcs

Copy link
Copy Markdown
Contributor

Currently, containers do not have isolated resource views. As a result, commands such as free, vmstat executed within a container reflect the host's global memory state rather than the container's actual constrained resources. This behavior is inherently misleading—especially when memory limits are imposed on the container, since the output does not represent the resources truly available to it.

To resolve this discrepancy, we now extract memory usage data directly from the container's corresponding cgroup. This enhancement enables free, vmstat to report container-specific memory information. When run inside a container, these commands can utilize dedicated parameters to display memory usage based on cgroup limits, providing an accurate view of the container's resource context instead of inheriting the host's memory statistics.

https://gitlab.com/procps-ng/procps/-/issues/389

Added a stype field to the struct meminfo_info to
explicitly store the source type of meminfo data.

Additionally, extracted the meminfo buffer reading
logic into a dedicated procps_meminfo_read_buf.

Signed-off-by: Chunsheng Luo <luffyluo@tencent.com>
Introduces comprehensive cgroup memory information
support, enabling the library to read memory limits,
usage, swap, and cache statistics from cgroup v1 and
v2 hierarchies.

The implementation includes automatic cgroup version
detection, path traversal to locate the memory controller,
and parsing of memory statistics including active/inactive
memory, slab information, and swap usage.

The module provides a unified interface to access
container memory metrics while maintaining compatibility
with traditional /proc/meminfo format。

Signed-off-by: Chunsheng Luo <luffyluo@tencent.com>
Introduces cgroup-aware memory information to the
meminfo, enabling applications to read memory
statistics from cgroup controllers instead of the
traditional /proc/meminfo. This enhancement allows
applications to access accurate memory limits and
usage that reflect container constraints rather
than host-wide system resources.

The implementation adds a new CGROUP_MEMINFO_TYPE
source type that leverages the existing cgmeminfo
module to provide container-aware memory reporting.

Changes:
- adding the cgroup_meminfo_new() public API function

Signed-off-by: Chunsheng Luo <luffyluo@tencent.com>
Introduces container-aware memory reporting to the
free command by adding a new --container option.

When specified, the command uses cgroup_meminfo_new()
instead of procps_meminfo_new() to retrieve memory
information from cgroup controllers rather than
/proc/meminfo.

This enhancement allows users to view memory statistics
that reflect container limits and usage in containerized
environments, providing more accurate resource information
for applications running within containers.

Signed-off-by: Chunsheng Luo <luffyluo@tencent.com>
Introduces container-aware memory reporting to the
vmstat command by adding a new --container (-C) option.

When specified, the command uses cgroup_meminfo_new()
instead of procps_meminfo_new() to retrieve memory
information from cgroup controllers rather than
/proc/meminfo.

This enhancement enables vmstat to display memory
statistics that reflect container limits and usage
in containerized environments, providing more accurate
resource information for vmstat memory output.

Signed-off-by: Chunsheng Luo <luffyluo@tencent.com>
Introduces detailed documentation for the new container
memory support feature in procps project. The documentation
covers the implementation of cgroup-aware memory reporting
for free and vmstat commands, enabling accurate resource
information display within containerized environments.

Signed-off-by: Chunsheng Luo <luffyluo@tencent.com>
Test suite for cgmeminfo module:
- Basic tests: Cgroup memory info creation, cleanup
- Parsing tests: cgroup v1/v2 memory statistics parsing

Signed-off-by: Chunsheng Luo <luffyluo@tencent.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant