diff options
| author | Joern Engel <joern@logfs.org> | 2013-07-03 18:09:19 -0400 |
|---|---|---|
| committer | Linus Torvalds <torvalds@linux-foundation.org> | 2013-07-03 19:08:07 -0400 |
| commit | 7e50533d4b84289e4f01de56d6f98e9c64e2229e (patch) | |
| tree | 87ddd7bb0f6ccc41e74e2ae22f38e71ff03df152 /tools | |
| parent | fc256f04eb82abcd2bd828fee4af0c36763ffee0 (diff) | |
selftests: add hugetlbfstest
As the confusing naming indicates, this test has some overlap with
pre-existing tests. Would be nice to merge them eventually. But since it
is only test code, cleanliness is much less important than mere existence.
Signed-off-by: Joern Engel <joern@logfs.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/testing/selftests/vm/Makefile | 2 | ||||
| -rw-r--r-- | tools/testing/selftests/vm/hugetlbfstest.c | 84 | ||||
| -rw-r--r-- | tools/testing/selftests/vm/run_vmtests | 11 |
3 files changed, 96 insertions, 1 deletions
diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile index cb3f5f2e0f5f..3f94e1afd6cf 100644 --- a/tools/testing/selftests/vm/Makefile +++ b/tools/testing/selftests/vm/Makefile | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | CC = $(CROSS_COMPILE)gcc | 3 | CC = $(CROSS_COMPILE)gcc |
| 4 | CFLAGS = -Wall | 4 | CFLAGS = -Wall |
| 5 | BINARIES = hugepage-mmap hugepage-shm map_hugetlb thuge-gen | 5 | BINARIES = hugepage-mmap hugepage-shm map_hugetlb thuge-gen hugetlbfstest |
| 6 | 6 | ||
| 7 | all: $(BINARIES) | 7 | all: $(BINARIES) |
| 8 | %: %.c | 8 | %: %.c |
diff --git a/tools/testing/selftests/vm/hugetlbfstest.c b/tools/testing/selftests/vm/hugetlbfstest.c new file mode 100644 index 000000000000..ea40ff8c2391 --- /dev/null +++ b/tools/testing/selftests/vm/hugetlbfstest.c | |||
| @@ -0,0 +1,84 @@ | |||
| 1 | #define _GNU_SOURCE | ||
| 2 | #include <assert.h> | ||
| 3 | #include <fcntl.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | #include <stdlib.h> | ||
| 6 | #include <string.h> | ||
| 7 | #include <sys/mman.h> | ||
| 8 | #include <sys/stat.h> | ||
| 9 | #include <sys/types.h> | ||
| 10 | #include <unistd.h> | ||
| 11 | |||
| 12 | typedef unsigned long long u64; | ||
| 13 | |||
| 14 | static size_t length = 1 << 24; | ||
| 15 | |||
| 16 | static u64 read_rss(void) | ||
| 17 | { | ||
| 18 | char buf[4096], *s = buf; | ||
| 19 | int i, fd; | ||
| 20 | u64 rss; | ||
| 21 | |||
| 22 | fd = open("/proc/self/statm", O_RDONLY); | ||
| 23 | assert(fd > 2); | ||
| 24 | memset(buf, 0, sizeof(buf)); | ||
| 25 | read(fd, buf, sizeof(buf) - 1); | ||
| 26 | for (i = 0; i < 1; i++) | ||
| 27 | s = strchr(s, ' ') + 1; | ||
| 28 | rss = strtoull(s, NULL, 10); | ||
| 29 | return rss << 12; /* assumes 4k pagesize */ | ||
| 30 | } | ||
| 31 | |||
| 32 | static void do_mmap(int fd, int extra_flags, int unmap) | ||
| 33 | { | ||
| 34 | int *p; | ||
| 35 | int flags = MAP_PRIVATE | MAP_POPULATE | extra_flags; | ||
| 36 | u64 before, after; | ||
| 37 | |||
| 38 | before = read_rss(); | ||
| 39 | p = mmap(NULL, length, PROT_READ | PROT_WRITE, flags, fd, 0); | ||
| 40 | assert(p != MAP_FAILED || | ||
| 41 | !"mmap returned an unexpected error"); | ||
| 42 | after = read_rss(); | ||
| 43 | assert(llabs(after - before - length) < 0x40000 || | ||
| 44 | !"rss didn't grow as expected"); | ||
| 45 | if (!unmap) | ||
| 46 | return; | ||
| 47 | munmap(p, length); | ||
| 48 | after = read_rss(); | ||
| 49 | assert(llabs(after - before) < 0x40000 || | ||
| 50 | !"rss didn't shrink as expected"); | ||
| 51 | } | ||
| 52 | |||
| 53 | static int open_file(const char *path) | ||
| 54 | { | ||
| 55 | int fd, err; | ||
| 56 | |||
| 57 | unlink(path); | ||
| 58 | fd = open(path, O_CREAT | O_RDWR | O_TRUNC | O_EXCL | ||
| 59 | | O_LARGEFILE | O_CLOEXEC, 0600); | ||
| 60 | assert(fd > 2); | ||
| 61 | unlink(path); | ||
| 62 | err = ftruncate(fd, length); | ||
| 63 | assert(!err); | ||
| 64 | return fd; | ||
| 65 | } | ||
| 66 | |||
| 67 | int main(void) | ||
| 68 | { | ||
| 69 | int hugefd, fd; | ||
| 70 | |||
| 71 | fd = open_file("/dev/shm/hugetlbhog"); | ||
| 72 | hugefd = open_file("/hugepages/hugetlbhog"); | ||
| 73 | |||
| 74 | system("echo 100 > /proc/sys/vm/nr_hugepages"); | ||
| 75 | do_mmap(-1, MAP_ANONYMOUS, 1); | ||
| 76 | do_mmap(fd, 0, 1); | ||
| 77 | do_mmap(-1, MAP_ANONYMOUS | MAP_HUGETLB, 1); | ||
| 78 | do_mmap(hugefd, 0, 1); | ||
| 79 | do_mmap(hugefd, MAP_HUGETLB, 1); | ||
| 80 | /* Leak the last one to test do_exit() */ | ||
| 81 | do_mmap(-1, MAP_ANONYMOUS | MAP_HUGETLB, 0); | ||
| 82 | printf("oll korrekt.\n"); | ||
| 83 | return 0; | ||
| 84 | } | ||
diff --git a/tools/testing/selftests/vm/run_vmtests b/tools/testing/selftests/vm/run_vmtests index 7a9072d52e70..c87b6812300d 100644 --- a/tools/testing/selftests/vm/run_vmtests +++ b/tools/testing/selftests/vm/run_vmtests | |||
| @@ -75,6 +75,17 @@ else | |||
| 75 | echo "[PASS]" | 75 | echo "[PASS]" |
| 76 | fi | 76 | fi |
| 77 | 77 | ||
| 78 | echo "--------------------" | ||
| 79 | echo "running hugetlbfstest" | ||
| 80 | echo "--------------------" | ||
| 81 | ./hugetlbfstest | ||
| 82 | if [ $? -ne 0 ]; then | ||
| 83 | echo "[FAIL]" | ||
| 84 | exitcode=1 | ||
| 85 | else | ||
| 86 | echo "[PASS]" | ||
| 87 | fi | ||
| 88 | |||
| 78 | #cleanup | 89 | #cleanup |
| 79 | umount $mnt | 90 | umount $mnt |
| 80 | rm -rf $mnt | 91 | rm -rf $mnt |
