mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hui Zhu <teawater@gmail.com>
To: fengguang.wu@intel.com, linux-kernel@vger.kernel.org
Cc: Hui Zhu <teawater@gmail.com>, Hui Zhu <teawaterz@linux.alibaba.com>
Subject: [PATCH for vm-scalability] usemem: Add new option --fragmentation for generating fragmented pages
Date: Mon, 20 Jan 2020 09:50:51 +0800	[thread overview]
Message-ID: <1579485051-19624-1-git-send-email-teawater@gmail.com> (raw)

This commit adds new option --fragmentation.  usemem will generate
size/2/pagesize fragmented pages with this option.
Its implementation is to use madvise to release a page every other page.
For example:
usemem --fragment -s -1 400m
Ideally, this command will generate 200m fragmented pages in the system.

This command can help test anti-fragmentation function and other features
that are affected by fragmentation issues of the Linux kernel.

Signed-off-by: Hui Zhu <teawaterz@linux.alibaba.com>
---
 usemem.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)

diff --git a/usemem.c b/usemem.c
index 9ac36e4..10e8874 100644
--- a/usemem.c
+++ b/usemem.c
@@ -95,6 +95,7 @@ int opt_sync_free = 0;
 int opt_bind_interval = 0;
 unsigned long opt_delay = 0;
 int opt_read_again = 0;
+int opt_fragmentation = 0;
 int nr_task;
 int nr_thread;
 int nr_cpu;
@@ -153,6 +154,7 @@ void usage(int ok)
 	"    -O|--anonymous      mmap with MAP_ANONYMOUS\n"
 	"    -U|--hugetlb        allocate hugetlbfs page\n"
 	"    -Z|--read-again     read memory again after access the memory\n"
+	"    --fragmentation     generate size/2/pagesize fragmented pages\n"
 	"    -h|--help           show this message\n"
 	,		ourname);
 
@@ -191,6 +193,7 @@ static const struct option opts[] = {
 	{ "delay"	, 1, NULL, 'e' },
 	{ "hugetlb"	, 0, NULL, 'U' },
 	{ "read-again"	, 0, NULL, 'Z' },
+	{ "fragmentation", 0, NULL, 0 },
 	{ "help"	, 0, NULL, 'h' },
 	{ NULL		, 0, NULL, 0 }
 };
@@ -655,6 +658,26 @@ static void timing_free(void *ptrs[], unsigned int nptr,
 
 static void wait_for_sigusr1(int signal) {}
 
+static void do_fragmentation(void *addr, unsigned long len)
+{
+	unsigned long offset;
+	int should_free = 1;
+
+	for (offset = 0; offset <= len - pagesize; offset += pagesize) {
+		if (should_free) {
+			if (madvise(addr + offset, pagesize,
+				MADV_DONTNEED) != 0) {
+				fprintf(stderr,
+					"msync failed with error %s\n",
+					strerror(errno));
+				exit(1);
+			}
+			should_free = 0;
+		} else
+			should_free = 1;
+	}
+}
+
 long do_units(void)
 {
 	struct drand48_data rand_data;
@@ -752,6 +775,15 @@ long do_units(void)
 		}
 	}
 
+	if (opt_fragmentation) {
+		if (prealloc)
+			do_fragmentation(prealloc, opt_bytes);
+		else {
+			for (i = 0; i < nptr; i++)
+				do_fragmentation(ptrs[i], lens[i]);
+		}
+	}
+
 	while (sleep_secs)
 		sleep_secs = sleep(sleep_secs);
 
@@ -896,6 +928,7 @@ int do_tasks(void)
 int main(int argc, char *argv[])
 {
 	int c;
+	int opt_index = 0;
 
 #ifdef DBG
 	/* print the command line parameters passed on to main */
@@ -910,9 +943,18 @@ int main(int argc, char *argv[])
 	pagesize = getpagesize();
 
 	while ((c = getopt_long(argc, argv,
-				"aAB:f:FPp:gqowRMm:n:t:b:ds:T:Sr:u:j:e:EHDNLWyxOUZh", opts, NULL)) != -1)
+				"aAB:f:FPp:gqowRMm:n:t:b:ds:T:Sr:u:j:e:EHDNLWyxOUZh",
+				opts, &opt_index)) != -1)
 		{
 		switch (c) {
+		case 0:
+			if (strcmp(opts[opt_index].name,
+				"fragmentation") == 0) {
+				opt_fragmentation = 1;
+			} else
+				usage(1);
+			break;
+
 		case 'a':
 			opt_malloc++;
 			break;
@@ -1045,6 +1087,13 @@ int main(int argc, char *argv[])
 		}
 	}
 
+	if (opt_fragmentation && opt_malloc) {
+		fprintf(stderr,
+			"%s: malloc options ignored for fragmentation\n",
+			ourname);
+		opt_malloc = 0;
+	}
+
 	if (opt_malloc) {
 		if (map_populate|map_anonymous|map_hugetlb)
 			fprintf(stderr,
-- 
2.7.4


             reply	other threads:[~2020-01-20  1:51 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-20  1:50 Hui Zhu [this message]
2020-03-03  6:25 Hui Zhu

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=1579485051-19624-1-git-send-email-teawater@gmail.com \
    --to=teawater@gmail.com \
    --cc=fengguang.wu@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=teawaterz@linux.alibaba.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

all inboxes | Powered by JetHome®