vppinfra: autodetect default hugepage size

Change-Id: I5ff713ad0b254c74c5622e3b9425cca365b5ee97
Signed-off-by: Damjan Marion <damarion@cisco.com>
diff --git a/src/plugins/dpdk/device/init.c b/src/plugins/dpdk/device/init.c
index 6553fca..b9b3b15 100644
--- a/src/plugins/dpdk/device/init.c
+++ b/src/plugins/dpdk/device/init.c
@@ -1284,15 +1284,17 @@
 	  /* *INDENT-ON* */
 	}
 
+      uword default_hugepage_sz = clib_mem_get_default_hugepage_size ();
       /* *INDENT-OFF* */
       clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
         {
 	  clib_error_t *e;
-
+	  uword n_pages;
 	  vec_validate(mem_by_socket, c);
+	  n_pages = round_pow2 (mem_by_socket[c], default_hugepage_sz);
+	  n_pages /= default_hugepage_sz;
 
-	  e = clib_sysfs_prealloc_hugepages(c, 0, mem_by_socket[c] / 2);
-	  if (e)
+	  if ((e = clib_sysfs_prealloc_hugepages(c, 0, n_pages)))
 	    clib_error_report (e);
       }));
       /* *INDENT-ON* */
diff --git a/src/vppinfra/linux/mem.c b/src/vppinfra/linux/mem.c
index 2fbda96..6a551ec 100644
--- a/src/vppinfra/linux/mem.c
+++ b/src/vppinfra/linux/mem.c
@@ -46,6 +46,41 @@
 #define F_SEAL_WRITE    0x0008	/* prevent writes */
 #endif
 
