Debian Codegraph @

tests/libtestutils.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2008 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
20 #define _LARGEFILE64_SOURCE
21 #define _GNU_SOURCE
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <limits.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <unistd.h>
30 #include <signal.h>
31 #include <sys/types.h>
32 #include <sys/vfs.h>
33 #include <sys/ipc.h>
34 #include <sys/shm.h>
35 #include <sys/stat.h>
36 #include <sys/mman.h>
37 #include <fcntl.h>
38
39 #include "hugetlbfs.h"
40 #include "libhugetlbfs_privutils.h"
41 #include "hugetests.h"
42
43 void check_free_huge_pages(int nr_pages_needed)
44 {
45 long hpage_size = gethugepagesize();
46 int freepages = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
47 if (freepages < nr_pages_needed)
48 CONFIG("Must have at least %i free hugepages", nr_pages_needed);
49 }
50
51 int using_system_hpage_size(const char *mount)
52 {
53 struct statfs64 sb;
54 int err;
55 long meminfo_size, mount_size;
56
57 if (!mount)
58 FAIL("using_system_hpage_size: hugetlbfs is not mounted\n");
59
60 err = statfs64(mount, &sb);
61 if (err)
62 FAIL("statfs64: %s\n", strerror(errno));
63
64 meminfo_size = read_meminfo("Hugepagesize:");
65 if (meminfo_size < 0)
66 FAIL("using_system_hpage_size: Failed to read /proc/meminfo\n");
67
68 mount_size = sb.f_bsize / 1024; /* Compare to meminfo in kB */
69 if (mount_size == meminfo_size)
70 return 1;
71 else
72 return 0;
73 }
74
75 /* WARNING: This function relies on the hugetlb pool counters in a way that
76 * is known to be racy. Due to the expected usage of hugetlbfs test cases, the
77 * risk of a race is acceptible. This function should NOT be used for real
78 * applications.
79 */
80 int kernel_has_private_reservations(void)
81 {
82 int fd;
83 long t, f, r, s;
84 long nt, nf, nr, ns;
85 long hpage_size = gethugepagesize();
86 void *map;
87
88 /* Read pool counters */
89 t = get_huge_page_counter(hpage_size, HUGEPAGES_TOTAL);
90 f = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
91 r = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
92 s = get_huge_page_counter(hpage_size, HUGEPAGES_SURP);
93
94 fd = hugetlbfs_unlinked_fd();
95 if (fd < 0) {
96 ERROR("kernel_has_private_reservations: hugetlbfs_unlinked_fd: "
97 "%s\n", strerror(errno));
98 return -1;
99 }
100 map = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
101 if (map == MAP_FAILED) {
102 ERROR("kernel_has_private_reservations: mmap: %s\n",
103 strerror(errno));
104 return -1;
105 }
106
107 /* Recheck the counters */
108 nt = get_huge_page_counter(hpage_size, HUGEPAGES_TOTAL);
109 nf = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
110 nr = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
111 ns = get_huge_page_counter(hpage_size, HUGEPAGES_SURP);
112
113 munmap(map, hpage_size);
114 close(fd);
115
116 /*
117 * There are only three valid cases:
118 * 1) If a surplus page was allocated to create a reservation, all
119 * four pool counters increment
120 * 2) All counters remain the same except for Hugepages_Rsvd, then
121 * a reservation was created using an existing pool page.
122 * 3) All counters remain the same, indicates that no reservation has
123 * been created
124 */
125 if ((nt == t + 1) && (nf == f + 1) && (ns == s + 1) && (nr == r + 1)) {
126 return 1;
127 } else if ((nt == t) && (nf == f) && (ns == s)) {
128 if (nr == r + 1)
129 return 1;
130 else if (nr == r)
131 return 0;
132 } else {
133 ERROR("kernel_has_private_reservations: bad counter state - "
134 "T:%li F:%li R:%li S:%li -> T:%li F:%li R:%li S:%li\n",
135 t, f, r, s, nt, nf, nr, ns);
136 }
137 return -1;
138 }