aboutsummaryrefslogtreecommitdiffstats
path: root/src/vppinfra
diff options
context:
space:
mode:
Diffstat (limited to 'src/vppinfra')
-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
6 files changed, 59 insertions, 78 deletions
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);