linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Minchan Kim <minchan@kernel.org>
To: linux-mm <linux-mm@kvack.org>
Cc: LKML <linux-kernel@vger.kernel.org>,
	leonid.moiseichuk@nokia.com, kamezawa.hiroyu@jp.fujitsu.com,
	penberg@kernel.org, Rik van Riel <riel@redhat.com>,
	mel@csn.ul.ie, rientjes@google.com,
	KOSAKI Motohiro <kosaki.motohiro@gmail.com>,
	Johannes Weiner <hannes@cmpxchg.org>,
	Marcelo Tosatti <mtosatti@redhat.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Ronen Hod <rhod@redhat.com>, Minchan Kim <minchan@kernel.org>
Subject: [RFC 3/3] test program
Date: Tue, 17 Jan 2012 17:13:58 +0900	[thread overview]
Message-ID: <1326788038-29141-4-git-send-email-minchan@kernel.org> (raw)
In-Reply-To: <1326788038-29141-1-git-send-email-minchan@kernel.org>

This test program allocates 10M per second and when
memory pressure notify happens, it releases 20M.

I tested this patch on 512M qemu machine with 3 test program.
I saw some swapout but not too many and even didn't see OOM.
It obviously reduces swap out.

Signed-off-by: Minchan Kim <minchan@kernel.org>
---
 poll.c |  121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 121 insertions(+), 0 deletions(-)
 create mode 100644 poll.c

diff --git a/poll.c b/poll.c
new file mode 100644
index 0000000..3215f8b
--- /dev/null
+++ b/poll.c
@@ -0,0 +1,121 @@
+#include <poll.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define ALLOC_UNIT	10 /* MB */
+#define FREE_UNIT	20 /* MB */
+
+void alloc_memory();
+void free_memory();
+
+unsigned int total_memory = 0; /* MB */
+
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 
+
+/*
+ * If total memory is higher than 200M
+ */
+bool memory_full()
+{
+	return total_memory >= 400 ? true : false;
+}
+
+struct alloc_chunk {
+	void *ptr;
+	struct alloc_chunk *next;
+};
+
+struct alloc_chunk head_chunk;
+
+void init_alloc_chunk(void)
+{
+	head_chunk.ptr = NULL;
+	head_chunk.next = NULL;
+}
+
+void add_memory(void *ptr)
+{
+	struct alloc_chunk *new_chunk = malloc(sizeof(struct alloc_chunk));
+	new_chunk->ptr = ptr;
+
+	pthread_mutex_lock(&mutex);
+	new_chunk->next = head_chunk.next;
+	head_chunk.next = new_chunk;
+	total_memory += ALLOC_UNIT;
+	pthread_mutex_unlock(&mutex);
+
+	printf("[%d] Add total memory %d(MB)\n", getpid(), total_memory);
+}
+
+void alloc_memory(void)
+{
+	while(1) {
+		if (memory_full()) {
+			sleep(10);
+			continue;
+		}
+
+		void *new = malloc(ALLOC_UNIT*1024*1024);
+		memset(new, 0, ALLOC_UNIT*1024*1024);
+		add_memory(new);
+		sleep(1);
+	}
+}
+
+void free_memory(void)
+{
+	int count = FREE_UNIT / ALLOC_UNIT;
+	while(count--) {
+		struct alloc_chunk *chunk = head_chunk.next;
+		if (chunk == NULL)
+			break;
+
+		pthread_mutex_lock(&mutex);
+		head_chunk.next = chunk->next;
+		total_memory -= ALLOC_UNIT;
+		pthread_mutex_unlock(&mutex);
+
+		free(chunk->ptr);
+		free(chunk);
+
+		printf("[%d] Free total memory %d(MB)\n", getpid(), total_memory);
+	}
+}
+
+void *poll_thread(void *dummy)
+{
+	struct pollfd pfd;	
+	int fd = open("/dev/low_mem_notify", O_RDONLY); 
+	if (fd == -1) {
+		fprintf(stderr, "Fail to open\n");
+		return;
+	}
+
+	pfd.fd = fd;
+	pfd.events = POLLIN;
+
+	while(1) {
+		poll(&pfd, 1, -1);
+		free_memory();
+	}
+}
+
+int main()
+{
+	pthread_t threadid;
+	init_alloc_chunk();
+
+	if (pthread_create(&threadid, NULL, poll_thread, NULL)) {
+		fprintf(stderr, "pthread create fail\n");
+		return 1;
+	}
+
+	alloc_memory();
+	return 0;
+}
-- 
1.7.7.5

