Skip to contentSkip to conversation

mmap/mmap12.c: new case to test MAP_POPULATE of mmap

PATCH In review DAN LI 4 participants 6 messages Jun 19, 2013 – Jun 20, 2013

lore
Share & tools
lore

DAN LI

author
#0 lore raw

PATCH 1/1 mmap/mmap12.c: new case to test MAP_POPULATE of mmap

Create test case for MAP_POPULATE of mmap.

Verify the following statement
"For a file mapping,this causes read-ahead on the file.
Later accesses to the mapping will not be blocked by page faults."
by mmap-ing a file with specified size and checking change of memory size.

---
 testcases/kernel/syscalls/mmap/mmap12.c | 163 ++++++++++++++++++++++++++++++++
 1 file changed, 163 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mmap/mmap12.c
testcases/kernel/syscalls/mmap/mmap12.c new file +163 -0
new file mode 100644
index 0000000..4092806
@@ -0,0 +1,163 @@
+/*
+ * Copyright (c) 2013 FNST, DAN LI <[email protected]>
+ *
+ * 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
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY;  without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+ * the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program;  if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/*
+ * Test Description:
+ *  Verify MAP_POPULATE works fine.
+ *  "For a file mapping, this causes read-ahead on the file.
+ *   Later accesses to the mapping will not be blocked by page faults"
+ *
+ * Expected Result:
+ *  mmap() with MAP_POPULATE should succeed returning the address of the
+ *  mapped region and this file has been read into RAM, so RAM should
+ *  is reduced by at least size of file.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <signal.h>
+#include <stdint.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/shm.h>
+#include <sys/sysinfo.h>
+
+#include "test.h"
+#include "usctest.h"
+
+#define TEMPFILE	"mmapfile"
+#define MMAPSIZE	(400UL<<20)
+#define FLUCTUATION	(50UL<<20)
+
+char *TCID = "mmap12";
+int TST_TOTAL = 1;
+
+static char *addr;
+static int fildes;
+static struct sysinfo info;
+static unsigned long long freeram_before;
+static unsigned long long freeram_after;
+
+static void loop_setup(void);
+static void loop_cleanup(void);
+static void setup(void);
+static void cleanup(void);
+
+int main(int argc, char *argv[])
+{
+	int lc;
+	int ret = 0;
+	char *msg;
+
+	msg = parse_opts(argc, argv, NULL, NULL);
+	if (msg != NULL)
+		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
+
+	setup();
+
+	for (lc = 0; TEST_LOOPING(lc); lc++) {
+
+		tst_count = 0;
+
+		loop_setup();
+
+		errno = 0;
+		addr = mmap(NULL, MMAPSIZE, PROT_READ | PROT_WRITE,
+			    MAP_FILE | MAP_PRIVATE | MAP_POPULATE, fildes, 0);
+
+		if (addr == MAP_FAILED) {
+			tst_resm(TFAIL | TERRNO, "mmap of %s failed", TEMPFILE);
+			continue;
+		}
+
+		if (STD_FUNCTIONAL_TEST) {
+			ret = sysinfo(&info);
+			if (ret != 0)
+				tst_brkm(TFAIL, cleanup, "sysinfo failed");
+			freeram_after =
+				(unsigned long long)info.freeram *
+				(unsigned long long)info.mem_unit;
+
+			if ((freeram_before - freeram_after) >=
+			    (MMAPSIZE - FLUCTUATION))
+				tst_resm(TPASS,
+					 "Functionality of mmap() successful");
+			else
+				tst_resm(TFAIL,
+					 "Seems read-ahead do not work");
+		}
+		loop_cleanup();
+	}
+
+	cleanup();
+	tst_exit();
+
+}
+
+static void loop_setup(void)
+{
+	int ret;
+
+	fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666);
+	if (fildes < 0)
+		tst_brkm(TFAIL, cleanup, "opening %s failed", TEMPFILE);
+
+
+	if (ftruncate(fildes, MMAPSIZE) < 0)
+		tst_brkm(TFAIL | TERRNO, cleanup, "ftruncate file failed");
+
+	ret = sysinfo(&info);
+	if (ret != 0)
+		tst_brkm(TFAIL, cleanup, "sysinfo failed");
+
+	freeram_before =
+		(unsigned long long)info.freeram *
+		(unsigned long long)info.mem_unit;
+}
+
+static void loop_cleanup(void)
+{
+	if (munmap(addr, MMAPSIZE) != 0)
+		tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");
+
+	close(fildes);
+}
+
+static void setup(void)
+{
+	tst_sig(FORK, DEF_HANDLER, cleanup);
+
+	if ((tst_kvercmp(2, 6, 23)) < 0)
+		tst_brkm(TCONF, NULL,
+			"This test can only run on kernels that are 2.6.23 and "
+			"higher");
+
+	TEST_PAUSE;
+
+	tst_tmpdir();
+}
+
+static void cleanup(void)
+{
+	TEST_CLEANUP;
+	tst_rmdir();
+}
Signature · 12 lines
1.8.1

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#1 lore raw

testcases/kernel/syscalls/mmap/mmap12.c line 98

Quoted 6 lines · DAN LI
testcases/kernel/syscalls/mmap/mmap12.c new file +6 -0
+			ret = sysinfo(&info);
+			if (ret != 0)
+				tst_brkm(TFAIL, cleanup, "sysinfo failed");
+			freeram_after =
+				(unsigned long long)info.freeram *
+				(unsigned long long)info.mem_unit;

man sysinfo:

This function is Linux-specific, and should not be used in
programs intended to be portable.

We're expecting all LTP tests are POSIX-compatible, sysinfo is Linux-specific, would it better to read the info from /proc/meminfo directly?

Thanks,
Caspar

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

#2 lore raw

Hi!

Quoted 9 lines

testcases/kernel/syscalls/mmap/mmap12.c line 98

Quoted 6 lines
testcases/kernel/syscalls/mmap/mmap12.c new file +6 -0
+			ret = sysinfo(&info);
+			if (ret != 0)
+				tst_brkm(TFAIL, cleanup, "sysinfo failed");
+			freeram_after =
+				(unsigned long long)info.freeram *
+				(unsigned long long)info.mem_unit;

man sysinfo:

Quoted 6 lines

This function is Linux-specific, and should not be used in
programs intended to be portable.

We're expecting all LTP tests are POSIX-compatible, sysinfo is Linux-specific, would it better to read the info from /proc/meminfo directly?

That is true for the Open Posix Testsuite, but this test is added under testcases/kernel/ for which it is fine.

Note also that MAP_POPULATE is itself Linux specific ;).

Signature · 13 lines
Cyril Hrubis
[email protected]

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#3 lore raw
In reply to DAN LI

Hi!

Quoted 8 lines

Create test case for MAP_POPULATE of mmap.

Verify the following statement
"For a file mapping,this causes read-ahead on the file.
Later accesses to the mapping will not be blocked by page faults."
by mmap-ing a file with specified size and checking change of memory size.

Quoted 5 lines
---
 testcases/kernel/syscalls/mmap/mmap12.c | 163 ++++++++++++++++++++++++++++++++
 1 file changed, 163 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mmap/mmap12.c

You should add runtest entry, the rest of the mmap tests are in runtests/syscalls.

Quoted 79 lines
testcases/kernel/syscalls/mmap/mmap12.c new file +73 -0
new file mode 100644
index 0000000..4092806
@@ -0,0 +1,163 @@
+/*
+ * Copyright (c) 2013 FNST, DAN LI <[email protected]>
+ *
+ * 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
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY;  without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+ * the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program;  if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/*
+ * Test Description:
+ *  Verify MAP_POPULATE works fine.
+ *  "For a file mapping, this causes read-ahead on the file.
+ *   Later accesses to the mapping will not be blocked by page faults"
+ *
+ * Expected Result:
+ *  mmap() with MAP_POPULATE should succeed returning the address of the
+ *  mapped region and this file has been read into RAM, so RAM should
+ *  is reduced by at least size of file.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <signal.h>
+#include <stdint.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/shm.h>
+#include <sys/sysinfo.h>
+
+#include "test.h"
+#include "usctest.h"
+
+#define TEMPFILE	"mmapfile"
+#define MMAPSIZE	(400UL<<20)
+#define FLUCTUATION	(50UL<<20)
+
+char *TCID = "mmap12";
+int TST_TOTAL = 1;
+
+static char *addr;
+static int fildes;
+static struct sysinfo info;
+static unsigned long long freeram_before;
+static unsigned long long freeram_after;
+
+static void loop_setup(void);
+static void loop_cleanup(void);
+static void setup(void);
+static void cleanup(void);
+
+int main(int argc, char *argv[])
+{
+	int lc;
+	int ret = 0;
+	char *msg;
+
+	msg = parse_opts(argc, argv, NULL, NULL);
+	if (msg != NULL)
+		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);

testcases/kernel/syscalls/mmap/mmap12.c line 83

Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap12.c new file +10 -0
+
+	setup();
+
+	for (lc = 0; TEST_LOOPING(lc); lc++) {
+
+		tst_count = 0;
+
+		loop_setup();
+
+		errno = 0;

There is no need to zero the errno as you use it only when mmap() failed, right?

Quoted 33 lines
testcases/kernel/syscalls/mmap/mmap12.c new file +33 -0
+		addr = mmap(NULL, MMAPSIZE, PROT_READ | PROT_WRITE,
+			    MAP_FILE | MAP_PRIVATE | MAP_POPULATE, fildes, 0);
+
+		if (addr == MAP_FAILED) {
+			tst_resm(TFAIL | TERRNO, "mmap of %s failed", TEMPFILE);
+			continue;
+		}
+
+		if (STD_FUNCTIONAL_TEST) {
+			ret = sysinfo(&info);
+			if (ret != 0)
+				tst_brkm(TFAIL, cleanup, "sysinfo failed");
+			freeram_after =
+				(unsigned long long)info.freeram *
+				(unsigned long long)info.mem_unit;
+
+			if ((freeram_before - freeram_after) >=
+			    (MMAPSIZE - FLUCTUATION))
+				tst_resm(TPASS,
+					 "Functionality of mmap() successful");
+			else
+				tst_resm(TFAIL,
+					 "Seems read-ahead do not work");
+		}
+		loop_cleanup();
+	}
+
+	cleanup();
+	tst_exit();
+
+}
+
+static void loop_setup(void)

testcases/kernel/syscalls/mmap/mmap12.c line 126

Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap12.c new file +10 -0
+{
+	int ret;
+
+	fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666);
+	if (fildes < 0)
+		tst_brkm(TFAIL, cleanup, "opening %s failed", TEMPFILE);
+
+
+	if (ftruncate(fildes, MMAPSIZE) < 0)
+		tst_brkm(TFAIL | TERRNO, cleanup, "ftruncate file failed");

This part could be done once in the setup.

Quoted 36 lines
testcases/kernel/syscalls/mmap/mmap12.c new file +36 -0
+	ret = sysinfo(&info);
+	if (ret != 0)
+		tst_brkm(TFAIL, cleanup, "sysinfo failed");
+
+	freeram_before =
+		(unsigned long long)info.freeram *
+		(unsigned long long)info.mem_unit;
+}
+
+static void loop_cleanup(void)
+{
+	if (munmap(addr, MMAPSIZE) != 0)
+		tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");
+
+	close(fildes);
+}
+
+static void setup(void)
+{
+	tst_sig(FORK, DEF_HANDLER, cleanup);
+
+	if ((tst_kvercmp(2, 6, 23)) < 0)
+		tst_brkm(TCONF, NULL,
+			"This test can only run on kernels that are 2.6.23 and "
+			"higher");
+
+	TEST_PAUSE;
+
+	tst_tmpdir();
+}
+
+static void cleanup(void)
+{
+	TEST_CLEANUP;
+	tst_rmdir();
+}
Signature · 13 lines
Cyril Hrubis
[email protected]

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#4 lore raw
In reply to DAN LI

----- Original Message -----

Quoted 8 lines

From: "DAN LI" <[email protected]>
To: "LTP list" <[email protected]>
Sent: Wednesday, 19 June, 2013 8:52:43 AM
Subject: [LTP] [PATCH] mmap/mmap12.c: new case to test MAP_POPULATE of mmap

Create test case for MAP_POPULATE of mmap.

Quoted 7 lines

Verify the following statement
"For a file mapping,this causes read-ahead on the file.
Later accesses to the mapping will not be blocked by page faults."
by mmap-ing a file with specified size and checking change of memory size.

Hi,

I think this isn't very reliable when you are low on free memory. I could get it to fail easily just by reading random data to fill cache:

# free -m
             total       used       free     shared    buffers     cached
Mem:           994        704        289          0          0        525
-/+ buffers/cache:        179        814
Swap:         2015          0       2015
# ./mmap12 
mmap12      1  TFAIL  :  Seems read-ahead do not work

Dropping caches after you create test file might not be bad idea:
system("echo 1 > /proc/sys/vm/drop_caches");
because I think sysinfo() sees that as used memory.
And also making sure there is enough free mem (say 2x of testfile)

It's still approximate, but I'm not sure if there's better way, considering other options:
reading /proc/pid/io for read_bytes
reading /proc/pid/pagemap and checking that those pages are present

Regards,
Jan

Quoted 183 lines
---
 testcases/kernel/syscalls/mmap/mmap12.c | 163
 ++++++++++++++++++++++++++++++++
 1 file changed, 163 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mmap/mmap12.c
testcases/kernel/syscalls/mmap/mmap12.c new file +16 -0
new file mode 100644
index 0000000..4092806
@@ -0,0 +1,163 @@
+/*
+ * Copyright (c) 2013 FNST, DAN LI <[email protected]>
+ *
+ * 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
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY;  without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+ * the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program;  if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301

USA
+ */

