Dump the mbuf history to the console or to a file.

The dump will contain:
- Operation history for each mbuf
- Summary and statistics about all mbufs

Dump the history of all mbufs:
        testpmd> dump mbuf history all [file]

Dump the history of one mbuf pool:
        testpmd> dump mbuf history mempool <mp_name> [file]

Dump the history of one mbuf:
        testpmd> dump mbuf history one <mbuf_addr> [file]

Signed-off-by: Shani Peretz <[email protected]>
Signed-off-by: Thomas Monjalon <[email protected]>
---
 app/test-pmd/cmdline.c                      | 174 ++++++++++++++++++++
 doc/guides/testpmd_app_ug/testpmd_funcs.rst |  13 ++
 2 files changed, 187 insertions(+)

diff --git a/app/test-pmd/cmdline.c b/app/test-pmd/cmdline.c
index b6db44c65f..76b8841a5c 100644
--- a/app/test-pmd/cmdline.c
+++ b/app/test-pmd/cmdline.c
@@ -40,6 +40,7 @@
 #include <rte_gro.h>
 #endif
 #include <rte_mbuf_dyn.h>
+#include <rte_mbuf_history.h>
 #include <rte_trace.h>
 
 #include <cmdline_rdline.h>
@@ -296,6 +297,15 @@ static void cmd_help_long_parsed(void *parsed_result,
                        "dump log_types\n"
                        "    Dumps the log level for all the dpdk modules\n\n"
 
+                       "dump mbuf history all [file_name]\n"
+                       "    Dumps the mbuf history for all mempools\n\n"
+
+                       "dump mbuf history mempool <mempool_name> [file_name]\n"
+                       "    Dumps the history for a specific mempool\n\n"
+
+                       "dump mbuf history one <mbuf_addr> [file_name]\n"
+                       "    Dumps the history for a specific mbuf (use 
0x<address> format)\n\n"
+
                        "show port (port_id) speed_lanes capabilities"
                        "       Show speed lanes capabilities of a port.\n\n"
                );
@@ -9243,6 +9253,164 @@ static cmdline_parse_inst_t cmd_dump_named = {
        },
 };
 
