1 /* Test selecting other page sizes for mmap/shmget.
3 Before running this huge pages for each huge page size must have been
5 For large pages beyond MAX_ORDER (like 1GB on x86) boot options must be used.
6 Also shmmax must be increased.
7 And you need to run as root to work around some weird permissions in shm.
8 And nothing using huge pages should run in parallel.
9 When the program aborts you may need to clean up the shm segments with
10 ipcrm -m by hand, like this
11 sudo ipcs | awk '$1 == "0x00000000" {print $2}' | xargs -n1 sudo ipcrm -m
12 (warning this will remove all if someone else uses them) */
27 #define err(x) perror(x), exit(1)
29 #define MAP_HUGE_2MB (21 << MAP_HUGE_SHIFT)
30 #define MAP_HUGE_1GB (30 << MAP_HUGE_SHIFT)
31 #define MAP_HUGE_SHIFT 26
32 #define MAP_HUGE_MASK 0x3f
33 #define MAP_HUGETLB 0x40000
35 #define SHM_HUGETLB 04000 /* segment will use huge TLB pages */
36 #define SHM_HUGE_SHIFT 26
37 #define SHM_HUGE_MASK 0x3f
38 #define SHM_HUGE_2MB (21 << SHM_HUGE_SHIFT)
39 #define SHM_HUGE_1GB (30 << SHM_HUGE_SHIFT)
41 #define NUM_PAGESIZES 5
45 #define Dprintf(fmt...) // printf(fmt)
47 unsigned long page_sizes
[NUM_PAGESIZES
];
50 int ilog2(unsigned long v
)
53 while ((1UL << l
) < v
)
58 void find_pagesizes(void)
62 glob("/sys/kernel/mm/hugepages/hugepages-*kB", 0, NULL
, &g
);
63 assert(g
.gl_pathc
<= NUM_PAGESIZES
);
64 for (i
= 0; i
< g
.gl_pathc
; i
++) {
65 sscanf(g
.gl_pathv
[i
], "/sys/kernel/mm/hugepages/hugepages-%lukB",
68 printf("Found %luMB\n", page_sizes
[i
] >> 20);
70 num_page_sizes
= g
.gl_pathc
;
74 unsigned long default_huge_page_size(void)
76 unsigned long hps
= 0;
79 FILE *f
= fopen("/proc/meminfo", "r");
82 while (getline(&line
, &linelen
, f
) > 0) {
83 if (sscanf(line
, "Hugepagesize: %lu kB", &hps
) == 1) {
92 void show(unsigned long ps
)
95 if (ps
== getpagesize())
97 printf("%luMB: ", ps
>> 20);
99 snprintf(buf
, sizeof buf
,
100 "cat /sys/kernel/mm/hugepages/hugepages-%lukB/free_hugepages",
105 unsigned long read_sysfs(int warn
, char *fmt
, ...)
112 unsigned long val
= 0;
115 vsnprintf(buf
, sizeof buf
, fmt
, ap
);
121 printf("missing %s\n", buf
);
124 if (getline(&line
, &linelen
, f
) > 0) {
125 sscanf(line
, "%lu", &val
);
132 unsigned long read_free(unsigned long ps
)
134 return read_sysfs(ps
!= getpagesize(),
135 "/sys/kernel/mm/hugepages/hugepages-%lukB/free_hugepages",
139 void test_mmap(unsigned long size
, unsigned flags
)
142 unsigned long before
, after
;
145 before
= read_free(size
);
146 map
= mmap(NULL
, size
*NUM_PAGES
, PROT_READ
|PROT_WRITE
,
147 MAP_PRIVATE
|MAP_ANONYMOUS
|MAP_HUGETLB
|flags
, 0, 0);
149 if (map
== (char *)-1) err("mmap");
150 memset(map
, 0xff, size
*NUM_PAGES
);
151 after
= read_free(size
);
152 Dprintf("before %lu after %lu diff %ld size %lu\n",
153 before
, after
, before
- after
, size
);
154 assert(size
== getpagesize() || (before
- after
) == NUM_PAGES
);
156 err
= munmap(map
, size
);
160 void test_shmget(unsigned long size
, unsigned flags
)
163 unsigned long before
, after
;
166 before
= read_free(size
);
167 id
= shmget(IPC_PRIVATE
, size
* NUM_PAGES
, IPC_CREAT
|0600|flags
);
168 if (id
< 0) err("shmget");
171 if (shmctl(id
, SHM_INFO
, (void *)&i
) < 0) err("shmctl");
172 Dprintf("alloc %lu res %lu\n", i
.shm_tot
, i
.shm_rss
);
175 Dprintf("id %d\n", id
);
176 char *map
= shmat(id
, NULL
, 0600);
177 if (map
== (char*)-1) err("shmat");
179 shmctl(id
, IPC_RMID
, NULL
);
181 memset(map
, 0xff, size
*NUM_PAGES
);
182 after
= read_free(size
);
184 Dprintf("before %lu after %lu diff %ld size %lu\n",
185 before
, after
, before
- after
, size
);
186 assert(size
== getpagesize() || (before
- after
) == NUM_PAGES
);
192 void sanity_checks(void)
195 unsigned long largest
= getpagesize();
197 for (i
= 0; i
< num_page_sizes
; i
++) {
198 if (page_sizes
[i
] > largest
)
199 largest
= page_sizes
[i
];
201 if (read_free(page_sizes
[i
]) < NUM_PAGES
) {
202 printf("Not enough huge pages for page size %lu MB, need %u\n",
209 if (read_sysfs(0, "/proc/sys/kernel/shmmax") < NUM_PAGES
* largest
) {
210 printf("Please do echo %lu > /proc/sys/kernel/shmmax", largest
* NUM_PAGES
);
214 #if defined(__x86_64__)
215 if (largest
!= 1U<<30) {
216 printf("No GB pages available on x86-64\n"
217 "Please boot with hugepagesz=1G hugepages=%d\n", NUM_PAGES
);
226 unsigned default_hps
= default_huge_page_size();
232 for (i
= 0; i
< num_page_sizes
; i
++) {
233 unsigned long ps
= page_sizes
[i
];
234 int arg
= ilog2(ps
) << MAP_HUGE_SHIFT
;
235 printf("Testing %luMB mmap with shift %x\n", ps
>> 20, arg
);
236 test_mmap(ps
, MAP_HUGETLB
| arg
);
238 printf("Testing default huge mmap\n");
239 test_mmap(default_hps
, SHM_HUGETLB
);
241 puts("Testing non-huge shmget");
242 test_shmget(getpagesize(), 0);
244 for (i
= 0; i
< num_page_sizes
; i
++) {
245 unsigned long ps
= page_sizes
[i
];
246 int arg
= ilog2(ps
) << SHM_HUGE_SHIFT
;
247 printf("Testing %luMB shmget with shift %x\n", ps
>> 20, arg
);
248 test_shmget(ps
, SHM_HUGETLB
| arg
);
250 puts("default huge shmget");
251 test_shmget(default_hps
, SHM_HUGETLB
);