power: supply: bq24190_charger: Add disable-reset device-property
[linux/fpc-iii.git] / tools / testing / selftests / vm / compaction_test.c
blob298f69e2834ca04948b45badbb7cc04dc4316744
1 /*
3 * A test for the patch "Allow compaction of unevictable pages".
4 * With this patch we should be able to allocate at least 1/4
5 * of RAM in huge pages. Without the patch much less is
6 * allocated.
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/mman.h>
12 #include <sys/resource.h>
13 #include <fcntl.h>
14 #include <errno.h>
15 #include <unistd.h>
16 #include <string.h>
18 #define MAP_SIZE 1048576
20 struct map_list {
21 void *map;
22 struct map_list *next;
25 int read_memory_info(unsigned long *memfree, unsigned long *hugepagesize)
27 char buffer[256] = {0};
28 char *cmd = "cat /proc/meminfo | grep -i memfree | grep -o '[0-9]*'";
29 FILE *cmdfile = popen(cmd, "r");
31 if (!(fgets(buffer, sizeof(buffer), cmdfile))) {
32 perror("Failed to read meminfo\n");
33 return -1;
36 pclose(cmdfile);
38 *memfree = atoll(buffer);
39 cmd = "cat /proc/meminfo | grep -i hugepagesize | grep -o '[0-9]*'";
40 cmdfile = popen(cmd, "r");
42 if (!(fgets(buffer, sizeof(buffer), cmdfile))) {
43 perror("Failed to read meminfo\n");
44 return -1;
47 pclose(cmdfile);
48 *hugepagesize = atoll(buffer);
50 return 0;
53 int prereq(void)
55 char allowed;
56 int fd;
58 fd = open("/proc/sys/vm/compact_unevictable_allowed",
59 O_RDONLY | O_NONBLOCK);
60 if (fd < 0) {
61 perror("Failed to open\n"
62 "/proc/sys/vm/compact_unevictable_allowed\n");
63 return -1;
66 if (read(fd, &allowed, sizeof(char)) != sizeof(char)) {
67 perror("Failed to read from\n"
68 "/proc/sys/vm/compact_unevictable_allowed\n");
69 close(fd);
70 return -1;
73 close(fd);
74 if (allowed == '1')
75 return 0;
77 return -1;
80 int check_compaction(unsigned long mem_free, unsigned int hugepage_size)
82 int fd;
83 int compaction_index = 0;
84 char initial_nr_hugepages[10] = {0};
85 char nr_hugepages[10] = {0};
87 /* We want to test with 80% of available memory. Else, OOM killer comes
88 in to play */
89 mem_free = mem_free * 0.8;
91 fd = open("/proc/sys/vm/nr_hugepages", O_RDWR | O_NONBLOCK);
92 if (fd < 0) {
93 perror("Failed to open /proc/sys/vm/nr_hugepages");
94 return -1;
97 if (read(fd, initial_nr_hugepages, sizeof(initial_nr_hugepages)) <= 0) {
98 perror("Failed to read from /proc/sys/vm/nr_hugepages");
99 goto close_fd;
102 /* Start with the initial condition of 0 huge pages*/
103 if (write(fd, "0", sizeof(char)) != sizeof(char)) {
104 perror("Failed to write 0 to /proc/sys/vm/nr_hugepages\n");
105 goto close_fd;
108 lseek(fd, 0, SEEK_SET);
110 /* Request a large number of huge pages. The Kernel will allocate
111 as much as it can */
112 if (write(fd, "100000", (6*sizeof(char))) != (6*sizeof(char))) {
113 perror("Failed to write 100000 to /proc/sys/vm/nr_hugepages\n");
114 goto close_fd;
117 lseek(fd, 0, SEEK_SET);
119 if (read(fd, nr_hugepages, sizeof(nr_hugepages)) <= 0) {
120 perror("Failed to re-read from /proc/sys/vm/nr_hugepages\n");
121 goto close_fd;
124 /* We should have been able to request at least 1/3 rd of the memory in
125 huge pages */
126 compaction_index = mem_free/(atoi(nr_hugepages) * hugepage_size);
128 if (compaction_index > 3) {
129 printf("No of huge pages allocated = %d\n",
130 (atoi(nr_hugepages)));
131 fprintf(stderr, "ERROR: Less that 1/%d of memory is available\n"
132 "as huge pages\n", compaction_index);
133 goto close_fd;
136 printf("No of huge pages allocated = %d\n",
137 (atoi(nr_hugepages)));
139 lseek(fd, 0, SEEK_SET);
141 if (write(fd, initial_nr_hugepages, strlen(initial_nr_hugepages))
142 != strlen(initial_nr_hugepages)) {
143 perror("Failed to write value to /proc/sys/vm/nr_hugepages\n");
144 goto close_fd;
147 close(fd);
148 return 0;
150 close_fd:
151 close(fd);
152 printf("Not OK. Compaction test failed.");
153 return -1;
157 int main(int argc, char **argv)
159 struct rlimit lim;
160 struct map_list *list, *entry;
161 size_t page_size, i;
162 void *map = NULL;
163 unsigned long mem_free = 0;
164 unsigned long hugepage_size = 0;
165 unsigned long mem_fragmentable = 0;
167 if (prereq() != 0) {
168 printf("Either the sysctl compact_unevictable_allowed is not\n"
169 "set to 1 or couldn't read the proc file.\n"
170 "Skipping the test\n");
171 return 0;
174 lim.rlim_cur = RLIM_INFINITY;
175 lim.rlim_max = RLIM_INFINITY;
176 if (setrlimit(RLIMIT_MEMLOCK, &lim)) {
177 perror("Failed to set rlimit:\n");
178 return -1;
181 page_size = getpagesize();
183 list = NULL;
185 if (read_memory_info(&mem_free, &hugepage_size) != 0) {
186 printf("ERROR: Cannot read meminfo\n");
187 return -1;
190 mem_fragmentable = mem_free * 0.8 / 1024;
192 while (mem_fragmentable > 0) {
193 map = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE,
194 MAP_ANONYMOUS | MAP_PRIVATE | MAP_LOCKED, -1, 0);
195 if (map == MAP_FAILED)
196 break;
198 entry = malloc(sizeof(struct map_list));
199 if (!entry) {
200 munmap(map, MAP_SIZE);
201 break;
203 entry->map = map;
204 entry->next = list;
205 list = entry;
207 /* Write something (in this case the address of the map) to
208 * ensure that KSM can't merge the mapped pages
210 for (i = 0; i < MAP_SIZE; i += page_size)
211 *(unsigned long *)(map + i) = (unsigned long)map + i;
213 mem_fragmentable--;
216 for (entry = list; entry != NULL; entry = entry->next) {
217 munmap(entry->map, MAP_SIZE);
218 if (!entry->next)
219 break;
220 entry = entry->next;
223 if (check_compaction(mem_free, hugepage_size) == 0)
224 return 0;
226 return -1;