+struct cmd_dump_mbuf_history_result {
+       cmdline_fixed_string_t dump;
+       cmdline_fixed_string_t mbuf;
+       cmdline_fixed_string_t history;
+       cmdline_fixed_string_t obj;
+       cmdline_fixed_string_t name;
+       cmdline_fixed_string_t addr;
+       cmdline_fixed_string_t file;
+};
+static cmdline_parse_token_string_t cmd_dump_mbuf_history_dump =
+       TOKEN_STRING_INITIALIZER(struct cmd_dump_mbuf_history_result, dump, 
"dump");
+static cmdline_parse_token_string_t cmd_dump_mbuf_history_mbuf =
+       TOKEN_STRING_INITIALIZER(struct cmd_dump_mbuf_history_result, mbuf, 
"mbuf");
+static cmdline_parse_token_string_t cmd_dump_mbuf_history_history =
+       TOKEN_STRING_INITIALIZER(struct cmd_dump_mbuf_history_result, history, 
"history");
+static cmdline_parse_token_string_t cmd_dump_mbuf_history_one =
+       TOKEN_STRING_INITIALIZER(struct cmd_dump_mbuf_history_result, obj, 
"one");
+static cmdline_parse_token_string_t cmd_dump_mbuf_history_mempool =
+       TOKEN_STRING_INITIALIZER(struct cmd_dump_mbuf_history_result, obj, 
"mempool");
+static cmdline_parse_token_string_t cmd_dump_mbuf_history_all =
+       TOKEN_STRING_INITIALIZER(struct cmd_dump_mbuf_history_result, obj, 
"all");
+static cmdline_parse_token_string_t cmd_dump_mbuf_history_name =
+       TOKEN_STRING_INITIALIZER(struct cmd_dump_mbuf_history_result, name, 
NULL);
+static cmdline_parse_token_string_t cmd_dump_mbuf_history_addr =
+       TOKEN_STRING_INITIALIZER(struct cmd_dump_mbuf_history_result, addr, 
NULL);
+static cmdline_parse_token_string_t cmd_dump_mbuf_history_file =
+       TOKEN_STRING_INITIALIZER(struct cmd_dump_mbuf_history_result, file, 
NULL);
+
+static void cmd_dump_mbuf_history_parsed(void *parsed_result,
+               struct cmdline *cl,
+               __rte_unused void *data)
+{
+       struct cmd_dump_mbuf_history_result *res = parsed_result;
+       FILE *out = stdout;
+
+       if (strcmp(res->file, "") != 0) {
+               out = fopen(res->file, "w");
+               if (out == NULL) {
+                       cmdline_printf(cl,
+                                       "Failed to open file '%s'\n",
+                                       res->file);
+                       return;
+               }
+       }
+
+       if (strcmp(res->obj, "all") == 0) {
+               rte_mbuf_history_dump_all(out);
+               if (out != stdout)
+                       cmdline_printf(cl,
+                                       "Dump written in '%s'\n",
+                                       res->file);
+       } else if (strcmp(res->obj, "mempool") == 0) {
+               struct rte_mempool *mp = rte_mempool_lookup(res->name);
+               if (mp == NULL) {
+                       cmdline_printf(cl,
+                                       "Failed to find mempool '%s'\n",
+                                       res->name);
+                       return;
+               }
+               rte_mbuf_history_dump_mempool(out, mp);
+               if (out != stdout)
+                       cmdline_printf(cl,
+                                       "Dump for mempool '%s' written in 
'%s'\n",
+                                       res->name, res->file);
+       } else if (strcmp(res->obj, "one") == 0) {
+               struct rte_mbuf *mbuf;
+               uintptr_t mbuf_addr;
+               if (sscanf(res->addr, "0x%" SCNxPTR, &mbuf_addr) != 1) {
+                       cmdline_printf(cl,
+                                       "Invalid mbuf pointer format. Use 
0x<address>\n");
+                       return;
+               }
+               mbuf = (struct rte_mbuf *)mbuf_addr;
+               rte_mbuf_history_dump(out, mbuf);
+               if (out != stdout)
+                       cmdline_printf(cl,
+                                       "Dump for mbuf '%p' written in '%s'\n",
+                                       mbuf, res->file);
+       }
+
+       if (out != stdout)
+               fclose(out);
+}
+
+static cmdline_parse_inst_t cmd_dump_mbuf_history_one_stdout = {
+       .f = cmd_dump_mbuf_history_parsed,
+       .help_str = "dump mbuf history one <address>",
+       .tokens = {
+               (void *)&cmd_dump_mbuf_history_dump,
+               (void *)&cmd_dump_mbuf_history_mbuf,
+               (void *)&cmd_dump_mbuf_history_history,
+               (void *)&cmd_dump_mbuf_history_one,
+               (void *)&cmd_dump_mbuf_history_addr,
+               NULL,
+       },
+};
+static cmdline_parse_inst_t cmd_dump_mbuf_history_one_to_file = {
+       .f = cmd_dump_mbuf_history_parsed,
+       .help_str = "dump mbuf history one <address> [file]",
+       .tokens = {
+               (void *)&cmd_dump_mbuf_history_dump,
+               (void *)&cmd_dump_mbuf_history_mbuf,
+               (void *)&cmd_dump_mbuf_history_history,
+               (void *)&cmd_dump_mbuf_history_one,
+               (void *)&cmd_dump_mbuf_history_addr,
+               (void *)&cmd_dump_mbuf_history_file,
+               NULL,
+       },
+};
+static cmdline_parse_inst_t cmd_dump_mbuf_history_mempool_stdout = {
+       .f = cmd_dump_mbuf_history_parsed,
+       .help_str = "dump mbuf history mempool <name>",
+       .tokens = {
+               (void *)&cmd_dump_mbuf_history_dump,
+               (void *)&cmd_dump_mbuf_history_mbuf,
+               (void *)&cmd_dump_mbuf_history_history,
+               (void *)&cmd_dump_mbuf_history_mempool,
+               (void *)&cmd_dump_mbuf_history_name,
+               NULL,
+       },
+};
+static cmdline_parse_inst_t cmd_dump_mbuf_history_mempool_to_file = {
+       .f = cmd_dump_mbuf_history_parsed,
+       .help_str = "dump mbuf history mempool <name> [file]",
+       .tokens = {
+               (void *)&cmd_dump_mbuf_history_dump,
+               (void *)&cmd_dump_mbuf_history_mbuf,
+               (void *)&cmd_dump_mbuf_history_history,
+               (void *)&cmd_dump_mbuf_history_mempool,
+               (void *)&cmd_dump_mbuf_history_name,
+               (void *)&cmd_dump_mbuf_history_file,
+               NULL,
+       },
+};
+static cmdline_parse_inst_t cmd_dump_mbuf_history_all_stdout = {
+       .f = cmd_dump_mbuf_history_parsed,
+       .help_str = "dump mbuf history all",
+       .tokens = {
+               (void *)&cmd_dump_mbuf_history_dump,
+               (void *)&cmd_dump_mbuf_history_mbuf,
+               (void *)&cmd_dump_mbuf_history_history,
+               (void *)&cmd_dump_mbuf_history_all,
+               NULL,
+       },
+};
+static cmdline_parse_inst_t cmd_dump_mbuf_history_all_to_file = {
+       .f = cmd_dump_mbuf_history_parsed,
+       .help_str = "dump mbuf history all [file]",
+       .tokens = {
+               (void *)&cmd_dump_mbuf_history_dump,
+               (void *)&cmd_dump_mbuf_history_mbuf,
+               (void *)&cmd_dump_mbuf_history_history,
+               (void *)&cmd_dump_mbuf_history_all,
+               (void *)&cmd_dump_mbuf_history_file,
+               NULL,
+       },
+};
+
 /* *** Filters Control *** */
 
 #define IPV4_ADDR_TO_UINT(ip_addr, ip) \
@@ -13982,6 +14150,12 @@ static cmdline_parse_ctx_t builtin_ctx[] = {
        &cmd_cleanup_txq_mbufs,
        &cmd_dump_simple,
        &cmd_dump_named,
+       &cmd_dump_mbuf_history_all_stdout,
+       &cmd_dump_mbuf_history_all_to_file,
+       &cmd_dump_mbuf_history_mempool_stdout,
+       &cmd_dump_mbuf_history_mempool_to_file,
+       &cmd_dump_mbuf_history_one_stdout,
+       &cmd_dump_mbuf_history_one_to_file,
        &cmd_flow,
        &cmd_show_port_meter_cap,
        &cmd_add_port_meter_profile_srtcm,
diff --git a/doc/guides/testpmd_app_ug/testpmd_funcs.rst 
b/doc/guides/testpmd_app_ug/testpmd_funcs.rst
index e4bef31d06..2e984bf5f1 100644
--- a/doc/guides/testpmd_app_ug/testpmd_funcs.rst
+++ b/doc/guides/testpmd_app_ug/testpmd_funcs.rst
@@ -633,6 +633,19 @@ Dumps the log level for all the dpdk modules::
 
    testpmd> dump log_types
 
+dump mbuf history
+~~~~~~~~~~~~~~~~~
+
+The mbuf history is tracked
+if enabled at compilation with ``RTE_MBUF_HISTORY_DEBUG``.
+
+This tracking can be displayed or saved to a file
+for all mbufs or specific ones selected by pointer or mempool name::
+
+   testpmd> dump mbuf history all [file]
+   testpmd> dump mbuf history mempool <mp_name> [file]
+   testpmd> dump mbuf history one <mbuf_addr> [file]
+
 show (raw_encap|raw_decap)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-- 
2.51.0

Reply via email to