]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blob - tools/testing/selftests/vm/compaction_test.c
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[mirror_ubuntu-kernels.git] / tools / testing / selftests / vm / compaction_test.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 *
4 * A test for the patch "Allow compaction of unevictable pages".
5 * With this patch we should be able to allocate at least 1/4
6 * of RAM in huge pages. Without the patch much less is
7 * allocated.
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <sys/mman.h>
13 #include <sys/resource.h>
14 #include <fcntl.h>
15 #include <errno.h>
16 #include <unistd.h>
17 #include <string.h>
18
19 #define MAP_SIZE 1048576
20
21 struct map_list {
22 void *map;
23 struct map_list *next;
24 };
25
26 int read_memory_info(unsigned long *memfree, unsigned long *hugepagesize)
27 {
28 char buffer[256] = {0};
29 char *cmd = "cat /proc/meminfo | grep -i memfree | grep -o '[0-9]*'";
30 FILE *cmdfile = popen(cmd, "r");
31
32 if (!(fgets(buffer, sizeof(buffer), cmdfile))) {
33 perror("Failed to read meminfo\n");
34 return -1;
35 }
36
37 pclose(cmdfile);
38
39 *memfree = atoll(buffer);
40 cmd = "cat /proc/meminfo | grep -i hugepagesize | grep -o '[0-9]*'";
41 cmdfile = popen(cmd, "r");
42
43 if (!(fgets(buffer, sizeof(buffer), cmdfile))) {
44 perror("Failed to read meminfo\n");
45 return -1;
46 }
47
48 pclose(cmdfile);
49 *hugepagesize = atoll(buffer);
50
51 return 0;
52 }
53
54 int prereq(void)
55 {
56 char allowed;
57 int fd;
58
59 fd = open("/proc/sys/vm/compact_unevictable_allowed",
60 O_RDONLY | O_NONBLOCK);
61 if (fd < 0) {
62 perror("Failed to open\n"
63 "/proc/sys/vm/compact_unevictable_allowed\n");
64 return -1;
65 }
66
67 if (read(fd, &allowed, sizeof(char)) != sizeof(char)) {
68 perror("Failed to read from\n"
69 "/proc/sys/vm/compact_unevictable_allowed\n");
70 close(fd);
71 return -1;
72 }
73
74 close(fd);
75 if (allowed == '1')
76 return 0;
77
78 return -1;
79 }
80
81 int check_compaction(unsigned long mem_free, unsigned int hugepage_size)
82 {
83 int fd;
84 int compaction_index = 0;
85 char initial_nr_hugepages[10] = {0};
86 char nr_hugepages[10] = {0};
87
88 /* We want to test with 80% of available memory. Else, OOM killer comes
89 in to play */
90 mem_free = mem_free * 0.8;
91
92 fd = open("/proc/sys/vm/nr_hugepages", O_RDWR | O_NONBLOCK);
93 if (fd < 0) {
94 perror("Failed to open /proc/sys/vm/nr_hugepages");
95 return -1;
96 }
97
98 if (read(fd, initial_nr_hugepages, sizeof(initial_nr_hugepages)) <= 0) {
99 perror("Failed to read from /proc/sys/vm/nr_hugepages");
100 goto close_fd;
101 }
102
103 /* Start with the initial condition of 0 huge pages*/
104 if (write(fd, "0", sizeof(char)) != sizeof(char)) {
105 perror("Failed to write 0 to /proc/sys/vm/nr_hugepages\n");
106 goto close_fd;
107 }
108
109 lseek(fd, 0, SEEK_SET);
110
111 /* Request a large number of huge pages. The Kernel will allocate
112 as much as it can */
113 if (write(fd, "100000", (6*sizeof(char))) != (6*sizeof(char))) {
114 perror("Failed to write 100000 to /proc/sys/vm/nr_hugepages\n");
115 goto close_fd;
116 }
117
118 lseek(fd, 0, SEEK_SET);
119
120 if (read(fd, nr_hugepages, sizeof(nr_hugepages)) <= 0) {
121 perror("Failed to re-read from /proc/sys/vm/nr_hugepages\n");
122 goto close_fd;
123 }
124
125 /* We should have been able to request at least 1/3 rd of the memory in
126 huge pages */
127 compaction_index = mem_free/(atoi(nr_hugepages) * hugepage_size);
128
129 if (compaction_index > 3) {
130 printf("No of huge pages allocated = %d\n",
131 (atoi(nr_hugepages)));
132 fprintf(stderr, "ERROR: Less that 1/%d of memory is available\n"
133 "as huge pages\n", compaction_index);
134 goto close_fd;
135 }
136
137 printf("No of huge pages allocated = %d\n",
138 (atoi(nr_hugepages)));
139
140 if (write(fd, initial_nr_hugepages, strlen(initial_nr_hugepages))
141 != strlen(initial_nr_hugepages)) {
142 perror("Failed to write value to /proc/sys/vm/nr_hugepages\n");
143 goto close_fd;
144 }
145
146 close(fd);
147 return 0;
148
149 close_fd:
150 close(fd);
151 printf("Not OK. Compaction test failed.");
152 return -1;
153 }
154
155
156 int main(int argc, char **argv)
157 {
158 struct rlimit lim;
159 struct map_list *list, *entry;
160 size_t page_size, i;
161 void *map = NULL;
162 unsigned long mem_free = 0;
163 unsigned long hugepage_size = 0;
164 unsigned long mem_fragmentable = 0;
165
166 if (prereq() != 0) {
167 printf("Either the sysctl compact_unevictable_allowed is not\n"
168 "set to 1 or couldn't read the proc file.\n"
169 "Skipping the test\n");
170 return 0;
171 }
172
173 lim.rlim_cur = RLIM_INFINITY;
174 lim.rlim_max = RLIM_INFINITY;
175 if (setrlimit(RLIMIT_MEMLOCK, &lim)) {
176 perror("Failed to set rlimit:\n");
177 return -1;
178 }
179
180 page_size = getpagesize();
181
182 list = NULL;
183
184 if (read_memory_info(&mem_free, &hugepage_size) != 0) {
185 printf("ERROR: Cannot read meminfo\n");
186 return -1;
187 }
188
189 mem_fragmentable = mem_free * 0.8 / 1024;
190
191 while (mem_fragmentable > 0) {
192 map = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE,
193 MAP_ANONYMOUS | MAP_PRIVATE | MAP_LOCKED, -1, 0);
194 if (map == MAP_FAILED)
195 break;
196
197 entry = malloc(sizeof(struct map_list));
198 if (!entry) {
199 munmap(map, MAP_SIZE);
200 break;
201 }
202 entry->map = map;
203 entry->next = list;
204 list = entry;
205
206 /* Write something (in this case the address of the map) to
207 * ensure that KSM can't merge the mapped pages
208 */
209 for (i = 0; i < MAP_SIZE; i += page_size)
210 *(unsigned long *)(map + i) = (unsigned long)map + i;
211
212 mem_fragmentable--;
213 }
214
215 for (entry = list; entry != NULL; entry = entry->next) {
216 munmap(entry->map, MAP_SIZE);
217 if (!entry->next)
218 break;
219 entry = entry->next;
220 }
221
222 if (check_compaction(mem_free, hugepage_size) == 0)
223 return 0;
224
225 return -1;
226 }