1
1

mpool/hugepage: set mntent API instead of manually parsing /proc/mounts

Refs open-mpi/ompi#1822
Этот коммит содержится в:
Gilles Gouaillardet 2016-07-04 16:43:30 +09:00
родитель ae8444682f
Коммит 1ba7e2b20b

Просмотреть файл

@ -55,6 +55,9 @@
#ifdef HAVE_SYS_MMAN_H #ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h> #include <sys/mman.h>
#endif #endif
#ifdef HAVE_MNTENT_H
#include <mntent.h>
#endif
#include <fcntl.h> #include <fcntl.h>
@ -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) { static void mca_mpool_hugepage_find_hugepages (void) {
#ifdef HAVE_MNTENT_H
mca_mpool_hugepage_hugepage_t *hp; mca_mpool_hugepage_hugepage_t *hp;
FILE *fh; FILE *fh;
char *path; struct mntent *mntent;
char buffer[1024]; char *opts, *tok, *ctx;
char *ctx, *tok;
fh = fopen ("/proc/mounts", "r"); fh = setmntent ("/proc/mounts", "r");
if (NULL == fh) { if (NULL == fh) {
return; return;
} }
while (fgets (buffer, 1024, fh)) { while (NULL != (mntent = getmntent(fh))) {
unsigned long page_size = 0; unsigned long page_size = 0;
(void) strtok_r (buffer, " ", &ctx); if (0 != strcmp(mntent->mnt_type, "hugetlbfs")) {
path = strtok_r (NULL, " ", &ctx);
tok = strtok_r (NULL, " ", &ctx);
if (0 != strcmp (tok, "hugetlbfs")) {
continue; continue;
} }
tok = strtok_r (NULL, " ", &ctx); opts = strdup(mntent->mnt_opts);
tok = strtok_r (tok, ",", &ctx); if (NULL == opts) {
break;
}
tok = strtok_r (opts, ",", &ctx);
do { do {
if (0 == strncmp (tok, "pagesize", 8)) { if (0 == strncmp (tok, "pagesize", 8)) {
free(opts);
break; break;
} }
tok = strtok_r (NULL, ",", &ctx); tok = strtok_r (NULL, ",", &ctx);
@ -236,15 +240,16 @@ static void mca_mpool_hugepage_find_hugepages (void) {
#if defined(USE_STATFS) #if defined(USE_STATFS)
struct statfs info; struct statfs info;
statfs (path, &info); statfs (mntent->mnt_dir, &info);
#elif defined(HAVE_STATVFS) #elif defined(HAVE_STATVFS)
struct statvfs info; struct statvfs info;
statvfs (path, &info); statvfs (mntent->mnt_dir, &info);
#endif #endif
page_size = info.f_bsize; page_size = info.f_bsize;
} else { } else {
(void) sscanf (tok, "pagesize=%lu", &page_size); (void) sscanf (tok, "pagesize=%lu", &page_size);
} }
free(opts);
if (0 == page_size) { if (0 == page_size) {
/* could not get page size */ /* could not get page size */
@ -256,7 +261,7 @@ static void mca_mpool_hugepage_find_hugepages (void) {
break; break;
} }
hp->path = strdup (path); hp->path = strdup (mntent->mnt_dir);
hp->page_size = page_size; hp->page_size = page_size;
OPAL_OUTPUT_VERBOSE((MCA_BASE_VERBOSE_INFO, opal_mpool_base_framework.framework_output, 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); 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, static int mca_mpool_hugepage_query (const char *hints, int *priority_out,