Skip to content

Commit f305443

Browse files
committed
hostap: Add heap allocation tracking for hostap
Create "wifi mem" command that can show how the heap memory is being used in hostap. The command is available if CONFIG_WIFI_NM_WPA_SUPPLICANT_DEBUG_ALLOC is enabled. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 15b13be commit f305443

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

modules/hostap/Kconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ choice LOG_MODE
8585
endchoice
8686
endif # WIFI_NM_WPA_SUPPLICANT_LOG_LEVEL_DBG
8787

88+
config WIFI_NM_WPA_SUPPLICANT_DEBUG_ALLOC
89+
bool "Debug memory allocation in hostap"
90+
help
91+
Enables printing of network packet and buffer allocations and frees for
92+
each allocation. This can produce lot of output so it is disabled by
93+
default.
94+
95+
config WIFI_NM_WPA_SUPPLICANT_DEBUG_ALLOC_LEN
96+
int "Max number of allocation tracking entries"
97+
default 200
98+
depends on WIFI_NM_WPA_SUPPLICANT_DEBUG_ALLOC
99+
help
100+
Each entry can track one allocation. Increase this if needed.
101+
88102
# Memory optimizations
89103
config WIFI_NM_WPA_SUPPLICANT_ADVANCED_FEATURES
90104
bool "Advanced features"

subsys/net/l2/wifi/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
zephyr_library()
44
zephyr_library_include_directories(${ZEPHYR_BASE}/subsys/net/ip)
5+
zephyr_library_include_directories(${HOSTAP_BASE})
56
zephyr_library_include_directories_ifdef(
67
CONFIG_NET_L2_WIFI_SHELL ${ZEPHYR_BASE}/subsys/net/lib/shell
78
)

subsys/net/l2/wifi/wifi_shell.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2913,6 +2913,63 @@ void parse_mode_args_to_params(const struct shell *sh, int argc,
29132913
}
29142914
}
29152915

2916+
#if defined(CONFIG_WIFI_NM_WPA_SUPPLICANT_DEBUG_ALLOC)
2917+
#include <src/utils/os.h>
2918+
2919+
static void mem_alloc_cb(void *data,
2920+
size_t size,
2921+
const char *file_alloc,
2922+
const char *func_alloc,
2923+
int line_alloc,
2924+
const char *file_free,
2925+
const char *func_free,
2926+
int line_free,
2927+
bool in_use,
2928+
void *user_data)
2929+
{
2930+
const struct shell *sh = (const struct shell *)user_data;
2931+
2932+
if (in_use) {
2933+
if (func_free != NULL) {
2934+
PR("Alloc: %zu bytes at %s():%d, free at %s():%d\n",
2935+
size, func_alloc, line_alloc, func_free, line_free);
2936+
} else {
2937+
PR("Alloc: %zu bytes at %s():%d (%s)\n",
2938+
size, func_alloc, line_alloc, file_alloc);
2939+
}
2940+
} else {
2941+
if (size > 0) {
2942+
PR("Free: %zu bytes at %s():%d (%s)\n",
2943+
size, func_free, line_free, file_free);
2944+
}
2945+
}
2946+
}
2947+
#endif
2948+
2949+
static int cmd_wifi_mem(const struct shell *sh, size_t argc, char *argv[])
2950+
{
2951+
ARG_UNUSED(argc);
2952+
ARG_UNUSED(argv);
2953+
2954+
#if defined(CONFIG_WIFI_NM_WPA_SUPPLICANT_DEBUG_ALLOC)
2955+
int64_t total_usage;
2956+
2957+
total_usage = hostap_get_total_allocs();
2958+
2959+
PR("Total heap memory usage: %lld bytes\n", total_usage);
2960+
PR("Max heap memory usage: %zu bytes\n\n", hostap_get_max_allocs());
2961+
2962+
hostap_allocs_foreach(mem_alloc_cb, (void *)sh);
2963+
2964+
return 0;
2965+
#else
2966+
PR_INFO("Set %s to enable %s support.\n",
2967+
"CONFIG_WIFI_NM_WPA_SUPPLICANT_DEBUG_ALLOC", "hostap memory usage");
2968+
2969+
return -ENOEXEC;
2970+
#endif
2971+
}
2972+
29162973
static int cmd_wifi_mode(const struct shell *sh, size_t argc, char *argv[])
29172974
{
29182975
struct net_if *iface;
@@ -4054,6 +4111,11 @@ SHELL_SUBCMD_ADD((wifi), disconnect, NULL,
40544111
cmd_wifi_disconnect,
40554112
1, 2);
40564113

4114+
SHELL_SUBCMD_ADD((wifi), mem, NULL,
4115+
"Show memory allocations in hostap.\n",
4116+
cmd_wifi_mem,
4117+
1, 0);
4118+
40574119
SHELL_SUBCMD_ADD((wifi), mode, NULL,
40584120
"mode operational setting\n"
40594121
"This command may be used to set the Wi-Fi device into a specific "

0 commit comments

Comments
 (0)