Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / tools / testing / selftests / vm / thuge-gen.c
blobc87957295f74ae6830f6348ea0482427901c4493
1 /* Test selecting other page sizes for mmap/shmget.
3 Before running this huge pages for each huge page size must have been
4 reserved.
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) */
14 #define _GNU_SOURCE 1
15 #include <sys/mman.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <sys/ipc.h>
19 #include <sys/shm.h>
20 #include <sys/stat.h>
21 #include <glob.h>
22 #include <assert.h>
23 #include <unistd.h>
24 #include <stdarg.h>
25 #include <string.h>
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
43 #define NUM_PAGES 4
45 #define Dprintf(fmt...) // printf(fmt)
47 unsigned long page_sizes[NUM_PAGESIZES];
48 int num_page_sizes;
50 int ilog2(unsigned long v)
52 int l = 0;
53 while ((1UL << l) < v)
54 l++;
55 return l;
58 void find_pagesizes(void)
60 glob_t g;
61 int i;
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",
66 &page_sizes[i]);
67 page_sizes[i] <<= 10;
68 printf("Found %luMB\n", page_sizes[i] >> 20);
70 num_page_sizes = g.gl_pathc;
71 globfree(&g);
74 unsigned long default_huge_page_size(void)
76 unsigned long hps = 0;
77 char *line = NULL;
78 size_t linelen = 0;
79 FILE *f = fopen("/proc/meminfo", "r");
80 if (!f)
81 return 0;
82 while (getline(&line, &linelen, f) > 0) {
83 if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) {
84 hps <<= 10;
85 break;
88 free(line);
89 return hps;
92 void show(unsigned long ps)
94 char buf[100];
95 if (ps == getpagesize())
96 return;
97 printf("%luMB: ", ps >> 20);
98 fflush(stdout);
99 snprintf(buf, sizeof buf,
100 "cat /sys/kernel/mm/hugepages/hugepages-%lukB/free_hugepages",
101 ps >> 10);
102 system(buf);
105 unsigned long read_sysfs(int warn, char *fmt, ...)
107 char *line = NULL;
108 size_t linelen = 0;
109 char buf[100];
110 FILE *f;
111 va_list ap;
112 unsigned long val = 0;
114 va_start(ap, fmt);
115 vsnprintf(buf, sizeof buf, fmt, ap);
116 va_end(ap);
118 f = fopen(buf, "r");
119 if (!f) {
120 if (warn)
121 printf("missing %s\n", buf);
122 return 0;
124 if (getline(&line, &linelen, f) > 0) {
125 sscanf(line, "%lu", &val);
127 fclose(f);
128 free(line);
129 return val;
132 unsigned long read_free(unsigned long ps)
134 return read_sysfs(ps != getpagesize(),
135 "/sys/kernel/mm/hugepages/hugepages-%lukB/free_hugepages",
136 ps >> 10);
139 void test_mmap(unsigned long size, unsigned flags)
141 char *map;
142 unsigned long before, after;
143 int err;
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);
155 show(size);
156 err = munmap(map, size);
157 assert(!err);
160 void test_shmget(unsigned long size, unsigned flags)
162 int id;
163 unsigned long before, after;
164 int err;
166 before = read_free(size);
167 id = shmget(IPC_PRIVATE, size * NUM_PAGES, IPC_CREAT|0600|flags);
168 if (id < 0) err("shmget");
170 struct shm_info i;
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);
187 show(size);
188 err = shmdt(map);
189 assert(!err);
192 void sanity_checks(void)
194 int i;
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",
203 page_sizes[i] >> 20,
204 NUM_PAGES);
205 exit(0);
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);
211 exit(0);
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);
218 exit(0);
220 #endif
223 int main(void)
225 int i;
226 unsigned default_hps = default_huge_page_size();
228 find_pagesizes();
230 sanity_checks();
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);
253 return 0;