testcases/kernel/syscalls/mmap/mmap12.c new file +146 -1
+
+/*
+ * Test Description:
+ *  Verify MAP_POPULATE works fine.
+ *  "For a file mapping, this causes read-ahead on the file.
+ *   Later accesses to the mapping will not be blocked by page faults"
+ *
+ * Expected Result:
+ *  mmap() with MAP_POPULATE should succeed returning the address of the
+ *  mapped region and this file has been read into RAM, so RAM should
+ *  is reduced by at least size of file.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <signal.h>
+#include <stdint.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/shm.h>
+#include <sys/sysinfo.h>
+
+#include "test.h"
+#include "usctest.h"
+
+#define TEMPFILE	"mmapfile"
+#define MMAPSIZE	(400UL<<20)
+#define FLUCTUATION	(50UL<<20)
+
+char *TCID = "mmap12";
+int TST_TOTAL = 1;
+
+static char *addr;
+static int fildes;
+static struct sysinfo info;
+static unsigned long long freeram_before;
+static unsigned long long freeram_after;
+
+static void loop_setup(void);
+static void loop_cleanup(void);
+static void setup(void);
+static void cleanup(void);
+
+int main(int argc, char *argv[])
+{
+	int lc;
+	int ret = 0;
+	char *msg;
+
+	msg = parse_opts(argc, argv, NULL, NULL);
+	if (msg != NULL)
+		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
+
+	setup();
+
+	for (lc = 0; TEST_LOOPING(lc); lc++) {
+
+		tst_count = 0;
+
+		loop_setup();
+
+		errno = 0;
+		addr = mmap(NULL, MMAPSIZE, PROT_READ | PROT_WRITE,
+			    MAP_FILE | MAP_PRIVATE | MAP_POPULATE, fildes, 0);
+
+		if (addr == MAP_FAILED) {
+			tst_resm(TFAIL | TERRNO, "mmap of %s failed", TEMPFILE);
+			continue;
+		}
+
+		if (STD_FUNCTIONAL_TEST) {
+			ret = sysinfo(&info);
+			if (ret != 0)
+				tst_brkm(TFAIL, cleanup, "sysinfo failed");
+			freeram_after =
+				(unsigned long long)info.freeram *
+				(unsigned long long)info.mem_unit;
+
+			if ((freeram_before - freeram_after) >=
+			    (MMAPSIZE - FLUCTUATION))
+				tst_resm(TPASS,
+					 "Functionality of mmap() successful");
+			else
+				tst_resm(TFAIL,
+					 "Seems read-ahead do not work");
+		}
+		loop_cleanup();
+	}
+
+	cleanup();
+	tst_exit();
+
+}
+
+static void loop_setup(void)
+{
+	int ret;
+
+	fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666);
+	if (fildes < 0)
+		tst_brkm(TFAIL, cleanup, "opening %s failed", TEMPFILE);
+
+
+	if (ftruncate(fildes, MMAPSIZE) < 0)
+		tst_brkm(TFAIL | TERRNO, cleanup, "ftruncate file failed");
+
+	ret = sysinfo(&info);
+	if (ret != 0)
+		tst_brkm(TFAIL, cleanup, "sysinfo failed");
+
+	freeram_before =
+		(unsigned long long)info.freeram *
+		(unsigned long long)info.mem_unit;
+}
+
+static void loop_cleanup(void)
+{
+	if (munmap(addr, MMAPSIZE) != 0)
+		tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");
+
+	close(fildes);
+}
+
+static void setup(void)
+{
+	tst_sig(FORK, DEF_HANDLER, cleanup);
+
+	if ((tst_kvercmp(2, 6, 23)) < 0)
+		tst_brkm(TCONF, NULL,
+			"This test can only run on kernels that are 2.6.23 and "
+			"higher");
+
+	TEST_PAUSE;
+
+	tst_tmpdir();
+}
+
+static void cleanup(void)
+{
+	TEST_CLEANUP;
+	tst_rmdir();
+}
--

1.8.1

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Quoted 8 lines

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

author
#5 lore raw
Quoted 30 lines · Jan Stancek

----- Original Message -----

Quoted 8 lines

From: "DAN LI" <[email protected]>
To: "LTP list" <[email protected]>
Sent: Wednesday, 19 June, 2013 8:52:43 AM
Subject: [LTP] [PATCH] mmap/mmap12.c: new case to test MAP_POPULATE of mmap

Create test case for MAP_POPULATE of mmap.

Quoted 7 lines

Verify the following statement
"For a file mapping,this causes read-ahead on the file.
Later accesses to the mapping will not be blocked by page faults."
by mmap-ing a file with specified size and checking change of memory size.

Hi,

I think this isn't very reliable when you are low on free memory. I could get it to fail easily just by reading random data to fill cache:

# free -m
             total       used       free     shared    buffers     cached
Mem:           994        704        289          0          0        525
-/+ buffers/cache:        179        814
Swap:         2015          0       2015
Quoted 8 lines
# ./mmap12 
mmap12      1  TFAIL  :  Seems read-ahead do not work

Dropping caches after you create test file might not be bad idea:
system("echo 1 > /proc/sys/vm/drop_caches");
because I think sysinfo() sees that as used memory.
And also making sure there is enough free mem (say 2x of testfile)

Maybe i could cut the length of the mapping, and as you suggested, drop caches, check if there is enough free mem before testing, to make it more reliable.

Quoted 5 lines

It's still approximate, but I'm not sure if there's better way, considering other options:
reading /proc/pid/io for read_bytes
reading /proc/pid/pagemap and checking that those pages are present

I'll try this first. :)

Regards,
DAN LI

Quoted 187 lines

Regards,
Jan

Quoted 183 lines
---
 testcases/kernel/syscalls/mmap/mmap12.c | 163
 ++++++++++++++++++++++++++++++++
 1 file changed, 163 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mmap/mmap12.c
testcases/kernel/syscalls/mmap/mmap12.c new file +16 -0
new file mode 100644
index 0000000..4092806
@@ -0,0 +1,163 @@
+/*
+ * Copyright (c) 2013 FNST, DAN LI <[email protected]>
+ *
+ * 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
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY;  without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+ * the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program;  if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301

USA
+ */

