From patchwork Wed Dec 10 00:07:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 875 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325366; bh=7NlqR/btHofmvlthWxoJAfmNqyOja8pidYLs9fKNY6c=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=Ou4lx9s0lJxC/cSqNruEO+QOsudhAHE5CtrLrJqcB4+N3++cHQBsWDRiD1wR2AHtW agx3Rw6zh2fB8riyQGtDf1jl2SLkYUoy7JEC4YvB34iPfdXFr+CXnBuP2rkl2SfReQ /69IJj0JXLs2O3EhQVYwbnIBZgL2MipQFQ/SML0FhnEVmYJ6+PhFJ7/XlLoXPDAtam gra5mt7RWcMYrd/xWyGFFG9ac5dx7cdCbtI05p+EMzip2eguKomIeZcz9nHUH2BCYz KwPCjZvOaFU0J4TmFxhhLR2x5XJs9rS541GJua09VQH7FLqXUT2J+K58mcIitWyiSE NkWVD3sTqyLkA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id BC5C068A3E for ; Tue, 9 Dec 2025 17:09:26 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id oGFeIny8j_fm for ; Tue, 9 Dec 2025 17:09:26 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325366; bh=7NlqR/btHofmvlthWxoJAfmNqyOja8pidYLs9fKNY6c=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=Ou4lx9s0lJxC/cSqNruEO+QOsudhAHE5CtrLrJqcB4+N3++cHQBsWDRiD1wR2AHtW agx3Rw6zh2fB8riyQGtDf1jl2SLkYUoy7JEC4YvB34iPfdXFr+CXnBuP2rkl2SfReQ /69IJj0JXLs2O3EhQVYwbnIBZgL2MipQFQ/SML0FhnEVmYJ6+PhFJ7/XlLoXPDAtam gra5mt7RWcMYrd/xWyGFFG9ac5dx7cdCbtI05p+EMzip2eguKomIeZcz9nHUH2BCYz KwPCjZvOaFU0J4TmFxhhLR2x5XJs9rS541GJua09VQH7FLqXUT2J+K58mcIitWyiSE NkWVD3sTqyLkA== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id AB22868957 for ; Tue, 9 Dec 2025 17:09:26 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325365; bh=XP8YSXp8GFWBIlVrj6dqMgU6IcBhLa8J0nY0RDwZ6oc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VFxwaw99Co21Ch7UKxNOnfWMWhJ//vfbvmPW8DtEfzPVbu8GPOZ6aq59+0IxPhraJ 1dTrgclkQ9ZD2CVdKRGCtd+TWlneEvwHREKrj4ikwLh9hquI9oQ57vvKpX/1bOdcwy hTz8wKEYt4UJpryQ+KbFPfDTbtuEt0iSc3f3wHDcnbWze71Zd8pkfyWI2+d4ae51vH 4a6fZpU4AXXpbfEsZAyLFl1JRxmRxUoZLWP/lPvcCTTa0wWeiu/4sc1zWx6GmcNNhq /mhE3rZ/q04NQBEVNs5J8Tzub3jQnQarrR/O5rwe7auvKixmzKu68a8LQbodCuNh7R /i63EwJqd53Lg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 5E9D568957; Tue, 9 Dec 2025 17:09:25 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id Qdw8rVfGFWL4; Tue, 9 Dec 2025 17:09:25 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325361; bh=6rC/aYWgERQ183A+cfkFVxTUJhBiFklQwU8fPWkfbWg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bxl2OLf1NTLph3WwEbWLX4cCrwNR89libf8yc8+PpchMtFi9Kgd5Y0mEEgRAJcxci abwclt9OPMUQ385lm3XZxtZB9GIQPY9amlSw9WnbWAQpaIzOx830wAPX4YeK5XXW1X CwvHugZE0x6jjfh/oV3pGdzDZa8o2DsjtpO9ojteKESX8B6vxRzzxWt9543sOfVVBx ZhtcBOKVnT5xudhn4j83ipC0YGuW3l2rv0GUSnXNlIQELNdyd1hi+Cdh1C8iIFlmAM ra86Fe8U5P4PrMxrsSkJ9Mq/8KCpm+yJHkETdwMhsm5WqaBiX34XBqFlkJQKMSNhcy Vm8voTKD7x5ig== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id CE5146884F; Tue, 9 Dec 2025 17:09:20 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:09 -0700 Message-ID: <20251210000737.180797-19-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251210000737.180797-1-sjg@u-boot.org> References: <20251210000737.180797-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: ZJTPG2B5SLTBJZYQEAXV77R3BR2REPTM X-Message-ID-Hash: ZJTPG2B5SLTBJZYQEAXV77R3BR2REPTM X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Heinrich Schuchardt , Simon Glass , Claude X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 18/35] malloc: Add 'malloc' command with 'info' subcommand List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass Add a command to display malloc heap statistics, showing total heap size and memory currently in use. Co-developed-by: Claude Signed-off-by: Simon Glass --- 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 diff --git a/cmd/Kconfig b/cmd/Kconfig index ff5f6f85144..43e12e413f8 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -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 diff --git a/cmd/Makefile b/cmd/Makefile index ebf66ea0d3c..c010da8dc3b 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -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 diff --git a/cmd/malloc.c b/cmd/malloc.c new file mode 100644 index 00000000000..cb2fa34155b --- /dev/null +++ b/cmd/malloc.c @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * malloc command - show malloc information + * + * Copyright 2025 Canonical Ltd + * Written by Simon Glass + */ + +#include +#include +#include + +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)); diff --git a/doc/usage/cmd/malloc.rst b/doc/usage/cmd/malloc.rst new file mode 100644 index 00000000000..d21aa6c1421 --- /dev/null +++ b/doc/usage/cmd/malloc.rst @@ -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. diff --git a/doc/usage/index.rst b/doc/usage/index.rst index bee7884a066..7ed79f89af6 100644 --- a/doc/usage/index.rst +++ b/doc/usage/index.rst @@ -97,6 +97,7 @@ Shell commands cmd/loadx cmd/loady cmd/luks + cmd/malloc cmd/meminfo cmd/mbr cmd/md diff --git a/test/cmd/Makefile b/test/cmd/Makefile index c43aefb4eb3..14cbdc3a6e6 100644 --- a/test/cmd/Makefile +++ b/test/cmd/Makefile @@ -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 diff --git a/test/cmd/malloc.c b/test/cmd/malloc.c new file mode 100644 index 00000000000..6cd52b68900 --- /dev/null +++ b/test/cmd/malloc.c @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Test for 'malloc' command + * + * Copyright 2025 Canonical Ltd + * Written by Simon Glass + */ + +#include +#include +#include +#include + +/* 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);