Skip to content

Commit 5ba3913

Browse files
musamaanjumgregkh
authored andcommitted
selftests/mm: conform test to TAP format output
[ Upstream commit 9a21701 ] 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/[email protected] Signed-off-by: Muhammad Usama Anjum <[email protected]> Cc: Shuah Khan <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Stable-dep-of: d4202e6 ("selftests/mm: compaction_test: fix bogus test success on Aarch64") Signed-off-by: Sasha Levin <[email protected]>
1 parent 499fd1d commit 5ba3913

File tree

1 file changed

+44
-47
lines changed

1 file changed

+44
-47
lines changed

tools/testing/selftests/vm/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,20 +95,23 @@ 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
lseek(fd, 0, SEEK_SET);
107110

108111
/* Start with the initial condition of 0 huge pages*/
109112
if (write(fd, "0", sizeof(char)) != sizeof(char)) {
110-
perror("Failed to write 0 to /proc/sys/vm/nr_hugepages\n");
113+
ksft_test_result_fail("Failed to write 0 to /proc/sys/vm/nr_hugepages: %s\n",
114+
strerror(errno));
111115
goto close_fd;
112116
}
113117

@@ -116,82 +120,75 @@ int check_compaction(unsigned long mem_free, unsigned int hugepage_size)
116120
/* Request a large number of huge pages. The Kernel will allocate
117121
as much as it can */
118122
if (write(fd, "100000", (6*sizeof(char))) != (6*sizeof(char))) {
119-
perror("Failed to write 100000 to /proc/sys/vm/nr_hugepages\n");
123+
ksft_test_result_fail("Failed to write 100000 to /proc/sys/vm/nr_hugepages: %s\n",
124+
strerror(errno));
120125
goto close_fd;
121126
}
122127

123128
lseek(fd, 0, SEEK_SET);
124129

125130
if (read(fd, nr_hugepages, sizeof(nr_hugepages)) <= 0) {
126-
perror("Failed to re-read from /proc/sys/vm/nr_hugepages\n");
131+
ksft_test_result_fail("Failed to re-read from /proc/sys/vm/nr_hugepages: %s\n",
132+
strerror(errno));
127133
goto close_fd;
128134
}
129135

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

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

147142
if (write(fd, initial_nr_hugepages, strlen(initial_nr_hugepages))
148143
!= strlen(initial_nr_hugepages)) {
149-
perror("Failed to write value to /proc/sys/vm/nr_hugepages\n");
144+
ksft_test_result_fail("Failed to write value to /proc/sys/vm/nr_hugepages: %s\n",
145+
strerror(errno));
150146
goto close_fd;
151147
}
152148

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

156160
close_fd:
157161
close(fd);
158-
printf("Not OK. Compaction test failed.");
159-
return -1;
162+
return ret;
160163
}
161164

162165

163166
int main(int argc, char **argv)
164167
{
165168
struct rlimit lim;
166-
struct map_list *list, *entry;
169+
struct map_list *list = NULL, *entry;
167170
size_t page_size, i;
168171
void *map = NULL;
169172
unsigned long mem_free = 0;
170173
unsigned long hugepage_size = 0;
171174
long mem_fragmentable_MB = 0;
172175

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

180183
lim.rlim_cur = RLIM_INFINITY;
181184
lim.rlim_max = RLIM_INFINITY;
182-
if (setrlimit(RLIMIT_MEMLOCK, &lim)) {
183-
perror("Failed to set rlimit:\n");
184-
return -1;
185-
}
185+
if (setrlimit(RLIMIT_MEMLOCK, &lim))
186+
ksft_exit_fail_msg("Failed to set rlimit: %s\n", strerror(errno));
186187

187188
page_size = getpagesize();
188189

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

196193
mem_fragmentable_MB = mem_free * 0.8 / 1024;
197194

@@ -227,7 +224,7 @@ int main(int argc, char **argv)
227224
}
228225

229226
if (check_compaction(mem_free, hugepage_size) == 0)
230-
return 0;
227+
return ksft_exit_pass();
231228

232-
return -1;
229+
return ksft_exit_fail();
233230
}

0 commit comments

Comments
 (0)