+
+uword
+clib_mem_get_page_size (void)
+{
+  return getpagesize ();
+}
+
+uword
+clib_mem_get_default_hugepage_size (void)
+{
+  unformat_input_t input;
+  static u32 size = 0;
+  int fd;
+
+  if (size)
+    goto done;
+
+  if ((fd = open ("/proc/meminfo", 0)) == -1)
+    return 0;
+
+  unformat_init_clib_file (&input, fd);
+
+  while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (&input, "Hugepagesize:%_%u kB", &size))
+	;
+      else
+	unformat_skip_line (&input);
+    }
+  unformat_free (&input);
+  close (fd);
+done:
+  return 1024ULL * size;
+}
+
 u64
 clib_mem_vm_get_page_size (int fd)
 {
diff --git a/src/vppinfra/linux/sysfs.c b/src/vppinfra/linux/sysfs.c
index 199e6bb..c45897f 100644
--- a/src/vppinfra/linux/sysfs.c
+++ b/src/vppinfra/linux/sysfs.c
@@ -22,8 +22,6 @@
 #include <fcntl.h>
 #include <dirent.h>
 
-#define DEFAULT_HUGETLB_SIZE 2048
-
 clib_error_t *
 clib_sysfs_write (char *file_name, char *fmt, ...)
 {
@@ -121,8 +119,12 @@
   clib_error_t *error = 0;
   struct stat sb;
   u8 *p = 0;
-  int page_size = log2_page_size ? 1ULL << (log2_page_size - 10) :
-    DEFAULT_HUGETLB_SIZE;
+  uword page_size;
+
+  if (log2_page_size == 0)
+    log2_page_size = min_log2 (clib_mem_get_default_hugepage_size ());
+
+  page_size = 1ULL << (log2_page_size - 10);
 
   p = format (p, "/sys/devices/system/node/node%u%c", numa_node, 0);
 
@@ -168,8 +170,14 @@
   clib_error_t *error = 0;
   struct stat sb;
   u8 *p = 0;
-  int page_size = log2_page_size ? 1ULL << (log2_page_size - 10) :
-    DEFAULT_HUGETLB_SIZE;
+
+  uword page_size;
+
+  if (log2_page_size == 0)
+    log2_page_size = min_log2 (clib_mem_get_default_hugepage_size ());
+
+  page_size = 1ULL << (log2_page_size - 10);
+
 
   p = format (p, "/sys/devices/system/node/node%u%c", numa_node, 0);
 
@@ -232,8 +240,13 @@
 {
   clib_error_t *error = 0;
   int n, needed;
-  int page_size = log2_page_size ? 1ULL << (log2_page_size - 10) :
-    DEFAULT_HUGETLB_SIZE;
+  uword page_size;
+
+  if (log2_page_size == 0)
+    log2_page_size = min_log2 (clib_mem_get_default_hugepage_size ());
+
+  page_size = 1ULL << (log2_page_size - 10);
+
   error = clib_sysfs_get_free_hugepages (numa_node, log2_page_size, &n);
   if (error)
     return error;
diff --git a/src/vppinfra/mem.h b/src/vppinfra/mem.h
index 04c26d2..813ff6c 100644
--- a/src/vppinfra/mem.h
+++ b/src/vppinfra/mem.h
@@ -390,6 +390,7 @@
 clib_error_t *clib_mem_vm_ext_alloc (clib_mem_vm_alloc_t * a);
 void clib_mem_vm_ext_free (clib_mem_vm_alloc_t * a);
 u64 clib_mem_vm_get_page_size (int fd);
+uword clib_mem_get_default_hugepage_size (void);
 int clib_mem_vm_get_log2_page_size (int fd);
 u64 *clib_mem_vm_get_paddr (void *mem, int log2_page_size, int n_pages);
 
diff --git a/src/vppinfra/mem_dlmalloc.c b/src/vppinfra/mem_dlmalloc.c
index 6268709..e83e0e8 100644
--- a/src/vppinfra/mem_dlmalloc.c
+++ b/src/vppinfra/mem_dlmalloc.c
@@ -229,33 +229,6 @@
   return clib_mem_init (memory, memory_size);
 }
 
-#ifdef CLIB_LINUX_KERNEL
-#include <asm/page.h>
-
-uword
-clib_mem_get_page_size (void)
-{
-  return PAGE_SIZE;
-}
-#endif
-
-#ifdef CLIB_UNIX
-uword
-clib_mem_get_page_size (void)
-{
-  return getpagesize ();
-}
-#endif
-
-/* Make a guess for standalone. */
-#ifdef CLIB_STANDALONE
-uword
-clib_mem_get_page_size (void)
-{
-  return 4096;
-}
-#endif
-
 u8 *
 format_clib_mem_usage (u8 * s, va_list * va)
 {
diff --git a/src/vppinfra/mem_mheap.c b/src/vppinfra/mem_mheap.c
index f3387b7..4077bf2 100644
--- a/src/vppinfra/mem_mheap.c
+++ b/src/vppinfra/mem_mheap.c
@@ -114,33 +114,6 @@
   return heap;
 }
 
-#ifdef CLIB_LINUX_KERNEL
-#include <asm/page.h>
-
-uword
-clib_mem_get_page_size (void)
-{
-  return PAGE_SIZE;
-}
-#endif
-
-#ifdef CLIB_UNIX
-uword
-clib_mem_get_page_size (void)
-{
-  return getpagesize ();
-}
-#endif
-
-/* Make a guess for standalone. */
-#ifdef CLIB_STANDALONE
-uword
-clib_mem_get_page_size (void)
-{
-  return 4096;
-}
-#endif
-
 u8 *
 format_clib_mem_usage (u8 * s, va_list * va)
 {
diff --git a/src/vppinfra/pmalloc.c b/src/vppinfra/pmalloc.c
index 0f9df9c..a8b47d7 100644
--- a/src/vppinfra/pmalloc.c
+++ b/src/vppinfra/pmalloc.c
@@ -55,26 +55,12 @@
 int
 clib_pmalloc_init (clib_pmalloc_main_t * pm, uword size)
 {
-  struct stat st;
   uword off, pagesize;
-  int fd;
 
   ASSERT (pm->error == 0);
 
-  pm->log2_page_sz = 21;
-  pm->error = clib_mem_create_hugetlb_fd ("detect_hugepage_size", &fd);
-
-  if (pm->error)
-    return -1;
-
-  if (fd != -1)
-    {
-      if (fstat (fd, &st) == -1)
-	pm->log2_page_sz = min_log2 (st.st_blksize);
-      close (fd);
-    }
-
-  pagesize = 1ULL << pm->log2_page_sz;
+  pagesize = clib_mem_get_default_hugepage_size ();
+  pm->log2_page_sz = min_log2 (pagesize);
 
   size = size ? size : ((u64) DEFAULT_RESERVED_MB) << 20;
   size = round_pow2 (size, pagesize);