--
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-01-17  8:14 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-17  8:13 [RFC 0/3] low memory notify Minchan Kim
2012-01-17  8:13 ` [RFC 1/3] /dev/low_mem_notify Minchan Kim
2012-01-17  9:27   ` Pekka Enberg
2012-01-17 16:35     ` Rik van Riel
2012-01-17 18:51       ` Pekka Enberg
2012-01-17 19:30         ` Rik van Riel
2012-01-17 19:49           ` Pekka Enberg
2012-01-17 19:54             ` Pekka Enberg
2012-01-17 19:57             ` Pekka Enberg
2012-01-17 23:20         ` Minchan Kim
2012-01-18  7:16           ` Pekka Enberg
2012-01-18  7:49             ` Minchan Kim
2012-01-18  9:06         ` leonid.moiseichuk
2012-01-18  9:15           ` Pekka Enberg
2012-01-18  9:41             ` leonid.moiseichuk
2012-01-18 10:40               ` Pekka Enberg
2012-01-18 10:44                 ` leonid.moiseichuk
2012-01-18 23:34                   ` Ronen Hod
2012-01-19  7:25                     ` Pekka Enberg
2012-01-19  9:05                       ` Ronen Hod
2012-01-19  9:10                         ` Pekka Enberg
2012-01-19  9:20                           ` Ronen Hod
2012-01-19 10:53                             ` leonid.moiseichuk
2012-01-19 11:07                               ` Pekka Enberg
2012-01-19 11:54                                 ` leonid.moiseichuk
2012-01-19 11:59                                   ` Pekka Enberg
2012-01-19 12:06                                   ` Pekka Enberg
2012-01-24 15:38                               ` Marcelo Tosatti
2012-01-24 16:08                                 ` Ronen Hod
2012-01-24 18:10                                   ` Marcelo Tosatti
2012-01-25  8:52                                     ` Ronen Hod
2012-01-25 10:12                                       ` Marcelo Tosatti
2012-01-25 10:48                                         ` Ronen Hod
2012-01-26 16:17                                           ` Marcelo Tosatti
2012-01-24 16:10                                 ` Pekka Enberg
2012-01-24 18:29                                   ` Marcelo Tosatti
2012-01-25  8:19                                   ` leonid.moiseichuk
2012-01-19  7:34                   ` Pekka Enberg
2012-01-24 16:22             ` Arnd Bergmann
2012-01-18 14:30           ` Rik van Riel
2012-01-18 15:29             ` Pekka Enberg
2012-01-24 15:40         ` Marcelo Tosatti
2012-01-24 16:01           ` Pekka Enberg
2012-01-24 16:25             ` Arnd Bergmann
2012-01-24 18:32               ` Marcelo Tosatti
2012-01-24 21:57         ` Jonathan Corbet
2012-01-17  9:45   ` Pekka Enberg
2012-01-17  8:13 ` [RFC 2/3] vmscan hook Minchan Kim
2012-01-17  8:39   ` KAMEZAWA Hiroyuki
2012-01-17  9:13     ` Minchan Kim
2012-01-17 10:05       ` KAMEZAWA Hiroyuki
2012-01-17 23:08         ` Minchan Kim
2012-01-18  0:18           ` KAMEZAWA Hiroyuki
2012-01-18 14:17             ` Rik van Riel
2012-01-19  2:25               ` KAMEZAWA Hiroyuki
2012-01-19 14:42                 ` Rik van Riel
2012-01-20  0:24                   ` KAMEZAWA Hiroyuki
2012-01-17  8:13 ` Minchan Kim [this message]
2012-01-17 14:38 ` [RFC 0/3] low memory notify Colin Walters
2012-01-17 15:04   ` Pekka Enberg
2012-01-17 16:44   ` Rik van Riel
2012-01-17 17:16 ` Olof Johansson

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=1326788038-29141-4-git-send-email-minchan@kernel.org \
    --to=minchan@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=hannes@cmpxchg.org \
    --cc=kamezawa.hiroyu@jp.fujitsu.com \
    --cc=kosaki.motohiro@gmail.com \
    --cc=leonid.moiseichuk@nokia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mel@csn.ul.ie \
    --cc=mtosatti@redhat.com \
    --cc=penberg@kernel.org \
    --cc=rhod@redhat.com \
    --cc=riel@redhat.com \
    --cc=rientjes@google.com \
    /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).