aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDamjan Marion <damarion@cisco.com>2018-10-24 12:56:32 +0200
committerDave Barach <openvpp@barachs.net>2018-10-24 13:26:41 +0000
commit9787f5f90a6802e39fa7954c344ba89b03a6c4af (patch)
tree0035506066ab4cadec411fc19d8af6f3f5481705
parenta35fc46c0ae6e6d21b35413bdc993171265fd4e5 (diff)
vppinfra: autodetect default hugepage size
Change-Id: I5ff713ad0b254c74c5622e3b9425cca365b5ee97 Signed-off-by: Damjan Marion <damarion@cisco.com>
-rw-r--r--src/plugins/dpdk/device/init.c8
-rw-r--r--src/vppinfra/linux/mem.c35
-rw-r--r--src/vppinfra/linux/sysfs.c29
-rw-r--r--src/vppinfra/mem.h1
-rw-r--r--src/vppinfra/mem_dlmalloc.c27
-rw-r--r--src/vppinfra/mem_mheap.c27
-rw-r--r--src/vppinfra/pmalloc.c18
7 files changed, 64 insertions, 81 deletions
diff --git a/src/plugins/dpdk/device/init.c b/src/plugins/dpdk/device/init.c
index 6553fca77ec..b9b3b15f2dc 100644
--- a/src/plugins/dpdk/device/init.c
+++ b/src/plugins/dpdk/device/init.c
@@ -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* */
diff --git a/src/vppinfra/linux/mem.c b/src/vppinfra/linux/mem.c
index 2fbda966bc0..6a551ec0554 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 199e6bbfd69..c45897fc83b 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_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;
diff --git a/src/vppinfra/mem.h b/src/vppinfra/mem.h
index 04c26d218aa..813ff6c88d8 100644
--- a/src/vppinfra/mem.h
+++ b/src/vppinfra/mem.h
@@ -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);
diff --git a/src/vppinfra/mem_dlmalloc.c b/src/vppinfra/mem_dlmalloc.c
index 6268709bb36..e83e0e89d21 100644
--- a/src/vppinfra/mem_dlmalloc.c
+++ b/src/vppinfra/mem_dlmalloc.c
@@ -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)
{
diff --git a/src/vppinfra/mem_mheap.c b/src/vppinfra/mem_mheap.c
index f3387b72417..4077bf279fd 100644
--- a/src/vppinfra/mem_mheap.c
+++ b/src/vppinfra/mem_mheap.c
@@ -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)
{
diff --git a/src/vppinfra/pmalloc.c b/src/vppinfra/pmalloc.c
index 0f9df9c6641..a8b47d74c9b 100644
--- a/src/vppinfra/pmalloc.c
+++ b/src/vppinfra/pmalloc.c
@@ -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);