linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
To: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Cc: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-mm@kvack.org" <linux-mm@kvack.org>,
	"balbir@linux.vnet.ibm.com" <balbir@linux.vnet.ibm.com>,
	"kosaki.motohiro@jp.fujitsu.com" <kosaki.motohiro@jp.fujitsu.com>,
	"nishimura@mxp.nes.nec.co.jp" <nishimura@mxp.nes.nec.co.jp>
Subject: [RFC][PATCH 6/8] soft limit victim select
Date: Fri, 27 Mar 2009 14:11:02 +0900	[thread overview]
Message-ID: <20090327141102.a22753e6.kamezawa.hiroyu@jp.fujitsu.com> (raw)
In-Reply-To: <20090327135933.789729cb.kamezawa.hiroyu@jp.fujitsu.com>

From: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>

Soft Limit victim selection/cache logic.

This patch implements victim selection logic and caching method.

victim memcg is selected in following way, assume a zone under shrinking
is specified. Selected memcg will be
  - has the highest priority (high usage)
  - has memory on the zone.

When a memcg is selected, it's rotated and cached per cpu with tickets.

This cache is refreshed when
  - given ticket is exhausetd
  - very long time since last update.
  - the cached memcg doesn't include proper zone.

Even when no proper memcg is not found in victim selection logic,
some tickets are assigned to NULL victim.

As softlimitq, this cache's information has 2 ents for anon and file.

TODO:
  - need to handle cpu hotplug (in other patch)

Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
---
 mm/memcontrol.c |  121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 121 insertions(+)

Index: mmotm-2.6.29-Mar23/mm/memcontrol.c
===================================================================
--- mmotm-2.6.29-Mar23.orig/mm/memcontrol.c
+++ mmotm-2.6.29-Mar23/mm/memcontrol.c
@@ -1055,6 +1055,127 @@ static void mem_cgroup_update_soft_limit
 	return;
 }
 
