Re: [PATCH] memcg: reclaim memory from nodes in round robin

From: KAMEZAWA Hiroyuki
Date: Wed Apr 27 2011 - 00:35:08 EST


On Tue, 26 Apr 2011 20:52:39 -0700
Ying Han <yinghan@xxxxxxxxxx> wrote:

> On Tue, Apr 26, 2011 at 7:57 PM, KAMEZAWA Hiroyuki
> <kamezawa.hiroyu@xxxxxxxxxxxxxx> wrote:
> > Now, memory cgroup's direct reclaim frees memory from the current node.
> > But this has some troubles. In usual, when a set of threads works in
> > cooperative way, they are tend to on the same node. So, if they hit
> > limits under memcg, it will reclaim memory from themselves, it may be
> > active working set.
> >
> > For example, assume 2 node system which has Node 0 and Node 1
> > and a memcg which has 1G limit. After some work, file cacne remains and
> > and usages are
> > Â Node 0: Â1M
> > Â Node 1: Â998M.
> >
> > and run an application on Node 0, it will eats its foot before freeing
> > unnecessary file caches.
> >
> > This patch adds round-robin for NUMA and adds equal pressure to each
> > node. When using cpuset's spread memory feature, this will work very well.
> >
> > But yes, better algorithm is appreciated.
> >
> > From: Ying Han <yinghan@xxxxxxxxxx>
> > Signed-off-by: Ying Han <yinghan@xxxxxxxxxx>
> > Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@xxxxxxxxxxxxxx>
> > ---
> > Âinclude/linux/memcontrol.h | Â Â1 +
> > Âmm/memcontrol.c      Â|  25 +++++++++++++++++++++++++
> > Âmm/vmscan.c        Â|  Â9 ++++++++-
> > Â3 files changed, 34 insertions(+), 1 deletion(-)
> >
> > Index: memcg/include/linux/memcontrol.h
> > ===================================================================
> > --- memcg.orig/include/linux/memcontrol.h
> > +++ memcg/include/linux/memcontrol.h
> > @@ -108,6 +108,7 @@ extern void mem_cgroup_end_migration(str
> > Â*/
> > Âint mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg);
> > Âint mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg);
> > +int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
> > Âunsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct zone *zone,
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â enum lru_list lru);
> > Index: memcg/mm/memcontrol.c
> > ===================================================================
> > --- memcg.orig/mm/memcontrol.c
> > +++ memcg/mm/memcontrol.c
> > @@ -237,6 +237,7 @@ struct mem_cgroup {
> > Â Â Â Â * reclaimed from.
> > Â Â Â Â */
> > Â Â Â Âint last_scanned_child;
> > + Â Â Â int last_scanned_node;
> > Â Â Â Â/*
> > Â Â Â Â * Should the accounting and control be hierarchical, per subtree?
> > Â Â Â Â */
> > @@ -1472,6 +1473,29 @@ mem_cgroup_select_victim(struct mem_cgro
> > Â}
> >
> > Â/*
> > + * Selecting a node where we start reclaim from. Because what we need is just
> > + * reducing usage counter, start from anywhere is O,K. When considering
> > + * memory reclaim from current node, there are pros. and cons.
> > + * Freeing memory from current node means freeing memory from a node which
> > + * we'll use or we've used. So, it may make LRU bad. And if several threads
> > + * hit limits, it will see a contention on a node. But freeing from remote
> > + * node mean more costs for memory reclaim because of memory latency.
> > + *
> > + * Now, we use round-robin. Better algorithm is welcomed.
> > + */
> > +int mem_cgroup_select_victim_node(struct mem_cgroup *mem)
> > +{
> > + Â Â Â int node;
> > +
> > + Â Â Â node = next_node(mem->last_scanned_node, node_states[N_HIGH_MEMORY]);
> > + Â Â Â if (node == MAX_NUMNODES)
> > + Â Â Â Â Â Â Â node = first_node(node_states[N_HIGH_MEMORY]);
> > +
> > + Â Â Â mem->last_scanned_node = node;
> > + Â Â Â return node;
> > +}
> > +
> > +/*
> > Â* Scan the hierarchy if needed to reclaim memory. We remember the last child
> > Â* we reclaimed from, so that we don't end up penalizing one child extensively
> > Â* based on its position in the children list.
> > @@ -4678,6 +4702,7 @@ mem_cgroup_create(struct cgroup_subsys *
> > Â Â Â Â Â Â Â Âres_counter_init(&mem->memsw, NULL);
> > Â Â Â Â}
> > Â Â Â Âmem->last_scanned_child = 0;
> > + Â Â Â mem->last_scanned_node = MAX_NUMNODES;
> > Â Â Â ÂINIT_LIST_HEAD(&mem->oom_notify);
> >
> > Â Â Â Âif (parent)
> > Index: memcg/mm/vmscan.c
> > ===================================================================
> > --- memcg.orig/mm/vmscan.c
> > +++ memcg/mm/vmscan.c
> > @@ -2198,6 +2198,7 @@ unsigned long try_to_free_mem_cgroup_pag
> > Â{
> > Â Â Â Âstruct zonelist *zonelist;
> > Â Â Â Âunsigned long nr_reclaimed;
> > + Â Â Â int nid;
> > Â Â Â Âstruct scan_control sc = {
> > Â Â Â Â Â Â Â Â.may_writepage = !laptop_mode,
> > Â Â Â Â Â Â Â Â.may_unmap = 1,
> > @@ -2208,10 +2209,16 @@ unsigned long try_to_free_mem_cgroup_pag
> > Â Â Â Â Â Â Â Â.mem_cgroup = mem_cont,
> > Â Â Â Â Â Â Â Â.nodemask = NULL, /* we don't care the placement */
> > Â Â Â Â};
> > + Â Â Â /*
> > + Â Â Â Â* Unlike direct reclaim via allo_pages(), memcg's reclaim
> > + Â Â Â Â* don't take care from where we get free resouce. So, the node where
> > + Â Â Â Â* we need to start scan is not need to be current node.
> > + Â Â Â Â*/
> Sorry, some typos. alloc_pages() instead of alloc_pages(). And "free resource".
>
ok, will fix. Thank you for pointing out.

Thanks,
-Kame

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/