Sungju's Slow Life

Personal journal


How to calculate available memory for mem_cgroup.

mem_cgroup is checking available amount of memory the group can charge by calling the below function.

/**
 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
 * @memcg: the memory cgroup
 *
 * Returns the maximum amount of memory @mem can be charged with, in
 * pages.
 */
static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
{
    unsigned long margin = 0;
    unsigned long count;
    unsigned long limit;

    count = page_counter_read(&memcg->memory);
    limit = ACCESS_ONCE(memcg->memory.limit);
    if (count memsw);
        limit = ACCESS_ONCE(memcg->memsw.limit);
        if (count <= limit)
            margin = min(margin, limit - count);
    }

    return margin;
}

In the below example, it can charge 32 (128KB) more pages. Even though do_swap_account was on, it can’t use mews as ‘counter’ value is bigger than limit.

crash> mem_cgroup.memory,memsw 0xffff9b34e4856000
  memory = {
    count = {
      counter = 0x3ffe0
    }, 
    limit = 0x40000, 
    parent = 0xffff9b3cde6c5848, 
    watermark = 0x40095, 
    failcnt = 0x2f759ae
  }
    memsw = {
      count = {
        counter = 0x80002
      }, 
      limit = 0x80000, 
      parent = 0xffff9b3cde6c5908, 
      watermark = 0x80006, 
      failcnt = 0x1a8484d
    }
crash> p do_swap_account
do_swap_account = $15 = 0x1
crash> pd 0x40000-0x3ffe0
$16 = 32


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About Me

A software engineer who loves any technologies that makes life easier. That’s why I love Linux and Mac at the same time.

Newsletter

%d bloggers like this: