diff options
author | Dave Young <dyoung@redhat.com> | 2012-03-28 17:42:56 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-03-28 20:14:37 -0400 |
commit | f0f57b2b1488251970c25deea0ea150a8d0911ed (patch) | |
tree | 6a1021691892c6bfa6c7471861b87eb72cd1be1a /tools/testing/selftests | |
parent | 63e315535abe0d820d0e3db4c06bc5de74aeefc8 (diff) |
mm: move hugepage test examples to tools/testing/selftests/vm
hugepage-mmap.c, hugepage-shm.c and map_hugetlb.c in Documentation/vm are
simple pass/fail tests, It's better to promote them to
tools/testing/selftests.
Thanks suggestion of Andrew Morton about this. They all need firstly
setting up proper nr_hugepages and hugepage-mmap need to mount hugetlbfs.
So I add a shell script run_vmtests to do such work which will call the
three test programs and check the return value of them.
Changes to original code including below:
a. add run_vmtests script
b. return error when read_bytes mismatch with writed bytes.
c. coding style fixes: do not use assignment in if condition
[akpm@linux-foundation.org: build the targets before trying to execute them]
[akpm@linux-foundation.org: Documentation/vm/ no longer has a Makefile. Fixes "make clean"]
Signed-off-by: Dave Young <dyoung@redhat.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: Pekka Enberg <penberg@cs.helsinki.fi>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'tools/testing/selftests')
-rw-r--r-- | tools/testing/selftests/Makefile | 2 | ||||
-rw-r--r-- | tools/testing/selftests/vm/Makefile | 14 | ||||
-rw-r--r-- | tools/testing/selftests/vm/hugepage-mmap.c | 92 | ||||
-rw-r--r-- | tools/testing/selftests/vm/hugepage-shm.c | 100 | ||||
-rw-r--r-- | tools/testing/selftests/vm/map_hugetlb.c | 79 | ||||
-rw-r--r-- | tools/testing/selftests/vm/run_vmtests | 77 |
6 files changed, 363 insertions, 1 deletions
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 9203cd77fc33..28bc57ee757c 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile | |||
@@ -1,4 +1,4 @@ | |||
1 | TARGETS = breakpoints | 1 | TARGETS = breakpoints vm |
2 | 2 | ||
3 | all: | 3 | all: |
4 | for TARGET in $(TARGETS); do \ | 4 | for TARGET in $(TARGETS); do \ |
diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile new file mode 100644 index 000000000000..b336b24aa6c0 --- /dev/null +++ b/tools/testing/selftests/vm/Makefile | |||
@@ -0,0 +1,14 @@ | |||
1 | # Makefile for vm selftests | ||
2 | |||
3 | CC = $(CROSS_COMPILE)gcc | ||
4 | CFLAGS = -Wall -Wextra | ||
5 | |||
6 | all: hugepage-mmap hugepage-shm map_hugetlb | ||
7 | %: %.c | ||
8 | $(CC) $(CFLAGS) -o $@ $^ | ||
9 | |||
10 | run_tests: all | ||
11 | /bin/sh ./run_vmtests | ||
12 | |||
13 | clean: | ||
14 | $(RM) hugepage-mmap hugepage-shm map_hugetlb | ||
diff --git a/tools/testing/selftests/vm/hugepage-mmap.c b/tools/testing/selftests/vm/hugepage-mmap.c new file mode 100644 index 000000000000..a10f310d2362 --- /dev/null +++ b/tools/testing/selftests/vm/hugepage-mmap.c | |||
@@ -0,0 +1,92 @@ | |||
1 | /* | ||
2 | * hugepage-mmap: | ||
3 | * | ||
4 | * Example of using huge page memory in a user application using the mmap | ||
5 | * system call. Before running this application, make sure that the | ||
6 | * administrator has mounted the hugetlbfs filesystem (on some directory | ||
7 | * like /mnt) using the command mount -t hugetlbfs nodev /mnt. In this | ||
8 | * example, the app is requesting memory of size 256MB that is backed by | ||
9 | * huge pages. | ||
10 | * | ||
11 | * For the ia64 architecture, the Linux kernel reserves Region number 4 for | ||
12 | * huge pages. That means that if one requires a fixed address, a huge page | ||
13 | * aligned address starting with 0x800000... will be required. If a fixed | ||
14 | * address is not required, the kernel will select an address in the proper | ||
15 | * range. | ||
16 | * Other architectures, such as ppc64, i386 or x86_64 are not so constrained. | ||
17 | */ | ||
18 | |||
19 | #include <stdlib.h> | ||
20 | #include <stdio.h> | ||
21 | #include <unistd.h> | ||
22 | #include <sys/mman.h> | ||
23 | #include <fcntl.h> | ||
24 | |||
25 | #define FILE_NAME "huge/hugepagefile" | ||
26 | #define LENGTH (256UL*1024*1024) | ||
27 | #define PROTECTION (PROT_READ | PROT_WRITE) | ||
28 | |||
29 | /* Only ia64 requires this */ | ||
30 | #ifdef __ia64__ | ||
31 | #define ADDR (void *)(0x8000000000000000UL) | ||
32 | #define FLAGS (MAP_SHARED | MAP_FIXED) | ||
33 | #else | ||
34 | #define ADDR (void *)(0x0UL) | ||
35 | #define FLAGS (MAP_SHARED) | ||
36 | #endif | ||
37 | |||
38 | static void check_bytes(char *addr) | ||
39 | { | ||
40 | printf("First hex is %x\n", *((unsigned int *)addr)); | ||
41 | } | ||
42 | |||
43 | static void write_bytes(char *addr) | ||
44 | { | ||
45 | unsigned long i; | ||
46 | |||
47 | for (i = 0; i < LENGTH; i++) | ||
48 | *(addr + i) = (char)i; | ||
49 | } | ||
50 | |||
51 | static int read_bytes(char *addr) | ||
52 | { | ||
53 | unsigned long i; | ||
54 | |||
55 | check_bytes(addr); | ||
56 | for (i = 0; i < LENGTH; i++) | ||
57 | if (*(addr + i) != (char)i) { | ||
58 | printf("Mismatch at %lu\n", i); | ||
59 | return 1; | ||
60 | } | ||
61 | return 0; | ||
62 | } | ||
63 | |||
64 | int main(void) | ||
65 | { | ||
66 | void *addr; | ||
67 | int fd, ret; | ||
68 | |||
69 | fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755); | ||
70 | if (fd < 0) { | ||
71 | perror("Open failed"); | ||
72 | exit(1); | ||
73 | } | ||
74 | |||
75 | addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, fd, 0); | ||
76 | if (addr == MAP_FAILED) { | ||
77 | perror("mmap"); | ||
78 | unlink(FILE_NAME); | ||
79 | exit(1); | ||
80 | } | ||
81 | |||
82 | printf("Returned address is %p\n", addr); | ||
83 | check_bytes(addr); | ||
84 | write_bytes(addr); | ||
85 | ret = read_bytes(addr); | ||
86 | |||
87 | munmap(addr, LENGTH); | ||
88 | close(fd); | ||
89 | unlink(FILE_NAME); | ||
90 | |||
91 | return ret; | ||
92 | } | ||
diff --git a/tools/testing/selftests/vm/hugepage-shm.c b/tools/testing/selftests/vm/hugepage-shm.c new file mode 100644 index 000000000000..0d0ef4fc0c04 --- /dev/null +++ b/tools/testing/selftests/vm/hugepage-shm.c | |||
@@ -0,0 +1,100 @@ | |||
1 | /* | ||
2 | * hugepage-shm: | ||
3 | * | ||
4 | * Example of using huge page memory in a user application using Sys V shared | ||
5 | * memory system calls. In this example the app is requesting 256MB of | ||
6 | * memory that is backed by huge pages. The application uses the flag | ||
7 | * SHM_HUGETLB in the shmget system call to inform the kernel that it is | ||
8 | * requesting huge pages. | ||
9 | * | ||
10 | * For the ia64 architecture, the Linux kernel reserves Region number 4 for | ||
11 | * huge pages. That means that if one requires a fixed address, a huge page | ||
12 | * aligned address starting with 0x800000... will be required. If a fixed | ||
13 | * address is not required, the kernel will select an address in the proper | ||
14 | * range. | ||
15 | * Other architectures, such as ppc64, i386 or x86_64 are not so constrained. | ||
16 | * | ||
17 | * Note: The default shared memory limit is quite low on many kernels, | ||
18 | * you may need to increase it via: | ||
19 | * | ||
20 | * echo 268435456 > /proc/sys/kernel/shmmax | ||
21 | * | ||
22 | * This will increase the maximum size per shared memory segment to 256MB. | ||
23 | * The other limit that you will hit eventually is shmall which is the | ||
24 | * total amount of shared memory in pages. To set it to 16GB on a system | ||
25 | * with a 4kB pagesize do: | ||
26 | * | ||
27 | * echo 4194304 > /proc/sys/kernel/shmall | ||
28 | */ | ||
29 | |||
30 | #include <stdlib.h> | ||
31 | #include <stdio.h> | ||
32 | #include <sys/types.h> | ||
33 | #include <sys/ipc.h> | ||
34 | #include <sys/shm.h> | ||
35 | #include <sys/mman.h> | ||
36 | |||
37 | #ifndef SHM_HUGETLB | ||
38 | #define SHM_HUGETLB 04000 | ||
39 | #endif | ||
40 | |||
41 | #define LENGTH (256UL*1024*1024) | ||
42 | |||
43 | #define dprintf(x) printf(x) | ||
44 | |||
45 | /* Only ia64 requires this */ | ||
46 | #ifdef __ia64__ | ||
47 | #define ADDR (void *)(0x8000000000000000UL) | ||
48 | #define SHMAT_FLAGS (SHM_RND) | ||
49 | #else | ||
50 | #define ADDR (void *)(0x0UL) | ||
51 | #define SHMAT_FLAGS (0) | ||
52 | #endif | ||
53 | |||
54 | int main(void) | ||
55 | { | ||
56 | int shmid; | ||
57 | unsigned long i; | ||
58 | char *shmaddr; | ||
59 | |||
60 | shmid = shmget(2, LENGTH, SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W); | ||
61 | if (shmid < 0) { | ||
62 | perror("shmget"); | ||
63 | exit(1); | ||
64 | } | ||
65 | printf("shmid: 0x%x\n", shmid); | ||
66 | |||
67 | shmaddr = shmat(shmid, ADDR, SHMAT_FLAGS); | ||
68 | if (shmaddr == (char *)-1) { | ||
69 | perror("Shared memory attach failure"); | ||
70 | shmctl(shmid, IPC_RMID, NULL); | ||
71 | exit(2); | ||
72 | } | ||
73 | printf("shmaddr: %p\n", shmaddr); | ||
74 | |||
75 | dprintf("Starting the writes:\n"); | ||
76 | for (i = 0; i < LENGTH; i++) { | ||
77 | shmaddr[i] = (char)(i); | ||
78 | if (!(i % (1024 * 1024))) | ||
79 | dprintf("."); | ||
80 | } | ||
81 | dprintf("\n"); | ||
82 | |||
83 | dprintf("Starting the Check..."); | ||
84 | for (i = 0; i < LENGTH; i++) | ||
85 | if (shmaddr[i] != (char)i) { | ||
86 | printf("\nIndex %lu mismatched\n", i); | ||
87 | exit(3); | ||
88 | } | ||
89 | dprintf("Done.\n"); | ||
90 | |||
91 | if (shmdt((const void *)shmaddr) != 0) { | ||
92 | perror("Detach failure"); | ||
93 | shmctl(shmid, IPC_RMID, NULL); | ||
94 | exit(4); | ||
95 | } | ||
96 | |||
97 | shmctl(shmid, IPC_RMID, NULL); | ||
98 | |||
99 | return 0; | ||
100 | } | ||
diff --git a/tools/testing/selftests/vm/map_hugetlb.c b/tools/testing/selftests/vm/map_hugetlb.c new file mode 100644 index 000000000000..ac56639dd4a9 --- /dev/null +++ b/tools/testing/selftests/vm/map_hugetlb.c | |||
@@ -0,0 +1,79 @@ | |||
1 | /* | ||
2 | * Example of using hugepage memory in a user application using the mmap | ||
3 | * system call with MAP_HUGETLB flag. Before running this program make | ||
4 | * sure the administrator has allocated enough default sized huge pages | ||
5 | * to cover the 256 MB allocation. | ||
6 | * | ||
7 | * For ia64 architecture, Linux kernel reserves Region number 4 for hugepages. | ||
8 | * That means the addresses starting with 0x800000... will need to be | ||
9 | * specified. Specifying a fixed address is not required on ppc64, i386 | ||
10 | * or x86_64. | ||
11 | */ | ||
12 | #include <stdlib.h> | ||
13 | #include <stdio.h> | ||
14 | #include <unistd.h> | ||
15 | #include <sys/mman.h> | ||
16 | #include <fcntl.h> | ||
17 | |||
18 | #define LENGTH (256UL*1024*1024) | ||
19 | #define PROTECTION (PROT_READ | PROT_WRITE) | ||
20 | |||
21 | #ifndef MAP_HUGETLB | ||
22 | #define MAP_HUGETLB 0x40000 /* arch specific */ | ||
23 | #endif | ||
24 | |||
25 | /* Only ia64 requires this */ | ||
26 | #ifdef __ia64__ | ||
27 | #define ADDR (void *)(0x8000000000000000UL) | ||
28 | #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_FIXED) | ||
29 | #else | ||
30 | #define ADDR (void *)(0x0UL) | ||
31 | #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB) | ||
32 | #endif | ||
33 | |||
34 | static void check_bytes(char *addr) | ||
35 | { | ||
36 | printf("First hex is %x\n", *((unsigned int *)addr)); | ||
37 | } | ||
38 | |||
39 | static void write_bytes(char *addr) | ||
40 | { | ||
41 | unsigned long i; | ||
42 | |||
43 | for (i = 0; i < LENGTH; i++) | ||
44 | *(addr + i) = (char)i; | ||
45 | } | ||
46 | |||
47 | static int read_bytes(char *addr) | ||
48 | { | ||
49 | unsigned long i; | ||
50 | |||
51 | check_bytes(addr); | ||
52 | for (i = 0; i < LENGTH; i++) | ||
53 | if (*(addr + i) != (char)i) { | ||
54 | printf("Mismatch at %lu\n", i); | ||
55 | return 1; | ||
56 | } | ||
57 | return 0; | ||
58 | } | ||
59 | |||
60 | int main(void) | ||
61 | { | ||
62 | void *addr; | ||
63 | int ret; | ||
64 | |||
65 | addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, 0, 0); | ||
66 | if (addr == MAP_FAILED) { | ||
67 | perror("mmap"); | ||
68 | exit(1); | ||
69 | } | ||
70 | |||
71 | printf("Returned address is %p\n", addr); | ||
72 | check_bytes(addr); | ||
73 | write_bytes(addr); | ||
74 | ret = read_bytes(addr); | ||
75 | |||
76 | munmap(addr, LENGTH); | ||
77 | |||
78 | return ret; | ||
79 | } | ||
diff --git a/tools/testing/selftests/vm/run_vmtests b/tools/testing/selftests/vm/run_vmtests new file mode 100644 index 000000000000..8b40bd5e5cc2 --- /dev/null +++ b/tools/testing/selftests/vm/run_vmtests | |||
@@ -0,0 +1,77 @@ | |||
1 | #!/bin/bash | ||
2 | #please run as root | ||
3 | |||
4 | #we need 256M, below is the size in kB | ||
5 | needmem=262144 | ||
6 | mnt=./huge | ||
7 | |||
8 | #get pagesize and freepages from /proc/meminfo | ||
9 | while read name size unit; do | ||
10 | if [ "$name" = "HugePages_Free:" ]; then | ||
11 | freepgs=$size | ||
12 | fi | ||
13 | if [ "$name" = "Hugepagesize:" ]; then | ||
14 | pgsize=$size | ||
15 | fi | ||
16 | done < /proc/meminfo | ||
17 | |||
18 | #set proper nr_hugepages | ||
19 | if [ -n "$freepgs" ] && [ -n "$pgsize" ]; then | ||
20 | nr_hugepgs=`cat /proc/sys/vm/nr_hugepages` | ||
21 | needpgs=`expr $needmem / $pgsize` | ||
22 | if [ $freepgs -lt $needpgs ]; then | ||
23 | lackpgs=$(( $needpgs - $freepgs )) | ||
24 | echo $(( $lackpgs + $nr_hugepgs )) > /proc/sys/vm/nr_hugepages | ||
25 | if [ $? -ne 0 ]; then | ||
26 | echo "Please run this test as root" | ||
27 | exit 1 | ||
28 | fi | ||
29 | fi | ||
30 | else | ||
31 | echo "no hugetlbfs support in kernel?" | ||
32 | exit 1 | ||
33 | fi | ||
34 | |||
35 | mkdir $mnt | ||
36 | mount -t hugetlbfs none $mnt | ||
37 | |||
38 | echo "--------------------" | ||
39 | echo "runing hugepage-mmap" | ||
40 | echo "--------------------" | ||
41 | ./hugepage-mmap | ||
42 | if [ $? -ne 0 ]; then | ||
43 | echo "[FAIL]" | ||
44 | else | ||
45 | echo "[PASS]" | ||
46 | fi | ||
47 | |||
48 | shmmax=`cat /proc/sys/kernel/shmmax` | ||
49 | shmall=`cat /proc/sys/kernel/shmall` | ||
50 | echo 268435456 > /proc/sys/kernel/shmmax | ||
51 | echo 4194304 > /proc/sys/kernel/shmall | ||
52 | echo "--------------------" | ||
53 | echo "runing hugepage-shm" | ||
54 | echo "--------------------" | ||
55 | ./hugepage-shm | ||
56 | if [ $? -ne 0 ]; then | ||
57 | echo "[FAIL]" | ||
58 | else | ||
59 | echo "[PASS]" | ||
60 | fi | ||
61 | echo $shmmax > /proc/sys/kernel/shmmax | ||
62 | echo $shmall > /proc/sys/kernel/shmall | ||
63 | |||
64 | echo "--------------------" | ||
65 | echo "runing map_hugetlb" | ||
66 | echo "--------------------" | ||
67 | ./map_hugetlb | ||
68 | if [ $? -ne 0 ]; then | ||
69 | echo "[FAIL]" | ||
70 | else | ||
71 | echo "[PASS]" | ||
72 | fi | ||
73 | |||
74 | #cleanup | ||
75 | umount $mnt | ||
76 | rm -rf $mnt | ||
77 | echo $nr_hugepgs > /proc/sys/vm/nr_hugepages | ||