From: Simon Glass <simon.glass@canonical.com>
Add a command to display malloc heap statistics, showing total heap
size and memory currently in use.
Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---
cmd/Kconfig | 9 ++++++++
cmd/Makefile | 1 +
cmd/malloc.c | 34 ++++++++++++++++++++++++++++++
doc/usage/cmd/malloc.rst | 45 ++++++++++++++++++++++++++++++++++++++++
doc/usage/index.rst | 1 +
test/cmd/Makefile | 1 +
test/cmd/malloc.c | 30 +++++++++++++++++++++++++++
7 files changed, 121 insertions(+)
create mode 100644 cmd/malloc.c
create mode 100644 doc/usage/cmd/malloc.rst
create mode 100644 test/cmd/malloc.c
@@ -3018,6 +3018,15 @@ config CMD_LOG
maximum log level for emitting of records). It also provides access
to a command used for testing the log system.
+config CMD_MALLOC
+ bool "malloc - Show malloc statistics"
+ depends on MALLOC_DEBUG
+ default y
+ help
+ This provides access to malloc information. It shows statistics
+ about memory allocation, such as total memory allocated and
+ currently in use.
+
config CMD_MOUSE
bool "mouse - Show mouse input"
default y if MOUSE
@@ -113,6 +113,7 @@ obj-$(CONFIG_CMD_LED) += led.o
obj-$(CONFIG_CMD_LICENSE) += license.o
obj-y += load.o
obj-$(CONFIG_CMD_LOG) += log.o
+obj-$(CONFIG_CMD_MALLOC) += malloc.o
obj-$(CONFIG_CMD_LSBLK) += lsblk.o
obj-$(CONFIG_CMD_MD5SUM) += md5sum.o
obj-$(CONFIG_CMD_MEMORY) += mem.o
new file mode 100644
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * malloc command - show malloc information
+ *
+ * Copyright 2025 Canonical Ltd
+ * Written by Simon Glass <simon.glass@canonical.com>
+ */
+
+#include <command.h>
+#include <display_options.h>
+#include <malloc.h>
+
+static int do_malloc_info(struct cmd_tbl *cmdtp, int flag, int argc,
+ char *const argv[])
+{
+ struct malloc_info info;
+ char buf[12];
+ int ret;
+
+ ret = malloc_get_info(&info);
+ if (ret)
+ return CMD_RET_FAILURE;
+
+ printf("total bytes = %s\n", format_size(buf, info.total_bytes));
+ printf("in use bytes = %s\n", format_size(buf, info.in_use_bytes));
+
+ return 0;
+}
+
+U_BOOT_LONGHELP(malloc,
+ "info - display malloc statistics\n");
+
+U_BOOT_CMD_WITH_SUBCMDS(malloc, "malloc information", malloc_help_text,
+ U_BOOT_SUBCMD_MKENT(info, 1, 1, do_malloc_info));
new file mode 100644
@@ -0,0 +1,45 @@
+.. SPDX-License-Identifier: GPL-2.0+:
+
+.. index::
+ single: malloc (command)
+
+malloc command
+==============
+
+Synopsis
+--------
+
+::
+
+ malloc info
+
+Description
+-----------
+
+The malloc command shows information about the malloc heap.
+
+info
+ Shows memory-allocation statistics, including the total heap size and the
+ amount currently in use.
+
+The total heap size is set by ``CONFIG_SYS_MALLOC_LEN``.
+
+Example
+-------
+
+::
+
+ => malloc info
+ total bytes = 96 MiB
+ in use bytes = 700.9 KiB
+
+Configuration
+-------------
+
+The malloc command is enabled by CONFIG_CMD_MALLOC which depends on
+CONFIG_MALLOC_DEBUG.
+
+Return value
+------------
+
+The return value $? is 0 (true) on success, 1 (false) on failure.
@@ -97,6 +97,7 @@ Shell commands
cmd/loadx
cmd/loady
cmd/luks
+ cmd/malloc
cmd/meminfo
cmd/mbr
cmd/md
@@ -26,6 +26,7 @@ endif
obj-$(CONFIG_CMD_HASH) += hash.o
obj-$(CONFIG_CMD_HISTORY) += history.o
obj-$(CONFIG_CMD_LOADM) += loadm.o
+obj-$(CONFIG_CMD_MALLOC) += malloc.o
ifdef CONFIG_SANDBOX
obj-$(CONFIG_CMD_MEMINFO) += meminfo.o
endif
new file mode 100644
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Test for 'malloc' command
+ *
+ * Copyright 2025 Canonical Ltd
+ * Written by Simon Glass <simon.glass@canonical.com>
+ */
+
+#include <malloc.h>
+#include <dm/test.h>
+#include <test/cmd.h>
+#include <test/ut.h>
+
+/* Test 'malloc info' command */
+static int cmd_test_malloc_info(struct unit_test_state *uts)
+{
+ struct malloc_info info;
+
+ ut_assertok(malloc_get_info(&info));
+ ut_assert(info.total_bytes >= CONFIG_SYS_MALLOC_LEN);
+ ut_assert(info.in_use_bytes < info.total_bytes);
+
+ ut_assertok(run_command("malloc info", 0));
+ ut_assert_nextlinen("total bytes = ");
+ ut_assert_nextlinen("in use bytes = ");
+ ut_assert_console_end();
+
+ return 0;
+}
+CMD_TEST(cmd_test_malloc_info, UTF_CONSOLE);