2
2
#include <string.h>
3
3
#include <fcntl.h>
4
4
#include <dirent.h>
5
+ #include <inttypes.h>
5
6
#include <sys/ioctl.h>
6
7
#include <linux/userfaultfd.h>
7
8
#include <linux/fs.h>
@@ -193,13 +194,11 @@ unsigned long rss_anon(void)
193
194
return rss_anon ;
194
195
}
195
196
196
- bool __check_huge (void * addr , char * pattern , int nr_hpages ,
197
- uint64_t hpage_size )
197
+ char * __get_smap_entry (void * addr , const char * pattern , char * buf , size_t len )
198
198
{
199
- uint64_t thp = -1 ;
200
199
int ret ;
201
200
FILE * fp ;
202
- char buffer [ MAX_LINE_LENGTH ] ;
201
+ char * entry = NULL ;
203
202
char addr_pattern [MAX_LINE_LENGTH ];
204
203
205
204
ret = snprintf (addr_pattern , MAX_LINE_LENGTH , "%08lx-" ,
@@ -211,23 +210,40 @@ bool __check_huge(void *addr, char *pattern, int nr_hpages,
211
210
if (!fp )
212
211
ksft_exit_fail_msg ("%s: Failed to open file %s\n" , __func__ , SMAP_FILE_PATH );
213
212
214
- if (!check_for_pattern (fp , addr_pattern , buffer , sizeof ( buffer ) ))
213
+ if (!check_for_pattern (fp , addr_pattern , buf , len ))
215
214
goto err_out ;
216
215
217
- /*
218
- * Fetch the pattern in the same block and check the number of
219
- * hugepages.
220
- */
221
- if (!check_for_pattern (fp , pattern , buffer , sizeof (buffer )))
216
+ /* Fetch the pattern in the same block */
217
+ if (!check_for_pattern (fp , pattern , buf , len ))
222
218
goto err_out ;
223
219
224
- snprintf (addr_pattern , MAX_LINE_LENGTH , "%s%%9ld kB" , pattern );
220
+ /* Trim trailing newline */
221
+ entry = strchr (buf , '\n' );
222
+ if (entry )
223
+ * entry = '\0' ;
225
224
226
- if (sscanf (buffer , addr_pattern , & thp ) != 1 )
227
- ksft_exit_fail_msg ("Reading smap error\n" );
225
+ entry = buf + strlen (pattern );
228
226
229
227
err_out :
230
228
fclose (fp );
229
+ return entry ;
230
+ }
231
+
232
+ bool __check_huge (void * addr , char * pattern , int nr_hpages ,
233
+ uint64_t hpage_size )
234
+ {
235
+ char buffer [MAX_LINE_LENGTH ];
236
+ uint64_t thp = -1 ;
237
+ char * entry ;
238
+
239
+ entry = __get_smap_entry (addr , pattern , buffer , sizeof (buffer ));
240
+ if (!entry )
241
+ goto err_out ;
242
+
243
+ if (sscanf (entry , "%9" SCNu64 " kB" , & thp ) != 1 )
244
+ ksft_exit_fail_msg ("Reading smap error\n" );
245
+
246
+ err_out :
231
247
return thp == (nr_hpages * (hpage_size >> 10 ));
232
248
}
233
249
0 commit comments