malloc: Add 'malloc' command with 'info' subcommand

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>
This commit is contained in:
Simon Glass
2025-11-28 08:54:29 -07:00
parent bd0f9a753c
commit cfbee94582
7 changed files with 121 additions and 0 deletions

View File

@@ -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

View File

@@ -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

34
cmd/malloc.c Normal file
View File

@@ -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));