From 1ba7e2b20b115e8884cc2e8ac7c1443dfe7c6c6f Mon Sep 17 00:00:00 2001 From: Gilles Gouaillardet Date: Mon, 4 Jul 2016 16:43:30 +0900 Subject: [PATCH] mpool/hugepage: set mntent API instead of manually parsing /proc/mounts Refs open-mpi/ompi#1822 --- .../mpool/hugepage/mpool_hugepage_component.c | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/opal/mca/mpool/hugepage/mpool_hugepage_component.c b/opal/mca/mpool/hugepage/mpool_hugepage_component.c index 067f97c64c..54f332d7d5 100644 --- a/opal/mca/mpool/hugepage/mpool_hugepage_component.c +++ b/opal/mca/mpool/hugepage/mpool_hugepage_component.c @@ -55,6 +55,9 @@ #ifdef HAVE_SYS_MMAN_H #include #endif +#ifdef HAVE_MNTENT_H +#include +#endif #include @@ -200,33 +203,34 @@ static int page_compare (opal_list_item_t **a, opal_list_item_t **b) { } static void mca_mpool_hugepage_find_hugepages (void) { +#ifdef HAVE_MNTENT_H mca_mpool_hugepage_hugepage_t *hp; FILE *fh; - char *path; - char buffer[1024]; - char *ctx, *tok; + struct mntent *mntent; + char *opts, *tok, *ctx; - fh = fopen ("/proc/mounts", "r"); + fh = setmntent ("/proc/mounts", "r"); if (NULL == fh) { return; } - while (fgets (buffer, 1024, fh)) { + while (NULL != (mntent = getmntent(fh))) { unsigned long page_size = 0; - (void) strtok_r (buffer, " ", &ctx); - path = strtok_r (NULL, " ", &ctx); - tok = strtok_r (NULL, " ", &ctx); - - if (0 != strcmp (tok, "hugetlbfs")) { + if (0 != strcmp(mntent->mnt_type, "hugetlbfs")) { continue; } - tok = strtok_r (NULL, " ", &ctx); - tok = strtok_r (tok, ",", &ctx); + opts = strdup(mntent->mnt_opts); + if (NULL == opts) { + break; + } + + tok = strtok_r (opts, ",", &ctx); do { if (0 == strncmp (tok, "pagesize", 8)) { + free(opts); break; } tok = strtok_r (NULL, ",", &ctx); @@ -236,15 +240,16 @@ static void mca_mpool_hugepage_find_hugepages (void) { #if defined(USE_STATFS) struct statfs info; - statfs (path, &info); + statfs (mntent->mnt_dir, &info); #elif defined(HAVE_STATVFS) struct statvfs info; - statvfs (path, &info); + statvfs (mntent->mnt_dir, &info); #endif page_size = info.f_bsize; } else { (void) sscanf (tok, "pagesize=%lu", &page_size); } + free(opts); if (0 == page_size) { /* could not get page size */ @@ -256,7 +261,7 @@ static void mca_mpool_hugepage_find_hugepages (void) { break; } - hp->path = strdup (path); + hp->path = strdup (mntent->mnt_dir); hp->page_size = page_size; 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) { opal_list_sort (&mca_mpool_hugepage_component.huge_pages, page_compare); - fclose (fh); + endmntent (fh); +#endif } static int mca_mpool_hugepage_query (const char *hints, int *priority_out,