linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Anton Vorontsov <anton.vorontsov@linaro.org>
To: Pekka Enberg <penberg@kernel.org>
Cc: Leonid Moiseichuk <leonid.moiseichuk@nokia.com>,
	John Stultz <john.stultz@linaro.org>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	linaro-kernel@lists.linaro.org
Subject: [PATCH 3/3] vmevent: Implement cross event type
Date: Mon, 9 Apr 2012 03:38:35 +0400	[thread overview]
Message-ID: <20120408233835.GC4839@panacea> (raw)
In-Reply-To: <20120408233550.GA3791@panacea>

This patch implements a new event type, it will trigger whenever a
value crosses a user-specified threshold. It works two-way, i.e. when
a value crosses the threshold from a lesser values side to a greater
values side, and vice versa.

We use the event type in an userspace low-memory killer: we get a
notification when memory becomes low, so we start freeing memory by
killing unneeded processes, and we get notification when memory hits
the threshold from another side, so we know that we freed enough of
memory.

Signed-off-by: Anton Vorontsov <anton.vorontsov@linaro.org>
---
 include/linux/vmevent.h              |    9 +++++++++
 mm/vmevent.c                         |   21 +++++++++++++++++++++
 tools/testing/vmevent/vmevent-test.c |   15 ++++++++++-----
 3 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/include/linux/vmevent.h b/include/linux/vmevent.h
index 64357e4..00cc04f 100644
--- a/include/linux/vmevent.h
+++ b/include/linux/vmevent.h
@@ -22,6 +22,15 @@ enum {
 	 * Sample value is less than user-specified value
 	 */
 	VMEVENT_ATTR_STATE_VALUE_LT	= (1UL << 0),
+	/*
+	 * Sample value crossed user-specified value
+	 */
+	VMEVENT_ATTR_STATE_VALUE_CROSS	= (1UL << 2),
+
+	/* Last saved state, used internally by the kernel. */
+	__VMEVENT_ATTR_STATE_LAST	= (1UL << 30),
+	/* Not first sample, used internally by the kernel. */
+	__VMEVENT_ATTR_STATE_NFIRST	= (1UL << 31),
 };
 
 struct vmevent_attr {
diff --git a/mm/vmevent.c b/mm/vmevent.c
index a56174f..f8fd2d6 100644
--- a/mm/vmevent.c
+++ b/mm/vmevent.c
@@ -1,5 +1,6 @@
 #include <linux/anon_inodes.h>
 #include <linux/atomic.h>
+#include <linux/compiler.h>
 #include <linux/vmevent.h>
 #include <linux/syscalls.h>
 #include <linux/timer.h>
@@ -94,6 +95,26 @@ static bool vmevent_match(struct vmevent_watch *watch)
 		if (attr->state & VMEVENT_ATTR_STATE_VALUE_LT) {
 			if (value < attr->value)
 				return true;
+		} else if (attr->state & VMEVENT_ATTR_STATE_VALUE_CROSS) {
+			bool fst = !(attr->state & __VMEVENT_ATTR_STATE_NFIRST);
+			bool old = attr->state & __VMEVENT_ATTR_STATE_LAST;
+			bool new = value < attr->value;
+			bool chg = old ^ new;
+			bool ret = chg;
+
+			/*
+			 * This is not 'lt' or 'gt' match, so on the first
+			 * sample assume we crossed the threshold.
+			 */
+			if (unlikely(fst)) {
+				attr->state |= __VMEVENT_ATTR_STATE_NFIRST;
+				ret = true;
+			}
+
+			attr->state &= ~__VMEVENT_ATTR_STATE_LAST;
+			attr->state |= new ? __VMEVENT_ATTR_STATE_LAST : 0;
+
+			return ret;
 		}
 	}
 
diff --git a/tools/testing/vmevent/vmevent-test.c b/tools/testing/vmevent/vmevent-test.c
index 534f827..39e93af 100644
--- a/tools/testing/vmevent/vmevent-test.c
+++ b/tools/testing/vmevent/vmevent-test.c
@@ -33,20 +33,25 @@ int main(int argc, char *argv[])
 
 	config = (struct vmevent_config) {
 		.sample_period_ns	= 1000000000L,
-		.counter		= 4,
+		.counter		= 5,
 		.attrs			= {
-			[0]			= {
+			{
+				.type	= VMEVENT_ATTR_NR_FREE_PAGES,
+				.state	= VMEVENT_ATTR_STATE_VALUE_CROSS,
+				.value	= phys_pages / 2,
+			},
+			{
 				.type	= VMEVENT_ATTR_NR_FREE_PAGES,
 				.state	= VMEVENT_ATTR_STATE_VALUE_LT,
 				.value	= phys_pages,
 			},
-			[1]			= {
+			{
 				.type	= VMEVENT_ATTR_NR_AVAIL_PAGES,
 			},
-			[2]			= {
+			{
 				.type	= VMEVENT_ATTR_NR_SWAP_PAGES,
 			},
-			[3]			= {
+			{
 				.type	= 0xffff, /* invalid */
 			},
 		},
-- 
1.7.7.6

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

  parent reply	other threads:[~2012-04-08 23:38 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-08 23:36 [PATCH 0/3] vmevent: Some fixes + a new event type Anton Vorontsov
2012-04-08 23:38 ` [PATCH 1/3] vmevent: Should not grab mutex in the atomic context Anton Vorontsov
2012-04-09  8:40   ` Pekka Enberg
2012-04-09 12:29     ` Anton Vorontsov
2012-04-09 12:37       ` Pekka Enberg
2012-04-08 23:38 ` [PATCH 2/3] vmevent-test: No need for SDL library Anton Vorontsov
2012-04-09  8:20   ` Pekka Enberg
2012-04-08 23:38 ` Anton Vorontsov [this message]
2012-04-09  8:30   ` [PATCH 3/3] vmevent: Implement cross event type Pekka Enberg

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=20120408233835.GC4839@panacea \
    --to=anton.vorontsov@linaro.org \
    --cc=john.stultz@linaro.org \
    --cc=leonid.moiseichuk@nokia.com \
    --cc=linaro-kernel@lists.linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=penberg@kernel.org \
    /path/to/YOUR_REPLY

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

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