This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
commit f39ee7bc46e1ea17afea8d1f008ef67189b0c8e2 Author: yukangzhi <[email protected]> AuthorDate: Mon Oct 26 17:10:34 2026 +0800 nshlib/top: shrink per-task I/O buffer to reduce peak heap. top keeps one I/O buffer alive per task simultaneously (all tasks are collected, sorted, then printed), so peak heap usage is roughly (number of tasks) * IOBUFFERSIZE. Each buffer only needs to hold a single task's status + loadavg + cmdline (typically a few hundred bytes), so a full IOBUFFERSIZE (e.g. 1024) per task wastes heap and can exhaust a nearly-full heap. Introduce CONFIG_NSH_TOP_TASKBUF_SIZE (default 512) and use it for the per-task td_buf allocation instead of IOBUFFERSIZE, halving top's peak heap usage in the typical case. Oversized output is truncated by nsh_readfile() and does not overflow. Assisted-by: Kiro Signed-off-by: yukangzhi <[email protected]> --- nshlib/Kconfig | 15 +++++++++++++++ nshlib/nsh_proccmds.c | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/nshlib/Kconfig b/nshlib/Kconfig index b76bce475..a66379e4a 100644 --- a/nshlib/Kconfig +++ b/nshlib/Kconfig @@ -720,6 +720,21 @@ config NSH_DISABLE_TOP default DEFAULT_SMALL depends on !NSH_DISABLE_PS +config NSH_TOP_TASKBUF_SIZE + int "top per-task I/O buffer size" + default 512 + depends on !NSH_DISABLE_TOP + ---help--- + The 'top' command keeps one I/O buffer alive per task + simultaneously (all tasks are collected, sorted, then printed), + so peak heap usage is roughly (number of tasks) * this value. + + Each buffer only needs to hold the concatenation of a single + task's status + loadavg + cmdline, which is typically a few + hundred bytes, so this can be much smaller than NSH_FILEIOSIZE. + If a task's combined output exceeds this size it will be + truncated (fields may be missing) but will not overflow. + config NSH_DISABLE_TRUNCATE bool "Disable truncate" default DEFAULT_SMALL diff --git a/nshlib/nsh_proccmds.c b/nshlib/nsh_proccmds.c index dfe3c25de..0992f6e38 100644 --- a/nshlib/nsh_proccmds.c +++ b/nshlib/nsh_proccmds.c @@ -821,14 +821,14 @@ static int top_callback(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath, } topstatus->status[index] = status; - status->td_buf = zalloc(IOBUFFERSIZE); + status->td_buf = zalloc(CONFIG_NSH_TOP_TASKBUF_SIZE); if (status->td_buf == NULL) { nsh_error(vtbl, g_fmtcmdfailed, "top", "zalloc", NSH_ERRNO); return -ENOMEM; } - status->td_bufsize = IOBUFFERSIZE; + status->td_bufsize = CONFIG_NSH_TOP_TASKBUF_SIZE; } ret = ps_record(vtbl, dirpath, entryp, topstatus->heap, status);