+/* softlimit victim selection logic */
+
+/* Returns the amount of evictable memory in memcg */
+static int mem_cgroup_usage(struct mem_cgroup *mem, struct zone *zone, int file)
+{
+	struct mem_cgroup_per_zone *mz;
+	int nid = zone->zone_pgdat->node_id;
+	int zid = zone_idx(zone);
+	unsigned long usage = 0;
+
+	mz = mem_cgroup_zoneinfo(mem, nid, zid);
+	if (!file) {
+		usage = MEM_CGROUP_ZSTAT(mz, LRU_ACTIVE_ANON)
+			+ MEM_CGROUP_ZSTAT(mz, LRU_INACTIVE_ANON);
+	} else {
+		usage = MEM_CGROUP_ZSTAT(mz, LRU_ACTIVE_FILE)
+			+ MEM_CGROUP_ZSTAT(mz, LRU_INACTIVE_FILE);
+	}
+	return usage;
+}
+
+struct soft_limit_cache {
+	/* If ticket is 0, refresh and refill the cache.*/
+	unsigned long ticket[2];
+	/* next update time for ticket(jiffies)*/
+	unsigned long next_update;
+	/* An event count per cpu. */
+	unsigned long total_events;
+	/* victim memcg */
+	struct mem_cgroup *mem[2];
+};
+/* In fast-path, 32pages are reclaimed per call. 4*32=128pages as base ticket */
+#define SLCACHE_NULL_TICKET (4)
+#define SLCACHE_UPDATE_JIFFIES (HZ*5) /* 5 minutes is very long. */
+DEFINE_PER_CPU(struct soft_limit_cache, soft_limit_cache);
+
+/* This is called under preempt disabled context....*/
+static void reload_softlimit_victim(struct soft_limit_cache *slc,
+				    struct zone *zone, int file)
+{
+	struct mem_cgroup *mem = NULL;
+	struct mem_cgroup *tmp;
+	struct list_head *queue;
+	int prio, bonus;
+
+	if (slc->mem[file]) {
+		mem_cgroup_put(slc->mem[file]);
+		slc->mem[file] = NULL;
+	}
+	slc->ticket[file] = SLCACHE_NULL_TICKET;
+	slc->next_update = jiffies + SLCACHE_UPDATE_JIFFIES;
+	slc->total_events++;
+
+	/* brief check the queue */
+	for (prio = SLQ_MAXPRIO - 1; prio > 0; prio--) {
+		if (!list_empty(&softlimitq.queue[prio][file]))
+			break;
+	}
+retry:
+	if (prio == 0)
+		return;
+
+	/* check queue in priority order */
+
+	queue = &softlimitq.queue[prio][file];
+	spin_lock(&softlimitq.lock);
+	if (file) {
+		list_for_each_entry(tmp, queue, soft_limit_file) {
+			if (mem_cgroup_usage(tmp, zone, file)) {
+				mem = tmp;
+				break;
+			}
+		}
+		if (mem)
+			list_move_tail(&mem->soft_limit_file, queue);
+	} else {
+		list_for_each_entry(tmp, queue, soft_limit_anon) {
+			if (mem_cgroup_usage(tmp, zone, file)) {
+				mem = tmp;
+				break;
+			}
+		}
+		if (mem)
+			list_move_tail(&mem->soft_limit_anon, queue);
+	}
+	spin_unlock(&softlimitq.lock);
+	/* If not found, goes to next priority */
+	if (!mem) {
+		prio--;
+		goto retry;
+	}
+	if (!css_is_removed(&mem->css)) {
+		slc->mem[file] = mem;
+		bonus = prio * 2;
+		slc->ticket[file] += bonus;
+		mem_cgroup_get(mem);
+	}
+}
+
+static struct mem_cgroup *get_soft_limit_victim(struct zone *zone, int file)
+{
+	struct mem_cgroup *ret;
+	struct soft_limit_cache *slc;
+
+	slc = &get_cpu_var(soft_limit_cache);
+	/*
+	 * If ticket is expired or long time since last ticket or
+	 * there are no evictables in memcg, reload victim.
+	 */
+	ret = slc->mem[file];
+	if ((!slc->ticket[file]-- ||
+	     time_after(jiffies, slc->next_update)) ||
+	    (ret && !mem_cgroup_usage(ret, zone, file))) {
+		reload_softlimit_victim(slc, zone, file);
+		ret = slc->mem[file];
+	}
+	put_cpu_var(soft_limit_cache);
+	return ret;
+}
+
+
 static void softlimitq_init(void)
 {
 	int i;

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

  parent reply	other threads:[~2009-03-27  5:05 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-27  4:59 [RFC][PATCH] memcg soft limit (yet another new design) v1 KAMEZAWA Hiroyuki
2009-03-27  5:01 ` [RFC][PATCH 1/8] soft limit support in res_counter KAMEZAWA Hiroyuki
2009-03-27  5:03 ` [RFC][PATCH 2/8] soft limit framework in memcg KAMEZAWA Hiroyuki
2009-03-27  8:01   ` KAMEZAWA Hiroyuki
2009-03-29 17:22   ` Balbir Singh
2009-03-27  5:05 ` [RFC][PATCH 3/8] trigger for updating soft limit information KAMEZAWA Hiroyuki
2009-03-27  5:06 ` [RFC][PATCH 4/8] memcg soft limit priority array queue KAMEZAWA Hiroyuki
2009-03-29 16:56   ` Balbir Singh
2009-03-30 23:58     ` KAMEZAWA Hiroyuki
2009-03-27  5:09 ` [RFC][PATCH 5/8] memcg soft limit (yet another new design) v1 KAMEZAWA Hiroyuki
2009-03-27  5:14   ` KAMEZAWA Hiroyuki
2009-03-31  8:18   ` KAMEZAWA Hiroyuki
2009-03-27  5:11 ` KAMEZAWA Hiroyuki [this message]
2009-03-27  5:12 ` [RFC][PATCH 7/8] memcg soft limit LRU reorder KAMEZAWA Hiroyuki
2009-03-30  7:52   ` Balbir Singh
2009-03-31  0:00     ` KAMEZAWA Hiroyuki
2009-03-31  6:06       ` Balbir Singh
2009-03-31  6:19         ` KAMEZAWA Hiroyuki
2009-03-27  5:13 ` [RFC][PATCH 8/8] extends soft limit event filter KAMEZAWA Hiroyuki
2009-03-28  8:23 ` [RFC][PATCH] memcg soft limit (yet another new design) v1 Balbir Singh
2009-03-28 16:10   ` KAMEZAWA Hiroyuki
2009-03-28 18:11 ` Balbir Singh
2009-03-28 18:27   ` Balbir Singh
2009-03-30 23:55     ` KAMEZAWA Hiroyuki
2009-03-31  5:00       ` Balbir Singh
2009-03-31  5:05         ` KAMEZAWA Hiroyuki
2009-03-31  5:18           ` KAMEZAWA Hiroyuki
2009-03-31  6:10           ` Balbir Singh
2009-03-31  6:28             ` KAMEZAWA Hiroyuki
2009-03-31  6:49               ` Balbir Singh
2009-03-31  6:56                 ` KAMEZAWA Hiroyuki
2009-03-31  6:58                 ` KAMEZAWA Hiroyuki
2009-03-31  0:06     ` KAMEZAWA Hiroyuki
2009-03-31  5:01       ` Balbir Singh
2009-03-31  5:11         ` KAMEZAWA Hiroyuki
2009-03-31  6:07           ` Balbir Singh
2009-03-30 23:54   ` KAMEZAWA Hiroyuki
2009-03-29 13:01 ` Balbir Singh
2009-03-30 23:57   ` KAMEZAWA Hiroyuki
2009-04-01 14:42 ` Balbir Singh
2009-04-01 15:11   ` KAMEZAWA Hiroyuki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090327141102.a22753e6.kamezawa.hiroyu@jp.fujitsu.com \
    --to=kamezawa.hiroyu@jp.fujitsu.com \
    --cc=balbir@linux.vnet.ibm.com \
    --cc=kosaki.motohiro@jp.fujitsu.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=nishimura@mxp.nes.nec.co.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox