Skip to contentSkip to conversation

cgroup notifications API and memory thresholds

PATCH Superseded Kirill A. Shutemov 8 participants 35 messages Dec 27, 2009 – Jan 4, 2010 10 messages not in the archive

Subsystems (4)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover cgroup notifications API and memory thresholds

This patchset introduces eventfd-based API for notifications in cgroups and implements memory notifications on top of it.

It uses statistics in memory controler to track memory usage.

Output of time(1) on building kernel on tmpfs:

Root cgroup before changes:

	make -j2  506.37 user 60.93s system 193% cpu 4:52.77 total
Non-root cgroup before changes:
	make -j2  507.14 user 62.66s system 193% cpu 4:54.74 total
Root cgroup after changes (0 thresholds):
	make -j2  507.13 user 62.20s system 193% cpu 4:53.55 total
Non-root cgroup after changes (0 thresholds):
	make -j2  507.70 user 64.20s system 193% cpu 4:55.70 total
Root cgroup after changes (1 thresholds, never crossed):
	make -j2  506.97 user 62.20s system 193% cpu 4:53.90 total
Non-root cgroup after changes (1 thresholds, never crossed):
	make -j2  507.55 user 64.08s system 193% cpu 4:55.63 total

Any comments?

v3 -> v4:
- documentation.

v2 -> v3:
- remove [RFC];
- rebased to 2.6.33-rc2;
- fixes based on comments;
- fixed potential race on event removing;
- use RCU-protected arrays to track trasholds.

v1 -> v2:
- use statistics instead of res_counter to track resource usage;
- fix bugs with locking.

v0 -> v1:
- memsw support implemented.

Kirill A. Shutemov (4):
cgroup: implement eventfd-based generic API for notifications
memcg: extract mem_group_usage() from mem_cgroup_read()
memcg: rework usage of stats by soft limit
memcg: implement memory thresholds

 Documentation/cgroups/cgroups.txt |   20 ++
 Documentation/cgroups/memory.txt  |   19 ++-
 include/linux/cgroup.h            |   24 +++
 kernel/cgroup.c                   |  208 ++++++++++++++++++++++-
 mm/memcontrol.c                   |  348 ++++++++++++++++++++++++++++++++++---
 5 files changed, 590 insertions(+), 29 deletions(-)

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

#1 lore raw

PATCH v4 1/4 cgroup: implement eventfd-based generic API for notifications

This patch introduces write-only file "cgroup.event_control" in every cgroup.

To register new notification handler you need:
- create an eventfd;
- open a control file to be monitored. Callbacks register_event() and unregister_event() must be defined for the control file;
- write "<event_fd> <control_fd> <args>" to cgroup.event_control. Interpretation of args is defined by control file implementation;

eventfd will be woken up by control file implementation or when the cgroup is removed.

To unregister notification handler just close eventfd.

If you need notification functionality for a control file you have to implement callbacks register_event() and unregister_event() in the struct cftype.

---
 Documentation/cgroups/cgroups.txt |   20 ++++
 include/linux/cgroup.h            |   24 +++++
 kernel/cgroup.c                   |  208 ++++++++++++++++++++++++++++++++++++-
 3 files changed, 251 insertions(+), 1 deletions(-)
Documentation/cgroups/cgroups.txt +20 -0
index 3bda601..b73c306 100644
@@ -23,6 +23,7 @@ CONTENTS:
   2.1 Basic Usage
   2.2 Attaching processes
   2.3 Mounting hierarchies by name
+  2.4 Notification API
 3. Kernel API
   3.1 Overview
   3.2 Synchronization
@@ -435,6 +436,25 @@ you give a subsystem a name.
 The name of the subsystem appears as part of the hierarchy description
 in /proc/mounts and /proc/<pid>/cgroups.
 
+2.4 Notification API
+--------------------
+
+There is mechanism which allows to get notifications about changing
+status of a cgroup.
+
+To register new notification handler you need:
+ - create a file descriptor for event notification using eventfd(2);
+ - open a control file to be monitored (e.g. memory.usage_in_bytes);
+ - write "<event_fd> <control_fd> <args>" to cgroup.event_control.
+   Interpretation of args is defined by control file implementation;
+
+eventfd will be woken up by control file implementation or when the
+cgroup is removed.
+
+To unregister notification handler just close eventfd.
+
+NOTE: Support of notifications should be implemented for the control
+file. See documentation for the subsystem.
 
 3. Kernel API
 =============
include/linux/cgroup.h +24 -0
index 0008dee..b078409 100644
@@ -220,6 +220,10 @@ struct cgroup {
 
 	/* For RCU-protected deletion */
 	struct rcu_head rcu_head;
+
+	/* List of events which userspace want to recieve */
+	struct list_head event_list;
+	spinlock_t event_list_lock;
 };
 
 /*
@@ -362,6 +366,26 @@ struct cftype {
 	int (*trigger)(struct cgroup *cgrp, unsigned int event);
 
 	int (*release)(struct inode *inode, struct file *file);
+
+	/*
+	 * register_event() callback will be used to add new userspace
+	 * waiter for changes related to the cftype. Implement it if
+	 * you want to provide this functionality. Use eventfd_signal()
+	 * on eventfd to send notification to userspace.
+	 */
+	int (*register_event)(struct cgroup *cgrp, struct cftype *cft,
+			struct eventfd_ctx *eventfd, const char *args);
+	/*
+	 * unregister_event() callback will be called when userspace
+	 * closes the eventfd or on cgroup removing.
+	 * This callback must be implemented, if you want provide
+	 * notification functionality.
+	 *
+	 * Be careful. It can be called after destroy(), so you have
+	 * to keep all nesessary data, until all events are removed.
+	 */
+	int (*unregister_event)(struct cgroup *cgrp, struct cftype *cft,
+			struct eventfd_ctx *eventfd);
 };
 
 struct cgroup_scanner {
kernel/cgroup.c +207 -1

263 lines not on this page · raw message

Signature · 7 lines
1.6.5.7

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#2 lore raw

PATCH v4 2/4 memcg: extract mem_group_usage() from mem_cgroup_read()

Helper to get memory or mem+swap usage of the cgroup.

---
 mm/memcontrol.c |   54 ++++++++++++++++++++++++++++++++----------------------
 1 files changed, 32 insertions(+), 22 deletions(-)
mm/memcontrol.c +32 -22
index 488b644..1d71cb4 100644
@@ -2722,40 +2722,50 @@ mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
 	*val = d.val;
 }
 
+static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
+{
+	u64 idx_val, val;
+
+	if (!mem_cgroup_is_root(mem)) {
+		if (!swap)
+			return res_counter_read_u64(&mem->res, RES_USAGE);
+		else
+			return res_counter_read_u64(&mem->memsw, RES_USAGE);
+	}
+
+	mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_CACHE, &idx_val);
+	val = idx_val;
+	mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_RSS, &idx_val);
+	val += idx_val;
+
+	if (swap) {
+		mem_cgroup_get_recursive_idx_stat(mem,
+				MEM_CGROUP_STAT_SWAPOUT, &idx_val);
+		val += idx_val;
+	}
+
+	return val << PAGE_SHIFT;
+}
+
 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
 {
 	struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
-	u64 idx_val, val;
+	u64 val;
 	int type, name;
 
 	type = MEMFILE_TYPE(cft->private);
 	name = MEMFILE_ATTR(cft->private);
 	switch (type) {
 	case _MEM:
-		if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
-			mem_cgroup_get_recursive_idx_stat(mem,
-				MEM_CGROUP_STAT_CACHE, &idx_val);
-			val = idx_val;
-			mem_cgroup_get_recursive_idx_stat(mem,
-				MEM_CGROUP_STAT_RSS, &idx_val);
-			val += idx_val;
-			val <<= PAGE_SHIFT;
-		} else
+		if (name == RES_USAGE)
+			val = mem_cgroup_usage(mem, false);
+		else
 			val = res_counter_read_u64(&mem->res, name);
 		break;
 	case _MEMSWAP:
-		if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
-			mem_cgroup_get_recursive_idx_stat(mem,
-				MEM_CGROUP_STAT_CACHE, &idx_val);
-			val = idx_val;
-			mem_cgroup_get_recursive_idx_stat(mem,
-				MEM_CGROUP_STAT_RSS, &idx_val);
-			val += idx_val;
-			mem_cgroup_get_recursive_idx_stat(mem,
-				MEM_CGROUP_STAT_SWAPOUT, &idx_val);
-			val += idx_val;
-			val <<= PAGE_SHIFT;
-		} else
+		if (name == RES_USAGE)
+			val = mem_cgroup_usage(mem, true);
+		else
 			val = res_counter_read_u64(&mem->memsw, name);
 		break;
 	default:
Signature · 7 lines
1.6.5.7

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

A message that is not in the archive (sent off-list or not yet received): cover.1261858972.git.kirill-oKw7cIdHH8eLwutG50LtGA@public.gmane.org

#4 lore raw

PATCH v4 1/4 cgroup: implement eventfd-based generic API for notifications

This patch introduces write-only file "cgroup.event_control" in every cgroup.

To register new notification handler you need:
- create an eventfd;
- open a control file to be monitored. Callbacks register_event() and unregister_event() must be defined for the control file;
- write "<event_fd> <control_fd> <args>" to cgroup.event_control. Interpretation of args is defined by control file implementation;

eventfd will be woken up by control file implementation or when the cgroup is removed.

To unregister notification handler just close eventfd.

If you need notification functionality for a control file you have to implement callbacks register_event() and unregister_event() in the struct cftype.

---
 Documentation/cgroups/cgroups.txt |   20 ++++
 include/linux/cgroup.h            |   24 +++++
 kernel/cgroup.c                   |  208 ++++++++++++++++++++++++++++++++++++-
 3 files changed, 251 insertions(+), 1 deletions(-)
Documentation/cgroups/cgroups.txt +20 -0
index 3bda601..b73c306 100644
@@ -23,6 +23,7 @@ CONTENTS:
   2.1 Basic Usage
   2.2 Attaching processes
   2.3 Mounting hierarchies by name
+  2.4 Notification API
 3. Kernel API
   3.1 Overview
   3.2 Synchronization
@@ -435,6 +436,25 @@ you give a subsystem a name.
 The name of the subsystem appears as part of the hierarchy description
 in /proc/mounts and /proc/<pid>/cgroups.
 
+2.4 Notification API
+--------------------
+
+There is mechanism which allows to get notifications about changing
+status of a cgroup.
+
+To register new notification handler you need:
+ - create a file descriptor for event notification using eventfd(2);
+ - open a control file to be monitored (e.g. memory.usage_in_bytes);
+ - write "<event_fd> <control_fd> <args>" to cgroup.event_control.
+   Interpretation of args is defined by control file implementation;
+
+eventfd will be woken up by control file implementation or when the
+cgroup is removed.
+
+To unregister notification handler just close eventfd.
+
+NOTE: Support of notifications should be implemented for the control
+file. See documentation for the subsystem.
 
 3. Kernel API
 =============
include/linux/cgroup.h +24 -0
index 0008dee..b078409 100644
@@ -220,6 +220,10 @@ struct cgroup {
 
 	/* For RCU-protected deletion */
 	struct rcu_head rcu_head;
+
+	/* List of events which userspace want to recieve */
+	struct list_head event_list;
+	spinlock_t event_list_lock;
 };
 
 /*
@@ -362,6 +366,26 @@ struct cftype {
 	int (*trigger)(struct cgroup *cgrp, unsigned int event);
 
 	int (*release)(struct inode *inode, struct file *file);
+
+	/*
+	 * register_event() callback will be used to add new userspace
+	 * waiter for changes related to the cftype. Implement it if
+	 * you want to provide this functionality. Use eventfd_signal()
+	 * on eventfd to send notification to userspace.
+	 */
+	int (*register_event)(struct cgroup *cgrp, struct cftype *cft,
+			struct eventfd_ctx *eventfd, const char *args);
+	/*
+	 * unregister_event() callback will be called when userspace
+	 * closes the eventfd or on cgroup removing.
+	 * This callback must be implemented, if you want provide
+	 * notification functionality.
+	 *
+	 * Be careful. It can be called after destroy(), so you have
+	 * to keep all nesessary data, until all events are removed.
+	 */
+	int (*unregister_event)(struct cgroup *cgrp, struct cftype *cft,
+			struct eventfd_ctx *eventfd);
 };
 
 struct cgroup_scanner {
kernel/cgroup.c +207 -1

263 lines not on this page · raw message

Signature · 1 line
1.6.5.7

A message that is not in the archive (sent off-list or not yet received): 3f29ccc3c93e2defd70fc1c4ca8c133908b70b0b.1261858972.git.kirill-oKw7cIdHH8eLwutG50LtGA@public.gmane.org

#6 lore raw

PATCH v4 2/4 memcg: extract mem_group_usage() from mem_cgroup_read()

Helper to get memory or mem+swap usage of the cgroup.

---
 mm/memcontrol.c |   54 ++++++++++++++++++++++++++++++++----------------------
 1 files changed, 32 insertions(+), 22 deletions(-)
mm/memcontrol.c +32 -22
index 488b644..1d71cb4 100644
@@ -2722,40 +2722,50 @@ mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
 	*val = d.val;
 }
 
+static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
+{
+	u64 idx_val, val;
+
+	if (!mem_cgroup_is_root(mem)) {
+		if (!swap)
+			return res_counter_read_u64(&mem->res, RES_USAGE);
+		else
+			return res_counter_read_u64(&mem->memsw, RES_USAGE);
+	}
+
+	mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_CACHE, &idx_val);
+	val = idx_val;
+	mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_RSS, &idx_val);
+	val += idx_val;
+
+	if (swap) {
+		mem_cgroup_get_recursive_idx_stat(mem,
+				MEM_CGROUP_STAT_SWAPOUT, &idx_val);
+		val += idx_val;
+	}
+
+	return val << PAGE_SHIFT;
+}
+
 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
 {
 	struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
-	u64 idx_val, val;
+	u64 val;
 	int type, name;
 
 	type = MEMFILE_TYPE(cft->private);
 	name = MEMFILE_ATTR(cft->private);
 	switch (type) {
 	case _MEM:
-		if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
-			mem_cgroup_get_recursive_idx_stat(mem,
-				MEM_CGROUP_STAT_CACHE, &idx_val);
-			val = idx_val;
-			mem_cgroup_get_recursive_idx_stat(mem,
-				MEM_CGROUP_STAT_RSS, &idx_val);
-			val += idx_val;
-			val <<= PAGE_SHIFT;
-		} else
+		if (name == RES_USAGE)
+			val = mem_cgroup_usage(mem, false);
+		else
 			val = res_counter_read_u64(&mem->res, name);
 		break;
 	case _MEMSWAP:
-		if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
-			mem_cgroup_get_recursive_idx_stat(mem,
-				MEM_CGROUP_STAT_CACHE, &idx_val);
-			val = idx_val;
-			mem_cgroup_get_recursive_idx_stat(mem,
-				MEM_CGROUP_STAT_RSS, &idx_val);
-			val += idx_val;
-			mem_cgroup_get_recursive_idx_stat(mem,
-				MEM_CGROUP_STAT_SWAPOUT, &idx_val);
-			val += idx_val;
-			val <<= PAGE_SHIFT;
-		} else
+		if (name == RES_USAGE)
+			val = mem_cgroup_usage(mem, true);
+		else
 			val = res_counter_read_u64(&mem->memsw, name);
 		break;
 	default:
Signature · 1 line
1.6.5.7
Reviewed-by
#7 lore raw

Re: [PATCH v4 1/4] cgroup: implement eventfd-based generic API for notifications

In reply to a message not in the archive
Quoted 13 lines · On Sun, 27 Dec 2009 04:08:59 +0200 "Kirill A. Shutemov"…

This patch introduces write-only file "cgroup.event_control" in every cgroup.

To register new notification handler you need:
- create an eventfd;
- open a control file to be monitored. Callbacks register_event() and unregister_event() must be defined for the control file;
- write "<event_fd> <control_fd> <args>" to cgroup.event_control. Interpretation of args is defined by control file implementation;

eventfd will be woken up by control file implementation or when the cgroup is removed.

Quoted 7 lines

To unregister notification handler just close eventfd.

If you need notification functionality for a control file you have to implement callbacks register_event() and unregister_event() in the struct cftype.

A message that is not in the archive (sent off-list or not yet received): 59a7f92356bf1508f06d12c501a7aa4feffb1bbc.1261858972.git.kirill-oKw7cIdHH8eLwutG50LtGA@public.gmane.org

#9 lore raw

PATCH v4 3/4 memcg: rework usage of stats by soft limit

Instead of incrementing counter on each page in/out and comparing it with constant, we set counter to constant, decrement counter on each page in/out and compare it with zero. We want to make comparing as fast as possible. On many RISC systems (probably not only RISC) comparing with zero is more effective than comparing with a constant, since not every constant can be immediate operand for compare instruction.

Also, I've renamed MEM_CGROUP_STAT_EVENTS to MEM_CGROUP_STAT_SOFTLIMIT, since really it's not a generic counter.

---
 mm/memcontrol.c |   19 ++++++++++++++-----
 1 files changed, 14 insertions(+), 5 deletions(-)
mm/memcontrol.c +14 -5
index 1d71cb4..36eb7af 100644
@@ -69,8 +69,9 @@ enum mem_cgroup_stat_index {
 	MEM_CGROUP_STAT_FILE_MAPPED,  /* # of pages charged as file rss */
 	MEM_CGROUP_STAT_PGPGIN_COUNT,	/* # of pages paged in */
 	MEM_CGROUP_STAT_PGPGOUT_COUNT,	/* # of pages paged out */
-	MEM_CGROUP_STAT_EVENTS,	/* sum of pagein + pageout for internal use */
 	MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
+	MEM_CGROUP_STAT_SOFTLIMIT, /* decrements on each page in/out.
+					used by soft limit implementation */
 
 	MEM_CGROUP_STAT_NSTATS,
 };
@@ -90,6 +91,13 @@ __mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat,
 	stat->count[idx] = 0;
 }
 
+static inline void
+__mem_cgroup_stat_set(struct mem_cgroup_stat_cpu *stat,
+		enum mem_cgroup_stat_index idx, s64 val)
+{
+	stat->count[idx] = val;
+}
+
 static inline s64
 __mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat,
 				enum mem_cgroup_stat_index idx)
@@ -380,9 +388,10 @@ static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
 
 	cpu = get_cpu();
 	cpustat = &mem->stat.cpustat[cpu];
-	val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
-	if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
-		__mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
+	val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_SOFTLIMIT);
+	if (unlikely(val < 0)) {
+		__mem_cgroup_stat_set(cpustat, MEM_CGROUP_STAT_SOFTLIMIT,
+				SOFTLIMIT_EVENTS_THRESH);
 		ret = true;
 	}
 	put_cpu();
@@ -515,7 +524,7 @@ static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
 	else
 		__mem_cgroup_stat_add_safe(cpustat,
 				MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
-	__mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
+	__mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SOFTLIMIT, -1);
 	put_cpu();
 }
 
Signature · 1 line
1.6.5.7

A message that is not in the archive (sent off-list or not yet received): c2379f3965225b6d62e64c64f8c0e67fee085d7f.1261858972.git.kirill-oKw7cIdHH8eLwutG50LtGA@public.gmane.org

#12 lore raw

PATCH v4 4/4 memcg: implement memory thresholds

It allows to register multiple memory and memsw thresholds and gets notifications when it crosses.

To register a threshold application need:
- create an eventfd;
- open memory.usage_in_bytes or memory.memsw.usage_in_bytes;
- write string like "<event_fd> <memory.usage_in_bytes> <threshold>" to cgroup.event_control.

Application will be notified through eventfd when memory usage crosses threshold in any direction.

It's applicable for root and non-root cgroup.

It uses stats to track memory usage, simmilar to soft limits. It checks if we need to send event to userspace on every 100 page in/out. I guess it's good compromise between performance and accuracy of thresholds.

---
 Documentation/cgroups/memory.txt |   19 +++-
 mm/memcontrol.c                  |  275 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 293 insertions(+), 1 deletions(-)
Documentation/cgroups/memory.txt +18 -1
index b871f25..195af07 100644
@@ -414,7 +414,24 @@ NOTE1: Soft limits take effect over a long period of time, since they involve
 NOTE2: It is recommended to set the soft limit always below the hard limit,
        otherwise the hard limit will take precedence.
 
-8. TODO
+8. Memory thresholds
+
+Memory controler implements memory thresholds using cgroups notification
+API (see cgroups.txt). It allows to register multiple memory and memsw
+thresholds and gets notifications when it crosses.
+
+To register a threshold application need:
+ - create an eventfd using eventfd(2);
+ - open memory.usage_in_bytes or memory.memsw.usage_in_bytes;
+ - write string like "<event_fd> <memory.usage_in_bytes> <threshold>" to
+   cgroup.event_control.
+
+Application will be notified through eventfd when memory usage crosses
+threshold in any direction.
+
+It's applicable for root and non-root cgroup.
+
+9. TODO
 
 1. Add support for accounting huge pages (as a separate controller)
 2. Make per-cgroup scanner reclaim not-shared pages first
mm/memcontrol.c +275 -0

353 lines not on this page · raw message

Signature · 1 line
1.6.5.7
#13 lore raw

Re: [PATCH v4 3/4] memcg: rework usage of stats by soft limit

In reply to a message not in the archive
Quoted 11 lines · On Sun, 27 Dec 2009 04:09:01 +0200 "Kirill A. Shutemov"…

Instead of incrementing counter on each page in/out and comparing it with constant, we set counter to constant, decrement counter on each page in/out and compare it with zero. We want to make comparing as fast as possible. On many RISC systems (probably not only RISC) comparing with zero is more effective than comparing with a constant, since not every constant can be immediate operand for compare instruction.

Also, I've renamed MEM_CGROUP_STAT_EVENTS to MEM_CGROUP_STAT_SOFTLIMIT, since really it's not a generic counter.

I have to sort out these counter stuff after this. But now,

#14 lore raw

Re: [PATCH v4 3/4] memcg: rework usage of stats by soft limit

In reply to a message not in the archive
Quoted 31 lines · Kirill A. Shutemov

Instead of incrementing counter on each page in/out and comparing it with constant, we set counter to constant, decrement counter on each page in/out and compare it with zero. We want to make comparing as fast as possible. On many RISC systems (probably not only RISC) comparing with zero is more effective than comparing with a constant, since not every constant can be immediate operand for compare instruction.

Also, I've renamed MEM_CGROUP_STAT_EVENTS to MEM_CGROUP_STAT_SOFTLIMIT, since really it's not a generic counter.

---
 mm/memcontrol.c |   19 ++++++++++++++-----
 1 files changed, 14 insertions(+), 5 deletions(-)
mm/memcontrol.c +2 -1
index 1d71cb4..36eb7af 100644
@@ -69,8 +69,9 @@ enum mem_cgroup_stat_index {
 	MEM_CGROUP_STAT_FILE_MAPPED,  /* # of pages charged as file rss */
 	MEM_CGROUP_STAT_PGPGIN_COUNT,	/* # of pages paged in */
 	MEM_CGROUP_STAT_PGPGOUT_COUNT,	/* # of pages paged out */
-	MEM_CGROUP_STAT_EVENTS,	/* sum of pagein + pageout for internal use */
 	MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
+	MEM_CGROUP_STAT_SOFTLIMIT, /* decrements on each page in/out.
+					used by soft limit implementation */
 
 	MEM_CGROUP_STAT_NSTATS,
 };

mm/memcontrol.c __mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat, · line 100

Quoted 10 lines
mm/memcontrol.c +7 -0
@@ -90,6 +91,13 @@ __mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat,
 	stat->count[idx] = 0;
 }
 
+static inline void
+__mem_cgroup_stat_set(struct mem_cgroup_stat_cpu *stat,
+		enum mem_cgroup_stat_index idx, s64 val)
+{
+	stat->count[idx] = val;
+}
+

I think it would be better to name it __mem_cgroup_stat_set_safe. And could you remove the definition of __mem_cgroup_stat_reset ?

Thanks,
Daisuke Nishimura.

Quoted 29 lines
mm/memcontrol.c +5 -4
 static inline s64
 __mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat,
 				enum mem_cgroup_stat_index idx)
@@ -380,9 +388,10 @@ static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
 
 	cpu = get_cpu();
 	cpustat = &mem->stat.cpustat[cpu];
-	val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
-	if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
-		__mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
+	val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_SOFTLIMIT);
+	if (unlikely(val < 0)) {
+		__mem_cgroup_stat_set(cpustat, MEM_CGROUP_STAT_SOFTLIMIT,
+				SOFTLIMIT_EVENTS_THRESH);
 		ret = true;
 	}
 	put_cpu();
@@ -515,7 +524,7 @@ static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
 	else
 		__mem_cgroup_stat_add_safe(cpustat,
 				MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
-	__mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
+	__mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SOFTLIMIT, -1);
 	put_cpu();
 }
 
Signature · 1 line
1.6.5.7
#15 lore raw
In reply to a message not in the archive
Quoted 20 lines · Kirill A. Shutemov

This patchset introduces eventfd-based API for notifications in cgroups and implements memory notifications on top of it.

It uses statistics in memory controler to track memory usage.

Output of time(1) on building kernel on tmpfs:

Root cgroup before changes:

	make -j2  506.37 user 60.93s system 193% cpu 4:52.77 total
Non-root cgroup before changes:
	make -j2  507.14 user 62.66s system 193% cpu 4:54.74 total
Root cgroup after changes (0 thresholds):
	make -j2  507.13 user 62.20s system 193% cpu 4:53.55 total
Non-root cgroup after changes (0 thresholds):
	make -j2  507.70 user 64.20s system 193% cpu 4:55.70 total
Root cgroup after changes (1 thresholds, never crossed):
	make -j2  506.97 user 62.20s system 193% cpu 4:53.90 total
Non-root cgroup after changes (1 thresholds, never crossed):
	make -j2  507.55 user 64.08s system 193% cpu 4:55.63 total
Quoted 1 line

Any comments?

Thanks for adding the documentation, now on to more critical questions

1. Any reasons for not using cgroupstats?
2. Is there a user space test application to test this code. IIUC, I need to write a program that uses eventfd(2) and then passes the eventfd descriptor and thresold to cgroup.*event* file and then the program will get notified when the threshold is reached?

Signature · 1 line
	Balbir
#16 lore raw
In reply to a message not in the archive
Quoted 20 lines · On Sun, 27 Dec 2009 04:08:58 +0200 "Kirill A. Shutemov"…

This patchset introduces eventfd-based API for notifications in cgroups and implements memory notifications on top of it.

It uses statistics in memory controler to track memory usage.

Output of time(1) on building kernel on tmpfs:

Root cgroup before changes:

	make -j2  506.37 user 60.93s system 193% cpu 4:52.77 total
Non-root cgroup before changes:
	make -j2  507.14 user 62.66s system 193% cpu 4:54.74 total
Root cgroup after changes (0 thresholds):
	make -j2  507.13 user 62.20s system 193% cpu 4:53.55 total
Non-root cgroup after changes (0 thresholds):
	make -j2  507.70 user 64.20s system 193% cpu 4:55.70 total
Root cgroup after changes (1 thresholds, never crossed):
	make -j2  506.97 user 62.20s system 193% cpu 4:53.90 total
Non-root cgroup after changes (1 thresholds, never crossed):
	make -j2  507.55 user 64.08s system 193% cpu 4:55.63 total
Quoted 2 lines

Any comments?

Hmm, 2 secs of overhead is added by this.
(larger than expected.)

But optimization/claun up after merging is okay to me because the function itself is attractive.

Thanks,
-Kame

Quoted 35 lines

v3 -> v4:
- documentation.

v2 -> v3:
- remove [RFC];
- rebased to 2.6.33-rc2;
- fixes based on comments;
- fixed potential race on event removing;
- use RCU-protected arrays to track trasholds.

v1 -> v2:
- use statistics instead of res_counter to track resource usage;
- fix bugs with locking.

v0 -> v1:
- memsw support implemented.

Kirill A. Shutemov (4):
cgroup: implement eventfd-based generic API for notifications
memcg: extract mem_group_usage() from mem_cgroup_read()
memcg: rework usage of stats by soft limit
memcg: implement memory thresholds

 Documentation/cgroups/cgroups.txt |   20 ++
 Documentation/cgroups/memory.txt  |   19 ++-
 include/linux/cgroup.h            |   24 +++
 kernel/cgroup.c                   |  208 ++++++++++++++++++++++-
 mm/memcontrol.c                   |  348 ++++++++++++++++++++++++++++++++++---
 5 files changed, 590 insertions(+), 29 deletions(-)

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

#17 lore raw

PATCH v4 3/4 memcg: rework usage of stats by soft limit

In reply to Kirill A. Shutemov

Instead of incrementing counter on each page in/out and comparing it with constant, we set counter to constant, decrement counter on each page in/out and compare it with zero. We want to make comparing as fast as possible. On many RISC systems (probably not only RISC) comparing with zero is more effective than comparing with a constant, since not every constant can be immediate operand for compare instruction.

Also, I've renamed MEM_CGROUP_STAT_EVENTS to MEM_CGROUP_STAT_SOFTLIMIT, since really it's not a generic counter.

---
 mm/memcontrol.c |   19 ++++++++++++++-----
 1 files changed, 14 insertions(+), 5 deletions(-)
mm/memcontrol.c +14 -5
index 1d71cb4..36eb7af 100644
@@ -69,8 +69,9 @@ enum mem_cgroup_stat_index {
 	MEM_CGROUP_STAT_FILE_MAPPED,  /* # of pages charged as file rss */
 	MEM_CGROUP_STAT_PGPGIN_COUNT,	/* # of pages paged in */
 	MEM_CGROUP_STAT_PGPGOUT_COUNT,	/* # of pages paged out */
-	MEM_CGROUP_STAT_EVENTS,	/* sum of pagein + pageout for internal use */
 	MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
+	MEM_CGROUP_STAT_SOFTLIMIT, /* decrements on each page in/out.
+					used by soft limit implementation */
 
 	MEM_CGROUP_STAT_NSTATS,
 };
@@ -90,6 +91,13 @@ __mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat,
 	stat->count[idx] = 0;
 }
 
+static inline void
+__mem_cgroup_stat_set(struct mem_cgroup_stat_cpu *stat,
+		enum mem_cgroup_stat_index idx, s64 val)
+{
+	stat->count[idx] = val;
+}
+
 static inline s64
 __mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat,
 				enum mem_cgroup_stat_index idx)
@@ -380,9 +388,10 @@ static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
 
 	cpu = get_cpu();
 	cpustat = &mem->stat.cpustat[cpu];
-	val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
-	if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
-		__mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
+	val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_SOFTLIMIT);
+	if (unlikely(val < 0)) {
+		__mem_cgroup_stat_set(cpustat, MEM_CGROUP_STAT_SOFTLIMIT,
+				SOFTLIMIT_EVENTS_THRESH);
 		ret = true;
 	}
 	put_cpu();
@@ -515,7 +524,7 @@ static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
 	else
 		__mem_cgroup_stat_add_safe(cpustat,
 				MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
-	__mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
+	__mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SOFTLIMIT, -1);
 	put_cpu();
 }
 
Signature · 7 lines
1.6.5.7

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#18 lore raw

PATCH v4 4/4 memcg: implement memory thresholds

It allows to register multiple memory and memsw thresholds and gets notifications when it crosses.

To register a threshold application need:
- create an eventfd;
- open memory.usage_in_bytes or memory.memsw.usage_in_bytes;
- write string like "<event_fd> <memory.usage_in_bytes> <threshold>" to cgroup.event_control.

Application will be notified through eventfd when memory usage crosses threshold in any direction.

It's applicable for root and non-root cgroup.

It uses stats to track memory usage, simmilar to soft limits. It checks if we need to send event to userspace on every 100 page in/out. I guess it's good compromise between performance and accuracy of thresholds.

---
 Documentation/cgroups/memory.txt |   19 +++-
 mm/memcontrol.c                  |  275 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 293 insertions(+), 1 deletions(-)
Documentation/cgroups/memory.txt +18 -1
index b871f25..195af07 100644
@@ -414,7 +414,24 @@ NOTE1: Soft limits take effect over a long period of time, since they involve
 NOTE2: It is recommended to set the soft limit always below the hard limit,
        otherwise the hard limit will take precedence.
 
-8. TODO
+8. Memory thresholds
+
+Memory controler implements memory thresholds using cgroups notification
+API (see cgroups.txt). It allows to register multiple memory and memsw
+thresholds and gets notifications when it crosses.
+
+To register a threshold application need:
+ - create an eventfd using eventfd(2);
+ - open memory.usage_in_bytes or memory.memsw.usage_in_bytes;
+ - write string like "<event_fd> <memory.usage_in_bytes> <threshold>" to
+   cgroup.event_control.
+
+Application will be notified through eventfd when memory usage crosses
+threshold in any direction.
+
+It's applicable for root and non-root cgroup.
+
+9. TODO
 
 1. Add support for accounting huge pages (as a separate controller)
 2. Make per-cgroup scanner reclaim not-shared pages first
mm/memcontrol.c +275 -0

353 lines not on this page · raw message

Signature · 7 lines
1.6.5.7

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

A message that is not in the archive (sent off-list or not yet received): 7a4e1d758b98ca633a0be06e883644ad8813c077.1261858972.git.kirill-oKw7cIdHH8eLwutG50LtGA@public.gmane.org

#20 lore raw

Re: [PATCH v4 4/4] memcg: implement memory thresholds

Quoted 100 lines · On Sun, 27 Dec 2009 04:09:02 +0200 "Kirill A. Shutemov"…

It allows to register multiple memory and memsw thresholds and gets notifications when it crosses.

To register a threshold application need:
- create an eventfd;
- open memory.usage_in_bytes or memory.memsw.usage_in_bytes;
- write string like "<event_fd> <memory.usage_in_bytes> <threshold>" to cgroup.event_control.

Application will be notified through eventfd when memory usage crosses threshold in any direction.

It's applicable for root and non-root cgroup.

It uses stats to track memory usage, simmilar to soft limits. It checks if we need to send event to userspace on every 100 page in/out. I guess it's good compromise between performance and accuracy of thresholds.

---
 Documentation/cgroups/memory.txt |   19 +++-
 mm/memcontrol.c                  |  275 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 293 insertions(+), 1 deletions(-)
Documentation/cgroups/memory.txt +18 -1
index b871f25..195af07 100644
@@ -414,7 +414,24 @@ NOTE1: Soft limits take effect over a long period of time, since they involve
 NOTE2: It is recommended to set the soft limit always below the hard limit,
        otherwise the hard limit will take precedence.
 
-8. TODO
+8. Memory thresholds
+
+Memory controler implements memory thresholds using cgroups notification
+API (see cgroups.txt). It allows to register multiple memory and memsw
+thresholds and gets notifications when it crosses.
+
+To register a threshold application need:
+ - create an eventfd using eventfd(2);
+ - open memory.usage_in_bytes or memory.memsw.usage_in_bytes;
+ - write string like "<event_fd> <memory.usage_in_bytes> <threshold>" to
+   cgroup.event_control.
+
+Application will be notified through eventfd when memory usage crosses
+threshold in any direction.
+
+It's applicable for root and non-root cgroup.
+
+9. TODO
 
 1. Add support for accounting huge pages (as a separate controller)
 2. Make per-cgroup scanner reclaim not-shared pages first
mm/memcontrol.c +10 -0
index 36eb7af..3a0a6a1 100644
@@ -6,6 +6,10 @@
  * Copyright 2007 OpenVZ SWsoft Inc
  * Author: Pavel Emelianov <[email protected]>
  *
+ * Memory thresholds
+ * Copyright (C) 2009 Nokia Corporation
+ * Author: Kirill A. Shutemov
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
@@ -39,6 +43,8 @@
 #include <linux/mm_inline.h>
 #include <linux/page_cgroup.h>
 #include <linux/cpu.h>
+#include <linux/eventfd.h>
+#include <linux/sort.h>
 #include "internal.h"
 
 #include <asm/uaccess.h>
@@ -56,6 +62,7 @@ static int really_do_swap_account __initdata = 1; /* for remember boot option*/
 #endif
 
 #define SOFTLIMIT_EVENTS_THRESH (1000)
+#define THRESHOLDS_EVENTS_THRESH (100)
 
 /*
  * Statistics for memory cgroup.
@@ -72,6 +79,8 @@ enum mem_cgroup_stat_index {
 	MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
 	MEM_CGROUP_STAT_SOFTLIMIT, /* decrements on each page in/out.
 					used by soft limit implementation */
+	MEM_CGROUP_STAT_THRESHOLDS, /* decrements on each page in/out.
+					used by threshold implementation */
 
 	MEM_CGROUP_STAT_NSTATS,
 };
@@ -182,6 +191,20 @@ struct mem_cgroup_tree {
 
 static struct mem_cgroup_tree soft_limit_tree __read_mostly;
 
+struct mem_cgroup_threshold {

mm/memcontrol.c struct mem_cgroup_tree { · line 204

Quoted 10 lines
mm/memcontrol.c +10 -0
+	struct eventfd_ctx *eventfd;
+	u64 threshold;
+};
+
+struct mem_cgroup_threshold_ary {
+	unsigned int size;
+	atomic_t cur;
+	struct mem_cgroup_threshold entries[0];
+};
+

Why "array" is a choice here ? IOW, why not list ? How many waiters are expected as usual workload ?

Quoted 188 lines
mm/memcontrol.c +153 -0
+static bool mem_cgroup_threshold_check(struct mem_cgroup* mem);
+static void mem_cgroup_threshold(struct mem_cgroup* mem);
+
 /*
  * The memory controller data structure. The memory controller controls both
  * page cache and RSS per cgroup. We would eventually like to provide
@@ -233,6 +256,15 @@ struct mem_cgroup {
 	/* set when res.limit == memsw.limit */
 	bool		memsw_is_minimum;
 
+	/* protect arrays of thresholds */
+	struct mutex thresholds_lock;
+
+	/* thresholds for memory usage. RCU-protected */
+	struct mem_cgroup_threshold_ary *thresholds;
+
+	/* thresholds for mem+swap usage. RCU-protected */
+	struct mem_cgroup_threshold_ary *memsw_thresholds;
+
 	/*
 	 * statistics. This must be placed at the end of memcg.
 	 */
@@ -525,6 +557,8 @@ static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
 		__mem_cgroup_stat_add_safe(cpustat,
 				MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
 	__mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SOFTLIMIT, -1);
+	__mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_THRESHOLDS, -1);
+
 	put_cpu();
 }
 
@@ -1510,6 +1544,8 @@ charged:
 	if (mem_cgroup_soft_limit_check(mem))
 		mem_cgroup_update_tree(mem, page);
 done:
+	if (mem_cgroup_threshold_check(mem))
+		mem_cgroup_threshold(mem);
 	return 0;
 nomem:
 	css_put(&mem->css);
@@ -2075,6 +2111,8 @@ __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
 
 	if (mem_cgroup_soft_limit_check(mem))
 		mem_cgroup_update_tree(mem, page);
+	if (mem_cgroup_threshold_check(mem))
+		mem_cgroup_threshold(mem);
 	/* at swapout, this memcg will be accessed to record to swap */
 	if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
 		css_put(&mem->css);
@@ -3071,12 +3109,246 @@ static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
 	return 0;
 }
 
+static bool mem_cgroup_threshold_check(struct mem_cgroup *mem)
+{
+	bool ret = false;
+	int cpu;
+	s64 val;
+	struct mem_cgroup_stat_cpu *cpustat;
+
+	cpu = get_cpu();
+	cpustat = &mem->stat.cpustat[cpu];
+	val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_THRESHOLDS);
+	if (unlikely(val < 0)) {
+		__mem_cgroup_stat_set(cpustat, MEM_CGROUP_STAT_THRESHOLDS,
+				THRESHOLDS_EVENTS_THRESH);
+		ret = true;
+	}
+	put_cpu();
+	return ret;
+}
+
+static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
+{
+	struct mem_cgroup_threshold_ary *thresholds;
+	u64 usage = mem_cgroup_usage(memcg, swap);
+	int i, cur;
+
+	rcu_read_lock();
+	if (!swap) {
+		thresholds = rcu_dereference(memcg->thresholds);
+	} else {
+		thresholds = rcu_dereference(memcg->memsw_thresholds);
+	}
+
+	if (!thresholds)
+		goto unlock;
+
+	cur = atomic_read(&thresholds->cur);
+
+	/* Check if a threshold crossed in any direction */
+
+	for(i = cur; i >= 0 &&
+		unlikely(thresholds->entries[i].threshold > usage); i--) {
+		atomic_dec(&thresholds->cur);
+		eventfd_signal(thresholds->entries[i].eventfd, 1);
+	}
+
+	for(i = cur + 1; i < thresholds->size &&
+		unlikely(thresholds->entries[i].threshold <= usage); i++) {
+		atomic_inc(&thresholds->cur);
+		eventfd_signal(thresholds->entries[i].eventfd, 1);
+	}
+unlock:
+	rcu_read_unlock();
+}
+
+static void mem_cgroup_threshold(struct mem_cgroup *memcg)
+{
+	__mem_cgroup_threshold(memcg, false);
+	if (do_swap_account)
+		__mem_cgroup_threshold(memcg, true);
+}
+
+static int compare_thresholds(const void *a, const void *b)
+{
+	const struct mem_cgroup_threshold *_a = a;
+	const struct mem_cgroup_threshold *_b = b;
+
+	return _a->threshold - _b->threshold;
+}
+
+static int mem_cgroup_register_event(struct cgroup *cgrp, struct cftype *cft,
+		struct eventfd_ctx *eventfd, const char *args)
+{
+	struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
+	struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
+	int type = MEMFILE_TYPE(cft->private);
+	u64 threshold, usage;
+	int size;
+	int i, ret;
+
+	ret = res_counter_memparse_write_strategy(args, &threshold);
+	if (ret)
+		return ret;
+
+	mutex_lock(&memcg->thresholds_lock);
+	if (type == _MEM)
+		thresholds = memcg->thresholds;
+	else if (type == _MEMSWAP)
+		thresholds = memcg->memsw_thresholds;
+	else
+		BUG();
+
+	usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
+
+	/* Check if a threshold crossed before adding a new one */
+	if (thresholds)
+		__mem_cgroup_threshold(memcg, type == _MEMSWAP);
+
+	if (thresholds)
+		size = thresholds->size + 1;
+	else
+		size = 1;
+
+	/* Allocate memory for new array of thresholds */
+	thresholds_new = kmalloc(sizeof(*thresholds_new) +
+			size * sizeof(struct mem_cgroup_threshold),
+			GFP_KERNEL);
+	if (!thresholds_new) {
+		ret = -ENOMEM;
+		goto unlock;
+	}
+	thresholds_new->size = size;
+
+	/* Copy thresholds (if any) to new array */
+	if (thresholds)
+		memcpy(thresholds_new->entries, thresholds->entries,
+				thresholds->size *
+				sizeof(struct mem_cgroup_threshold));
+	/* Add new threshold */
+	thresholds_new->entries[size - 1].eventfd = eventfd;
+	thresholds_new->entries[size - 1].threshold = threshold;
+
+	/* Sort thresholds. Registering of new threshold isn't time-critical */
+	sort(thresholds_new->entries, size,
+			sizeof(struct mem_cgroup_threshold),
+			compare_thresholds, NULL);
+
+	/* Find current threshold */
+	atomic_set(&thresholds_new->cur, -1);
+	for(i = 0; i < size; i++) {
+		if (thresholds_new->entries[i].threshold < usage)
+			atomic_inc(&thresholds_new->cur);
+	}
+
+	/*
+	 * We need to increment refcnt to be sure that all thresholds

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3256

Quoted 10 lines
mm/memcontrol.c +10 -0
+	 * will be unregistered before calling __mem_cgroup_free()
+	 */
+	mem_cgroup_get(memcg);
+
+	if (type == _MEM)
+		rcu_assign_pointer(memcg->thresholds, thresholds_new);
+	else
+		rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
+
+	synchronize_rcu();

Could you add explanation when you use synchronize_rcu() ?

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3257

Quoted 1 line
mm/memcontrol.c +1 -0
+	kfree(thresholds);

Can't this be freed by RCU instead of synchronize_rcu() ?

Quoted 56 lines
mm/memcontrol.c +56 -0
+unlock:
+	mutex_unlock(&memcg->thresholds_lock);
+
+	return ret;
+}
+
+static int mem_cgroup_unregister_event(struct cgroup *cgrp, struct cftype *cft,
+		struct eventfd_ctx *eventfd)
+{
+	struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
+	struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
+	int type = MEMFILE_TYPE(cft->private);
+	u64 usage;
+	int size = 0;
+	int i, j, ret;
+
+	mutex_lock(&memcg->thresholds_lock);
+	if (type == _MEM)
+		thresholds = memcg->thresholds;
+	else if (type == _MEMSWAP)
+		thresholds = memcg->memsw_thresholds;
+	else
+		BUG();
+
+	/*
+	 * Something went wrong if we trying to unregister a threshold
+	 * if we don't have thresholds
+	 */
+	BUG_ON(!thresholds);
+
+	usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
+
+	/* Check if a threshold crossed before removing */
+	__mem_cgroup_threshold(memcg, type == _MEMSWAP);
+
+	/* Calculate new number of threshold */
+	for(i = 0; i < thresholds->size; i++) {
+		if (thresholds->entries[i].eventfd != eventfd)
+			size++;
+	}
+
+	/* Set thresholds array to NULL if we don't have thresholds */
+	if (!size) {
+		thresholds_new = NULL;
+		goto assign;
+	}
+
+	/* Allocate memory for new array of thresholds */
+	thresholds_new = kmalloc(sizeof(*thresholds_new) +
+			size * sizeof(struct mem_cgroup_threshold),
+			GFP_KERNEL);
+	if (!thresholds_new) {
+		ret = -ENOMEM;
+		goto unlock;
+	}
+	thresholds_new->size = size;

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3323

Quoted 10 lines
mm/memcontrol.c +10 -0
+
+	/* Copy thresholds and find current threshold */
+	atomic_set(&thresholds_new->cur, -1);
+	for(i = 0, j = 0; i < thresholds->size; i++) {
+		if (thresholds->entries[i].eventfd == eventfd)
+			continue;
+
+		thresholds_new->entries[j] = thresholds->entries[i];
+		if (thresholds_new->entries[j].threshold < usage)
+			atomic_inc(&thresholds_new->cur);

It's better to do atomic set after loop.

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3325

Quoted 2 lines
mm/memcontrol.c +2 -0
+		j++;
+	}

Hmm..is this "copy array" usual coding style for handling eventfd ?

Quoted 54 lines
mm/memcontrol.c +23 -0
+
+assign:
+	if (type == _MEM)
+		rcu_assign_pointer(memcg->thresholds, thresholds_new);
+	else
+		rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
+
+	synchronize_rcu();
+
+	for(i = 0; i < thresholds->size - size; i++)
+		mem_cgroup_put(memcg);
+
+	kfree(thresholds);
+unlock:
+	mutex_unlock(&memcg->thresholds_lock);
+
+	return ret;
+}
 
 static struct cftype mem_cgroup_files[] = {
 	{
 		.name = "usage_in_bytes",
 		.private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
 		.read_u64 = mem_cgroup_read,
+		.register_event = mem_cgroup_register_event,
+		.unregister_event = mem_cgroup_unregister_event,
 	},
 	{
 		.name = "max_usage_in_bytes",
@@ -3128,6 +3400,8 @@ static struct cftype memsw_cgroup_files[] = {
 		.name = "memsw.usage_in_bytes",
 		.private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
 		.read_u64 = mem_cgroup_read,
+		.register_event = mem_cgroup_register_event,
+		.unregister_event = mem_cgroup_unregister_event,
 	},
 	{
 		.name = "memsw.max_usage_in_bytes",
@@ -3367,6 +3641,7 @@ mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
 	if (parent)
 		mem->swappiness = get_swappiness(parent);
 	atomic_set(&mem->refcnt, 1);
+	mutex_init(&mem->thresholds_lock);
 	return &mem->css;
 free_out:
 	__mem_cgroup_free(mem);
Signature · 7 lines
1.6.5.7

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
#21 lore raw

Re: [PATCH v4 4/4] memcg: implement memory thresholds

In reply to a message not in the archive

mm/memcontrol.c

Quoted 58 lines · Kirill A. Shutemov

It allows to register multiple memory and memsw thresholds and gets notifications when it crosses.

To register a threshold application need:
- create an eventfd;
- open memory.usage_in_bytes or memory.memsw.usage_in_bytes;
- write string like "<event_fd> <memory.usage_in_bytes> <threshold>" to cgroup.event_control.

Application will be notified through eventfd when memory usage crosses threshold in any direction.

It's applicable for root and non-root cgroup.

It uses stats to track memory usage, simmilar to soft limits. It checks if we need to send event to userspace on every 100 page in/out. I guess it's good compromise between performance and accuracy of thresholds.

---
 Documentation/cgroups/memory.txt |   19 +++-
 mm/memcontrol.c                  |  275 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 293 insertions(+), 1 deletions(-)
Documentation/cgroups/memory.txt +18 -1
index b871f25..195af07 100644
@@ -414,7 +414,24 @@ NOTE1: Soft limits take effect over a long period of time, since they involve
 NOTE2: It is recommended to set the soft limit always below the hard limit,
        otherwise the hard limit will take precedence.
 
-8. TODO
+8. Memory thresholds
+
+Memory controler implements memory thresholds using cgroups notification
+API (see cgroups.txt). It allows to register multiple memory and memsw
+thresholds and gets notifications when it crosses.
+
+To register a threshold application need:
+ - create an eventfd using eventfd(2);
+ - open memory.usage_in_bytes or memory.memsw.usage_in_bytes;
+ - write string like "<event_fd> <memory.usage_in_bytes> <threshold>" to
+   cgroup.event_control.
+
+Application will be notified through eventfd when memory usage crosses
+threshold in any direction.
+
+It's applicable for root and non-root cgroup.
+
+9. TODO
 
 1. Add support for accounting huge pages (as a separate controller)
 2. Make per-cgroup scanner reclaim not-shared pages first
mm/memcontrol.c +0 -0
index 36eb7af..3a0a6a1 100644

It would be a nitpick, but my patch(http://marc.info/?l=linux-mm-commits&m=126152804420992&w=2) has already modified here.

I think it might be better for you to apply my patches by hand or wait for next mmotm to be released to avoid bothering Andrew.
(There is enough time left till the next merge window :))

(snip)

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3136

Quoted 6 lines
mm/memcontrol.c +6 -0
+static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
+{
+	struct mem_cgroup_threshold_ary *thresholds;
+	u64 usage = mem_cgroup_usage(memcg, swap);
+	int i, cur;
+

I think calling mem_cgroup_usage() after checking "if(!thresholds)" decreases the overhead a little when we don't set any thresholds. I've confirmed that the change makes the assembler output different.

Quoted 19 lines
mm/memcontrol.c +19 -0
+	rcu_read_lock();
+	if (!swap) {
+		thresholds = rcu_dereference(memcg->thresholds);
+	} else {
+		thresholds = rcu_dereference(memcg->memsw_thresholds);
+	}
+
+	if (!thresholds)
+		goto unlock;
+
+	cur = atomic_read(&thresholds->cur);
+
+	/* Check if a threshold crossed in any direction */
+
+	for(i = cur; i >= 0 &&
+		unlikely(thresholds->entries[i].threshold > usage); i--) {
+		atomic_dec(&thresholds->cur);
+		eventfd_signal(thresholds->entries[i].eventfd, 1);
+	}

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3165

Quoted 10 lines
mm/memcontrol.c +10 -0
+
+	for(i = cur + 1; i < thresholds->size &&
+		unlikely(thresholds->entries[i].threshold <= usage); i++) {
+		atomic_inc(&thresholds->cur);
+		eventfd_signal(thresholds->entries[i].eventfd, 1);
+	}
+unlock:
+	rcu_read_unlock();
+}
+

Thanks,
Daisuke Nishimura.

#22 lore raw

Re: [PATCH v4 4/4] memcg: implement memory thresholds

In reply to Kirill A. Shutemov
Quoted 100 lines · On Sun, 27 Dec 2009 04:09:02 +0200 "Kirill A. Shutemov" <[email protected]>…

It allows to register multiple memory and memsw thresholds and gets notifications when it crosses.

To register a threshold application need:
- create an eventfd;
- open memory.usage_in_bytes or memory.memsw.usage_in_bytes;
- write string like "<event_fd> <memory.usage_in_bytes> <threshold>" to cgroup.event_control.

Application will be notified through eventfd when memory usage crosses threshold in any direction.

It's applicable for root and non-root cgroup.

It uses stats to track memory usage, simmilar to soft limits. It checks if we need to send event to userspace on every 100 page in/out. I guess it's good compromise between performance and accuracy of thresholds.

---
 Documentation/cgroups/memory.txt |   19 +++-
 mm/memcontrol.c                  |  275 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 293 insertions(+), 1 deletions(-)
Documentation/cgroups/memory.txt +18 -1
index b871f25..195af07 100644
@@ -414,7 +414,24 @@ NOTE1: Soft limits take effect over a long period of time, since they involve
 NOTE2: It is recommended to set the soft limit always below the hard limit,
        otherwise the hard limit will take precedence.
 
-8. TODO
+8. Memory thresholds
+
+Memory controler implements memory thresholds using cgroups notification
+API (see cgroups.txt). It allows to register multiple memory and memsw
+thresholds and gets notifications when it crosses.
+
+To register a threshold application need:
+ - create an eventfd using eventfd(2);
+ - open memory.usage_in_bytes or memory.memsw.usage_in_bytes;
+ - write string like "<event_fd> <memory.usage_in_bytes> <threshold>" to
+   cgroup.event_control.
+
+Application will be notified through eventfd when memory usage crosses
+threshold in any direction.
+
+It's applicable for root and non-root cgroup.
+
+9. TODO
 
 1. Add support for accounting huge pages (as a separate controller)
 2. Make per-cgroup scanner reclaim not-shared pages first
mm/memcontrol.c +10 -0
index 36eb7af..3a0a6a1 100644
@@ -6,6 +6,10 @@
  * Copyright 2007 OpenVZ SWsoft Inc
  * Author: Pavel Emelianov <[email protected]>
  *
+ * Memory thresholds
+ * Copyright (C) 2009 Nokia Corporation
+ * Author: Kirill A. Shutemov
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
@@ -39,6 +43,8 @@
 #include <linux/mm_inline.h>
 #include <linux/page_cgroup.h>
 #include <linux/cpu.h>
+#include <linux/eventfd.h>
+#include <linux/sort.h>
 #include "internal.h"
 
 #include <asm/uaccess.h>
@@ -56,6 +62,7 @@ static int really_do_swap_account __initdata = 1; /* for remember boot option*/
 #endif
 
 #define SOFTLIMIT_EVENTS_THRESH (1000)
+#define THRESHOLDS_EVENTS_THRESH (100)
 
 /*
  * Statistics for memory cgroup.
@@ -72,6 +79,8 @@ enum mem_cgroup_stat_index {
 	MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
 	MEM_CGROUP_STAT_SOFTLIMIT, /* decrements on each page in/out.
 					used by soft limit implementation */
+	MEM_CGROUP_STAT_THRESHOLDS, /* decrements on each page in/out.
+					used by threshold implementation */
 
 	MEM_CGROUP_STAT_NSTATS,
 };
@@ -182,6 +191,20 @@ struct mem_cgroup_tree {
 
 static struct mem_cgroup_tree soft_limit_tree __read_mostly;
 
+struct mem_cgroup_threshold {

mm/memcontrol.c struct mem_cgroup_tree { · line 204

Quoted 10 lines
mm/memcontrol.c +10 -0
+	struct eventfd_ctx *eventfd;
+	u64 threshold;
+};
+
+struct mem_cgroup_threshold_ary {
+	unsigned int size;
+	atomic_t cur;
+	struct mem_cgroup_threshold entries[0];
+};
+

Why "array" is a choice here ? IOW, why not list ? How many waiters are expected as usual workload ?

Quoted 188 lines
mm/memcontrol.c +153 -0
+static bool mem_cgroup_threshold_check(struct mem_cgroup* mem);
+static void mem_cgroup_threshold(struct mem_cgroup* mem);
+
 /*
  * The memory controller data structure. The memory controller controls both
  * page cache and RSS per cgroup. We would eventually like to provide
@@ -233,6 +256,15 @@ struct mem_cgroup {
 	/* set when res.limit == memsw.limit */
 	bool		memsw_is_minimum;
 
+	/* protect arrays of thresholds */
+	struct mutex thresholds_lock;
+
+	/* thresholds for memory usage. RCU-protected */
+	struct mem_cgroup_threshold_ary *thresholds;
+
+	/* thresholds for mem+swap usage. RCU-protected */
+	struct mem_cgroup_threshold_ary *memsw_thresholds;
+
 	/*
 	 * statistics. This must be placed at the end of memcg.
 	 */
@@ -525,6 +557,8 @@ static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
 		__mem_cgroup_stat_add_safe(cpustat,
 				MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
 	__mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SOFTLIMIT, -1);
+	__mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_THRESHOLDS, -1);
+
 	put_cpu();
 }
 
@@ -1510,6 +1544,8 @@ charged:
 	if (mem_cgroup_soft_limit_check(mem))
 		mem_cgroup_update_tree(mem, page);
 done:
+	if (mem_cgroup_threshold_check(mem))
+		mem_cgroup_threshold(mem);
 	return 0;
 nomem:
 	css_put(&mem->css);
@@ -2075,6 +2111,8 @@ __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
 
 	if (mem_cgroup_soft_limit_check(mem))
 		mem_cgroup_update_tree(mem, page);
+	if (mem_cgroup_threshold_check(mem))
+		mem_cgroup_threshold(mem);
 	/* at swapout, this memcg will be accessed to record to swap */
 	if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
 		css_put(&mem->css);
@@ -3071,12 +3109,246 @@ static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
 	return 0;
 }
 
+static bool mem_cgroup_threshold_check(struct mem_cgroup *mem)
+{
+	bool ret = false;
+	int cpu;
+	s64 val;
+	struct mem_cgroup_stat_cpu *cpustat;
+
+	cpu = get_cpu();
+	cpustat = &mem->stat.cpustat[cpu];
+	val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_THRESHOLDS);
+	if (unlikely(val < 0)) {
+		__mem_cgroup_stat_set(cpustat, MEM_CGROUP_STAT_THRESHOLDS,
+				THRESHOLDS_EVENTS_THRESH);
+		ret = true;
+	}
+	put_cpu();
+	return ret;
+}
+
+static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
+{
+	struct mem_cgroup_threshold_ary *thresholds;
+	u64 usage = mem_cgroup_usage(memcg, swap);
+	int i, cur;
+
+	rcu_read_lock();
+	if (!swap) {
+		thresholds = rcu_dereference(memcg->thresholds);
+	} else {
+		thresholds = rcu_dereference(memcg->memsw_thresholds);
+	}
+
+	if (!thresholds)
+		goto unlock;
+
+	cur = atomic_read(&thresholds->cur);
+
+	/* Check if a threshold crossed in any direction */
+
+	for(i = cur; i >= 0 &&
+		unlikely(thresholds->entries[i].threshold > usage); i--) {
+		atomic_dec(&thresholds->cur);
+		eventfd_signal(thresholds->entries[i].eventfd, 1);
+	}
+
+	for(i = cur + 1; i < thresholds->size &&
+		unlikely(thresholds->entries[i].threshold <= usage); i++) {
+		atomic_inc(&thresholds->cur);
+		eventfd_signal(thresholds->entries[i].eventfd, 1);
+	}
+unlock:
+	rcu_read_unlock();
+}
+
+static void mem_cgroup_threshold(struct mem_cgroup *memcg)
+{
+	__mem_cgroup_threshold(memcg, false);
+	if (do_swap_account)
+		__mem_cgroup_threshold(memcg, true);
+}
+
+static int compare_thresholds(const void *a, const void *b)
+{
+	const struct mem_cgroup_threshold *_a = a;
+	const struct mem_cgroup_threshold *_b = b;
+
+	return _a->threshold - _b->threshold;
+}
+
+static int mem_cgroup_register_event(struct cgroup *cgrp, struct cftype *cft,
+		struct eventfd_ctx *eventfd, const char *args)
+{
+	struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
+	struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
+	int type = MEMFILE_TYPE(cft->private);
+	u64 threshold, usage;
+	int size;
+	int i, ret;
+
+	ret = res_counter_memparse_write_strategy(args, &threshold);
+	if (ret)
+		return ret;
+
+	mutex_lock(&memcg->thresholds_lock);
+	if (type == _MEM)
+		thresholds = memcg->thresholds;
+	else if (type == _MEMSWAP)
+		thresholds = memcg->memsw_thresholds;
+	else
+		BUG();
+
+	usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
+
+	/* Check if a threshold crossed before adding a new one */
+	if (thresholds)
+		__mem_cgroup_threshold(memcg, type == _MEMSWAP);
+
+	if (thresholds)
+		size = thresholds->size + 1;
+	else
+		size = 1;
+
+	/* Allocate memory for new array of thresholds */
+	thresholds_new = kmalloc(sizeof(*thresholds_new) +
+			size * sizeof(struct mem_cgroup_threshold),
+			GFP_KERNEL);
+	if (!thresholds_new) {
+		ret = -ENOMEM;
+		goto unlock;
+	}
+	thresholds_new->size = size;
+
+	/* Copy thresholds (if any) to new array */
+	if (thresholds)
+		memcpy(thresholds_new->entries, thresholds->entries,
+				thresholds->size *
+				sizeof(struct mem_cgroup_threshold));
+	/* Add new threshold */
+	thresholds_new->entries[size - 1].eventfd = eventfd;
+	thresholds_new->entries[size - 1].threshold = threshold;
+
+	/* Sort thresholds. Registering of new threshold isn't time-critical */
+	sort(thresholds_new->entries, size,
+			sizeof(struct mem_cgroup_threshold),
+			compare_thresholds, NULL);
+
+	/* Find current threshold */
+	atomic_set(&thresholds_new->cur, -1);
+	for(i = 0; i < size; i++) {
+		if (thresholds_new->entries[i].threshold < usage)
+			atomic_inc(&thresholds_new->cur);
+	}
+
+	/*
+	 * We need to increment refcnt to be sure that all thresholds

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3256

Quoted 10 lines
mm/memcontrol.c +10 -0
+	 * will be unregistered before calling __mem_cgroup_free()
+	 */
+	mem_cgroup_get(memcg);
+
+	if (type == _MEM)
+		rcu_assign_pointer(memcg->thresholds, thresholds_new);
+	else
+		rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
+
+	synchronize_rcu();

Could you add explanation when you use synchronize_rcu() ?

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3257

Quoted 1 line
mm/memcontrol.c +1 -0
+	kfree(thresholds);

Can't this be freed by RCU instead of synchronize_rcu() ?

Quoted 56 lines
mm/memcontrol.c +56 -0
+unlock:
+	mutex_unlock(&memcg->thresholds_lock);
+
+	return ret;
+}
+
+static int mem_cgroup_unregister_event(struct cgroup *cgrp, struct cftype *cft,
+		struct eventfd_ctx *eventfd)
+{
+	struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
+	struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
+	int type = MEMFILE_TYPE(cft->private);
+	u64 usage;
+	int size = 0;
+	int i, j, ret;
+
+	mutex_lock(&memcg->thresholds_lock);
+	if (type == _MEM)
+		thresholds = memcg->thresholds;
+	else if (type == _MEMSWAP)
+		thresholds = memcg->memsw_thresholds;
+	else
+		BUG();
+
+	/*
+	 * Something went wrong if we trying to unregister a threshold
+	 * if we don't have thresholds
+	 */
+	BUG_ON(!thresholds);
+
+	usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
+
+	/* Check if a threshold crossed before removing */
+	__mem_cgroup_threshold(memcg, type == _MEMSWAP);
+
+	/* Calculate new number of threshold */
+	for(i = 0; i < thresholds->size; i++) {
+		if (thresholds->entries[i].eventfd != eventfd)
+			size++;
+	}
+
+	/* Set thresholds array to NULL if we don't have thresholds */
+	if (!size) {
+		thresholds_new = NULL;
+		goto assign;
+	}
+
+	/* Allocate memory for new array of thresholds */
+	thresholds_new = kmalloc(sizeof(*thresholds_new) +
+			size * sizeof(struct mem_cgroup_threshold),
+			GFP_KERNEL);
+	if (!thresholds_new) {
+		ret = -ENOMEM;
+		goto unlock;
+	}
+	thresholds_new->size = size;

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3323

Quoted 10 lines
mm/memcontrol.c +10 -0
+
+	/* Copy thresholds and find current threshold */
+	atomic_set(&thresholds_new->cur, -1);
+	for(i = 0, j = 0; i < thresholds->size; i++) {
+		if (thresholds->entries[i].eventfd == eventfd)
+			continue;
+
+		thresholds_new->entries[j] = thresholds->entries[i];
+		if (thresholds_new->entries[j].threshold < usage)
+			atomic_inc(&thresholds_new->cur);

It's better to do atomic set after loop.

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3325

Quoted 2 lines
mm/memcontrol.c +2 -0
+		j++;
+	}

Hmm..is this "copy array" usual coding style for handling eventfd ?

Quoted 54 lines
mm/memcontrol.c +23 -0
+
+assign:
+	if (type == _MEM)
+		rcu_assign_pointer(memcg->thresholds, thresholds_new);
+	else
+		rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
+
+	synchronize_rcu();
+
+	for(i = 0; i < thresholds->size - size; i++)
+		mem_cgroup_put(memcg);
+
+	kfree(thresholds);
+unlock:
+	mutex_unlock(&memcg->thresholds_lock);
+
+	return ret;
+}
 
 static struct cftype mem_cgroup_files[] = {
 	{
 		.name = "usage_in_bytes",
 		.private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
 		.read_u64 = mem_cgroup_read,
+		.register_event = mem_cgroup_register_event,
+		.unregister_event = mem_cgroup_unregister_event,
 	},
 	{
 		.name = "max_usage_in_bytes",
@@ -3128,6 +3400,8 @@ static struct cftype memsw_cgroup_files[] = {
 		.name = "memsw.usage_in_bytes",
 		.private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
 		.read_u64 = mem_cgroup_read,
+		.register_event = mem_cgroup_register_event,
+		.unregister_event = mem_cgroup_unregister_event,
 	},
 	{
 		.name = "memsw.max_usage_in_bytes",
@@ -3367,6 +3641,7 @@ mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
 	if (parent)
 		mem->swappiness = get_swappiness(parent);
 	atomic_set(&mem->refcnt, 1);
+	mutex_init(&mem->thresholds_lock);
 	return &mem->css;
 free_out:
 	__mem_cgroup_free(mem);
Signature · 7 lines
1.6.5.7

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

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

A message that is not in the archive (sent off-list or not yet received): 20091228114325.e9b3b3d6.kamezawa.hiroyu-+CUm20s59erQFUHtdCDX3A@public.gmane.org

#24 lore raw

Re: [PATCH v4 4/4] memcg: implement memory thresholds

Quoted 102 lines · KAMEZAWA Hiroyuki
Quoted 99 lines · On Sun, 27 Dec 2009 04:09:02 +0200 "Kirill A. Shutemov" <[email protected]>…

It allows to register multiple memory and memsw thresholds and gets notifications when it crosses.

To register a threshold application need:
- create an eventfd;
- open memory.usage_in_bytes or memory.memsw.usage_in_bytes;
- write string like "<event_fd> <memory.usage_in_bytes> <threshold>" to cgroup.event_control.

Application will be notified through eventfd when memory usage crosses threshold in any direction.

It's applicable for root and non-root cgroup.

It uses stats to track memory usage, simmilar to soft limits. It checks if we need to send event to userspace on every 100 page in/out. I guess it's good compromise between performance and accuracy of thresholds.

---
 Documentation/cgroups/memory.txt |   19 +++-
 mm/memcontrol.c                  |  275 ++++++++++++++++++++++++++++++++++++++

 2 files changed, 293 insertions(+), 1 deletions(-)
Documentation/cgroups/memory.txt +0 -0
index b871f25..195af07 100644
@@ -414,7 +414,24 @@ NOTE1: Soft limits take effect over a long period of time, since they involve
  NOTE2: It is recommended to set the soft limit always below the hard limit,

        otherwise the hard limit will take precedence.

-8. TODO
+8. Memory thresholds

Documentation/cgroups/memory.txt +17 -0
+
+Memory controler implements memory thresholds using cgroups notification
+API (see cgroups.txt). It allows to register multiple memory and memsw
+thresholds and gets notifications when it crosses.
+
+To register a threshold application need:
+ - create an eventfd using eventfd(2);
+ - open memory.usage_in_bytes or memory.memsw.usage_in_bytes;
+ - write string like "<event_fd> <memory.usage_in_bytes> <threshold>" to
+   cgroup.event_control.
+
+Application will be notified through eventfd when memory usage crosses
+threshold in any direction.
+
+It's applicable for root and non-root cgroup.
+
+9. TODO

 1. Add support for accounting huge pages (as a separate controller)
 2. Make per-cgroup scanner reclaim not-shared pages first

mm/memcontrol.c +7 -0
index 36eb7af..3a0a6a1 100644
@@ -6,6 +6,10 @@
   * Copyright 2007 OpenVZ SWsoft Inc
   * Author: Pavel Emelianov <[email protected]>
   *
+ * Memory thresholds
+ * Copyright (C) 2009 Nokia Corporation
+ * Author: Kirill A. Shutemov
+ *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
@@ -39,6 +43,8 @@
  #include <linux/mm_inline.h>
  #include <linux/page_cgroup.h>
  #include <linux/cpu.h>
+#include <linux/eventfd.h>
+#include <linux/sort.h>
  #include "internal.h"
 
  #include <asm/uaccess.h>
@@ -56,6 +62,7 @@ static int really_do_swap_account __initdata = 1; /* for remember boot option*/
  #endif
 
  #define SOFTLIMIT_EVENTS_THRESH (1000)
+#define THRESHOLDS_EVENTS_THRESH (100)
 
  /*
   * Statistics for memory cgroup.
@@ -72,6 +79,8 @@ enum mem_cgroup_stat_index {

      MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */ MEM_CGROUP_STAT_SOFTLIMIT, /* decrements on each page in/out. used by soft limit implementation */
+     MEM_CGROUP_STAT_THRESHOLDS, /* decrements on each page in/out.
+                                     used by threshold implementation */

      MEM_CGROUP_STAT_NSTATS,
 };

mm/memcontrol.c +0 -0
@@ -182,6 +191,20 @@ struct mem_cgroup_tree {
 
  static struct mem_cgroup_tree soft_limit_tree __read_mostly;
 
Quoted 12 lines

mm/memcontrol.c struct mem_cgroup_tree { · line 204

Quoted 11 lines
mm/memcontrol.c +11 -0
+struct mem_cgroup_threshold {
+     struct eventfd_ctx *eventfd;
+     u64 threshold;
+};
+
+struct mem_cgroup_threshold_ary {
+     unsigned int size;
+     atomic_t cur;
+     struct mem_cgroup_threshold entries[0];
+};
+

Why "array" is a choice here ? IOW, why not list ?

We need be able to walk by thresholds in both directions to be fast. AFAIK, It's impossible with RCU-protected list.

Quoted 1 line

How many waiters are expected as usual workload ?

Array of thresholds reads every 100 page in/out for every CPU. Write access only when registering new threshold.

Quoted 192 lines
Quoted 192 lines
mm/memcontrol.c +14 -0
+static bool mem_cgroup_threshold_check(struct mem_cgroup* mem);
+static void mem_cgroup_threshold(struct mem_cgroup* mem);
+
  /*
   * The memory controller data structure. The memory controller controls both
   * page cache and RSS per cgroup. We would eventually like to provide
@@ -233,6 +256,15 @@ struct mem_cgroup {
       /* set when res.limit == memsw.limit */
       bool            memsw_is_minimum;
 
+     /* protect arrays of thresholds */
+     struct mutex thresholds_lock;
+
+     /* thresholds for memory usage. RCU-protected */
+     struct mem_cgroup_threshold_ary *thresholds;
+
+     /* thresholds for mem+swap usage. RCU-protected */
+     struct mem_cgroup_threshold_ary *memsw_thresholds;
+
       /*
        * statistics. This must be placed at the end of memcg.
        */
@@ -525,6 +557,8 @@ static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
               __mem_cgroup_stat_add_safe(cpustat,
                               MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
       __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SOFTLIMIT, -1);
+     __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_THRESHOLDS, -1);
+
       put_cpu();
  }
 
@@ -1510,6 +1544,8 @@ charged:
       if (mem_cgroup_soft_limit_check(mem))
               mem_cgroup_update_tree(mem, page);

 done:
+     if (mem_cgroup_threshold_check(mem))
+             mem_cgroup_threshold(mem);
      return 0;
 nomem:
      css_put(&mem->css);

mm/memcontrol.c +141 -0
@@ -2075,6 +2111,8 @@ __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
 
       if (mem_cgroup_soft_limit_check(mem))
               mem_cgroup_update_tree(mem, page);
+     if (mem_cgroup_threshold_check(mem))
+             mem_cgroup_threshold(mem);
       /* at swapout, this memcg will be accessed to record to swap */
       if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
               css_put(&mem->css);
@@ -3071,12 +3109,246 @@ static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
       return 0;
  }
 
+static bool mem_cgroup_threshold_check(struct mem_cgroup *mem)
+{
+     bool ret = false;
+     int cpu;
+     s64 val;
+     struct mem_cgroup_stat_cpu *cpustat;
+
+     cpu = get_cpu();
+     cpustat = &mem->stat.cpustat[cpu];
+     val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_THRESHOLDS);
+     if (unlikely(val < 0)) {
+             __mem_cgroup_stat_set(cpustat, MEM_CGROUP_STAT_THRESHOLDS,
+                             THRESHOLDS_EVENTS_THRESH);
+             ret = true;
+     }
+     put_cpu();
+     return ret;
+}
+
+static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
+{
+     struct mem_cgroup_threshold_ary *thresholds;
+     u64 usage = mem_cgroup_usage(memcg, swap);
+     int i, cur;
+
+     rcu_read_lock();
+     if (!swap) {
+             thresholds = rcu_dereference(memcg->thresholds);
+     } else {
+             thresholds = rcu_dereference(memcg->memsw_thresholds);
+     }
+
+     if (!thresholds)
+             goto unlock;
+
+     cur = atomic_read(&thresholds->cur);
+
+     /* Check if a threshold crossed in any direction */
+
+     for(i = cur; i >= 0 &&
+             unlikely(thresholds->entries[i].threshold > usage); i--) {
+             atomic_dec(&thresholds->cur);
+             eventfd_signal(thresholds->entries[i].eventfd, 1);
+     }
+
+     for(i = cur + 1; i < thresholds->size &&
+             unlikely(thresholds->entries[i].threshold <= usage); i++) {
+             atomic_inc(&thresholds->cur);
+             eventfd_signal(thresholds->entries[i].eventfd, 1);
+     }
+unlock:
+     rcu_read_unlock();
+}
+
+static void mem_cgroup_threshold(struct mem_cgroup *memcg)
+{
+     __mem_cgroup_threshold(memcg, false);
+     if (do_swap_account)
+             __mem_cgroup_threshold(memcg, true);
+}
+
+static int compare_thresholds(const void *a, const void *b)
+{
+     const struct mem_cgroup_threshold *_a = a;
+     const struct mem_cgroup_threshold *_b = b;
+
+     return _a->threshold - _b->threshold;
+}
+
+static int mem_cgroup_register_event(struct cgroup *cgrp, struct cftype *cft,
+             struct eventfd_ctx *eventfd, const char *args)
+{
+     struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
+     struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
+     int type = MEMFILE_TYPE(cft->private);
+     u64 threshold, usage;
+     int size;
+     int i, ret;
+
+     ret = res_counter_memparse_write_strategy(args, &threshold);
+     if (ret)
+             return ret;
+
+     mutex_lock(&memcg->thresholds_lock);
+     if (type == _MEM)
+             thresholds = memcg->thresholds;
+     else if (type == _MEMSWAP)
+             thresholds = memcg->memsw_thresholds;
+     else
+             BUG();
+
+     usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
+
+     /* Check if a threshold crossed before adding a new one */
+     if (thresholds)
+             __mem_cgroup_threshold(memcg, type == _MEMSWAP);
+
+     if (thresholds)
+             size = thresholds->size + 1;
+     else
+             size = 1;
+
+     /* Allocate memory for new array of thresholds */
+     thresholds_new = kmalloc(sizeof(*thresholds_new) +
+                     size * sizeof(struct mem_cgroup_threshold),
+                     GFP_KERNEL);
+     if (!thresholds_new) {
+             ret = -ENOMEM;
+             goto unlock;
+     }
+     thresholds_new->size = size;
+
+     /* Copy thresholds (if any) to new array */
+     if (thresholds)
+             memcpy(thresholds_new->entries, thresholds->entries,
+                             thresholds->size *
+                             sizeof(struct mem_cgroup_threshold));
+     /* Add new threshold */
+     thresholds_new->entries[size - 1].eventfd = eventfd;
+     thresholds_new->entries[size - 1].threshold = threshold;
+
+     /* Sort thresholds. Registering of new threshold isn't time-critical */
+     sort(thresholds_new->entries, size,
+                     sizeof(struct mem_cgroup_threshold),
+                     compare_thresholds, NULL);
+
+     /* Find current threshold */
+     atomic_set(&thresholds_new->cur, -1);
+     for(i = 0; i < size; i++) {
+             if (thresholds_new->entries[i].threshold < usage)
+                     atomic_inc(&thresholds_new->cur);
+     }
+
+     /*
+      * We need to increment refcnt to be sure that all thresholds
+      * will be unregistered before calling __mem_cgroup_free()
+      */
+     mem_cgroup_get(memcg);
+
Quoted 8 lines

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3256

Quoted 6 lines
mm/memcontrol.c +6 -0
+     if (type == _MEM)
+             rcu_assign_pointer(memcg->thresholds, thresholds_new);
+     else
+             rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
+
+     synchronize_rcu();

Could you add explanation when you use synchronize_rcu() ?

It uses before freeing old array of thresholds to be sure than nobody uses it.

Quoted 3 lines

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3257

Quoted 1 line
mm/memcontrol.c +1 -0
+     kfree(thresholds);

Can't this be freed by RCU instead of synchronize_rcu() ?

Yes, this can. But I don't think that (un)registering os thresholds is time critical.
I think my variant is more clean.

Quoted 59 lines
Quoted 59 lines
mm/memcontrol.c +59 -0
+unlock:
+     mutex_unlock(&memcg->thresholds_lock);
+
+     return ret;
+}
+
+static int mem_cgroup_unregister_event(struct cgroup *cgrp, struct cftype *cft,
+             struct eventfd_ctx *eventfd)
+{
+     struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
+     struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
+     int type = MEMFILE_TYPE(cft->private);
+     u64 usage;
+     int size = 0;
+     int i, j, ret;
+
+     mutex_lock(&memcg->thresholds_lock);
+     if (type == _MEM)
+             thresholds = memcg->thresholds;
+     else if (type == _MEMSWAP)
+             thresholds = memcg->memsw_thresholds;
+     else
+             BUG();
+
+     /*
+      * Something went wrong if we trying to unregister a threshold
+      * if we don't have thresholds
+      */
+     BUG_ON(!thresholds);
+
+     usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
+
+     /* Check if a threshold crossed before removing */
+     __mem_cgroup_threshold(memcg, type == _MEMSWAP);
+
+     /* Calculate new number of threshold */
+     for(i = 0; i < thresholds->size; i++) {
+             if (thresholds->entries[i].eventfd != eventfd)
+                     size++;
+     }
+
+     /* Set thresholds array to NULL if we don't have thresholds */
+     if (!size) {
+             thresholds_new = NULL;
+             goto assign;
+     }
+
+     /* Allocate memory for new array of thresholds */
+     thresholds_new = kmalloc(sizeof(*thresholds_new) +
+                     size * sizeof(struct mem_cgroup_threshold),
+                     GFP_KERNEL);
+     if (!thresholds_new) {
+             ret = -ENOMEM;
+             goto unlock;
+     }
+     thresholds_new->size = size;
+
+     /* Copy thresholds and find current threshold */
+     atomic_set(&thresholds_new->cur, -1);
Quoted 8 lines

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3323

Quoted 7 lines
mm/memcontrol.c +7 -0
+     for(i = 0, j = 0; i < thresholds->size; i++) {
+             if (thresholds->entries[i].eventfd == eventfd)
+                     continue;
+
+             thresholds_new->entries[j] = thresholds->entries[i];
+             if (thresholds_new->entries[j].threshold < usage)
+                     atomic_inc(&thresholds_new->cur);

It's better to do atomic set after loop.

We need one more counter to do this. Do you like it?

Quoted 4 lines

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3325

Quoted 2 lines
mm/memcontrol.c +2 -0
+             j++;
+     }

Hmm..is this "copy array" usual coding style for handling eventfd ?

Since we store only pointer to struct eventfd_ctx, I don't see a problem.

Quoted 41 lines
Quoted 41 lines
mm/memcontrol.c +22 -0
+
+assign:
+     if (type == _MEM)
+             rcu_assign_pointer(memcg->thresholds, thresholds_new);
+     else
+             rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
+
+     synchronize_rcu();
+
+     for(i = 0; i < thresholds->size - size; i++)
+             mem_cgroup_put(memcg);
+
+     kfree(thresholds);
+unlock:
+     mutex_unlock(&memcg->thresholds_lock);
+
+     return ret;
+}
 
  static struct cftype mem_cgroup_files[] = {
       {
               .name = "usage_in_bytes",
               .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
               .read_u64 = mem_cgroup_read,
+             .register_event = mem_cgroup_register_event,
+             .unregister_event = mem_cgroup_unregister_event,
       },
       {
               .name = "max_usage_in_bytes",
@@ -3128,6 +3400,8 @@ static struct cftype memsw_cgroup_files[] = {
               .name = "memsw.usage_in_bytes",
               .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
               .read_u64 = mem_cgroup_read,
+             .register_event = mem_cgroup_register_event,
+             .unregister_event = mem_cgroup_unregister_event,
       },
       {
               .name = "memsw.max_usage_in_bytes",
@@ -3367,6 +3641,7 @@ mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
       if (parent)
               mem->swappiness = get_swappiness(parent);
Quoted 8 lines
Quoted 8 lines
mm/memcontrol.c +1 -0
       atomic_set(&mem->refcnt, 1);
+     mutex_init(&mem->thresholds_lock);
       return &mem->css;

 free_out:
      __mem_cgroup_free(mem);
--
1.6.5.7

_______________________________________________
Containers mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/containers

#25 lore raw

Re: [PATCH v4 4/4] memcg: implement memory thresholds

In reply to KAMEZAWA Hiroyuki
Quoted 102 lines · KAMEZAWA Hiroyuki
Quoted 99 lines · On Sun, 27 Dec 2009 04:09:02 +0200 "Kirill A. Shutemov" <[email protected]>…

It allows to register multiple memory and memsw thresholds and gets notifications when it crosses.

To register a threshold application need:
- create an eventfd;
- open memory.usage_in_bytes or memory.memsw.usage_in_bytes;
- write string like "<event_fd> <memory.usage_in_bytes> <threshold>" to cgroup.event_control.

Application will be notified through eventfd when memory usage crosses threshold in any direction.

It's applicable for root and non-root cgroup.

It uses stats to track memory usage, simmilar to soft limits. It checks if we need to send event to userspace on every 100 page in/out. I guess it's good compromise between performance and accuracy of thresholds.

---
 Documentation/cgroups/memory.txt |   19 +++-
 mm/memcontrol.c                  |  275 ++++++++++++++++++++++++++++++++++++++

 2 files changed, 293 insertions(+), 1 deletions(-)
Documentation/cgroups/memory.txt +0 -0
index b871f25..195af07 100644
@@ -414,7 +414,24 @@ NOTE1: Soft limits take effect over a long period of time, since they involve
  NOTE2: It is recommended to set the soft limit always below the hard limit,

        otherwise the hard limit will take precedence.

-8. TODO
+8. Memory thresholds

Documentation/cgroups/memory.txt +17 -0
+
+Memory controler implements memory thresholds using cgroups notification
+API (see cgroups.txt). It allows to register multiple memory and memsw
+thresholds and gets notifications when it crosses.
+
+To register a threshold application need:
+ - create an eventfd using eventfd(2);
+ - open memory.usage_in_bytes or memory.memsw.usage_in_bytes;
+ - write string like "<event_fd> <memory.usage_in_bytes> <threshold>" to
+   cgroup.event_control.
+
+Application will be notified through eventfd when memory usage crosses
+threshold in any direction.
+
+It's applicable for root and non-root cgroup.
+
+9. TODO

 1. Add support for accounting huge pages (as a separate controller)
 2. Make per-cgroup scanner reclaim not-shared pages first

mm/memcontrol.c +7 -0
index 36eb7af..3a0a6a1 100644
@@ -6,6 +6,10 @@
   * Copyright 2007 OpenVZ SWsoft Inc
   * Author: Pavel Emelianov <[email protected]>
   *
+ * Memory thresholds
+ * Copyright (C) 2009 Nokia Corporation
+ * Author: Kirill A. Shutemov
+ *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
@@ -39,6 +43,8 @@
  #include <linux/mm_inline.h>
  #include <linux/page_cgroup.h>
  #include <linux/cpu.h>
+#include <linux/eventfd.h>
+#include <linux/sort.h>
  #include "internal.h"
 
  #include <asm/uaccess.h>
@@ -56,6 +62,7 @@ static int really_do_swap_account __initdata = 1; /* for remember boot option*/
  #endif
 
  #define SOFTLIMIT_EVENTS_THRESH (1000)
+#define THRESHOLDS_EVENTS_THRESH (100)
 
  /*
   * Statistics for memory cgroup.
@@ -72,6 +79,8 @@ enum mem_cgroup_stat_index {

      MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */ MEM_CGROUP_STAT_SOFTLIMIT, /* decrements on each page in/out. used by soft limit implementation */
+     MEM_CGROUP_STAT_THRESHOLDS, /* decrements on each page in/out.
+                                     used by threshold implementation */

      MEM_CGROUP_STAT_NSTATS,
 };

mm/memcontrol.c +0 -0
@@ -182,6 +191,20 @@ struct mem_cgroup_tree {
 
  static struct mem_cgroup_tree soft_limit_tree __read_mostly;
 
Quoted 12 lines

mm/memcontrol.c struct mem_cgroup_tree { · line 204

Quoted 11 lines
mm/memcontrol.c +11 -0
+struct mem_cgroup_threshold {
+     struct eventfd_ctx *eventfd;
+     u64 threshold;
+};
+
+struct mem_cgroup_threshold_ary {
+     unsigned int size;
+     atomic_t cur;
+     struct mem_cgroup_threshold entries[0];
+};
+

Why "array" is a choice here ? IOW, why not list ?

We need be able to walk by thresholds in both directions to be fast. AFAIK, It's impossible with RCU-protected list.

Quoted 1 line

How many waiters are expected as usual workload ?

Array of thresholds reads every 100 page in/out for every CPU. Write access only when registering new threshold.

Quoted 192 lines
Quoted 192 lines
mm/memcontrol.c +14 -0
+static bool mem_cgroup_threshold_check(struct mem_cgroup* mem);
+static void mem_cgroup_threshold(struct mem_cgroup* mem);
+
  /*
   * The memory controller data structure. The memory controller controls both
   * page cache and RSS per cgroup. We would eventually like to provide
@@ -233,6 +256,15 @@ struct mem_cgroup {
       /* set when res.limit == memsw.limit */
       bool            memsw_is_minimum;
 
+     /* protect arrays of thresholds */
+     struct mutex thresholds_lock;
+
+     /* thresholds for memory usage. RCU-protected */
+     struct mem_cgroup_threshold_ary *thresholds;
+
+     /* thresholds for mem+swap usage. RCU-protected */
+     struct mem_cgroup_threshold_ary *memsw_thresholds;
+
       /*
        * statistics. This must be placed at the end of memcg.
        */
@@ -525,6 +557,8 @@ static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
               __mem_cgroup_stat_add_safe(cpustat,
                               MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
       __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SOFTLIMIT, -1);
+     __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_THRESHOLDS, -1);
+
       put_cpu();
  }
 
@@ -1510,6 +1544,8 @@ charged:
       if (mem_cgroup_soft_limit_check(mem))
               mem_cgroup_update_tree(mem, page);

 done:
+     if (mem_cgroup_threshold_check(mem))
+             mem_cgroup_threshold(mem);
      return 0;
 nomem:
      css_put(&mem->css);

mm/memcontrol.c +141 -0
@@ -2075,6 +2111,8 @@ __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
 
       if (mem_cgroup_soft_limit_check(mem))
               mem_cgroup_update_tree(mem, page);
+     if (mem_cgroup_threshold_check(mem))
+             mem_cgroup_threshold(mem);
       /* at swapout, this memcg will be accessed to record to swap */
       if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
               css_put(&mem->css);
@@ -3071,12 +3109,246 @@ static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
       return 0;
  }
 
+static bool mem_cgroup_threshold_check(struct mem_cgroup *mem)
+{
+     bool ret = false;
+     int cpu;
+     s64 val;
+     struct mem_cgroup_stat_cpu *cpustat;
+
+     cpu = get_cpu();
+     cpustat = &mem->stat.cpustat[cpu];
+     val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_THRESHOLDS);
+     if (unlikely(val < 0)) {
+             __mem_cgroup_stat_set(cpustat, MEM_CGROUP_STAT_THRESHOLDS,
+                             THRESHOLDS_EVENTS_THRESH);
+             ret = true;
+     }
+     put_cpu();
+     return ret;
+}
+
+static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
+{
+     struct mem_cgroup_threshold_ary *thresholds;
+     u64 usage = mem_cgroup_usage(memcg, swap);
+     int i, cur;
+
+     rcu_read_lock();
+     if (!swap) {
+             thresholds = rcu_dereference(memcg->thresholds);
+     } else {
+             thresholds = rcu_dereference(memcg->memsw_thresholds);
+     }
+
+     if (!thresholds)
+             goto unlock;
+
+     cur = atomic_read(&thresholds->cur);
+
+     /* Check if a threshold crossed in any direction */
+
+     for(i = cur; i >= 0 &&
+             unlikely(thresholds->entries[i].threshold > usage); i--) {
+             atomic_dec(&thresholds->cur);
+             eventfd_signal(thresholds->entries[i].eventfd, 1);
+     }
+
+     for(i = cur + 1; i < thresholds->size &&
+             unlikely(thresholds->entries[i].threshold <= usage); i++) {
+             atomic_inc(&thresholds->cur);
+             eventfd_signal(thresholds->entries[i].eventfd, 1);
+     }
+unlock:
+     rcu_read_unlock();
+}
+
+static void mem_cgroup_threshold(struct mem_cgroup *memcg)
+{
+     __mem_cgroup_threshold(memcg, false);
+     if (do_swap_account)
+             __mem_cgroup_threshold(memcg, true);
+}
+
+static int compare_thresholds(const void *a, const void *b)
+{
+     const struct mem_cgroup_threshold *_a = a;
+     const struct mem_cgroup_threshold *_b = b;
+
+     return _a->threshold - _b->threshold;
+}
+
+static int mem_cgroup_register_event(struct cgroup *cgrp, struct cftype *cft,
+             struct eventfd_ctx *eventfd, const char *args)
+{
+     struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
+     struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
+     int type = MEMFILE_TYPE(cft->private);
+     u64 threshold, usage;
+     int size;
+     int i, ret;
+
+     ret = res_counter_memparse_write_strategy(args, &threshold);
+     if (ret)
+             return ret;
+
+     mutex_lock(&memcg->thresholds_lock);
+     if (type == _MEM)
+             thresholds = memcg->thresholds;
+     else if (type == _MEMSWAP)
+             thresholds = memcg->memsw_thresholds;
+     else
+             BUG();
+
+     usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
+
+     /* Check if a threshold crossed before adding a new one */
+     if (thresholds)
+             __mem_cgroup_threshold(memcg, type == _MEMSWAP);
+
+     if (thresholds)
+             size = thresholds->size + 1;
+     else
+             size = 1;
+
+     /* Allocate memory for new array of thresholds */
+     thresholds_new = kmalloc(sizeof(*thresholds_new) +
+                     size * sizeof(struct mem_cgroup_threshold),
+                     GFP_KERNEL);
+     if (!thresholds_new) {
+             ret = -ENOMEM;
+             goto unlock;
+     }
+     thresholds_new->size = size;
+
+     /* Copy thresholds (if any) to new array */
+     if (thresholds)
+             memcpy(thresholds_new->entries, thresholds->entries,
+                             thresholds->size *
+                             sizeof(struct mem_cgroup_threshold));
+     /* Add new threshold */
+     thresholds_new->entries[size - 1].eventfd = eventfd;
+     thresholds_new->entries[size - 1].threshold = threshold;
+
+     /* Sort thresholds. Registering of new threshold isn't time-critical */
+     sort(thresholds_new->entries, size,
+                     sizeof(struct mem_cgroup_threshold),
+                     compare_thresholds, NULL);
+
+     /* Find current threshold */
+     atomic_set(&thresholds_new->cur, -1);
+     for(i = 0; i < size; i++) {
+             if (thresholds_new->entries[i].threshold < usage)
+                     atomic_inc(&thresholds_new->cur);
+     }
+
+     /*
+      * We need to increment refcnt to be sure that all thresholds
+      * will be unregistered before calling __mem_cgroup_free()
+      */
+     mem_cgroup_get(memcg);
+
Quoted 8 lines

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3256

Quoted 6 lines
mm/memcontrol.c +6 -0
+     if (type == _MEM)
+             rcu_assign_pointer(memcg->thresholds, thresholds_new);
+     else
+             rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
+
+     synchronize_rcu();

Could you add explanation when you use synchronize_rcu() ?

It uses before freeing old array of thresholds to be sure than nobody uses it.

Quoted 3 lines

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3257

Quoted 1 line
mm/memcontrol.c +1 -0
+     kfree(thresholds);

Can't this be freed by RCU instead of synchronize_rcu() ?

Yes, this can. But I don't think that (un)registering os thresholds is time critical.
I think my variant is more clean.

Quoted 59 lines
Quoted 59 lines
mm/memcontrol.c +59 -0
+unlock:
+     mutex_unlock(&memcg->thresholds_lock);
+
+     return ret;
+}
+
+static int mem_cgroup_unregister_event(struct cgroup *cgrp, struct cftype *cft,
+             struct eventfd_ctx *eventfd)
+{
+     struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
+     struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
+     int type = MEMFILE_TYPE(cft->private);
+     u64 usage;
+     int size = 0;
+     int i, j, ret;
+
+     mutex_lock(&memcg->thresholds_lock);
+     if (type == _MEM)
+             thresholds = memcg->thresholds;
+     else if (type == _MEMSWAP)
+             thresholds = memcg->memsw_thresholds;
+     else
+             BUG();
+
+     /*
+      * Something went wrong if we trying to unregister a threshold
+      * if we don't have thresholds
+      */
+     BUG_ON(!thresholds);
+
+     usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
+
+     /* Check if a threshold crossed before removing */
+     __mem_cgroup_threshold(memcg, type == _MEMSWAP);
+
+     /* Calculate new number of threshold */
+     for(i = 0; i < thresholds->size; i++) {
+             if (thresholds->entries[i].eventfd != eventfd)
+                     size++;
+     }
+
+     /* Set thresholds array to NULL if we don't have thresholds */
+     if (!size) {
+             thresholds_new = NULL;
+             goto assign;
+     }
+
+     /* Allocate memory for new array of thresholds */
+     thresholds_new = kmalloc(sizeof(*thresholds_new) +
+                     size * sizeof(struct mem_cgroup_threshold),
+                     GFP_KERNEL);
+     if (!thresholds_new) {
+             ret = -ENOMEM;
+             goto unlock;
+     }
+     thresholds_new->size = size;
+
+     /* Copy thresholds and find current threshold */
+     atomic_set(&thresholds_new->cur, -1);
Quoted 8 lines

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3323

Quoted 7 lines
mm/memcontrol.c +7 -0
+     for(i = 0, j = 0; i < thresholds->size; i++) {
+             if (thresholds->entries[i].eventfd == eventfd)
+                     continue;
+
+             thresholds_new->entries[j] = thresholds->entries[i];
+             if (thresholds_new->entries[j].threshold < usage)
+                     atomic_inc(&thresholds_new->cur);

It's better to do atomic set after loop.

We need one more counter to do this. Do you like it?

Quoted 4 lines

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3325

Quoted 2 lines
mm/memcontrol.c +2 -0
+             j++;
+     }

Hmm..is this "copy array" usual coding style for handling eventfd ?

Since we store only pointer to struct eventfd_ctx, I don't see a problem.

Quoted 41 lines
Quoted 41 lines
mm/memcontrol.c +22 -0
+
+assign:
+     if (type == _MEM)
+             rcu_assign_pointer(memcg->thresholds, thresholds_new);
+     else
+             rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
+
+     synchronize_rcu();
+
+     for(i = 0; i < thresholds->size - size; i++)
+             mem_cgroup_put(memcg);
+
+     kfree(thresholds);
+unlock:
+     mutex_unlock(&memcg->thresholds_lock);
+
+     return ret;
+}
 
  static struct cftype mem_cgroup_files[] = {
       {
               .name = "usage_in_bytes",
               .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
               .read_u64 = mem_cgroup_read,
+             .register_event = mem_cgroup_register_event,
+             .unregister_event = mem_cgroup_unregister_event,
       },
       {
               .name = "max_usage_in_bytes",
@@ -3128,6 +3400,8 @@ static struct cftype memsw_cgroup_files[] = {
               .name = "memsw.usage_in_bytes",
               .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
               .read_u64 = mem_cgroup_read,
+             .register_event = mem_cgroup_register_event,
+             .unregister_event = mem_cgroup_unregister_event,
       },
       {
               .name = "memsw.max_usage_in_bytes",
@@ -3367,6 +3641,7 @@ mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
       if (parent)
               mem->swappiness = get_swappiness(parent);
Quoted 8 lines
Quoted 8 lines
mm/memcontrol.c +1 -0
       atomic_set(&mem->refcnt, 1);
+     mutex_init(&mem->thresholds_lock);
       return &mem->css;

 free_out:
      __mem_cgroup_free(mem);
--
1.6.5.7

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

A message that is not in the archive (sent off-list or not yet received): cc557aab0912271923v4a4ed8cco168193c63efd44f-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#27 lore raw

Re: [PATCH v4 4/4] memcg: implement memory thresholds

Quoted 4 lines · On Mon, 28 Dec 2009 05:23:51 +0200 "Kirill A. Shutemov" <[email protected]>…
Quoted 2 lines · KAMEZAWA Hiroyuki

On Sun, 27 Dec 2009 04:09:02 +0200
"Kirill A. Shutemov" <[email protected]> wrote:

 /*

Quoted 27 lines
Quoted 14 lines
Quoted 14 lines

  * Statistics for memory cgroup.

(quoted diff) +0 -0
@@ -72,6 +79,8 @@ enum mem_cgroup_stat_index {

      MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */ MEM_CGROUP_STAT_SOFTLIMIT, /* decrements on each page in/out. used by soft limit implementation */
+     MEM_CGROUP_STAT_THRESHOLDS, /* decrements on each page in/out.
+                                     used by threshold implementation */

      MEM_CGROUP_STAT_NSTATS,
 };

mm/memcontrol.c +0 -0
@@ -182,6 +191,20 @@ struct mem_cgroup_tree {
 
  static struct mem_cgroup_tree soft_limit_tree __read_mostly;
 
Quoted 12 lines

mm/memcontrol.c struct mem_cgroup_tree { · line 204

Quoted 11 lines
mm/memcontrol.c +11 -0
+struct mem_cgroup_threshold {
+     struct eventfd_ctx *eventfd;
+     u64 threshold;
+};
+
+struct mem_cgroup_threshold_ary {
+     unsigned int size;
+     atomic_t cur;
+     struct mem_cgroup_threshold entries[0];
+};
+

Why "array" is a choice here ? IOW, why not list ?

Quoted 3 lines

We need be able to walk by thresholds in both directions to be fast. AFAIK, It's impossible with RCU-protected list.

I couldn't read your code correctly. Could you add a comment on

atomic_t cur; /* An array index points to XXXXX */

or use better name ?

Quoted 103 lines
Quoted 1 line

How many waiters are expected as usual workload ?

Array of thresholds reads every 100 page in/out for every CPU. Write access only when registering new threshold.

Quoted 95 lines
Quoted 95 lines
mm/memcontrol.c +14 -0
+static bool mem_cgroup_threshold_check(struct mem_cgroup* mem);
+static void mem_cgroup_threshold(struct mem_cgroup* mem);
+
  /*
   * The memory controller data structure. The memory controller controls both
   * page cache and RSS per cgroup. We would eventually like to provide
@@ -233,6 +256,15 @@ struct mem_cgroup {
       /* set when res.limit == memsw.limit */
       bool            memsw_is_minimum;
 
+     /* protect arrays of thresholds */
+     struct mutex thresholds_lock;
+
+     /* thresholds for memory usage. RCU-protected */
+     struct mem_cgroup_threshold_ary *thresholds;
+
+     /* thresholds for mem+swap usage. RCU-protected */
+     struct mem_cgroup_threshold_ary *memsw_thresholds;
+
       /*
        * statistics. This must be placed at the end of memcg.
        */
@@ -525,6 +557,8 @@ static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
               __mem_cgroup_stat_add_safe(cpustat,
                               MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
       __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SOFTLIMIT, -1);
+     __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_THRESHOLDS, -1);
+
       put_cpu();
  }
 
@@ -1510,6 +1544,8 @@ charged:
       if (mem_cgroup_soft_limit_check(mem))
               mem_cgroup_update_tree(mem, page);

 done:
+     if (mem_cgroup_threshold_check(mem))
+             mem_cgroup_threshold(mem);
      return 0;
 nomem:
      css_put(&mem->css);

mm/memcontrol.c +44 -0
@@ -2075,6 +2111,8 @@ __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
 
       if (mem_cgroup_soft_limit_check(mem))
               mem_cgroup_update_tree(mem, page);
+     if (mem_cgroup_threshold_check(mem))
+             mem_cgroup_threshold(mem);
       /* at swapout, this memcg will be accessed to record to swap */
       if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
               css_put(&mem->css);
@@ -3071,12 +3109,246 @@ static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
       return 0;
  }
 
+static bool mem_cgroup_threshold_check(struct mem_cgroup *mem)
+{
+     bool ret = false;
+     int cpu;
+     s64 val;
+     struct mem_cgroup_stat_cpu *cpustat;
+
+     cpu = get_cpu();
+     cpustat = &mem->stat.cpustat[cpu];
+     val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_THRESHOLDS);
+     if (unlikely(val < 0)) {
+             __mem_cgroup_stat_set(cpustat, MEM_CGROUP_STAT_THRESHOLDS,
+                             THRESHOLDS_EVENTS_THRESH);
+             ret = true;
+     }
+     put_cpu();
+     return ret;
+}
+
+static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
+{
+     struct mem_cgroup_threshold_ary *thresholds;
+     u64 usage = mem_cgroup_usage(memcg, swap);
+     int i, cur;
+
+     rcu_read_lock();
+     if (!swap) {
+             thresholds = rcu_dereference(memcg->thresholds);
+     } else {
+             thresholds = rcu_dereference(memcg->memsw_thresholds);
+     }
+
+     if (!thresholds)
+             goto unlock;
+
+     cur = atomic_read(&thresholds->cur);
+
+     /* Check if a threshold crossed in any direction */
+
+     for(i = cur; i >= 0 &&
+             unlikely(thresholds->entries[i].threshold > usage); i--) {
+             atomic_dec(&thresholds->cur);
Quoted 8 lines
Quoted 8 lines

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3161

Quoted 8 lines
mm/memcontrol.c +8 -0
+             eventfd_signal(thresholds->entries[i].eventfd, 1);
+     }
+
+     for(i = cur + 1; i < thresholds->size &&
+             unlikely(thresholds->entries[i].threshold <= usage); i++) {
+             atomic_inc(&thresholds->cur);
+             eventfd_signal(thresholds->entries[i].eventfd, 1);
+     }

Could you add explanation here ?

Quoted 100 lines
Quoted 89 lines
Quoted 89 lines
mm/memcontrol.c +89 -0
+unlock:
+     rcu_read_unlock();
+}
+
+static void mem_cgroup_threshold(struct mem_cgroup *memcg)
+{
+     __mem_cgroup_threshold(memcg, false);
+     if (do_swap_account)
+             __mem_cgroup_threshold(memcg, true);
+}
+
+static int compare_thresholds(const void *a, const void *b)
+{
+     const struct mem_cgroup_threshold *_a = a;
+     const struct mem_cgroup_threshold *_b = b;
+
+     return _a->threshold - _b->threshold;
+}
+
+static int mem_cgroup_register_event(struct cgroup *cgrp, struct cftype *cft,
+             struct eventfd_ctx *eventfd, const char *args)
+{
+     struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
+     struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
+     int type = MEMFILE_TYPE(cft->private);
+     u64 threshold, usage;
+     int size;
+     int i, ret;
+
+     ret = res_counter_memparse_write_strategy(args, &threshold);
+     if (ret)
+             return ret;
+
+     mutex_lock(&memcg->thresholds_lock);
+     if (type == _MEM)
+             thresholds = memcg->thresholds;
+     else if (type == _MEMSWAP)
+             thresholds = memcg->memsw_thresholds;
+     else
+             BUG();
+
+     usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
+
+     /* Check if a threshold crossed before adding a new one */
+     if (thresholds)
+             __mem_cgroup_threshold(memcg, type == _MEMSWAP);
+
+     if (thresholds)
+             size = thresholds->size + 1;
+     else
+             size = 1;
+
+     /* Allocate memory for new array of thresholds */
+     thresholds_new = kmalloc(sizeof(*thresholds_new) +
+                     size * sizeof(struct mem_cgroup_threshold),
+                     GFP_KERNEL);
+     if (!thresholds_new) {
+             ret = -ENOMEM;
+             goto unlock;
+     }
+     thresholds_new->size = size;
+
+     /* Copy thresholds (if any) to new array */
+     if (thresholds)
+             memcpy(thresholds_new->entries, thresholds->entries,
+                             thresholds->size *
+                             sizeof(struct mem_cgroup_threshold));
+     /* Add new threshold */
+     thresholds_new->entries[size - 1].eventfd = eventfd;
+     thresholds_new->entries[size - 1].threshold = threshold;
+
+     /* Sort thresholds. Registering of new threshold isn't time-critical */
+     sort(thresholds_new->entries, size,
+                     sizeof(struct mem_cgroup_threshold),
+                     compare_thresholds, NULL);
+
+     /* Find current threshold */
+     atomic_set(&thresholds_new->cur, -1);
+     for(i = 0; i < size; i++) {
+             if (thresholds_new->entries[i].threshold < usage)
+                     atomic_inc(&thresholds_new->cur);
+     }
+
+     /*
+      * We need to increment refcnt to be sure that all thresholds
+      * will be unregistered before calling __mem_cgroup_free()
+      */
+     mem_cgroup_get(memcg);
+
Quoted 8 lines

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3256

Quoted 6 lines
mm/memcontrol.c +6 -0
+     if (type == _MEM)
+             rcu_assign_pointer(memcg->thresholds, thresholds_new);
+     else
+             rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
+
+     synchronize_rcu();

Could you add explanation when you use synchronize_rcu() ?

It uses before freeing old array of thresholds to be sure than nobody uses it.

Quoted 8 lines
Quoted 3 lines

Review static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3257

Quoted 1 line
(quoted diff) +1 -0
+     kfree(thresholds);

Can't this be freed by RCU instead of synchronize_rcu() ?

Yes, this can. But I don't think that (un)registering os thresholds is time critical.
I think my variant is more clean.

I don't ;) But ok, this is a nitpick. Ignore me but add an explanation commentary in codes.

Quoted 68 lines
Quoted 59 lines
Quoted 59 lines
mm/memcontrol.c +59 -0
+unlock:
+     mutex_unlock(&memcg->thresholds_lock);
+
+     return ret;
+}
+
+static int mem_cgroup_unregister_event(struct cgroup *cgrp, struct cftype *cft,
+             struct eventfd_ctx *eventfd)
+{
+     struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
+     struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
+     int type = MEMFILE_TYPE(cft->private);
+     u64 usage;
+     int size = 0;
+     int i, j, ret;
+
+     mutex_lock(&memcg->thresholds_lock);
+     if (type == _MEM)
+             thresholds = memcg->thresholds;
+     else if (type == _MEMSWAP)
+             thresholds = memcg->memsw_thresholds;
+     else
+             BUG();
+
+     /*
+      * Something went wrong if we trying to unregister a threshold
+      * if we don't have thresholds
+      */
+     BUG_ON(!thresholds);
+
+     usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
+
+     /* Check if a threshold crossed before removing */
+     __mem_cgroup_threshold(memcg, type == _MEMSWAP);
+
+     /* Calculate new number of threshold */
+     for(i = 0; i < thresholds->size; i++) {
+             if (thresholds->entries[i].eventfd != eventfd)
+                     size++;
+     }
+
+     /* Set thresholds array to NULL if we don't have thresholds */
+     if (!size) {
+             thresholds_new = NULL;
+             goto assign;
+     }
+
+     /* Allocate memory for new array of thresholds */
+     thresholds_new = kmalloc(sizeof(*thresholds_new) +
+                     size * sizeof(struct mem_cgroup_threshold),
+                     GFP_KERNEL);
+     if (!thresholds_new) {
+             ret = -ENOMEM;
+             goto unlock;
+     }
+     thresholds_new->size = size;
+
+     /* Copy thresholds and find current threshold */
+     atomic_set(&thresholds_new->cur, -1);
Quoted 8 lines

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3323

Quoted 7 lines
mm/memcontrol.c +7 -0
+     for(i = 0, j = 0; i < thresholds->size; i++) {
+             if (thresholds->entries[i].eventfd == eventfd)
+                     continue;
+
+             thresholds_new->entries[j] = thresholds->entries[i];
+             if (thresholds_new->entries[j].threshold < usage)
+                     atomic_inc(&thresholds_new->cur);

It's better to do atomic set after loop.

Quoted 2 lines

We need one more counter to do this. Do you like it?

Please add a comment that "cur" is for what or use better name. Honestly, I don't understand fully how "cur" moves. I'm not sure whether updating at insert/delete is really necessary or not.

Quoted 7 lines
Quoted 4 lines

Review static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3325

Quoted 2 lines
(quoted diff) +2 -0
+             j++;
+     }

Hmm..is this "copy array" usual coding style for handling eventfd ?

Since we store only pointer to struct eventfd_ctx, I don't see a problem.

Following is just an suggestion after brief look...

IMO, "cur" is not necessary in the 1st version.
Using simple list and do full-scan always will be good as first step. (And do necessary optimization later.)
Then, size of patch will be dramatically small.

I think the "cur" magic complicates details too much.

Thanks,
-Kame

_______________________________________________
Containers mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/containers

#28 lore raw

Re: [PATCH v4 4/4] memcg: implement memory thresholds

In reply to Kirill A. Shutemov
Quoted 4 lines · On Mon, 28 Dec 2009 05:23:51 +0200 "Kirill A. Shutemov" <[email protected]>…
Quoted 2 lines · KAMEZAWA Hiroyuki

On Sun, 27 Dec 2009 04:09:02 +0200
"Kirill A. Shutemov" <[email protected]> wrote:

A /*

Quoted 27 lines
Quoted 14 lines
Quoted 14 lines

A * Statistics for memory cgroup.

(quoted diff) +0 -0
@@ -72,6 +79,8 @@ enum mem_cgroup_stat_index {
A  A  A  MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
A  A  A  MEM_CGROUP_STAT_SOFTLIMIT, /* decrements on each page in/out.
A  A  A  A  A  A  A  A  A  A  A  A  A  A  A  A  A  A  A  used by soft limit implementation */
+ A  A  MEM_CGROUP_STAT_THRESHOLDS, /* decrements on each page in/out.
+ A  A  A  A  A  A  A  A  A  A  A  A  A  A  A  A  A  A  used by threshold implementation */

A A A MEM_CGROUP_STAT_NSTATS,
A };

(quoted diff) +0 -0
@@ -182,6 +191,20 @@ struct mem_cgroup_tree {
 
 A static struct mem_cgroup_tree soft_limit_tree __read_mostly;
 
Quoted 12 lines

mm/memcontrol.c struct mem_cgroup_tree { · line 204

Quoted 11 lines
mm/memcontrol.c +11 -0
+struct mem_cgroup_threshold {
+ A  A  struct eventfd_ctx *eventfd;
+ A  A  u64 threshold;
+};
+
+struct mem_cgroup_threshold_ary {
+ A  A  unsigned int size;
+ A  A  atomic_t cur;
+ A  A  struct mem_cgroup_threshold entries[0];
+};
+

Why "array" is a choice here ? IOW, why not list ?

Quoted 3 lines

We need be able to walk by thresholds in both directions to be fast. AFAIK, It's impossible with RCU-protected list.

I couldn't read your code correctly. Could you add a comment on

atomic_t cur; /* An array index points to XXXXX */

or use better name ?

Quoted 103 lines
Quoted 1 line

How many waiters are expected as usual workload ?

Array of thresholds reads every 100 page in/out for every CPU. Write access only when registering new threshold.

Quoted 95 lines
Quoted 95 lines
mm/memcontrol.c +3 -0
+static bool mem_cgroup_threshold_check(struct mem_cgroup* mem);
+static void mem_cgroup_threshold(struct mem_cgroup* mem);
+

A /*
A * The memory controller data structure. The memory controller controls both A * page cache and RSS per cgroup. We would eventually like to provide

(quoted diff) +0 -0
@@ -233,6 +256,15 @@ struct mem_cgroup {
A  A  A  /* set when res.limit == memsw.limit */
A  A  A  bool A  A  A  A  A  A memsw_is_minimum;
+ A  A  /* protect arrays of thresholds */
+ A  A  struct mutex thresholds_lock;
(quoted diff) +7 -0
+
+ A  A  /* thresholds for memory usage. RCU-protected */
+ A  A  struct mem_cgroup_threshold_ary *thresholds;
+
+ A  A  /* thresholds for mem+swap usage. RCU-protected */
+ A  A  struct mem_cgroup_threshold_ary *memsw_thresholds;
+
A  A  A  /*
A  A  A  A * statistics. This must be placed at the end of memcg.
A  A  A  A */
(quoted diff) +2 -0
@@ -525,6 +557,8 @@ static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
 A  A  A  A  A  A  A  __mem_cgroup_stat_add_safe(cpustat,
 A  A  A  A  A  A  A  A  A  A  A  A  A  A  A  MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
 A  A  A  __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SOFTLIMIT, -1);
+ A  A  __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_THRESHOLDS, -1);
+
 A  A  A  put_cpu();
 A }
 
@@ -1510,6 +1544,8 @@ charged:
 A  A  A  if (mem_cgroup_soft_limit_check(mem))
 A  A  A  A  A  A  A  mem_cgroup_update_tree(mem, page);

A done:

+ A  A  if (mem_cgroup_threshold_check(mem))
+ A  A  A  A  A  A  mem_cgroup_threshold(mem);

A A A return 0;
A nomem:
A A A css_put(&mem->css);

(quoted diff) +2 -0
@@ -2075,6 +2111,8 @@ __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
 
 A  A  A  if (mem_cgroup_soft_limit_check(mem))
 A  A  A  A  A  A  A  mem_cgroup_update_tree(mem, page);
+ A  A  if (mem_cgroup_threshold_check(mem))
+ A  A  A  A  A  A  mem_cgroup_threshold(mem);
A  A  A  /* at swapout, this memcg will be accessed to record to swap */
A  A  A  if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
A  A  A  A  A  A  A  css_put(&mem->css);
(quoted diff) +42 -0
@@ -3071,12 +3109,246 @@ static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
 A  A  A  return 0;
 A }
 
+static bool mem_cgroup_threshold_check(struct mem_cgroup *mem)
+{
+ A  A  bool ret = false;
+ A  A  int cpu;
+ A  A  s64 val;
+ A  A  struct mem_cgroup_stat_cpu *cpustat;
+
+ A  A  cpu = get_cpu();
+ A  A  cpustat = &mem->stat.cpustat[cpu];
+ A  A  val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_THRESHOLDS);
+ A  A  if (unlikely(val < 0)) {
+ A  A  A  A  A  A  __mem_cgroup_stat_set(cpustat, MEM_CGROUP_STAT_THRESHOLDS,
+ A  A  A  A  A  A  A  A  A  A  A  A  A  A  THRESHOLDS_EVENTS_THRESH);
+ A  A  A  A  A  A  ret = true;
+ A  A  }
+ A  A  put_cpu();
+ A  A  return ret;
+}
+
+static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
+{
+ A  A  struct mem_cgroup_threshold_ary *thresholds;
+ A  A  u64 usage = mem_cgroup_usage(memcg, swap);
+ A  A  int i, cur;
+
+ A  A  rcu_read_lock();
+ A  A  if (!swap) {
+ A  A  A  A  A  A  thresholds = rcu_dereference(memcg->thresholds);
+ A  A  } else {
+ A  A  A  A  A  A  thresholds = rcu_dereference(memcg->memsw_thresholds);
+ A  A  }
+
+ A  A  if (!thresholds)
+ A  A  A  A  A  A  goto unlock;
+
+ A  A  cur = atomic_read(&thresholds->cur);
+
+ A  A  /* Check if a threshold crossed in any direction */
+
+ A  A  for(i = cur; i >= 0 &&
+ A  A  A  A  A  A  unlikely(thresholds->entries[i].threshold > usage); i--) {
+ A  A  A  A  A  A  atomic_dec(&thresholds->cur);
Quoted 8 lines
Quoted 8 lines

Review static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3161

Quoted 8 lines
(quoted diff) +8 -0
+ A  A  A  A  A  A  eventfd_signal(thresholds->entries[i].eventfd, 1);
+ A  A  }
+
+ A  A  for(i = cur + 1; i < thresholds->size &&
+ A  A  A  A  A  A  unlikely(thresholds->entries[i].threshold <= usage); i++) {
+ A  A  A  A  A  A  atomic_inc(&thresholds->cur);
+ A  A  A  A  A  A  eventfd_signal(thresholds->entries[i].eventfd, 1);
+ A  A  }

Could you add explanation here ?

Quoted 100 lines
Quoted 89 lines
Quoted 89 lines
(quoted diff) +89 -0
+unlock:
+ A  A  rcu_read_unlock();
+}
+
+static void mem_cgroup_threshold(struct mem_cgroup *memcg)
+{
+ A  A  __mem_cgroup_threshold(memcg, false);
+ A  A  if (do_swap_account)
+ A  A  A  A  A  A  __mem_cgroup_threshold(memcg, true);
+}
+
+static int compare_thresholds(const void *a, const void *b)
+{
+ A  A  const struct mem_cgroup_threshold *_a = a;
+ A  A  const struct mem_cgroup_threshold *_b = b;
+
+ A  A  return _a->threshold - _b->threshold;
+}
+
+static int mem_cgroup_register_event(struct cgroup *cgrp, struct cftype *cft,
+ A  A  A  A  A  A  struct eventfd_ctx *eventfd, const char *args)
+{
+ A  A  struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
+ A  A  struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
+ A  A  int type = MEMFILE_TYPE(cft->private);
+ A  A  u64 threshold, usage;
+ A  A  int size;
+ A  A  int i, ret;
+
+ A  A  ret = res_counter_memparse_write_strategy(args, &threshold);
+ A  A  if (ret)
+ A  A  A  A  A  A  return ret;
+
+ A  A  mutex_lock(&memcg->thresholds_lock);
+ A  A  if (type == _MEM)
+ A  A  A  A  A  A  thresholds = memcg->thresholds;
+ A  A  else if (type == _MEMSWAP)
+ A  A  A  A  A  A  thresholds = memcg->memsw_thresholds;
+ A  A  else
+ A  A  A  A  A  A  BUG();
+
+ A  A  usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
+
+ A  A  /* Check if a threshold crossed before adding a new one */
+ A  A  if (thresholds)
+ A  A  A  A  A  A  __mem_cgroup_threshold(memcg, type == _MEMSWAP);
+
+ A  A  if (thresholds)
+ A  A  A  A  A  A  size = thresholds->size + 1;
+ A  A  else
+ A  A  A  A  A  A  size = 1;
+
+ A  A  /* Allocate memory for new array of thresholds */
+ A  A  thresholds_new = kmalloc(sizeof(*thresholds_new) +
+ A  A  A  A  A  A  A  A  A  A  size * sizeof(struct mem_cgroup_threshold),
+ A  A  A  A  A  A  A  A  A  A  GFP_KERNEL);
+ A  A  if (!thresholds_new) {
+ A  A  A  A  A  A  ret = -ENOMEM;
+ A  A  A  A  A  A  goto unlock;
+ A  A  }
+ A  A  thresholds_new->size = size;
+
+ A  A  /* Copy thresholds (if any) to new array */
+ A  A  if (thresholds)
+ A  A  A  A  A  A  memcpy(thresholds_new->entries, thresholds->entries,
+ A  A  A  A  A  A  A  A  A  A  A  A  A  A  thresholds->size *
+ A  A  A  A  A  A  A  A  A  A  A  A  A  A  sizeof(struct mem_cgroup_threshold));
+ A  A  /* Add new threshold */
+ A  A  thresholds_new->entries[size - 1].eventfd = eventfd;
+ A  A  thresholds_new->entries[size - 1].threshold = threshold;
+
+ A  A  /* Sort thresholds. Registering of new threshold isn't time-critical */
+ A  A  sort(thresholds_new->entries, size,
+ A  A  A  A  A  A  A  A  A  A  sizeof(struct mem_cgroup_threshold),
+ A  A  A  A  A  A  A  A  A  A  compare_thresholds, NULL);
+
+ A  A  /* Find current threshold */
+ A  A  atomic_set(&thresholds_new->cur, -1);
+ A  A  for(i = 0; i < size; i++) {
+ A  A  A  A  A  A  if (thresholds_new->entries[i].threshold < usage)
+ A  A  A  A  A  A  A  A  A  A  atomic_inc(&thresholds_new->cur);
+ A  A  }
+
+ A  A  /*
+ A  A  A * We need to increment refcnt to be sure that all thresholds
+ A  A  A * will be unregistered before calling __mem_cgroup_free()
+ A  A  A */
+ A  A  mem_cgroup_get(memcg);
+
Quoted 8 lines

Review static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3256

Quoted 6 lines
(quoted diff) +6 -0
+ A  A  if (type == _MEM)
+ A  A  A  A  A  A  rcu_assign_pointer(memcg->thresholds, thresholds_new);
+ A  A  else
+ A  A  A  A  A  A  rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
+
+ A  A  synchronize_rcu();

Could you add explanation when you use synchronize_rcu() ?

It uses before freeing old array of thresholds to be sure than nobody uses it.

Quoted 8 lines
Quoted 3 lines

Review static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3257

Quoted 1 line
(quoted diff) +1 -0
+ A  A  kfree(thresholds);

Can't this be freed by RCU instead of synchronize_rcu() ?

Yes, this can. But I don't think that (un)registering os thresholds is time critical.
I think my variant is more clean.

I don't ;) But ok, this is a nitpick. Ignore me but add an explanation commentary in codes.

Quoted 68 lines
Quoted 59 lines
Quoted 59 lines
(quoted diff) +59 -0
+unlock:
+ A  A  mutex_unlock(&memcg->thresholds_lock);
+
+ A  A  return ret;
+}
+
+static int mem_cgroup_unregister_event(struct cgroup *cgrp, struct cftype *cft,
+ A  A  A  A  A  A  struct eventfd_ctx *eventfd)
+{
+ A  A  struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
+ A  A  struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
+ A  A  int type = MEMFILE_TYPE(cft->private);
+ A  A  u64 usage;
+ A  A  int size = 0;
+ A  A  int i, j, ret;
+
+ A  A  mutex_lock(&memcg->thresholds_lock);
+ A  A  if (type == _MEM)
+ A  A  A  A  A  A  thresholds = memcg->thresholds;
+ A  A  else if (type == _MEMSWAP)
+ A  A  A  A  A  A  thresholds = memcg->memsw_thresholds;
+ A  A  else
+ A  A  A  A  A  A  BUG();
+
+ A  A  /*
+ A  A  A * Something went wrong if we trying to unregister a threshold
+ A  A  A * if we don't have thresholds
+ A  A  A */
+ A  A  BUG_ON(!thresholds);
+
+ A  A  usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
+
+ A  A  /* Check if a threshold crossed before removing */
+ A  A  __mem_cgroup_threshold(memcg, type == _MEMSWAP);
+
+ A  A  /* Calculate new number of threshold */
+ A  A  for(i = 0; i < thresholds->size; i++) {
+ A  A  A  A  A  A  if (thresholds->entries[i].eventfd != eventfd)
+ A  A  A  A  A  A  A  A  A  A  size++;
+ A  A  }
+
+ A  A  /* Set thresholds array to NULL if we don't have thresholds */
+ A  A  if (!size) {
+ A  A  A  A  A  A  thresholds_new = NULL;
+ A  A  A  A  A  A  goto assign;
+ A  A  }
+
+ A  A  /* Allocate memory for new array of thresholds */
+ A  A  thresholds_new = kmalloc(sizeof(*thresholds_new) +
+ A  A  A  A  A  A  A  A  A  A  size * sizeof(struct mem_cgroup_threshold),
+ A  A  A  A  A  A  A  A  A  A  GFP_KERNEL);
+ A  A  if (!thresholds_new) {
+ A  A  A  A  A  A  ret = -ENOMEM;
+ A  A  A  A  A  A  goto unlock;
+ A  A  }
+ A  A  thresholds_new->size = size;
+
+ A  A  /* Copy thresholds and find current threshold */
+ A  A  atomic_set(&thresholds_new->cur, -1);
Quoted 8 lines

Review static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3323

Quoted 7 lines
(quoted diff) +7 -0
+ A  A  for(i = 0, j = 0; i < thresholds->size; i++) {
+ A  A  A  A  A  A  if (thresholds->entries[i].eventfd == eventfd)
+ A  A  A  A  A  A  A  A  A  A  continue;
+
+ A  A  A  A  A  A  thresholds_new->entries[j] = thresholds->entries[i];
+ A  A  A  A  A  A  if (thresholds_new->entries[j].threshold < usage)
+ A  A  A  A  A  A  A  A  A  A  atomic_inc(&thresholds_new->cur);

It's better to do atomic set after loop.

Quoted 2 lines

We need one more counter to do this. Do you like it?

Please add a comment that "cur" is for what or use better name. Honestly, I don't understand fully how "cur" moves. I'm not sure whether updating at insert/delete is really necessary or not.

Quoted 7 lines
Quoted 4 lines

Review static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3325

Quoted 2 lines
(quoted diff) +2 -0
+ A  A  A  A  A  A  j++;
+ A  A  }

Hmm..is this "copy array" usual coding style for handling eventfd ?

Since we store only pointer to struct eventfd_ctx, I don't see a problem.

Following is just an suggestion after brief look...

IMO, "cur" is not necessary in the 1st version.
Using simple list and do full-scan always will be good as first step. (And do necessary optimization later.)
Then, size of patch will be dramatically small.

I think the "cur" magic complicates details too much.

Thanks,
-Kame

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

#29 lore raw

Re: [PATCH v4 4/4] memcg: implement memory thresholds

In reply to Kirill A. Shutemov

mm/memcontrol.c

Quoted 58 lines · Kirill A. Shutemov

It allows to register multiple memory and memsw thresholds and gets notifications when it crosses.

To register a threshold application need:
- create an eventfd;
- open memory.usage_in_bytes or memory.memsw.usage_in_bytes;
- write string like "<event_fd> <memory.usage_in_bytes> <threshold>" to cgroup.event_control.

Application will be notified through eventfd when memory usage crosses threshold in any direction.

It's applicable for root and non-root cgroup.

It uses stats to track memory usage, simmilar to soft limits. It checks if we need to send event to userspace on every 100 page in/out. I guess it's good compromise between performance and accuracy of thresholds.

---
 Documentation/cgroups/memory.txt |   19 +++-
 mm/memcontrol.c                  |  275 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 293 insertions(+), 1 deletions(-)
Documentation/cgroups/memory.txt +18 -1
index b871f25..195af07 100644
@@ -414,7 +414,24 @@ NOTE1: Soft limits take effect over a long period of time, since they involve
 NOTE2: It is recommended to set the soft limit always below the hard limit,
        otherwise the hard limit will take precedence.
 
-8. TODO
+8. Memory thresholds
+
+Memory controler implements memory thresholds using cgroups notification
+API (see cgroups.txt). It allows to register multiple memory and memsw
+thresholds and gets notifications when it crosses.
+
+To register a threshold application need:
+ - create an eventfd using eventfd(2);
+ - open memory.usage_in_bytes or memory.memsw.usage_in_bytes;
+ - write string like "<event_fd> <memory.usage_in_bytes> <threshold>" to
+   cgroup.event_control.
+
+Application will be notified through eventfd when memory usage crosses
+threshold in any direction.
+
+It's applicable for root and non-root cgroup.
+
+9. TODO
 
 1. Add support for accounting huge pages (as a separate controller)
 2. Make per-cgroup scanner reclaim not-shared pages first
mm/memcontrol.c +0 -0
index 36eb7af..3a0a6a1 100644

It would be a nitpick, but my patch(http://marc.info/?l=linux-mm-commits&m=126152804420992&w=2) has already modified here.

I think it might be better for you to apply my patches by hand or wait for next mmotm to be released to avoid bothering Andrew.
(There is enough time left till the next merge window :))

(snip)

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3136

Quoted 6 lines
mm/memcontrol.c +6 -0
+static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
+{
+	struct mem_cgroup_threshold_ary *thresholds;
+	u64 usage = mem_cgroup_usage(memcg, swap);
+	int i, cur;
+

I think calling mem_cgroup_usage() after checking "if(!thresholds)" decreases the overhead a little when we don't set any thresholds. I've confirmed that the change makes the assembler output different.

Quoted 19 lines
mm/memcontrol.c +19 -0
+	rcu_read_lock();
+	if (!swap) {
+		thresholds = rcu_dereference(memcg->thresholds);
+	} else {
+		thresholds = rcu_dereference(memcg->memsw_thresholds);
+	}
+
+	if (!thresholds)
+		goto unlock;
+
+	cur = atomic_read(&thresholds->cur);
+
+	/* Check if a threshold crossed in any direction */
+
+	for(i = cur; i >= 0 &&
+		unlikely(thresholds->entries[i].threshold > usage); i--) {
+		atomic_dec(&thresholds->cur);
+		eventfd_signal(thresholds->entries[i].eventfd, 1);
+	}

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3165

Quoted 10 lines
mm/memcontrol.c +10 -0
+
+	for(i = cur + 1; i < thresholds->size &&
+		unlikely(thresholds->entries[i].threshold <= usage); i++) {
+		atomic_inc(&thresholds->cur);
+		eventfd_signal(thresholds->entries[i].eventfd, 1);
+	}
+unlock:
+	rcu_read_unlock();
+}
+

Thanks,
Daisuke Nishimura.

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

A message that is not in the archive (sent off-list or not yet received): 20091228134245.8db992d1.nishimura-YQH0OdQVrdy45+QrQBaojngSJqDPrsil@public.gmane.org

#31 lore raw

Re: [PATCH v4 4/4] memcg: implement memory thresholds

Quoted 6 lines · Daisuke Nishimura

It would be a nitpick, but my patch(http://marc.info/?l=linux-mm-commits&m=126152804420992&w=2) has already modified here.

I think it might be better for you to apply my patches by hand or wait for next mmotm to be released to avoid bothering Andrew.
(There is enough time left till the next merge window :))

I'll rebase it later.

Quoted 11 lines

(snip)

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3136

Quoted 6 lines
mm/memcontrol.c +6 -0
+static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
+{
+     struct mem_cgroup_threshold_ary *thresholds;
+     u64 usage = mem_cgroup_usage(memcg, swap);
+     int i, cur;
+

I think calling mem_cgroup_usage() after checking "if(!thresholds)" decreases the overhead a little when we don't set any thresholds. I've confirmed that the change makes the assembler output different.

Thanks.
_______________________________________________
Containers mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/containers

#32 lore raw

Re: [PATCH v4 4/4] memcg: implement memory thresholds

In reply to Daisuke Nishimura
Quoted 6 lines · Daisuke Nishimura

It would be a nitpick, but my patch(http://marc.info/?l=linux-mm-commits&m=126152804420992&w=2) has already modified here.

I think it might be better for you to apply my patches by hand or wait for next mmotm to be released to avoid bothering Andrew.
(There is enough time left till the next merge window :))

I'll rebase it later.

Quoted 11 lines

(snip)

mm/memcontrol.c static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, · line 3136

Quoted 6 lines
mm/memcontrol.c +6 -0
+static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
+{
+     struct mem_cgroup_threshold_ary *thresholds;
+     u64 usage = mem_cgroup_usage(memcg, swap);
+     int i, cur;
+

I think calling mem_cgroup_usage() after checking "if(!thresholds)" decreases the overhead a little when we don't set any thresholds. I've confirmed that the change makes the assembler output different.

Thanks.

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

Acked-by on v4 3/4
#33 lore raw

Re: [PATCH v4 3/4] memcg: rework usage of stats by soft limit

In reply to Kirill A. Shutemov
Quoted 11 lines · On Sun, 27 Dec 2009 04:09:01 +0200 "Kirill A. Shutemov" <[email protected]>…

Instead of incrementing counter on each page in/out and comparing it with constant, we set counter to constant, decrement counter on each page in/out and compare it with zero. We want to make comparing as fast as possible. On many RISC systems (probably not only RISC) comparing with zero is more effective than comparing with a constant, since not every constant can be immediate operand for compare instruction.

Also, I've renamed MEM_CGROUP_STAT_EVENTS to MEM_CGROUP_STAT_SOFTLIMIT, since really it's not a generic counter.

I have to sort out these counter stuff after this. But now,

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

#34 lore raw

Re: [PATCH v4 3/4] memcg: rework usage of stats by soft limit

In reply to Kirill A. Shutemov
Quoted 31 lines · Kirill A. Shutemov

Instead of incrementing counter on each page in/out and comparing it with constant, we set counter to constant, decrement counter on each page in/out and compare it with zero. We want to make comparing as fast as possible. On many RISC systems (probably not only RISC) comparing with zero is more effective than comparing with a constant, since not every constant can be immediate operand for compare instruction.

Also, I've renamed MEM_CGROUP_STAT_EVENTS to MEM_CGROUP_STAT_SOFTLIMIT, since really it's not a generic counter.

---
 mm/memcontrol.c |   19 ++++++++++++++-----
 1 files changed, 14 insertions(+), 5 deletions(-)
mm/memcontrol.c +2 -1
index 1d71cb4..36eb7af 100644
@@ -69,8 +69,9 @@ enum mem_cgroup_stat_index {
 	MEM_CGROUP_STAT_FILE_MAPPED,  /* # of pages charged as file rss */
 	MEM_CGROUP_STAT_PGPGIN_COUNT,	/* # of pages paged in */
 	MEM_CGROUP_STAT_PGPGOUT_COUNT,	/* # of pages paged out */
-	MEM_CGROUP_STAT_EVENTS,	/* sum of pagein + pageout for internal use */
 	MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
+	MEM_CGROUP_STAT_SOFTLIMIT, /* decrements on each page in/out.
+					used by soft limit implementation */
 
 	MEM_CGROUP_STAT_NSTATS,
 };

mm/memcontrol.c __mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat, · line 100

Quoted 10 lines
mm/memcontrol.c +7 -0
@@ -90,6 +91,13 @@ __mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat,
 	stat->count[idx] = 0;
 }
 
+static inline void
+__mem_cgroup_stat_set(struct mem_cgroup_stat_cpu *stat,
+		enum mem_cgroup_stat_index idx, s64 val)
+{
+	stat->count[idx] = val;
+}
+

I think it would be better to name it __mem_cgroup_stat_set_safe. And could you remove the definition of __mem_cgroup_stat_reset ?

Thanks,
Daisuke Nishimura.

Quoted 29 lines
mm/memcontrol.c +5 -4
 static inline s64
 __mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat,
 				enum mem_cgroup_stat_index idx)
@@ -380,9 +388,10 @@ static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
 
 	cpu = get_cpu();
 	cpustat = &mem->stat.cpustat[cpu];
-	val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
-	if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
-		__mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
+	val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_SOFTLIMIT);
+	if (unlikely(val < 0)) {
+		__mem_cgroup_stat_set(cpustat, MEM_CGROUP_STAT_SOFTLIMIT,
+				SOFTLIMIT_EVENTS_THRESH);
 		ret = true;
 	}
 	put_cpu();
@@ -515,7 +524,7 @@ static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
 	else
 		__mem_cgroup_stat_add_safe(cpustat,
 				MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
-	__mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
+	__mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SOFTLIMIT, -1);
 	put_cpu();
 }
 
Signature · 1 line
1.6.5.7

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

Acked-by on v4 2/4
#35 lore raw

Re: [PATCH v4 2/4] memcg: extract mem_group_usage() from mem_cgroup_read()

In reply to Kirill A. Shutemov
Quoted 4 lines · On Sun, 27 Dec 2009 04:09:00 +0200 "Kirill A. Shutemov" <[email protected]>…

Helper to get memory or mem+swap usage of the cgroup.

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

Reviewed-by on v4 1/4
#36 lore raw

Re: [PATCH v4 1/4] cgroup: implement eventfd-based generic API for notifications

In reply to Kirill A. Shutemov
Quoted 13 lines · On Sun, 27 Dec 2009 04:08:59 +0200 "Kirill A. Shutemov" <[email protected]>…

This patch introduces write-only file "cgroup.event_control" in every cgroup.

To register new notification handler you need:
- create an eventfd;
- open a control file to be monitored. Callbacks register_event() and unregister_event() must be defined for the control file;
- write "<event_fd> <control_fd> <args>" to cgroup.event_control. Interpretation of args is defined by control file implementation;

eventfd will be woken up by control file implementation or when the cgroup is removed.

Quoted 7 lines

To unregister notification handler just close eventfd.

If you need notification functionality for a control file you have to implement callbacks register_event() and unregister_event() in the struct cftype.

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

#37 lore raw
In reply to Kirill A. Shutemov
Quoted 20 lines · Kirill A. Shutemov

This patchset introduces eventfd-based API for notifications in cgroups and implements memory notifications on top of it.

It uses statistics in memory controler to track memory usage.

Output of time(1) on building kernel on tmpfs:

Root cgroup before changes:

	make -j2  506.37 user 60.93s system 193% cpu 4:52.77 total
Non-root cgroup before changes:
	make -j2  507.14 user 62.66s system 193% cpu 4:54.74 total
Root cgroup after changes (0 thresholds):
	make -j2  507.13 user 62.20s system 193% cpu 4:53.55 total
Non-root cgroup after changes (0 thresholds):
	make -j2  507.70 user 64.20s system 193% cpu 4:55.70 total
Root cgroup after changes (1 thresholds, never crossed):
	make -j2  506.97 user 62.20s system 193% cpu 4:53.90 total
Non-root cgroup after changes (1 thresholds, never crossed):
	make -j2  507.55 user 64.08s system 193% cpu 4:55.63 total
