DPDK patches and discussions
 help / color / mirror / Atom feed
From: Jianfeng Tan <jianfeng.tan@intel.com>
To: dev@dpdk.org
Subject: [dpdk-dev] [PATCH v2] mem: calculate space left in a hugetlbfs
Date: Thu, 12 Nov 2015 10:10:55 +0800	[thread overview]
Message-ID: <1447294255-69446-1-git-send-email-jianfeng.tan@intel.com> (raw)
In-Reply-To: <1447287477-49292-1-git-send-email-jianfeng.tan@intel.com>

This patch enables calculating space left in a hugetlbfs.
There are three sources to get the information: 1. from
sysfs; 2. from option size specified when mount; 3. use
statfs. We should use the minimum one of these three sizes.

Signed-off-by: Jianfeng Tan <jianfeng.tan@intel.com>
---
Changes in v2:
 - reword title
 - fix compiler error of v1

 lib/librte_eal/linuxapp/eal/eal_hugepage_info.c | 85 ++++++++++++++++++++++++-
 1 file changed, 84 insertions(+), 1 deletion(-)

diff --git a/lib/librte_eal/linuxapp/eal/eal_hugepage_info.c b/lib/librte_eal/linuxapp/eal/eal_hugepage_info.c
index 18858e2..8305a58 100644
--- a/lib/librte_eal/linuxapp/eal/eal_hugepage_info.c
+++ b/lib/librte_eal/linuxapp/eal/eal_hugepage_info.c
@@ -44,6 +44,8 @@
 #include <unistd.h>
 #include <errno.h>
 #include <sys/queue.h>
+#include <sys/vfs.h>
+#include <mntent.h>
 
 #include <rte_memory.h>
 #include <rte_memzone.h>
@@ -189,6 +191,70 @@ get_hugepage_dir(uint64_t hugepage_sz)
 	return retval;
 }
 
+/* Caller to make sure this mnt_dir exist
+ */
+static uint64_t
+get_hugetlbfs_mount_size(const char *mnt_dir)
+{
+	char *start, *end, *opt_size;
+	struct mntent *ent;
+	uint64_t size;
+	FILE *f;
+	int len;
+
+	f = setmntent("/proc/mounts", "r");
+	if (f == NULL) {
+		RTE_LOG(ERR, EAL, "setmntent() error: %s\n",
+			strerror(errno));
+		return 0;
+	}
+	while (NULL != (ent = getmntent(f))) {
+		if (!strcmp(ent->mnt_dir, mnt_dir))
+			break;
+	}
+
+	start = hasmntopt(ent, "size");
+	if (start == NULL) {
+		RTE_LOG(DEBUG, EAL, "option size not specified for %s\n",
+			mnt_dir);
+		size = 0;
+		goto end;
+	}
+	start += strlen("size=");
+	end = strstr(start, ",");
+	if (end != NULL)
+		len = end - start;
+	else
+		len = strlen(start);
+	opt_size = strndup(start, len);
+	size = rte_str_to_size(opt_size);
+	free(opt_size);
+
+end:
+	endmntent(f);
+	return size;
+}
+
+/* Caller to make sure this mount has option size
+ * so that statfs is not zero.
+ */
+static uint64_t
+get_hugetlbfs_free_size(const char *mnt_dir)
+{
+	int r;
+	struct statfs stats;
+
+	r = statfs(mnt_dir, &stats);
+	if (r != 0) {
+		RTE_LOG(ERR, EAL, "statfs() error: %s\n",
+			strerror(errno));
+		return 0;
+	}
+
+	return stats.f_bfree * stats.f_bsize;
+}
+
+
 /*
  * Clear the hugepage directory of whatever hugepage files
  * there are. Checks if the file is locked (i.e.
@@ -329,9 +395,26 @@ eal_hugepage_info_init(void)
 		if (clear_hugedir(hpi->hugedir) == -1)
 			break;
 
+		/* there are three souces of how much space left in a
+		 * hugetlbfs dir.
+		 */
+		uint64_t sz_left, sz_sysfs, sz_option, sz_statfs;
+
+		sz_sysfs = get_num_hugepages(dirent->d_name) *
+			hpi->hugepage_sz;
+		sz_left = sz_sysfs;
+		sz_option = get_hugetlbfs_mount_size(hpi->hugedir);
+		if (sz_option) {
+			sz_statfs = get_hugetlbfs_free_size(hpi->hugedir);
+			sz_left = RTE_MIN(sz_sysfs, sz_statfs);
+			RTE_LOG(INFO, EAL, "sz_sysfs: %"PRIu64", sz_option: "
+					"%"PRIu64", sz_statfs: %"PRIu64"\n",
+					sz_sysfs, sz_option, sz_statfs);
+		}
+
 		/* for now, put all pages into socket 0,
 		 * later they will be sorted */
-		hpi->num_pages[0] = get_num_hugepages(dirent->d_name);
+		hpi->num_pages[0] = sz_left / hpi->hugepage_sz;
 
 #ifndef RTE_ARCH_64
 		/* for 32-bit systems, limit number of hugepages to
-- 
2.1.4

  reply	other threads:[~2015-11-12  9:11 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-12  0:17 [dpdk-dev] [PATCH] mem: fix how to " Jianfeng Tan
2015-11-12  2:10 ` Jianfeng Tan [this message]
2015-11-12 13:14   ` [dpdk-dev] [PATCH v2] mem: " Sergio Gonzalez Monroy
2015-11-12  7:48 ` [dpdk-dev] [PATCH] mem: fix how to " De Lara Guarch, Pablo
2015-11-12  1:57   ` [dpdk-dev] [PATCH v2] mem: " Jianfeng Tan
2015-11-12 17:38 ` [dpdk-dev] [PATCH] mem: fix how to " Stephen Hemminger
2015-11-12 17:49   ` Thomas Monjalon
2015-11-18  2:42 ` [dpdk-dev] [PATCH v3] mem: " Jianfeng Tan
2015-11-18 10:28   ` Sergio Gonzalez Monroy
2015-12-21  8:34   ` Qiu, Michael

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1447294255-69446-1-git-send-email-jianfeng.tan@intel.com \
    --to=jianfeng.tan@intel.com \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).