vppinfra: autodetect default hugepage size 99/15499/2
authorDamjan Marion <damarion@cisco.com>
Wed, 24 Oct 2018 10:56:32 +0000 (12:56 +0200)
committerDave Barach <openvpp@barachs.net>
Wed, 24 Oct 2018 13:26:41 +0000 (13:26 +0000)
Change-Id: I5ff713ad0b254c74c5622e3b9425cca365b5ee97
Signed-off-by: Damjan Marion <damarion@cisco.com>
src/plugins/dpdk/device/init.c
src/vppinfra/linux/mem.c
src/vppinfra/linux/sysfs.c
src/vppinfra/mem.h
src/vppinfra/mem_dlmalloc.c
src/vppinfra/mem_mheap.c
src/vppinfra/pmalloc.c

index 6553fca..b9b3b15 100644 (file)
@@ -1284,15 +1284,17 @@ dpdk_config (vlib_main_t * vm, unformat_input_t * input)
          /* *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* */
index 2fbda96..6a551ec 100644 (file)
 #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)
 {
index 199e6bb..c45897f 100644 (file)
@@ -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_sysfs_set_nr_hugepages (int numa_node, int log2_page_size, int nr)
   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_sysfs_get_xxx_hugepages (char *type, int numa_node,
   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_sysfs_prealloc_hugepages (int numa_node, int log2_page_size, int nr)
 {
   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;
index 04c26d2..813ff6c 100644 (file)
@@ -390,6 +390,7 @@ clib_error_t *clib_mem_create_hugetlb_fd (char *name, int *fdp);
 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);
 
index 6268709..e83e0e8 100644 (file)
@@ -229,33 +229,6 @@ clib_mem_init_thread_safe (void *memory, uword memory_size)
   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)
 {
index f3387b7..4077bf2 100644 (file)
@@ -114,33 +114,6 @@ clib_mem_init_thread_safe (void *memory, uword memory_size)
   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)
 {
index 0f9df9c..a8b47d7 100644 (file)
@@ -55,26 +55,12 @@ pmalloc_validate_numa_node (u32 * numa_node)
 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);