Linux – Page cache usage listed in cgroup’s memory.stat file

cgrouplinuxmemory usage

A cgroup's memory.stat file lists the page cache usage for that cgroup (in cache and total_cache fields). How is the page cache usage derived?

For example, file a.txt is opened by both cgroup A and B. Do A and B both maintain their own cache of a.txt? Or is the cache of a.txt shared between A and B? If it's shared, will memory.stat's page cache usage for both A and B be incremented by the same amount (total amount)?

Also, why doesn't memory.stat include a field for buffer cache or swap cache?

Distro is Linux Mint 18.2.

Best Answer

This is discussed in section 2.3 of https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt...

2.3 Shared Page Accounting

Shared pages are accounted on the basis of the first touch approach. The cgroup that first touches a page is accounted for the page. The principle behind this approach is that a cgroup that aggressively uses a shared page will eventually get charged for it (once it is uncharged from the cgroup that brought it in -- this will happen on memory pressure).

As for the other questions...

Since Linux 2.4, the buffer has been added to the cache (see https://stackoverflow.com/a/14671569). As such, you won't find a separate field for buffer in memory.stat.

For swap to be listed in memory.stat, it's likely that the kernel needs to be compiled with CONFIG_MEMCG_SWAP_ENABLED.