55
55
#ifdef HAVE_SYS_MMAN_H
56
56
#include <sys/mman.h>
57
57
#endif
58
+ #ifdef HAVE_MNTENT_H
59
+ #include <mntent.h>
60
+ #endif
58
61
59
62
#include <fcntl.h>
60
63
@@ -200,33 +203,34 @@ static int page_compare (opal_list_item_t **a, opal_list_item_t **b) {
200
203
}
201
204
202
205
static void mca_mpool_hugepage_find_hugepages (void ) {
206
+ #ifdef HAVE_MNTENT_H
203
207
mca_mpool_hugepage_hugepage_t * hp ;
204
208
FILE * fh ;
205
- char * path ;
206
- char buffer [1024 ];
207
- char * ctx , * tok ;
209
+ struct mntent * mntent ;
210
+ char * opts , * tok , * ctx ;
208
211
209
- fh = fopen ("/proc/mounts" , "r" );
212
+ fh = setmntent ("/proc/mounts" , "r" );
210
213
if (NULL == fh ) {
211
214
return ;
212
215
}
213
216
214
- while (fgets ( buffer , 1024 , fh )) {
217
+ while (NULL != ( mntent = getmntent ( fh ) )) {
215
218
unsigned long page_size = 0 ;
216
219
217
- (void ) strtok_r (buffer , " " , & ctx );
218
- path = strtok_r (NULL , " " , & ctx );
219
- tok = strtok_r (NULL , " " , & ctx );
220
-
221
- if (0 != strcmp (tok , "hugetlbfs" )) {
220
+ if (0 != strcmp (mntent -> mnt_type , "hugetlbfs" )) {
222
221
continue ;
223
222
}
224
223
225
- tok = strtok_r (NULL , " " , & ctx );
226
- tok = strtok_r (tok , "," , & ctx );
224
+ opts = strdup (mntent -> mnt_opts );
225
+ if (NULL == opts ) {
226
+ break ;
227
+ }
228
+
229
+ tok = strtok_r (opts , "," , & ctx );
227
230
228
231
do {
229
232
if (0 == strncmp (tok , "pagesize" , 8 )) {
233
+ free (opts );
230
234
break ;
231
235
}
232
236
tok = strtok_r (NULL , "," , & ctx );
@@ -236,15 +240,16 @@ static void mca_mpool_hugepage_find_hugepages (void) {
236
240
#if defined(USE_STATFS )
237
241
struct statfs info ;
238
242
239
- statfs (path , & info );
243
+ statfs (mntent -> mnt_dir , & info );
240
244
#elif defined(HAVE_STATVFS )
241
245
struct statvfs info ;
242
- statvfs (path , & info );
246
+ statvfs (mntent -> mnt_dir , & info );
243
247
#endif
244
248
page_size = info .f_bsize ;
245
249
} else {
246
250
(void ) sscanf (tok , "pagesize=%lu" , & page_size );
247
251
}
252
+ free (opts );
248
253
249
254
if (0 == page_size ) {
250
255
/* could not get page size */
@@ -256,7 +261,7 @@ static void mca_mpool_hugepage_find_hugepages (void) {
256
261
break ;
257
262
}
258
263
259
- hp -> path = strdup (path );
264
+ hp -> path = strdup (mntent -> mnt_dir );
260
265
hp -> page_size = page_size ;
261
266
262
267
OPAL_OUTPUT_VERBOSE ((MCA_BASE_VERBOSE_INFO , opal_mpool_base_framework .framework_output ,
@@ -268,7 +273,8 @@ static void mca_mpool_hugepage_find_hugepages (void) {
268
273
269
274
opal_list_sort (& mca_mpool_hugepage_component .huge_pages , page_compare );
270
275
271
- fclose (fh );
276
+ endmntent (fh );
277
+ #endif
272
278
}
273
279
274
280
static int mca_mpool_hugepage_query (const char * hints , int * priority_out ,
0 commit comments