summaryrefslogtreecommitdiffstats
path: root/src/vppinfra/std-formats.c
diff options
context:
space:
mode:
authorDamjan Marion <damarion@cisco.com>2020-09-11 22:13:46 +0200
committerDamjan Marion <damarion@cisco.com>2020-09-14 14:04:06 +0200
commitb509504e6b6eb8590d2d7dad2447c1bd933ce94c (patch)
treef8f307985be5ea96d4bd908638903919358eb370 /src/vppinfra/std-formats.c
parent999395cd6644a297c01dd6de033cf1ffb4e9951b (diff)
vppinfra: add clib_log2_page_sz and format/unformat functions
Type: improvement Change-Id: Ie44dbf9396cfed19dba153810b7bd76ce5377cd4 Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'src/vppinfra/std-formats.c')
-rw-r--r--src/vppinfra/std-formats.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/vppinfra/std-formats.c b/src/vppinfra/std-formats.c
index 62d309e2fb0..f4892d6d6f4 100644
--- a/src/vppinfra/std-formats.c
+++ b/src/vppinfra/std-formats.c
@@ -268,6 +268,74 @@ unformat_memory_size (unformat_input_t * input, va_list * va)
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 *