1 // SPDX-License-Identifier: GPL-2.0
2 /* Test selecting other page sizes for mmap/shmget.
4 Before running this huge pages for each huge page size must have been
6 For large pages beyond MAX_ORDER (like 1GB on x86) boot options must be used.
7 Also shmmax must be increased.
8 And you need to run as root to work around some weird permissions in shm.
9 And nothing using huge pages should run in parallel.
10 When the program aborts you may need to clean up the shm segments with
11 ipcrm -m by hand, like this
12 sudo ipcs | awk '$1 == "0x00000000" {print $2}' | xargs -n1 sudo ipcrm -m
13 (warning this will remove all if someone else uses them) */
28 #define err(x) perror(x), exit(1)
30 #define MAP_HUGE_2MB (21 << MAP_HUGE_SHIFT)
31 #define MAP_HUGE_1GB (30 << MAP_HUGE_SHIFT)
32 #define MAP_HUGE_SHIFT 26
33 #define MAP_HUGE_MASK 0x3f
34 #if !defined(MAP_HUGETLB)
35 #define MAP_HUGETLB 0x40000
38 #define SHM_HUGETLB 04000 /* segment will use huge TLB pages */
39 #define SHM_HUGE_SHIFT 26
40 #define SHM_HUGE_MASK 0x3f
41 #define SHM_HUGE_2MB (21 << SHM_HUGE_SHIFT)
42 #define SHM_HUGE_1GB (30 << SHM_HUGE_SHIFT)
44 #define NUM_PAGESIZES 5
48 #define Dprintf(fmt...) // printf(fmt)
50 unsigned long page_sizes
[NUM_PAGESIZES
];
53 int ilog2(unsigned long v
)
56 while ((1UL << l
) < v
)
61 void find_pagesizes(void)
65 glob("/sys/kernel/mm/hugepages/hugepages-*kB", 0, NULL
, &g
);
66 assert(g
.gl_pathc
<= NUM_PAGESIZES
);
67 for (i
= 0; i
< g
.gl_pathc
; i
++) {
68 sscanf(g
.gl_pathv
[i
], "/sys/kernel/mm/hugepages/hugepages-%lukB",
71 printf("Found %luMB\n", page_sizes
[i
] >> 20);
73 num_page_sizes
= g
.gl_pathc
;
77 unsigned long default_huge_page_size(void)
79 unsigned long hps
= 0;
82 FILE *f
= fopen("/proc/meminfo", "r");
85 while (getline(&line
, &linelen
, f
) > 0) {
86 if (sscanf(line
, "Hugepagesize: %lu kB", &hps
) == 1) {
95 void show(unsigned long ps
)
98 if (ps
== getpagesize())
100 printf("%luMB: ", ps
>> 20);
102 snprintf(buf
, sizeof buf
,
103 "cat /sys/kernel/mm/hugepages/hugepages-%lukB/free_hugepages",
108 unsigned long read_sysfs(int warn
, char *fmt
, ...)
115 unsigned long val
= 0;
118 vsnprintf(buf
, sizeof buf
, fmt
, ap
);
124 printf("missing %s\n", buf
);
127 if (getline(&line
, &linelen
, f
) > 0) {
128 sscanf(line
, "%lu", &val
);
135 unsigned long read_free(unsigned long ps
)
137 return read_sysfs(ps
!= getpagesize(),
138 "/sys/kernel/mm/hugepages/hugepages-%lukB/free_hugepages",
142 void test_mmap(unsigned long size
, unsigned flags
)
145 unsigned long before
, after
;
148 before
= read_free(size
);
149 map
= mmap(NULL
, size
*NUM_PAGES
, PROT_READ
|PROT_WRITE
,
150 MAP_PRIVATE
|MAP_ANONYMOUS
|MAP_HUGETLB
|flags
, -1, 0);
152 if (map
== (char *)-1) err("mmap");
153 memset(map
, 0xff, size
*NUM_PAGES
);
154 after
= read_free(size
);
155 Dprintf("before %lu after %lu diff %ld size %lu\n",
156 before
, after
, before
- after
, size
);
157 assert(size
== getpagesize() || (before
- after
) == NUM_PAGES
);
159 err
= munmap(map
, size
);
163 void test_shmget(unsigned long size
, unsigned flags
)
166 unsigned long before
, after
;
169 before
= read_free(size
);
170 id
= shmget(IPC_PRIVATE
, size
* NUM_PAGES
, IPC_CREAT
|0600|flags
);
171 if (id
< 0) err("shmget");
174 if (shmctl(id
, SHM_INFO
, (void *)&i
) < 0) err("shmctl");
175 Dprintf("alloc %lu res %lu\n", i
.shm_tot
, i
.shm_rss
);
178 Dprintf("id %d\n", id
);
179 char *map
= shmat(id
, NULL
, 0600);
180 if (map
== (char*)-1) err("shmat");
182 shmctl(id
, IPC_RMID
, NULL
);
184 memset(map
, 0xff, size
*NUM_PAGES
);
185 after
= read_free(size
);
187 Dprintf("before %lu after %lu diff %ld size %lu\n",
188 before
, after
, before
- after
, size
);
189 assert(size
== getpagesize() || (before
- after
) == NUM_PAGES
);
195 void sanity_checks(void)
198 unsigned long largest
= getpagesize();
200 for (i
= 0; i
< num_page_sizes
; i
++) {
201 if (page_sizes
[i
] > largest
)
202 largest
= page_sizes
[i
];
204 if (read_free(page_sizes
[i
]) < NUM_PAGES
) {
205 printf("Not enough huge pages for page size %lu MB, need %u\n",
212 if (read_sysfs(0, "/proc/sys/kernel/shmmax") < NUM_PAGES
* largest
) {
213 printf("Please do echo %lu > /proc/sys/kernel/shmmax", largest
* NUM_PAGES
);
217 #if defined(__x86_64__)
218 if (largest
!= 1U<<30) {
219 printf("No GB pages available on x86-64\n"
220 "Please boot with hugepagesz=1G hugepages=%d\n", NUM_PAGES
);
229 unsigned default_hps
= default_huge_page_size();
235 for (i
= 0; i
< num_page_sizes
; i
++) {
236 unsigned long ps
= page_sizes
[i
];
237 int arg
= ilog2(ps
) << MAP_HUGE_SHIFT
;
238 printf("Testing %luMB mmap with shift %x\n", ps
>> 20, arg
);
239 test_mmap(ps
, MAP_HUGETLB
| arg
);
241 printf("Testing default huge mmap\n");
242 test_mmap(default_hps
, SHM_HUGETLB
);
244 puts("Testing non-huge shmget");
245 test_shmget(getpagesize(), 0);
247 for (i
= 0; i
< num_page_sizes
; i
++) {
248 unsigned long ps
= page_sizes
[i
];
249 int arg
= ilog2(ps
) << SHM_HUGE_SHIFT
;
250 printf("Testing %luMB shmget with shift %x\n", ps
>> 20, arg
);
251 test_shmget(ps
, SHM_HUGETLB
| arg
);
253 puts("default huge shmget");
254 test_shmget(default_hps
, SHM_HUGETLB
);