From fc34338d77f009befea5867feb58a9cba79a214f Mon Sep 17 00:00:00 2001 From: Tom Burdick Date: Mon, 11 Oct 2021 13:36:23 -0500 Subject: [PATCH] stats: Add a stats shell command Adds the stats shell command to list all stats and enable the command in the shell sample app. Signed-off-by: Tom Burdick --- samples/subsys/shell/shell_module/prj.conf | 2 + subsys/stats/CMakeLists.txt | 1 + subsys/stats/Kconfig | 9 ++++ subsys/stats/stats_shell.c | 51 ++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 subsys/stats/stats_shell.c diff --git a/samples/subsys/shell/shell_module/prj.conf b/samples/subsys/shell/shell_module/prj.conf index 2381918a415..c11e1133d25 100644 --- a/samples/subsys/shell/shell_module/prj.conf +++ b/samples/subsys/shell/shell_module/prj.conf @@ -12,3 +12,5 @@ CONFIG_POSIX_CLOCK=y CONFIG_DATE_SHELL=y CONFIG_THREAD_RUNTIME_STATS=y CONFIG_THREAD_RUNTIME_STATS_USE_TIMING_FUNCTIONS=y +CONFIG_STATS=y +CONFIG_STATS_SHELL=y diff --git a/subsys/stats/CMakeLists.txt b/subsys/stats/CMakeLists.txt index ddaa4c2e138..74e85147c80 100644 --- a/subsys/stats/CMakeLists.txt +++ b/subsys/stats/CMakeLists.txt @@ -1,3 +1,4 @@ # SPDX-License-Identifier: Apache-2.0 zephyr_sources_ifdef(CONFIG_STATS stats.c) +zephyr_sources_ifdef(CONFIG_STATS_SHELL stats_shell.c) diff --git a/subsys/stats/Kconfig b/subsys/stats/Kconfig index 2a9e12bdae3..4cfa422ffe0 100644 --- a/subsys/stats/Kconfig +++ b/subsys/stats/Kconfig @@ -17,3 +17,12 @@ config STATS_NAMES setting is disabled, statistics are assigned generic names of the form "s0", "s1", etc. Enabling this setting simplifies debugging, but results in a larger code size. + +config STATS_SHELL + bool "Statistics Shell Command" + depends on STATS && SHELL + help + Include a full name string for each statistic in the build. If this + setting is disabled, statistics are assigned generic names of the + form "s0", "s1", etc. Enabling this setting simplifies debugging, + but results in a larger code size. diff --git a/subsys/stats/stats_shell.c b/subsys/stats/stats_shell.c new file mode 100644 index 00000000000..d2a82e5ef6f --- /dev/null +++ b/subsys/stats/stats_shell.c @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2021 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +static int stats_cb(struct stats_hdr *hdr, void *arg, const char *name, uint16_t off) +{ + struct shell *sh = arg; + void *addr = (uint8_t *)hdr + off; + uint64_t val = 0; + + switch (hdr->s_size) { + case sizeof(uint16_t): + val = *(uint16_t *)(addr); + break; + + case sizeof(uint32_t): + val = *(uint32_t *)(addr); + break; + case sizeof(uint64_t): + val = *(uint64_t *)(addr); + break; + } + shell_print(sh, "\t%s (offset: %u, addr: %p): %llu", name, off, addr, val); + return 0; +} + +static int stats_group_cb(struct stats_hdr *hdr, void *arg) +{ + struct shell *sh = arg; + + shell_print(sh, "Stats Group %s (hdr addr: %x)", hdr->s_name, (void *)hdr); + return stats_walk(hdr, stats_cb, arg); +} + +static int cmd_stats_list(const struct shell *sh, size_t argc, + char **argv) +{ + return stats_group_walk(stats_group_cb, (struct shell *)sh); +} + +SHELL_STATIC_SUBCMD_SET_CREATE(sub_stats, + SHELL_CMD(list, NULL, "List stats", cmd_stats_list), + SHELL_SUBCMD_SET_END /* Array terminated. */ + ); + +SHELL_CMD_REGISTER(stats, &sub_stats, "Stats commands", NULL);