Quoted 1 line

Any comments?

Thanks for adding the documentation, now on to more critical questions

1. Any reasons for not using cgroupstats?
2. Is there a user space test application to test this code. IIUC, I need to write a program that uses eventfd(2) and then passes the eventfd descriptor and thresold to cgroup.*event* file and then the program will get notified when the threshold is reached?

Signature · 1 line
	Balbir

A message that is not in the archive (sent off-list or not yet received): 20091227124732.GA3601-SINUvgVNF2CyUtPGxGje5AC/[email protected]

#39 lore raw
Quoted 22 lines · Balbir Singh
Quoted 20 lines · Kirill A. Shutemov

This patchset introduces eventfd-based API for notifications in cgroups and implements memory notifications on top of it.

It uses statistics in memory controler to track memory usage.

Output of time(1) on building kernel on tmpfs:

Root cgroup before changes:
      make -j2  506.37 user 60.93s system 193% cpu 4:52.77 total Non-root cgroup before changes:
      make -j2  507.14 user 62.66s system 193% cpu 4:54.74 total Root cgroup after changes (0 thresholds):
      make -j2  507.13 user 62.20s system 193% cpu 4:53.55 total Non-root cgroup after changes (0 thresholds):
      make -j2  507.70 user 64.20s system 193% cpu 4:55.70 total Root cgroup after changes (1 thresholds, never crossed):
      make -j2  506.97 user 62.20s system 193% cpu 4:53.90 total Non-root cgroup after changes (1 thresholds, never crossed):
      make -j2  507.55 user 64.08s system 193% cpu 4:55.63 total

Quoted 5 lines
Quoted 1 line

Any comments?

Thanks for adding the documentation, now on to more critical questions

1. Any reasons for not using cgroupstats?

Could you explain the idea? I don't see how cgroupstats applicable for the task.

Quoted 1 line

2. Is there a user space test application to test this code.

Attached. It's not very clean, but good enough for testing propose. Example of usage:

$ echo '/cgroups/memory.usage_in_bytes 1G' | ./cgroup_event_monitor
Quoted 4 lines

 IIUC,
I need to write a program that uses eventfd(2) and then passes the eventfd descriptor and thresold to cgroup.*event* file and then the program will get notified when the threshold is reached?

You need to pass eventfd descriptor, descriptor of control file to be monitored (memory.usage_in_bytes or memory.memsw.usage_in_bytes) and threshold.

Do you want to rename cgroup.event_control to cgroup.event?

_______________________________________________
Containers mailing list Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
https://lists.linux-foundation.org/mailman/listinfo/containers

#40 lore raw
In reply to Balbir Singh
Quoted 22 lines · Balbir Singh
Quoted 20 lines · Kirill A. Shutemov

This patchset introduces eventfd-based API for notifications in cgroups and implements memory notifications on top of it.

It uses statistics in memory controler to track memory usage.

Output of time(1) on building kernel on tmpfs:

Root cgroup before changes:
      make -j2  506.37 user 60.93s system 193% cpu 4:52.77 total Non-root cgroup before changes:
      make -j2  507.14 user 62.66s system 193% cpu 4:54.74 total Root cgroup after changes (0 thresholds):
      make -j2  507.13 user 62.20s system 193% cpu 4:53.55 total Non-root cgroup after changes (0 thresholds):
      make -j2  507.70 user 64.20s system 193% cpu 4:55.70 total Root cgroup after changes (1 thresholds, never crossed):
      make -j2  506.97 user 62.20s system 193% cpu 4:53.90 total Non-root cgroup after changes (1 thresholds, never crossed):
      make -j2  507.55 user 64.08s system 193% cpu 4:55.63 total

Quoted 5 lines
Quoted 1 line

Any comments?

Thanks for adding the documentation, now on to more critical questions

1. Any reasons for not using cgroupstats?

Could you explain the idea? I don't see how cgroupstats applicable for the task.

Quoted 1 line

2. Is there a user space test application to test this code.

Attached. It's not very clean, but good enough for testing propose. Example of usage:

$ echo '/cgroups/memory.usage_in_bytes 1G' | ./cgroup_event_monitor
Quoted 4 lines

 IIUC,
I need to write a program that uses eventfd(2) and then passes the eventfd descriptor and thresold to cgroup.*event* file and then the program will get notified when the threshold is reached?

You need to pass eventfd descriptor, descriptor of control file to be monitored (memory.usage_in_bytes or memory.memsw.usage_in_bytes) and threshold.

Do you want to rename cgroup.event_control to cgroup.event?

A message that is not in the archive (sent off-list or not yet received): cc557aab0912271037scb29fe1xcebe9adfaea97b24-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#42 lore raw
Quoted 23 lines · Kirill A. Shutemov
Quoted 22 lines · Balbir Singh
Quoted 20 lines · Kirill A. Shutemov

This patchset introduces eventfd-based API for notifications in cgroups and implements memory notifications on top of it.

It uses statistics in memory controler to track memory usage.

Output of time(1) on building kernel on tmpfs:

Root cgroup before changes:
      make -j2  506.37 user 60.93s system 193% cpu 4:52.77 total Non-root cgroup before changes:
      make -j2  507.14 user 62.66s system 193% cpu 4:54.74 total Root cgroup after changes (0 thresholds):
      make -j2  507.13 user 62.20s system 193% cpu 4:53.55 total Non-root cgroup after changes (0 thresholds):
      make -j2  507.70 user 64.20s system 193% cpu 4:55.70 total Root cgroup after changes (1 thresholds, never crossed):
      make -j2  506.97 user 62.20s system 193% cpu 4:53.90 total Non-root cgroup after changes (1 thresholds, never crossed):
      make -j2  507.55 user 64.08s system 193% cpu 4:55.63 total

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Any comments?

Thanks for adding the documentation, now on to more critical questions

1. Any reasons for not using cgroupstats?

Could you explain the idea? I don't see how cgroupstats applicable for the task.

cgroupstats allows you to notify task statistics or send notifications, hence the applicability.

Quoted 8 lines
Quoted 2 lines

2. Is there a user space test application to test this code.

Attached. It's not very clean, but good enough for testing propose. Example of usage:

$ echo '/cgroups/memory.usage_in_bytes 1G' | ./cgroup_event_monitor

Thanks, I'll test it right now.

Quoted 10 lines
Quoted 4 lines

 IIUC,
I need to write a program that uses eventfd(2) and then passes the eventfd descriptor and thresold to cgroup.*event* file and then the program will get notified when the threshold is reached?

You need to pass eventfd descriptor, descriptor of control file to be monitored (memory.usage_in_bytes or memory.memsw.usage_in_bytes) and threshold.

Do you want to rename cgroup.event_control to cgroup.event?

No, event_control seems like a good name ATM.

Signature · 1 line
	Balbir
#43 lore raw
In reply to Kirill A. Shutemov
Quoted 23 lines · Kirill A. Shutemov
Quoted 22 lines · Balbir Singh
Quoted 20 lines · Kirill A. Shutemov

This patchset introduces eventfd-based API for notifications in cgroups and implements memory notifications on top of it.

It uses statistics in memory controler to track memory usage.

Output of time(1) on building kernel on tmpfs:

Root cgroup before changes:

      make -j2  506.37 user 60.93s system 193% cpu 4:52.77 total
Non-root cgroup before changes:
      make -j2  507.14 user 62.66s system 193% cpu 4:54.74 total
Root cgroup after changes (0 thresholds):
      make -j2  507.13 user 62.20s system 193% cpu 4:53.55 total
Non-root cgroup after changes (0 thresholds):
      make -j2  507.70 user 64.20s system 193% cpu 4:55.70 total
Root cgroup after changes (1 thresholds, never crossed):
      make -j2  506.97 user 62.20s system 193% cpu 4:53.90 total
Non-root cgroup after changes (1 thresholds, never crossed):
      make -j2  507.55 user 64.08s system 193% cpu 4:55.63 total
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Any comments?

Thanks for adding the documentation, now on to more critical questions

1. Any reasons for not using cgroupstats?

Could you explain the idea? I don't see how cgroupstats applicable for the task.

cgroupstats allows you to notify task statistics or send notifications, hence the applicability.

Quoted 8 lines
Quoted 2 lines

2. Is there a user space test application to test this code.

Attached. It's not very clean, but good enough for testing propose. Example of usage:

$ echo '/cgroups/memory.usage_in_bytes 1G' | ./cgroup_event_monitor

Thanks, I'll test it right now.

Quoted 10 lines
Quoted 4 lines

IIUC,
I need to write a program that uses eventfd(2) and then passes the eventfd descriptor and thresold to cgroup.*event* file and then the program will get notified when the threshold is reached?

You need to pass eventfd descriptor, descriptor of control file to be monitored (memory.usage_in_bytes or memory.memsw.usage_in_bytes) and threshold.

Do you want to rename cgroup.event_control to cgroup.event?

No, event_control seems like a good name ATM.

Signature · 7 lines
	Balbir

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#44 lore raw
In reply to Kirill A. Shutemov
Quoted 20 lines · On Sun, 27 Dec 2009 04:08:58 +0200 "Kirill A. Shutemov" <[email protected]>…

This patchset introduces eventfd-based API for notifications in cgroups and implements memory notifications on top of it.

It uses statistics in memory controler to track memory usage.

Output of time(1) on building kernel on tmpfs:

Root cgroup before changes:

	make -j2  506.37 user 60.93s system 193% cpu 4:52.77 total
Non-root cgroup before changes:
	make -j2  507.14 user 62.66s system 193% cpu 4:54.74 total
Root cgroup after changes (0 thresholds):
	make -j2  507.13 user 62.20s system 193% cpu 4:53.55 total
Non-root cgroup after changes (0 thresholds):
	make -j2  507.70 user 64.20s system 193% cpu 4:55.70 total
Root cgroup after changes (1 thresholds, never crossed):
	make -j2  506.97 user 62.20s system 193% cpu 4:53.90 total
Non-root cgroup after changes (1 thresholds, never crossed):
	make -j2  507.55 user 64.08s system 193% cpu 4:55.63 total
Quoted 2 lines

Any comments?

Hmm, 2 secs of overhead is added by this.
(larger than expected.)

But optimization/claun up after merging is okay to me because the function itself is attractive.

Thanks,
-Kame

Quoted 30 lines

v3 -> v4:
- documentation.

v2 -> v3:
- remove [RFC];
- rebased to 2.6.33-rc2;
- fixes based on comments;
- fixed potential race on event removing;
- use RCU-protected arrays to track trasholds.

v1 -> v2:
- use statistics instead of res_counter to track resource usage;
- fix bugs with locking.

v0 -> v1:
- memsw support implemented.

Kirill A. Shutemov (4):
cgroup: implement eventfd-based generic API for notifications
memcg: extract mem_group_usage() from mem_cgroup_read()
memcg: rework usage of stats by soft limit
memcg: implement memory thresholds

 Documentation/cgroups/cgroups.txt |   20 ++
 Documentation/cgroups/memory.txt  |   19 ++-
 include/linux/cgroup.h            |   24 +++
 kernel/cgroup.c                   |  208 ++++++++++++++++++++++-
 mm/memcontrol.c                   |  348 ++++++++++++++++++++++++++++++++++---
 5 files changed, 590 insertions(+), 29 deletions(-)
Quoted 5 lines

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

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