testcases/kernel/syscalls/mmap/mmap12.c new file +146 -1
+
+/*
+ * Test Description:
+ *  Verify MAP_POPULATE works fine.
+ *  "For a file mapping, this causes read-ahead on the file.
+ *   Later accesses to the mapping will not be blocked by page faults"
+ *
+ * Expected Result:
+ *  mmap() with MAP_POPULATE should succeed returning the address of the
+ *  mapped region and this file has been read into RAM, so RAM should
+ *  is reduced by at least size of file.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <signal.h>
+#include <stdint.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/shm.h>
+#include <sys/sysinfo.h>
+
+#include "test.h"
+#include "usctest.h"
+
+#define TEMPFILE	"mmapfile"
+#define MMAPSIZE	(400UL<<20)
+#define FLUCTUATION	(50UL<<20)
+
+char *TCID = "mmap12";
+int TST_TOTAL = 1;
+
+static char *addr;
+static int fildes;
+static struct sysinfo info;
+static unsigned long long freeram_before;
+static unsigned long long freeram_after;
+
+static void loop_setup(void);
+static void loop_cleanup(void);
+static void setup(void);
+static void cleanup(void);
+
+int main(int argc, char *argv[])
+{
+	int lc;
+	int ret = 0;
+	char *msg;
+
+	msg = parse_opts(argc, argv, NULL, NULL);
+	if (msg != NULL)
+		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
+
+	setup();
+
+	for (lc = 0; TEST_LOOPING(lc); lc++) {
+
+		tst_count = 0;
+
+		loop_setup();
+
+		errno = 0;
+		addr = mmap(NULL, MMAPSIZE, PROT_READ | PROT_WRITE,
+			    MAP_FILE | MAP_PRIVATE | MAP_POPULATE, fildes, 0);
+
+		if (addr == MAP_FAILED) {
+			tst_resm(TFAIL | TERRNO, "mmap of %s failed", TEMPFILE);
+			continue;
+		}
+
+		if (STD_FUNCTIONAL_TEST) {
+			ret = sysinfo(&info);
+			if (ret != 0)
+				tst_brkm(TFAIL, cleanup, "sysinfo failed");
+			freeram_after =
+				(unsigned long long)info.freeram *
+				(unsigned long long)info.mem_unit;
+
+			if ((freeram_before - freeram_after) >=
+			    (MMAPSIZE - FLUCTUATION))
+				tst_resm(TPASS,
+					 "Functionality of mmap() successful");
+			else
+				tst_resm(TFAIL,
+					 "Seems read-ahead do not work");
+		}
+		loop_cleanup();
+	}
+
+	cleanup();
+	tst_exit();
+
+}
+
+static void loop_setup(void)
+{
+	int ret;
+
+	fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666);
+	if (fildes < 0)
+		tst_brkm(TFAIL, cleanup, "opening %s failed", TEMPFILE);
+
+
+	if (ftruncate(fildes, MMAPSIZE) < 0)
+		tst_brkm(TFAIL | TERRNO, cleanup, "ftruncate file failed");
+
+	ret = sysinfo(&info);
+	if (ret != 0)
+		tst_brkm(TFAIL, cleanup, "sysinfo failed");
+
+	freeram_before =
+		(unsigned long long)info.freeram *
+		(unsigned long long)info.mem_unit;
+}
+
+static void loop_cleanup(void)
+{
+	if (munmap(addr, MMAPSIZE) != 0)
+		tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");
+
+	close(fildes);
+}
+
+static void setup(void)
+{
+	tst_sig(FORK, DEF_HANDLER, cleanup);
+
+	if ((tst_kvercmp(2, 6, 23)) < 0)
+		tst_brkm(TCONF, NULL,
+			"This test can only run on kernels that are 2.6.23 and "
+			"higher");
+
+	TEST_PAUSE;
+
+	tst_tmpdir();
+}
+
+static void cleanup(void)
+{
+	TEST_CLEANUP;
+	tst_rmdir();
+}
--

1.8.1

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Quoted 9 lines
Quoted 8 lines

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list