[PATCH 5/6] mem: try to parse /proc/meminfo to get huge page size

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Instead of relying on a platform-specific default huge page size, try to
parse /proc/meminfo to get the huge page size when it is not explicitly
specified as an option.

Signed-off-by: Vincent Fu <vincent.fu@xxxxxxxxxxx>
---
 memory.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 49 insertions(+), 2 deletions(-)

diff --git a/memory.c b/memory.c
index 6cf73333..3b58e0bc 100644
--- a/memory.c
+++ b/memory.c
@@ -2,6 +2,7 @@
  * Memory helpers
  */
 #include <fcntl.h>
+#include <ctype.h>
 #include <unistd.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
@@ -60,13 +61,59 @@ int fio_pin_memory(struct thread_data *td)
 	return 0;
 }
 
+static unsigned int get_hugepage_size(struct thread_data *td)
+{
+	char buf[2048];
+	FILE *fptr;
+	size_t ret;
+	char *start;
+	unsigned int val;
+
+	if (fio_option_is_set(&td->o, hugepage_size))
+		return td->o.hugepage_size;
+
+	fptr = fopen("/proc/meminfo", "r");
+	if (!fptr)
+		return td->o.hugepage_size;
+
+	ret = fread(buf, 1, sizeof(buf)-1, fptr);
+	if (!ret)
+		goto out;
+	buf[ret] = 0;
+
+	start = strstr(buf, "Hugepagesize:");
+	if (!start)
+		goto out;
+	start += strlen("Hugepagesize:");
+	val = strtoul(start, NULL, 10);
+	if (!val)
+		goto out;
+
+	/* skip all the spaces and digits */
+	while (*start == ' ' || isdigit(*start))
+		start++;
+
+	/* make sure it ends with kB */
+	if (*start++ != 'k')
+		goto out;
+	if (*start++ != 'B')
+		goto out;
+
+	td->o.hugepage_size = val * 1024;
+	dprint(FD_MEM, "parsed hugepagesize from /proc/meminfo %u\n", td->o.hugepage_size);
+
+out:
+	fclose(fptr);
+	return td->o.hugepage_size;
+}
+
 static int alloc_mem_shm(struct thread_data *td, unsigned int total_mem)
 {
 #ifndef CONFIG_NO_SHM
 	int flags = IPC_CREAT | S_IRUSR | S_IWUSR;
 
 	if (td->o.mem_type == MEM_SHMHUGE) {
-		unsigned long mask = td->o.hugepage_size - 1;
+		unsigned long mask = get_hugepage_size(td) - 1;
 
 		flags |= SHM_HUGETLB;
 		total_mem = (total_mem + mask) & ~mask;
@@ -128,7 +175,7 @@ static int alloc_mem_mmap(struct thread_data *td, size_t total_mem)
 	td->mmapfd = -1;
 
 	if (td->o.mem_type == MEM_MMAPHUGE) {
-		unsigned long mask = td->o.hugepage_size - 1;
+		unsigned long mask = get_hugepage_size(td) - 1;
 
 		/* TODO: make sure the file is a real hugetlbfs file */
 		if (!td->o.mmapfile)
-- 
2.25.1




[Index of Archives]     [Linux Kernel]     [Linux SCSI]     [Linux IDE]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux SCSI]

  Powered by Linux