@@ -33,7 +33,7 @@ int read_memory_info(unsigned long *memfree, unsigned long *hugepagesize)
33
33
FILE * cmdfile = popen (cmd , "r" );
34
34
35
35
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 ) );
37
37
return -1 ;
38
38
}
39
39
@@ -44,7 +44,7 @@ int read_memory_info(unsigned long *memfree, unsigned long *hugepagesize)
44
44
cmdfile = popen (cmd , "r" );
45
45
46
46
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 ) );
48
48
return -1 ;
49
49
}
50
50
@@ -62,14 +62,14 @@ int prereq(void)
62
62
fd = open ("/proc/sys/vm/compact_unevictable_allowed" ,
63
63
O_RDONLY | O_NONBLOCK );
64
64
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 ) );
67
67
return -1 ;
68
68
}
69
69
70
70
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 ) );
73
73
close (fd );
74
74
return -1 ;
75
75
}
@@ -78,12 +78,13 @@ int prereq(void)
78
78
if (allowed == '1' )
79
79
return 0 ;
80
80
81
+ ksft_print_msg ("Compaction isn't allowed\n" );
81
82
return -1 ;
82
83
}
83
84
84
85
int check_compaction (unsigned long mem_free , unsigned int hugepage_size )
85
86
{
86
- int fd ;
87
+ int fd , ret = -1 ;
87
88
int compaction_index = 0 ;
88
89
char initial_nr_hugepages [10 ] = {0 };
89
90
char nr_hugepages [10 ] = {0 };
@@ -94,20 +95,23 @@ int check_compaction(unsigned long mem_free, unsigned int hugepage_size)
94
95
95
96
fd = open ("/proc/sys/vm/nr_hugepages" , O_RDWR | O_NONBLOCK );
96
97
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 ));
98
100
return -1 ;
99
101
}
100
102
101
103
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 ));
103
106
goto close_fd ;
104
107
}
105
108
106
109
lseek (fd , 0 , SEEK_SET );
107
110
108
111
/* Start with the initial condition of 0 huge pages*/
109
112
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 ));
111
115
goto close_fd ;
112
116
}
113
117
@@ -116,82 +120,75 @@ int check_compaction(unsigned long mem_free, unsigned int hugepage_size)
116
120
/* Request a large number of huge pages. The Kernel will allocate
117
121
as much as it can */
118
122
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 ));
120
125
goto close_fd ;
121
126
}
122
127
123
128
lseek (fd , 0 , SEEK_SET );
124
129
125
130
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 ));
127
133
goto close_fd ;
128
134
}
129
135
130
136
/* We should have been able to request at least 1/3 rd of the memory in
131
137
huge pages */
132
138
compaction_index = mem_free /(atoi (nr_hugepages ) * hugepage_size );
133
139
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
-
145
140
lseek (fd , 0 , SEEK_SET );
146
141
147
142
if (write (fd , initial_nr_hugepages , strlen (initial_nr_hugepages ))
148
143
!= 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 ));
150
146
goto close_fd ;
151
147
}
152
148
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 ;
155
159
156
160
close_fd :
157
161
close (fd );
158
- printf ("Not OK. Compaction test failed." );
159
- return -1 ;
162
+ return ret ;
160
163
}
161
164
162
165
163
166
int main (int argc , char * * argv )
164
167
{
165
168
struct rlimit lim ;
166
- struct map_list * list , * entry ;
169
+ struct map_list * list = NULL , * entry ;
167
170
size_t page_size , i ;
168
171
void * map = NULL ;
169
172
unsigned long mem_free = 0 ;
170
173
unsigned long hugepage_size = 0 ;
171
174
long mem_fragmentable_MB = 0 ;
172
175
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 );
179
182
180
183
lim .rlim_cur = RLIM_INFINITY ;
181
184
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 ));
186
187
187
188
page_size = getpagesize ();
188
189
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" );
195
192
196
193
mem_fragmentable_MB = mem_free * 0.8 / 1024 ;
197
194
@@ -227,7 +224,7 @@ int main(int argc, char **argv)
227
224
}
228
225
229
226
if (check_compaction (mem_free , hugepage_size ) == 0 )
230
- return 0 ;
227
+ return ksft_exit_pass () ;
231
228
232
- return -1 ;
229
+ return ksft_exit_fail () ;
233
230
}
0 commit comments