linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] usemem: Add option touch-alloc
@ 2020-12-17  7:26 Hui Zhu
  0 siblings, 0 replies; 2+ messages in thread
From: Hui Zhu @ 2020-12-17  7:26 UTC (permalink / raw)
  To: wufengguang, linux-kernel; +Cc: Hui Zhu, Hui Zhu

Some environment will not fault in memory even if MAP_POPULATE is set.
This commit add option touch-alloc to read memory after allocate it to
make sure the pages is fault in.

Signed-off-by: Hui Zhu <teawaterz@linux.alibaba.com>
---
 usemem.c | 37 +++++++++++++++++++++++++------------
 1 file changed, 25 insertions(+), 12 deletions(-)

diff --git a/usemem.c b/usemem.c
index 6d1d575..d93691b 100644
--- a/usemem.c
+++ b/usemem.c
@@ -97,6 +97,7 @@ unsigned long opt_delay = 0;
 int opt_read_again = 0;
 int opt_punch_holes = 0;
 int opt_init_time = 0;
+int opt_touch_alloc = 0;
 int nr_task;
 int nr_thread;
 int nr_cpu;
@@ -157,6 +158,7 @@ void usage(int ok)
 	"    -Z|--read-again     read memory again after access the memory\n"
 	"    --punch-holes       free every other page after allocation\n"
 	"    --init-time         remove the initialization time from the run time and show the initialization time\n"
+	"    --touch-alloc       read memory after allocate it\n"
 	"    -h|--help           show this message\n"
 	,		ourname);
 
@@ -197,6 +199,7 @@ static const struct option opts[] = {
 	{ "read-again"	, 0, NULL, 'Z' },
 	{ "punch-holes"	, 0, NULL,   0 },
 	{ "init-time"	, 0, NULL,   0 },
+	{ "touch-alloc"	, 0, NULL,   0 },
 	{ "help"	, 0, NULL, 'h' },
 	{ NULL		, 0, NULL, 0 }
 };
@@ -326,6 +329,18 @@ void detach(void)
 	}
 }
 
+unsigned long do_access(unsigned long *p, unsigned long idx, int read)
+{
+	volatile unsigned long *vp = p;
+
+	if (read)
+		return vp[idx];	/* read data */
+	else {
+		vp[idx] = idx;	/* write data */
+		return 0;
+	}
+}
+
 unsigned long * allocate(unsigned long bytes)
 {
 	unsigned long *p;
@@ -352,6 +367,14 @@ unsigned long * allocate(unsigned long bytes)
 		p = (unsigned long *)ALIGN((unsigned long)p, pagesize - 1);
 	}
 
+	if (opt_touch_alloc) {
+		unsigned long i;
+		unsigned long m = bytes / sizeof(*p);
+
+		for (i = 0; i < m; i += 1)
+			do_access(p, i, 1);
+	}
+
 	return p;
 }
 
@@ -433,18 +456,6 @@ void shm_unlock(int seg_id)
 	shmctl(seg_id, SHM_UNLOCK, NULL);
 }
 
-unsigned long do_access(unsigned long *p, unsigned long idx, int read)
-{
-	volatile unsigned long *vp = p;
-
-	if (read)
-		return vp[idx];	/* read data */
-	else {
-		vp[idx] = idx;	/* write data */
-		return 0;
-	}
-}
-
 #define NSEC_PER_SEC  (1UL * 1000 * 1000 * 1000)
 
 long nsec_sub(long nsec1, long nsec2)
@@ -950,6 +961,8 @@ int main(int argc, char *argv[])
 				opt_punch_holes = 1;
 			} else if (strcmp(opts[opt_index].name, "init-time") == 0) { 
 				opt_init_time = 1;
+			} else if (strcmp(opts[opt_index].name, "touch-alloc") == 0) { 
+				opt_touch_alloc = 1;
 			} else
 				usage(1);
 			break;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] usemem: Add option touch-alloc
@ 2020-12-18 13:22 wufengguang
  0 siblings, 0 replies; 2+ messages in thread
From: wufengguang @ 2020-12-18 13:22 UTC (permalink / raw)
  To: Hui Zhu, linux-kernel; +Cc: Hui Zhu

Zhu Hui,

Applied both patches with minor cleanup. Thanks!

Regards,
Fengguang


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-12-18 13:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-17  7:26 [PATCH] usemem: Add option touch-alloc Hui Zhu
2020-12-18 13:22 wufengguang

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).