2 #include "tests/sys_mman.h"
6 /* The code testing MAP_HUGETLB huge pages is disabled by default,
7 as many distros do not have huge pages configured
9 To have e.g. 20 huge pages configured, do (as root)
10 echo 20 > /proc/sys/vm/nr_hugepages
11 Once this is done, uncomment the below, and recompile.
13 //#define TEST_MAP_HUGETLB 1
15 /* Similarly, testing SHM_HUGETLB huge pages is disabled by default.
16 To have shmget/shmat big pages working, do (as root)
17 echo 500 > /proc/sys/vm/hugetlb_shm_group
18 where 500 is the groupid of the user that runs this test
19 Once this is done, uncomment the below, and recompile.
21 //#define TEST_SHM_HUGETLB 1
23 // Size to use for huge pages
24 #define HUGESZ (4 * 1024 * 1024)
26 #ifdef TEST_MAP_HUGETLB
27 /* Ensure this compiles on pre 2.6 systems, or on glibc missing MAP_HUGETLB */
29 /* The below works for me on an f12/x86 linux */
30 #define MAP_HUGETLB 0x40000
33 #endif /* TEST_MAP_HUGETLB */
35 #ifdef TEST_SHM_HUGETLB
40 #define SHM_HUGETLB 04000
42 #endif /* TEST_SHM_HUGETLB */
44 static unsigned int pagesize
;
47 #define LEN (PAGES*pagesize)
49 static void *domap(size_t len
, int addflags
)
51 void *ret
= mmap(0, len
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
|MAP_ANONYMOUS
|addflags
, -1, 0);
53 if (ret
== (void *)-1) {
61 /* unmap in pieces to exercise munmap more */
62 static void nibblemap(void *p
)
67 off
= (random() % LEN
) & ~(pagesize
-1);
69 for(i
= 0; i
< PAGES
; i
++) {
70 /* printf("unmapping off=%d\n", off/pagesize); */
71 munmap((char *)p
+ off
, pagesize
);
80 sprintf(buf
, "/bin/cat /proc/%ld/maps", (long) getpid());
88 void *expect1
, *expect2
;
90 pagesize
= getpagesize();
92 expect1
= domap(LEN
, 0);
93 expect2
= domap(LEN
, 0);
97 for(i
= 0; i
< 5; i
++) {
102 printf("FAIL i=%d: m1=%p expect1=%p\n",
109 printf("FAIL i=%d: m2=%p expect2=%p\n",
118 #ifdef TEST_MAP_HUGETLB
121 expect3
= domap(HUGESZ
, MAP_HUGETLB
);
122 munmap(expect3
, HUGESZ
);
126 #ifdef TEST_SHM_HUGETLB
132 shmid
= shmget(IPC_PRIVATE
, HUGESZ
,
133 IPC_CREAT
| IPC_EXCL
| S_IRUSR
| S_IWUSR
| SHM_HUGETLB
);
138 expect4
= shmat(shmid
, NULL
, 0);
139 if (expect4
== (void*) -1){
143 if (shmdt(expect4
) != 0) {
147 if (shmctl(shmid
, IPC_RMID
, 0) != 0) {
148 perror("shmctl IPC_RMID");