From b0c281d1603db8510db39ddddbce8dfd4c8f1ca6 Mon Sep 17 00:00:00 2001 From: zhanxiaoqi Date: Sat, 28 Feb 2026 14:43:45 +0800 Subject: [PATCH] mm/dump:add command to support dump memory usage of all pids For the memdump command, when CONFIG_MM_BACKTRACE >= 0 is enabled, it currently only supports dumping all leaked memory nodes and memory usage for a specific PID. It does not support dumping memory usage of all processes in one go. In some scenarios, it is necessary to dump memory usage of all processes at once. For example, when the memory pressure monitoring system detects that memory falls below a certain threshold, it needs to obtain the memory usage of all processes. Similarly, in some automation scripts, it is necessary to periodically collect memory data of each process for memory leak analysis. Signed-off-by: zhanxiaoqi --- fs/procfs/fs_procfsmeminfo.c | 8 +++++++- include/malloc.h | 3 +++ mm/mm_heap/mm_memdump.c | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/fs/procfs/fs_procfsmeminfo.c b/fs/procfs/fs_procfsmeminfo.c index dcf2277c0d707..9a6a8cd4d11f6 100644 --- a/fs/procfs/fs_procfsmeminfo.c +++ b/fs/procfs/fs_procfsmeminfo.c @@ -445,7 +445,7 @@ static ssize_t memdump_read(FAR struct file *filep, FAR char *buffer, "/on/off" #endif #if CONFIG_MM_BACKTRACE >= 0 - "/leak/pid> \n" "used: dump all allocated node\n" @@ -463,6 +463,7 @@ static ssize_t memdump_read(FAR struct file *filep, FAR char *buffer, #if CONFIG_MM_BACKTRACE >= 0 "leak: dump all leaked node\n" "pid: dump pid allocated node\n" + "allpid: dump all pid memory allocated\n" # ifdef CONFIG_MM_BACKTRACE_SEQNO "The current sequence number %lu\n", g_mm_seqno @@ -616,6 +617,11 @@ static ssize_t memdump_write(FAR struct file *filep, FAR const char *buffer, break; #if CONFIG_MM_BACKTRACE >= 0 + case 'a': + dump.pid = PID_MM_ALL_PID; + p = (FAR char *)buffer + 6; + goto dump; + default: if (!isdigit(buffer[0])) { diff --git a/include/malloc.h b/include/malloc.h index 1955b175cea07..e1e95e605318f 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -35,6 +35,9 @@ /* Special PID to query the info about alloc, free and mempool */ +#if CONFIG_MM_BACKTRACE >= 0 +#define PID_MM_ALL_PID ((pid_t)-7) +#endif #define PID_MM_ORPHAN ((pid_t)-6) #define PID_MM_BIGGEST ((pid_t)-5) #define PID_MM_FREE ((pid_t)-4) diff --git a/mm/mm_heap/mm_memdump.c b/mm/mm_heap/mm_memdump.c index fc1e5f854b73f..47b2bf99e17c6 100644 --- a/mm/mm_heap/mm_memdump.c +++ b/mm/mm_heap/mm_memdump.c @@ -223,6 +223,29 @@ static void memdump_handler(FAR struct mm_allocnode_s *node, FAR void *arg) } } +#if CONFIG_MM_BACKTRACE >= 0 +struct memdump_tcb_arg_s +{ + FAR struct mm_heap_s *heap; + unsigned long seqmin; + unsigned long seqmax; +}; + +static void memdump_tcb_handler(FAR struct tcb_s *tcb, FAR void *arg) +{ + struct mallinfo_task info; + struct malltask task; + FAR struct memdump_tcb_arg_s *tcb_arg = arg; + + task.pid = tcb ? tcb->pid : PID_MM_LEAK; + task.seqmin = tcb_arg->seqmin; + task.seqmax = tcb_arg->seqmax; + info = mm_mallinfo_task(tcb_arg->heap, &task); + syslog(LOG_INFO, "pid:%5d, used:%10d, nused:%10d\n", + task.pid, info.uordblks, info.aordblks); +} +#endif + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -301,6 +324,18 @@ void mm_memdump(FAR struct mm_heap_s *heap, { syslog(LOG_INFO, "Dump allocated orphan nodes\n"); } +#if CONFIG_MM_BACKTRACE >= 0 + else if (pid == PID_MM_ALL_PID) + { + syslog(LOG_INFO, "Dump all pid memory allocated\n"); + struct memdump_tcb_arg_s tcb_arg; + tcb_arg.heap = heap; + tcb_arg.seqmin = dump->seqmin; + tcb_arg.seqmax = dump->seqmax; + nxsched_foreach(memdump_tcb_handler, &tcb_arg); + return; + } +#endif #if CONFIG_MM_BACKTRACE < 0 syslog(LOG_INFO, "%12s%9s%*s\n", "Size", "Overhead",