Debian Codegraph @

tests/counters.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2005-2007 David Gibson & Adam Litke, IBM Corporation.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 #include <sys/types.h>
20 #include <sys/shm.h>
21 #include <sys/types.h>
22 #include <sys/mman.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <hugetlbfs.h>
31 #include "hugetests.h"
32
33 /*
34 * Test Rationale:
35 *
36 * The hugetlb pool maintains 4 global counters to track pages as they
37 * transition between various states. Due to the complex relationships between
38 * the counters, regressions are likely to occur in the future. This test
39 * performs operations that change the counters in known ways. It emulates the
40 * expected kernel behavior and compares the expected result to the actual
41 * values after each operation.
42 */
43
44 extern int errno;
45
46 /* Global test configuration */
47 #define DYNAMIC_SYSCTL "/proc/sys/vm/nr_overcommit_hugepages"
48 static long saved_nr_hugepages = -1;
49 static long saved_oc_hugepages = -1;
50 static long hpage_size;
51 static int private_resv;
52
53 /* State arrays for our mmaps */
54 #define NR_SLOTS 2
55 #define SL_SETUP 0
56 #define SL_TEST 1
57 static int map_fd[NR_SLOTS];
58 static char *map_addr[NR_SLOTS];
59 static unsigned long map_size[NR_SLOTS];
60 static unsigned int touched[NR_SLOTS];
61
62 /* Keep track of expected counter values */
63 static long prev_total;
64 static long prev_free;
65 static long prev_resv;
66 static long prev_surp;
67
68 #define min(a,b) (((a) < (b)) ? (a) : (b))
69 #define max(a,b) (((a) > (b)) ? (a) : (b))
70
71 /* Restore original nr_hugepages */
72 void cleanup(void) {
73 if (hpage_size <= 0)
74 return;
75 if (saved_nr_hugepages >= 0)
76 set_nr_hugepages(hpage_size, saved_nr_hugepages);
77 if (saved_oc_hugepages >= 0)
78 set_nr_overcommit_hugepages(hpage_size, saved_oc_hugepages);
79 }
80
81 void verify_dynamic_pool_support(void)
82 {
83 saved_oc_hugepages = get_huge_page_counter(hpage_size, HUGEPAGES_OC);
84 if (saved_oc_hugepages < 0)
85 FAIL("Kernel appears to lack dynamic hugetlb pool support");
86 if (set_nr_overcommit_hugepages(hpage_size, 10) < 0) {
87 /*
88 * In case writing to nr_overcommit_hugepages failed with the
89 * reason that it was an attempt to write an invalid argument,
90 * it might be because the page size corresponds to gigantic
91 * pages which do not support this feature.
92 */
93 if (errno == EINVAL)
94 check_if_gigantic_page();
95 FAIL("Couldn't set overcommit limit");
96 }
97 }
98
99 void bad_value(int line, const char *name, long expect, long actual)
100 {
101 if (actual == -1)
102 ERROR("%s not found in /proc/meminfo", name);
103 else
104 FAIL("Line %i: Bad %s: expected %li, actual %li",
105 line, name, expect, actual);
106 }
107
108 void verify_counters(int line, long et, long ef, long er, long es)
109 {
110 long t, f, r, s;
111
112 t = get_huge_page_counter(hpage_size, HUGEPAGES_TOTAL);
113 f = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
114 r = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
115 s = get_huge_page_counter(hpage_size, HUGEPAGES_SURP);
116
117 /* Invariant checks */
118 if (t < 0 || f < 0 || r < 0 || s < 0)
119 ERROR("Negative counter value");
120 if (f < r)
121 ERROR("HugePages_Free < HugePages_Rsvd");
122
123 /* Check actual values against expected values */
124 if (t != et)
125 bad_value(line, "HugePages_Total", et, t);
126
127 if (f != ef)
128 bad_value(line, "HugePages_Free", ef, f);
129
130 if (r != er)
131 bad_value(line, "HugePages_Rsvd", er, r);
132
133 if (s != es)
134 bad_value(line, "HugePages_Surp", es, s);
135
136 /* Everything's good. Update counters */
137 prev_total = t;
138 prev_free = f;
139 prev_resv = r;
140 prev_surp = s;
141 }
142
143 /* Memory operations:
144 * Each of these has a predefined effect on the counters
145 */
146 #define persistent_huge_pages (et - es)
147 void _set_nr_hugepages(unsigned long count, int line)
148 {
149 long min_size;
150 long et, ef, er, es;
151
152 if (set_nr_hugepages(hpage_size, count))
153 FAIL("Cannot set nr_hugepages");
154
155 /* The code below is based on set_max_huge_pages in mm/hugetlb.c */
156 es = prev_surp;
157 et = prev_total;
158 ef = prev_free;
159 er = prev_resv;
160
161 /*
162 * Increase the pool size
163 * First take pages out of surplus state. Then make up the
164 * remaining difference by allocating fresh huge pages.
165 */
166 while (es && count > persistent_huge_pages)
167 es--;
168 while (count > persistent_huge_pages) {
169 et++;
170 ef++;
171 }
172 if (count >= persistent_huge_pages)
173 goto out;
174
175 /*
176 * Decrease the pool size
177 * First return free pages to the buddy allocator (being careful
178 * to keep enough around to satisfy reservations). Then place
179 * pages into surplus state as needed so the pool will shrink
180 * to the desired size as pages become free.
181 */
182 min_size = max(count, er + et - ef);
183 while (min_size < persistent_huge_pages) {
184 ef--;
185 et--;
186 }
187 while (count < persistent_huge_pages) {
188 es++;
189 }
190
191 out:
192 verify_counters(line, et, ef, er, es);
193 }
194 #undef set_nr_hugepages
195 #define set_nr_hugepages(c) _set_nr_hugepages(c, __LINE__)
196
197 void _map(int s, int hpages, int flags, int line)
198 {
199 long et, ef, er, es;
200
201 map_fd[s] = hugetlbfs_unlinked_fd();
202 if (map_fd[s] < 0)
203 CONFIG("Unable to open hugetlbfs file: %s", strerror(errno));
204 map_size[s] = hpages * hpage_size;
205 map_addr[s] = mmap(NULL, map_size[s], PROT_READ|PROT_WRITE, flags,
206 map_fd[s], 0);
207 if (map_addr[s] == MAP_FAILED)
208 FAIL("mmap failed: %s", strerror(errno));
209 touched[s] = 0;
210
211 et = prev_total;
212 ef = prev_free;
213 er = prev_resv;
214 es = prev_surp;
215
216 /*
217 * When using MAP_SHARED, a reservation will be created to guarantee
218 * pages to the process. If not enough pages are available to
219 * satisfy the reservation, surplus pages are added to the pool.
220 * NOTE: This code assumes that the whole mapping needs to be
221 * reserved and hence, will not work with partial reservations.
222 *
223 * If the kernel supports private reservations, then MAP_PRIVATE
224 * mappings behave like MAP_SHARED at mmap time. Otherwise,
225 * no counter updates will occur.
226 */
227 if ((flags & MAP_SHARED) || private_resv) {
228 unsigned long shortfall = 0;
229 if (hpages + prev_resv > prev_free)
230 shortfall = hpages - prev_free + prev_resv;
231 et += shortfall;
232 ef = prev_free + shortfall;
233 er = prev_resv + hpages;
234 es = prev_surp + shortfall;
235 }
236
237 verify_counters(line, et, ef, er, es);
238 }
239 #define map(s, h, f) _map(s, h, f, __LINE__)
240
241 void _unmap(int s, int hpages, int flags, int line)
242 {
243 long et, ef, er, es;
244 unsigned long i;
245
246 munmap(map_addr[s], map_size[s]);
247 close(map_fd[s]);
248 map_fd[s] = -1;
249 map_addr[s] = NULL;
250 map_size[s] = 0;
251
252 et = prev_total;
253 ef = prev_free;
254 er = prev_resv;
255 es = prev_surp;
256
257 /*
258 * When a VMA is unmapped, the instantiated (touched) pages are
259 * freed. If the pool is in a surplus state, pages are freed to the
260 * buddy allocator, otherwise they go back into the hugetlb pool.
261 * NOTE: This code assumes touched pages have only one user.
262 */
263 for (i = 0; i < touched[s]; i++) {
264 if (es) {
265 et--;
266 es--;
267 } else
268 ef++;
269 }
270
271 /*
272 * mmap may have created some surplus pages to accomodate a
273 * reservation. If those pages were not touched, then they will
274 * not have been freed by the code above. Free them here.
275 */
276 if ((flags & MAP_SHARED) || private_resv) {
277 int unused_surplus = min(hpages - touched[s], es);
278 et -= unused_surplus;
279 ef -= unused_surplus;
280 er -= hpages - touched[s];
281 es -= unused_surplus;
282 }
283
284 verify_counters(line, et, ef, er, es);
285 }
286 #define unmap(s, h, f) _unmap(s, h, f, __LINE__)
287
288 void _touch(int s, int hpages, int flags, int line)
289 {
290 long et, ef, er, es;
291 int nr;
292 char *c;
293
294 for (c = map_addr[s], nr = hpages;
295 hpages && c < map_addr[s] + map_size[s];
296 c += hpage_size, nr--)
297 *c = (char) (nr % 2);
298 /*
299 * Keep track of how many pages were touched since we can't easily
300 * detect that from user space.
301 * NOTE: Calling this function more than once for a mmap may yield
302 * results you don't expect. Be careful :)
303 */
304 touched[s] = max(touched[s], hpages);
305
306 /*
307 * Shared (and private when supported) mappings and consume resv pages
308 * that were previously allocated. Also deduct them from the free count.
309 *
310 * Unreserved private mappings may need to allocate surplus pages to
311 * satisfy the fault. The surplus pages become part of the pool
312 * which could elevate total, free, and surplus counts. resv is
313 * unchanged but free must be decreased.
314 */
315 if (flags & MAP_SHARED || private_resv) {
316 et = prev_total;
317 ef = prev_free - hpages;
318 er = prev_resv - hpages;
319 es = prev_surp;
320 } else {
321 if (hpages + prev_resv > prev_free)
322 et = prev_total + (hpages - prev_free + prev_resv);
323 else
324 et = prev_total;
325 er = prev_resv;
326 es = prev_surp + et - prev_total;
327 ef = prev_free - hpages + et - prev_total;
328 }
329 verify_counters(line, et, ef, er, es);
330 }
331 #define touch(s, h, f) _touch(s, h, f, __LINE__)
332
333 void run_test(char *desc, int base_nr)
334 {
335 verbose_printf("%s...\n", desc);
336 set_nr_hugepages(base_nr);
337
338 /* untouched, shared mmap */
339 map(SL_TEST, 1, MAP_SHARED);
340 unmap(SL_TEST, 1, MAP_SHARED);
341
342 /* untouched, private mmap */
343 map(SL_TEST, 1, MAP_PRIVATE);
344 unmap(SL_TEST, 1, MAP_PRIVATE);
345
346 /* touched, shared mmap */
347 map(SL_TEST, 1, MAP_SHARED);
348 touch(SL_TEST, 1, MAP_SHARED);
349 unmap(SL_TEST, 1, MAP_SHARED);
350
351 /* touched, private mmap */
352 map(SL_TEST, 1, MAP_PRIVATE);
353 touch(SL_TEST, 1, MAP_PRIVATE);
354 unmap(SL_TEST, 1, MAP_PRIVATE);
355
356 /* Explicit resizing during outstanding surplus */
357 /* Consume surplus when growing pool */
358 map(SL_TEST, 2, MAP_SHARED);
359 set_nr_hugepages(max(base_nr, 1));
360
361 /* Add pages once surplus is consumed */
362 set_nr_hugepages(max(base_nr, 3));
363
364 /* Release free huge pages first */
365 set_nr_hugepages(max(base_nr, 2));
366
367 /* When shrinking beyond committed level, increase surplus */
368 set_nr_hugepages(base_nr);
369
370 /* Upon releasing the reservation, reduce surplus counts */
371 unmap(SL_TEST, 2, MAP_SHARED);
372
373 verbose_printf("OK.\n");
374 }
375
376 int main(int argc, char ** argv)
377 {
378 int base_nr;
379
380 test_init(argc, argv);
381 hpage_size = check_hugepagesize();
382 saved_nr_hugepages = get_huge_page_counter(hpage_size, HUGEPAGES_TOTAL);
383 verify_dynamic_pool_support();
384 check_must_be_root();
385
386 if ((private_resv = kernel_has_private_reservations()) == -1)
387 FAIL("kernel_has_private_reservations() failed\n");
388
389 /*
390 * This test case should require a maximum of 3 huge pages.
391 * Run through the battery of tests multiple times, with an increasing
392 * base pool size. This alters the circumstances under which surplus
393 * pages need to be allocated and increases the corner cases tested.
394 */
395 for (base_nr = 0; base_nr <= 3; base_nr++) {
396 verbose_printf("Base pool size: %i\n", base_nr);
397 /* Run the tests with a clean slate */
398 run_test("Clean", base_nr);
399
400 /* Now with a pre-existing untouched, shared mmap */
401 map(SL_SETUP, 1, MAP_SHARED);
402 run_test("Untouched, shared", base_nr);
403 unmap(SL_SETUP, 1, MAP_SHARED);
404
405 /* Now with a pre-existing untouched, private mmap */
406 map(SL_SETUP, 1, MAP_PRIVATE);
407 run_test("Untouched, private", base_nr);
408 unmap(SL_SETUP, 1, MAP_PRIVATE);
409
410 /* Now with a pre-existing touched, shared mmap */
411 map(SL_SETUP, 1, MAP_SHARED);
412 touch(SL_SETUP, 1, MAP_SHARED);
413 run_test("Touched, shared", base_nr);
414 unmap(SL_SETUP, 1, MAP_SHARED);
415
416 /* Now with a pre-existing touched, private mmap */
417 map(SL_SETUP, 1, MAP_PRIVATE);
418 touch(SL_SETUP, 1, MAP_PRIVATE);
419 run_test("Touched, private", base_nr);
420 unmap(SL_SETUP, 1, MAP_PRIVATE);
421 }
422
423 PASS();
424 }