How to get an accurate value of VSZ for a cgroup tree?

49 Views Asked by At

--- Summary ---

One can get some memory values from the cgroup interface. For example, in cgroup v2 memory.current interface we have a value in bytes of the current anon and pagecache memory used by the cgroup tree. In memory.swap.current the amount of memory which is put into swap space for this cgroup tree is accounted for.

What I am not finding is how to get the VSZ of a cgroup. In memory.stat I read more fine-graned values, but none of them seems to represent VSZ. I am looking to the same VSZ I can read in /proc/'pid'/statm.

--- Questions ---

- Main question: How can I infer VSZ of a cgroup tree from the cgroup interfaces?

- Secondary question: What is the exact definition of memory.current and memory.swap.current? What do them include? Which is the criteria which developers have followed? A link to any lkml post would be great, cannot find any explanations and the kernel code is quite obscure to understand.

Note: I am looking for an answer specially for cgroup v2. But an answer for cgroup v1 would also be welcomed.

--- What I've tested ---

I did one simple test. The following code will malloc some memory but not touch it in the first 10 seconds, thus I expect VSZ to increase. After 10 seconds it will touch half of the malloc'ed memory, so I expect RSS to increase. I run this program into an empty cgroup. Then I can read /proc/'pid'/statm, memory.current and memory.stat, and I am not able to find a way to infer the VSZ value from /proc/'pid'/statm from memory.current or memory.stat.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void main() {

    char *p;
    // Allocate memory using malloc - vmem only
    p = malloc(1073741824);
    sleep(10);
    // Touch the memory, now it is RSS
    for (int i = 0; i < 536870912 ; i++) {
        *(p + i) = 0;
    }
    sleep(1000);
}

This is what ps shows after touching pages:

]$ ps -o pid,rss,vsize -p 291044
    PID   RSS    VSZ
 292095 525120 1051032

This is the relevant data which cat /proc/'pid'/statm shows before touching pages. There's no RssAnon used:

VmPeak:  1051032 kB
VmSize:  1051032 kB
VmLck:         0 kB
VmPin:         0 kB
VmHWM:       960 kB
VmRSS:       960 kB
RssAnon:      0 kB
RssFile:    960 kB
RssShmem:     0 kB

This is the relevant data which cat /proc/'pid'/statm shows after touching pages. VmSize is stable while RssAnon is increased:

VmPeak:  1051032 kB
VmSize:  1051032 kB
VmLck:         0 kB
VmPin:         0 kB
VmHWM:    525120 kB
VmRSS:    525120 kB
RssAnon:      524160 kB
RssFile:         960 kB
RssShmem:          0 kB
VmData:  1048780 kB
VmStk:       136 kB
VmExe:         4 kB
VmLib:      1556 kB
VmPTE:      1076 kB
VmSwap:        0 kB
HugetlbPages:          0 kB
CoreDumping:    0
THP_enabled:    1
Threads:    1

This table shows the readings of memory.stat when only malloc'ing, and after touching pages:

Reading from memory.stat:

 - column 1: field
 - column 2: before touching pages, only malloc
 - column 3: after touching pages

    anon    8192    536879104
    file    0   0
    kernel  16384   1064960
    kernel_stack    0   0
    pagetables  12288   1060864
    sec_pagetables  0   0
    percpu  0   0
    sock    0   0
    vmalloc 0   0
    shmem   0   0
    zswap   0   0
    zswapped    0   0
    file_mapped 0   0
    file_dirty  0   0
    file_writeback  0   0
    swapcached  0   0
    anon_thp    0   0
    file_thp    0   0
    shmem_thp   0   0
    inactive_anon   0   0
    active_anon 8192    536862720
    inactive_file   0   0
    active_file 0   0
    unevictable 0   0
    slab_reclaimable    0   0
    slab_unreclaimable  344 344
    slab    344 344
    workingset_refault_anon 0   0
    workingset_refault_file 0   0
    workingset_activate_anon    0   0
    workingset_activate_file    0   0
    workingset_restore_anon 0   0
    workingset_restore_file 0   0
    workingset_nodereclaim  0   0
    pgscan  0   0
    pgsteal 0   0
    pgscan_kswapd   0   0
    pgscan_direct   0   0
    pgscan_khugepaged   0   0
    pgsteal_kswapd  0   0
    pgsteal_direct  0   0
    pgsteal_khugepaged  0   0
    pgfault 2   131074
    pgmajfault  0   0
    pgrefill    0   0
    pgactivate  0   0
    pgdeactivate    0   0
    pglazyfree  0   0
    pglazyfreed 0   0
    zswpin  0   0
    zswpout 0   0
    thp_fault_alloc 0   0
    thp_collapse_alloc  0   0

Reading from memory.current:

 - column 1: field
 - column 2: before touching pages, only malloc
 - column 3: after touching pages


    memory.current     24576    202342400
0

There are 0 best solutions below