vppinfra: add clib_log2_page_sz and format/unformat functions
Type: improvement
Change-Id: Ie44dbf9396cfed19dba153810b7bd76ce5377cd4
Signed-off-by: Damjan Marion <damarion@cisco.com>
diff --git a/src/vppinfra/format.h b/src/vppinfra/format.h
index 678029e..e71c7b3 100644
--- a/src/vppinfra/format.h
+++ b/src/vppinfra/format.h
@@ -298,6 +298,12 @@
/* Unparse memory size e.g. 100, 100k, 100m, 100g. */
u8 *format_memory_size (u8 * s, va_list * va);
+/* Parse memory page size e.g. 4K, 2M */
+unformat_function_t unformat_log2_page_size;
+
+/* Unparse memory page size e.g. 4K, 2M */
+u8 *format_log2_page_size (u8 * s, va_list * va);
+
/* Format c identifier: e.g. a_name -> "a name". */
u8 *format_c_identifier (u8 * s, va_list * va);
diff --git a/src/vppinfra/linux/mem.c b/src/vppinfra/linux/mem.c
index 5826a6a..c581181 100644
--- a/src/vppinfra/linux/mem.c
+++ b/src/vppinfra/linux/mem.c
@@ -103,7 +103,8 @@
}
void
-clib_mem_vm_randomize_va (uword * requested_va, u32 log2_page_size)
+clib_mem_vm_randomize_va (uword * requested_va,
+ clib_mem_page_sz_t log2_page_size)
{
u8 bit_mask = 15;
@@ -345,7 +346,7 @@
}
uword
-clib_mem_vm_reserve (uword start, uword size, u32 log2_page_sz)
+clib_mem_vm_reserve (uword start, uword size, clib_mem_page_sz_t log2_page_sz)
{
uword off, pagesize = 1ULL << log2_page_sz;
int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS;
diff --git a/src/vppinfra/mem.h b/src/vppinfra/mem.h
index c97ee8a..31c5fd8 100644
--- a/src/vppinfra/mem.h
+++ b/src/vppinfra/mem.h
@@ -54,6 +54,23 @@
#define CLIB_MAX_MHEAPS 256
#define CLIB_MAX_NUMAS 8
+typedef enum
+{
+ CLIB_MEM_PAGE_SZ_UNKNOWN = 0,
+ CLIB_MEM_PAGE_SZ_DEFAULT = 1,
+ CLIB_MEM_PAGE_SZ_DEFAULT_HUGE = 2,
+ CLIB_MEM_PAGE_SZ_4K = 12,
+ CLIB_MEM_PAGE_SZ_16K = 14,
+ CLIB_MEM_PAGE_SZ_64K = 16,
+ CLIB_MEM_PAGE_SZ_1M = 20,
+ CLIB_MEM_PAGE_SZ_2M = 21,
+ CLIB_MEM_PAGE_SZ_16M = 24,
+ CLIB_MEM_PAGE_SZ_32M = 25,
+ CLIB_MEM_PAGE_SZ_512M = 29,
+ CLIB_MEM_PAGE_SZ_1G = 30,
+ CLIB_MEM_PAGE_SZ_16G = 34,
+} clib_mem_page_sz_t;
+
/* Unspecified NUMA socket */
#define VEC_NUMA_UNSPECIFIED (0xFF)
@@ -410,7 +427,8 @@
u64 clib_mem_get_fd_page_size (int fd);
uword clib_mem_get_default_hugepage_size (void);
int clib_mem_get_fd_log2_page_size (int fd);
-uword clib_mem_vm_reserve (uword start, uword size, u32 log2_page_sz);
+uword clib_mem_vm_reserve (uword start, uword size,
+ clib_mem_page_sz_t log2_page_sz);
u64 *clib_mem_vm_get_paddr (void *mem, int log2_page_size, int n_pages);
void clib_mem_destroy_mspace (void *mspace);
void clib_mem_destroy (void);
@@ -425,7 +443,8 @@
} clib_mem_vm_map_t;
clib_error_t *clib_mem_vm_ext_map (clib_mem_vm_map_t * a);
-void clib_mem_vm_randomize_va (uword * requested_va, u32 log2_page_size);
+void clib_mem_vm_randomize_va (uword * requested_va,
+ clib_mem_page_sz_t log2_page_size);
void mheap_trace (void *v, int enable);
uword clib_mem_trace_enable_disable (uword enable);
void clib_mem_trace (int enable);
diff --git a/src/vppinfra/pmalloc.c b/src/vppinfra/pmalloc.c
index e0f3f3a..870f364 100644
--- a/src/vppinfra/pmalloc.c
+++ b/src/vppinfra/pmalloc.c
@@ -584,24 +584,6 @@
}
static u8 *
-format_log2_page_size (u8 * s, va_list * va)
-{
- u32 log2_page_sz = va_arg (*va, u32);
-
- if (log2_page_sz >= 30)
- return format (s, "%uGB", 1 << (log2_page_sz - 30));
-
- if (log2_page_sz >= 20)
- return format (s, "%uMB", 1 << (log2_page_sz - 20));
-
- if (log2_page_sz >= 10)
- return format (s, "%uKB", 1 << (log2_page_sz - 10));
-
- return format (s, "%uB", 1 << log2_page_sz);
-}
-
-
-static u8 *
format_pmalloc_page (u8 * s, va_list * va)
{
clib_pmalloc_page_t *pp = va_arg (*va, clib_pmalloc_page_t *);
diff --git a/src/vppinfra/std-formats.c b/src/vppinfra/std-formats.c
index 62d309e..f4892d6 100644
--- a/src/vppinfra/std-formats.c
+++ b/src/vppinfra/std-formats.c
@@ -268,6 +268,74 @@
return 1;
}
+/* Unparse memory page size e.g. 4K, 2M */
+u8 *
+format_log2_page_size (u8 * s, va_list * va)
+{
+ clib_mem_page_sz_t log2_page_sz = va_arg (*va, clib_mem_page_sz_t);
+
+ if (log2_page_sz == CLIB_MEM_PAGE_SZ_UNKNOWN)
+ return format (s, "unknown");
+
+ if (log2_page_sz == CLIB_MEM_PAGE_SZ_DEFAULT)
+ return format (s, "default");
+
+ if (log2_page_sz == CLIB_MEM_PAGE_SZ_DEFAULT_HUGE)
+ return format (s, "default-hugepage");
+
+ if (log2_page_sz >= 30)
+ return format (s, "%uG", 1 << (log2_page_sz - 30));
+
+ if (log2_page_sz >= 20)
+ return format (s, "%uM", 1 << (log2_page_sz - 20));
+
+ if (log2_page_sz >= 10)
+ return format (s, "%uK", 1 << (log2_page_sz - 10));
+
+ return format (s, "%u", 1 << log2_page_sz);
+}
+
+/* Parse memory page size e.g. 4K, 2M */
+uword
+unformat_log2_page_size (unformat_input_t * input, va_list * va)
+{
+ uword amount, shift, c;
+ clib_mem_page_sz_t *result = va_arg (*va, clib_mem_page_sz_t *);
+
+ if (unformat (input, "default"))
+ return CLIB_MEM_PAGE_SZ_DEFAULT;
+
+ if (unformat (input, "default-hugepage"))
+ return CLIB_MEM_PAGE_SZ_DEFAULT_HUGE;
+
+ if (!unformat (input, "%wd%_", &amount))
+ return CLIB_MEM_PAGE_SZ_UNKNOWN;
+
+ c = unformat_get_input (input);
+ switch (c)
+ {
+ case 'k':
+ case 'K':
+ shift = 10;
+ break;
+ case 'm':
+ case 'M':
+ shift = 20;
+ break;
+ case 'g':
+ case 'G':
+ shift = 30;
+ break;
+ default:
+ shift = 0;
+ unformat_put_input (input);
+ break;
+ }
+
+ *result = min_log2 (amount) + shift;
+ return 1;
+}
+
/* Format c identifier: e.g. a_name -> "a name".
Works for both vector names and null terminated c strings. */
u8 *