Skip to content

Commit b0c281d

Browse files
committed
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 <zhanxiaoqi@bytedance.com>
1 parent 0db2567 commit b0c281d

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

fs/procfs/fs_procfsmeminfo.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ static ssize_t memdump_read(FAR struct file *filep, FAR char *buffer,
445445
"/on/off"
446446
#endif
447447
#if CONFIG_MM_BACKTRACE >= 0
448-
"/leak/pid> <seqmin> <seqmax"
448+
"/leak/pid/allpid> <seqmin> <seqmax"
449449
#endif
450450
">\n"
451451
"used: dump all allocated node\n"
@@ -463,6 +463,7 @@ static ssize_t memdump_read(FAR struct file *filep, FAR char *buffer,
463463
#if CONFIG_MM_BACKTRACE >= 0
464464
"leak: dump all leaked node\n"
465465
"pid: dump pid allocated node\n"
466+
"allpid: dump all pid memory allocated\n"
466467
# ifdef CONFIG_MM_BACKTRACE_SEQNO
467468
"The current sequence number %lu\n",
468469
g_mm_seqno
@@ -616,6 +617,11 @@ static ssize_t memdump_write(FAR struct file *filep, FAR const char *buffer,
616617
break;
617618

618619
#if CONFIG_MM_BACKTRACE >= 0
620+
case 'a':
621+
dump.pid = PID_MM_ALL_PID;
622+
p = (FAR char *)buffer + 6;
623+
goto dump;
624+
619625
default:
620626
if (!isdigit(buffer[0]))
621627
{

include/malloc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535

3636
/* Special PID to query the info about alloc, free and mempool */
3737

38+
#if CONFIG_MM_BACKTRACE >= 0
39+
#define PID_MM_ALL_PID ((pid_t)-7)
40+
#endif
3841
#define PID_MM_ORPHAN ((pid_t)-6)
3942
#define PID_MM_BIGGEST ((pid_t)-5)
4043
#define PID_MM_FREE ((pid_t)-4)

mm/mm_heap/mm_memdump.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,29 @@ static void memdump_handler(FAR struct mm_allocnode_s *node, FAR void *arg)
223223
}
224224
}
225225

226+
#if CONFIG_MM_BACKTRACE >= 0
227+
struct memdump_tcb_arg_s
228+
{
229+
FAR struct mm_heap_s *heap;
230+
unsigned long seqmin;
231+
unsigned long seqmax;
232+
};
233+
234+
static void memdump_tcb_handler(FAR struct tcb_s *tcb, FAR void *arg)
235+
{
236+
struct mallinfo_task info;
237+
struct malltask task;
238+
FAR struct memdump_tcb_arg_s *tcb_arg = arg;
239+
240+
task.pid = tcb ? tcb->pid : PID_MM_LEAK;
241+
task.seqmin = tcb_arg->seqmin;
242+
task.seqmax = tcb_arg->seqmax;
243+
info = mm_mallinfo_task(tcb_arg->heap, &task);
244+
syslog(LOG_INFO, "pid:%5d, used:%10d, nused:%10d\n",
245+
task.pid, info.uordblks, info.aordblks);
246+
}
247+
#endif
248+
226249
/****************************************************************************
227250
* Public Functions
228251
****************************************************************************/
@@ -301,6 +324,18 @@ void mm_memdump(FAR struct mm_heap_s *heap,
301324
{
302325
syslog(LOG_INFO, "Dump allocated orphan nodes\n");
303326
}
327+
#if CONFIG_MM_BACKTRACE >= 0
328+
else if (pid == PID_MM_ALL_PID)
329+
{
330+
syslog(LOG_INFO, "Dump all pid memory allocated\n");
331+
struct memdump_tcb_arg_s tcb_arg;
332+
tcb_arg.heap = heap;
333+
tcb_arg.seqmin = dump->seqmin;
334+
tcb_arg.seqmax = dump->seqmax;
335+
nxsched_foreach(memdump_tcb_handler, &tcb_arg);
336+
return;
337+
}
338+
#endif
304339

305340
#if CONFIG_MM_BACKTRACE < 0
306341
syslog(LOG_INFO, "%12s%9s%*s\n", "Size", "Overhead",

0 commit comments

Comments
 (0)