1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2019 Red Hat, Inc.
4 */
5
6 #define TST_NO_DEFAULT_MAIN
7
8 #include "tst_test.h"
9 #include "tst_hugepage.h"
10
11 unsigned long tst_hugepages;
12 char *nr_opt;
13 char *Hopt;
14
tst_get_hugepage_size(void)15 size_t tst_get_hugepage_size(void)
16 {
17 if (access(PATH_HUGEPAGES, F_OK))
18 return 0;
19
20 return SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
21 }
22
tst_request_hugepages(unsigned long hpages)23 unsigned long tst_request_hugepages(unsigned long hpages)
24 {
25 unsigned long val, max_hpages;
26
27 if (access(PATH_HUGEPAGES, F_OK)) {
28 tst_hugepages = 0;
29 goto out;
30 }
31
32 if (nr_opt)
33 tst_hugepages = SAFE_STRTOL(nr_opt, 1, LONG_MAX);
34 else
35 tst_hugepages = hpages;
36
37 if (hpages == TST_NO_HUGEPAGES) {
38 tst_hugepages = 0;
39 goto set_hugepages;
40 }
41
42 SAFE_FILE_PRINTF("/proc/sys/vm/drop_caches", "3");
43 max_hpages = SAFE_READ_MEMINFO("MemFree:") / SAFE_READ_MEMINFO("Hugepagesize:");
44
45 if (tst_hugepages > max_hpages) {
46 tst_res(TINFO, "Requested number(%lu) of hugepages is too large, "
47 "limiting to 80%% of the max hugepage count %lu",
48 tst_hugepages, max_hpages);
49 tst_hugepages = max_hpages * 0.8;
50
51 if (tst_hugepages < 1)
52 goto out;
53 }
54
55 set_hugepages:
56 tst_sys_conf_save("?/proc/sys/vm/nr_hugepages");
57 SAFE_FILE_PRINTF(PATH_NR_HPAGES, "%lu", tst_hugepages);
58 SAFE_FILE_SCANF(PATH_NR_HPAGES, "%lu", &val);
59 if (val != tst_hugepages)
60 tst_brk(TCONF, "nr_hugepages = %lu, but expect %lu. "
61 "Not enough hugepages for testing.",
62 val, tst_hugepages);
63
64 tst_res(TINFO, "%lu hugepage(s) reserved", tst_hugepages);
65 out:
66 return tst_hugepages;
67 }
68