power: supply: bq24190_charger: Add disable-reset device-property
[linux/fpc-iii.git] / tools / testing / selftests / vm / thuge-gen.c
blob0bc737a75150bf225f6b3508182341536b66c88e
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 #if !defined(MAP_HUGETLB)
34 #define MAP_HUGETLB 0x40000
35 #endif
37 #define SHM_HUGETLB 04000 /* segment will use huge TLB pages */
38 #define SHM_HUGE_SHIFT 26
39 #define SHM_HUGE_MASK 0x3f
40 #define SHM_HUGE_2MB (21 << SHM_HUGE_SHIFT)
41 #define SHM_HUGE_1GB (30 << SHM_HUGE_SHIFT)
43 #define NUM_PAGESIZES 5
45 #define NUM_PAGES 4
47 #define Dprintf(fmt...) // printf(fmt)
49 unsigned long page_sizes[NUM_PAGESIZES];
50 int num_page_sizes;
52 int ilog2(unsigned long v)
54 int l = 0;
55 while ((1UL << l) < v)
56 l++;
57 return l;
60 void find_pagesizes(void)
62 glob_t g;
63 int i;
64 glob("/sys/kernel/mm/hugepages/hugepages-*kB", 0, NULL, &g);
65 assert(g.gl_pathc <= NUM_PAGESIZES);
66 for (i = 0; i < g.gl_pathc; i++) {
67 sscanf(g.gl_pathv[i], "/sys/kernel/mm/hugepages/hugepages-%lukB",
68 &page_sizes[i]);
69 page_sizes[i] <<= 10;
70 printf("Found %luMB\n", page_sizes[i] >> 20);
72 num_page_sizes = g.gl_pathc;
73 globfree(&g);
76 unsigned long default_huge_page_size(void)
78 unsigned long hps = 0;
79 char *line = NULL;
80 size_t linelen = 0;
81 FILE *f = fopen("/proc/meminfo", "r");
82 if (!f)
83 return 0;
84 while (getline(&line, &linelen, f) > 0) {
85 if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) {
86 hps <<= 10;
87 break;
90 free(line);
91 return hps;
94 void show(unsigned long ps)
96 char buf[100];
97 if (ps == getpagesize())
98 return;
99 printf("%luMB: ", ps >> 20);
100 fflush(stdout);
101 snprintf(buf, sizeof buf,
102 "cat /sys/kernel/mm/hugepages/hugepages-%lukB/free_hugepages",
103 ps >> 10);
104 system(buf);
107 unsigned long read_sysfs(int warn, char *fmt, ...)
109 char *line = NULL;
110 size_t linelen = 0;
111 char buf[100];
112 FILE *f;
113 va_list ap;
114 unsigned long val = 0;
116 va_start(ap, fmt);
117 vsnprintf(buf, sizeof buf, fmt, ap);
118 va_end(ap);
120 f = fopen(buf, "r");
121 if (!f) {
122 if (warn)
123 printf("missing %s\n", buf);
124 return 0;
126 if (getline(&line, &linelen, f) > 0) {
127 sscanf(line, "%lu", &val);
129 fclose(f);
130 free(line);
131 return val;
134 unsigned long read_free(unsigned long ps)
136 return read_sysfs(ps != getpagesize(),
137 "/sys/kernel/mm/hugepages/hugepages-%lukB/free_hugepages",
138 ps >> 10);
141 void test_mmap(unsigned long size, unsigned flags)
143 char *map;
144 unsigned long before, after;
145 int err;
147 before = read_free(size);
148 map = mmap(NULL, size*NUM_PAGES, PROT_READ|PROT_WRITE,
149 MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB|flags, 0, 0);
151 if (map == (char *)-1) err("mmap");
152 memset(map, 0xff, size*NUM_PAGES);
153 after = read_free(size);
154 Dprintf("before %lu after %lu diff %ld size %lu\n",
155 before, after, before - after, size);
156 assert(size == getpagesize() || (before - after) == NUM_PAGES);
157 show(size);
158 err = munmap(map, size);
159 assert(!err);
162 void test_shmget(unsigned long size, unsigned flags)
164 int id;
165 unsigned long before, after;
166 int err;
168 before = read_free(size);
169 id = shmget(IPC_PRIVATE, size * NUM_PAGES, IPC_CREAT|0600|flags);
170 if (id < 0) err("shmget");
172 struct shm_info i;
173 if (shmctl(id, SHM_INFO, (void *)&i) < 0) err("shmctl");
174 Dprintf("alloc %lu res %lu\n", i.shm_tot, i.shm_rss);
177 Dprintf("id %d\n", id);
178 char *map = shmat(id, NULL, 0600);
179 if (map == (char*)-1) err("shmat");
181 shmctl(id, IPC_RMID, NULL);
183 memset(map, 0xff, size*NUM_PAGES);
184 after = read_free(size);
186 Dprintf("before %lu after %lu diff %ld size %lu\n",
187 before, after, before - after, size);
188 assert(size == getpagesize() || (before - after) == NUM_PAGES);
189 show(size);
190 err = shmdt(map);
191 assert(!err);
194 void sanity_checks(void)
196 int i;
197 unsigned long largest = getpagesize();
199 for (i = 0; i < num_page_sizes; i++) {
200 if (page_sizes[i] > largest)
201 largest = page_sizes[i];
203 if (read_free(page_sizes[i]) < NUM_PAGES) {
204 printf("Not enough huge pages for page size %lu MB, need %u\n",
205 page_sizes[i] >> 20,
206 NUM_PAGES);
207 exit(0);
211 if (read_sysfs(0, "/proc/sys/kernel/shmmax") < NUM_PAGES * largest) {
212 printf("Please do echo %lu > /proc/sys/kernel/shmmax", largest * NUM_PAGES);
213 exit(0);
216 #if defined(__x86_64__)
217 if (largest != 1U<<30) {
218 printf("No GB pages available on x86-64\n"
219 "Please boot with hugepagesz=1G hugepages=%d\n", NUM_PAGES);
220 exit(0);
222 #endif
225 int main(void)
227 int i;
228 unsigned default_hps = default_huge_page_size();
230 find_pagesizes();
232 sanity_checks();
234 for (i = 0; i < num_page_sizes; i++) {
235 unsigned long ps = page_sizes[i];
236 int arg = ilog2(ps) << MAP_HUGE_SHIFT;
237 printf("Testing %luMB mmap with shift %x\n", ps >> 20, arg);
238 test_mmap(ps, MAP_HUGETLB | arg);
240 printf("Testing default huge mmap\n");
241 test_mmap(default_hps, SHM_HUGETLB);
243 puts("Testing non-huge shmget");
244 test_shmget(getpagesize(), 0);
246 for (i = 0; i < num_page_sizes; i++) {
247 unsigned long ps = page_sizes[i];
248 int arg = ilog2(ps) << SHM_HUGE_SHIFT;
249 printf("Testing %luMB shmget with shift %x\n", ps >> 20, arg);
250 test_shmget(ps, SHM_HUGETLB | arg);
252 puts("default huge shmget");
253 test_shmget(default_hps, SHM_HUGETLB);
255 return 0;