Skip to content

Commit 9a21701

Browse files
Muhammad Usama Anjumakpm00
authored andcommitted
selftests/mm: conform test to TAP format output
Conform the layout, informational and status messages to TAP. No functional change is intended other than the layout of output messages. Link: https://lkml.kernel.org/r/20240101083614.1076768-1-usama.anjum@collabora.com Signed-off-by: Muhammad Usama Anjum <usama.anjum@collabora.com> Cc: Shuah Khan <shuah@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 84ba3f2 commit 9a21701

1 file changed

Lines changed: 44 additions & 47 deletions

File tree

tools/testing/selftests/mm/compaction_test.c

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int read_memory_info(unsigned long *memfree, unsigned long *hugepagesize)
3333
FILE *cmdfile = popen(cmd, "r");
3434

3535
if (!(fgets(buffer, sizeof(buffer), cmdfile))) {
36-
perror("Failed to read meminfo\n");
36+
ksft_print_msg("Failed to read meminfo: %s\n", strerror(errno));
3737
return -1;
3838
}
3939

@@ -44,7 +44,7 @@ int read_memory_info(unsigned long *memfree, unsigned long *hugepagesize)
4444
cmdfile = popen(cmd, "r");
4545

4646
if (!(fgets(buffer, sizeof(buffer), cmdfile))) {
47-
perror("Failed to read meminfo\n");
47+
ksft_print_msg("Failed to read meminfo: %s\n", strerror(errno));
4848
return -1;
4949
}
5050

@@ -62,14 +62,14 @@ int prereq(void)
6262
fd = open("/proc/sys/vm/compact_unevictable_allowed",
6363
O_RDONLY | O_NONBLOCK);
6464
if (fd < 0) {
65-
perror("Failed to open\n"
66-
"/proc/sys/vm/compact_unevictable_allowed\n");
65+
ksft_print_msg("Failed to open /proc/sys/vm/compact_unevictable_allowed: %s\n",
66+
strerror(errno));
6767
return -1;
6868
}
6969

7070
if (read(fd, &allowed, sizeof(char)) != sizeof(char)) {
71-
perror("Failed to read from\n"
72-
"/proc/sys/vm/compact_unevictable_allowed\n");
71+
ksft_print_msg("Failed to read from /proc/sys/vm/compact_unevictable_allowed: %s\n",
72+
strerror(errno));
7373
close(fd);
7474
return -1;
7575
}
@@ -78,12 +78,13 @@ int prereq(void)
7878
if (allowed == '1')
7979
return 0;
8080

81+
ksft_print_msg("Compaction isn't allowed\n");
8182
return -1;
8283
}
8384

8485
int check_compaction(unsigned long mem_free, unsigned int hugepage_size)
8586
{
86-
int fd;
87+
int fd, ret = -1;
8788
int compaction_index = 0;
8889
char initial_nr_hugepages[10] = {0};
8990
char nr_hugepages[10] = {0};
@@ -94,18 +95,21 @@ int check_compaction(unsigned long mem_free, unsigned int hugepage_size)
9495

9596
fd = open("/proc/sys/vm/nr_hugepages", O_RDWR | O_NONBLOCK);
9697
if (fd < 0) {
97-
perror("Failed to open /proc/sys/vm/nr_hugepages");
98+
ksft_test_result_fail("Failed to open /proc/sys/vm/nr_hugepages: %s\n",
99+
strerror(errno));
98100
return -1;
99101
}
100102

101103
if (read(fd, initial_nr_hugepages, sizeof(initial_nr_hugepages)) <= 0) {
102-
perror("Failed to read from /proc/sys/vm/nr_hugepages");
104+
ksft_test_result_fail("Failed to read from /proc/sys/vm/nr_hugepages: %s\n",
105+
strerror(errno));
103106
goto close_fd;
104107
}
105108

106109
/* Start with the initial condition of 0 huge pages*/
107110
if (write(fd, "0", sizeof(char)) != sizeof(char)) {
108-
perror("Failed to write 0 to /proc/sys/vm/nr_hugepages\n");
111+
ksft_test_result_fail("Failed to write 0 to /proc/sys/vm/nr_hugepages: %s\n",
112+
strerror(errno));
109113
goto close_fd;
110114
}
111115

@@ -114,82 +118,75 @@ int check_compaction(unsigned long mem_free, unsigned int hugepage_size)
114118
/* Request a large number of huge pages. The Kernel will allocate
115119
as much as it can */
116120
if (write(fd, "100000", (6*sizeof(char))) != (6*sizeof(char))) {
117-
perror("Failed to write 100000 to /proc/sys/vm/nr_hugepages\n");
121+
ksft_test_result_fail("Failed to write 100000 to /proc/sys/vm/nr_hugepages: %s\n",
122+
strerror(errno));
118123
goto close_fd;
119124
}
120125

121126
lseek(fd, 0, SEEK_SET);
122127

123128
if (read(fd, nr_hugepages, sizeof(nr_hugepages)) <= 0) {
124-
perror("Failed to re-read from /proc/sys/vm/nr_hugepages\n");
129+
ksft_test_result_fail("Failed to re-read from /proc/sys/vm/nr_hugepages: %s\n",
130+
strerror(errno));
125131
goto close_fd;
126132
}
127133

128134
/* We should have been able to request at least 1/3 rd of the memory in
129135
huge pages */
130136
compaction_index = mem_free/(atoi(nr_hugepages) * hugepage_size);
131137

132-
if (compaction_index > 3) {
133-
printf("No of huge pages allocated = %d\n",
134-
(atoi(nr_hugepages)));
135-
fprintf(stderr, "ERROR: Less that 1/%d of memory is available\n"
136-
"as huge pages\n", compaction_index);
137-
goto close_fd;
138-
}
139-
140-
printf("No of huge pages allocated = %d\n",
141-
(atoi(nr_hugepages)));
142-
143138
lseek(fd, 0, SEEK_SET);
144139

145140
if (write(fd, initial_nr_hugepages, strlen(initial_nr_hugepages))
146141
!= strlen(initial_nr_hugepages)) {
147-
perror("Failed to write value to /proc/sys/vm/nr_hugepages\n");
142+
ksft_test_result_fail("Failed to write value to /proc/sys/vm/nr_hugepages: %s\n",
143+
strerror(errno));
148144
goto close_fd;
149145
}
150146

151-
close(fd);
152-
return 0;
147+
if (compaction_index > 3) {
148+
ksft_print_msg("ERROR: Less that 1/%d of memory is available\n"
149+
"as huge pages\n", compaction_index);
150+
ksft_test_result_fail("No of huge pages allocated = %d\n", (atoi(nr_hugepages)));
151+
goto close_fd;
152+
}
153+
154+
ksft_test_result_pass("Memory compaction succeeded. No of huge pages allocated = %d\n",
155+
(atoi(nr_hugepages)));
156+
ret = 0;
153157

154158
close_fd:
155159
close(fd);
156-
printf("Not OK. Compaction test failed.");
157-
return -1;
160+
return ret;
158161
}
159162

160163

161164
int main(int argc, char **argv)
162165
{
163166
struct rlimit lim;
164-
struct map_list *list, *entry;
167+
struct map_list *list = NULL, *entry;
165168
size_t page_size, i;
166169
void *map = NULL;
167170
unsigned long mem_free = 0;
168171
unsigned long hugepage_size = 0;
169172
long mem_fragmentable_MB = 0;
170173

171-
if (prereq() != 0) {
172-
printf("Either the sysctl compact_unevictable_allowed is not\n"
173-
"set to 1 or couldn't read the proc file.\n"
174-
"Skipping the test\n");
175-
return KSFT_SKIP;
176-
}
174+
ksft_print_header();
175+
176+
if (prereq() != 0)
177+
return ksft_exit_pass();
178+
179+
ksft_set_plan(1);
177180

178181
lim.rlim_cur = RLIM_INFINITY;
179182
lim.rlim_max = RLIM_INFINITY;
180-
if (setrlimit(RLIMIT_MEMLOCK, &lim)) {
181-
perror("Failed to set rlimit:\n");
182-
return -1;
183-
}
183+
if (setrlimit(RLIMIT_MEMLOCK, &lim))
184+
ksft_exit_fail_msg("Failed to set rlimit: %s\n", strerror(errno));
184185

185186
page_size = getpagesize();
186187

187-
list = NULL;
188-
189-
if (read_memory_info(&mem_free, &hugepage_size) != 0) {
190-
printf("ERROR: Cannot read meminfo\n");
191-
return -1;
192-
}
188+
if (read_memory_info(&mem_free, &hugepage_size) != 0)
189+
ksft_exit_fail_msg("Failed to get meminfo\n");
193190

194191
mem_fragmentable_MB = mem_free * 0.8 / 1024;
195192

@@ -225,7 +222,7 @@ int main(int argc, char **argv)
225222
}
226223

227224
if (check_compaction(mem_free, hugepage_size) == 0)
228-
return 0;
225+
return ksft_exit_pass();
229226

230-
return -1;
227+
return ksft_exit_fail();
231228
}

0 commit comments

Comments
 (0)