Skip to content

Commit

Permalink
mm: rewrite the memdump code for more readable
Browse files Browse the repository at this point in the history
Signed-off-by: ligd <liguiding1@xiaomi.com>
  • Loading branch information
GUIDINGLI authored and xiaoxiang781216 committed Sep 24, 2023
1 parent dcb8188 commit 13f0051
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 38 deletions.
6 changes: 6 additions & 0 deletions include/nuttx/mm/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@
# define MM_INTERNAL_HEAP(heap) ((heap) == USR_HEAP)
#endif

#define MM_DUMP_ASSIGN(dump, pid) ((dump) == (pid))
#define MM_DUMP_ALLOC(dump, pid) \
((dump) == PID_MM_ALLOC && (pid) != PID_MM_MEMPOOL)
#define MM_DUMP_LEAK(dump, pid) \
((dump) == PID_MM_LEAK && (pid) >= 0 && nxsched_get_tcb(pid) == NULL)

/****************************************************************************
* Public Types
****************************************************************************/
Expand Down
14 changes: 8 additions & 6 deletions mm/mempool/mempool.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,12 @@ mempool_info_task(FAR struct mempool_s *pool,
{
FAR struct mempool_backtrace_s *buf;

list_for_every_entry(&pool->alist, buf, struct mempool_backtrace_s,
node)
list_for_every_entry(&pool->alist, buf,
struct mempool_backtrace_s, node)
{
if ((task->pid == buf->pid || task->pid == PID_MM_ALLOC ||
(task->pid == PID_MM_LEAK && !nxsched_get_tcb(buf->pid))) &&
if ((MM_DUMP_ASSIGN(task->pid, buf->pid) ||
MM_DUMP_ALLOC(task->pid, buf->pid) ||
MM_DUMP_LEAK(task->pid, buf->pid)) &&
buf->seqno >= task->seqmin && buf->seqno <= task->seqmax)
{
info.aordblks++;
Expand Down Expand Up @@ -500,8 +501,9 @@ void mempool_memdump(FAR struct mempool_s *pool,
list_for_every_entry(&pool->alist, buf,
struct mempool_backtrace_s, node)
{
if ((dump->pid == buf->pid || dump->pid == PID_MM_ALLOC ||
(dump->pid == PID_MM_LEAK && !nxsched_get_tcb(buf->pid))) &&
if ((MM_DUMP_ASSIGN(dump->pid, buf->pid) ||
MM_DUMP_ALLOC(dump->pid, buf->pid) ||
MM_DUMP_LEAK(dump->pid, buf->pid)) &&
buf->seqno >= dump->seqmin && buf->seqno <= dump->seqmax)
{
char tmp[CONFIG_MM_BACKTRACE * MM_PTR_FMT_WIDTH + 1] = "";
Expand Down
7 changes: 3 additions & 4 deletions mm/mm_heap/mm_mallinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,9 @@ static void mallinfo_task_handler(FAR struct mm_allocnode_s *node,
info->uordblks += nodesize;
}
#else
if ((task->pid == node->pid ||
(task->pid == PID_MM_ALLOC && node->pid != PID_MM_MEMPOOL) ||
(task->pid == PID_MM_LEAK && node->pid >= 0 &&
!nxsched_get_tcb(node->pid))) &&
if ((MM_DUMP_ASSIGN(task->pid, node->pid) ||
MM_DUMP_ALLOC(task->pid, node->pid) ||
MM_DUMP_LEAK(task->pid, node->pid)) &&
node->seqno >= task->seqmin && node->seqno <= task->seqmax)
{
info->aordblks++;
Expand Down
28 changes: 18 additions & 10 deletions mm/mm_heap/mm_memdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,30 @@ static void memdump_handler(FAR struct mm_allocnode_s *node, FAR void *arg)
DEBUGASSERT(nodesize >= MM_SIZEOF_ALLOCNODE);
#if CONFIG_MM_BACKTRACE < 0
if (dump->pid == PID_MM_ALLOC)
#else
if ((dump->pid == node->pid || dump->pid == PID_MM_ALLOC ||
(dump->pid == PID_MM_LEAK && node->pid >= 0 &&
!nxsched_get_tcb(node->pid))) &&
node->seqno >= dump->seqmin && node->seqno <= dump->seqmax)
#endif
{
#if CONFIG_MM_BACKTRACE < 0
syslog(LOG_INFO, "%12zu%*p\n",
nodesize, MM_PTR_FMT_WIDTH,
((FAR char *)node + MM_SIZEOF_ALLOCNODE));
}
#elif CONFIG_MM_BACKTRACE == 0
if ((MM_DUMP_ASSIGN(dump->pid, node->pid) ||
MM_DUMP_ALLOC(dump->pid, node->pid) ||
MM_DUMP_LEAK(dump->pid, node->pid)) &&
node->seqno >= dump->seqmin && node->seqno <= dump->seqmax)
{
syslog(LOG_INFO, "%6d%12zu%12lu%*p\n",
node->pid, nodesize, node->seqno,
MM_PTR_FMT_WIDTH,
((FAR char *)node + MM_SIZEOF_ALLOCNODE));
}
#else
if ((MM_DUMP_ASSIGN(dump->pid, node->pid) ||
MM_DUMP_ALLOC(dump->pid, node->pid) ||
MM_DUMP_LEAK(dump->pid, node->pid)) &&
node->seqno >= dump->seqmin && node->seqno <= dump->seqmax)
{
char buf[CONFIG_MM_BACKTRACE * MM_PTR_FMT_WIDTH + 1] = "";

# if CONFIG_MM_BACKTRACE > 0
FAR const char *format = " %0*p";
int i;

Expand All @@ -84,14 +93,13 @@ static void memdump_handler(FAR struct mm_allocnode_s *node, FAR void *arg)
sizeof(buf) - i * MM_PTR_FMT_WIDTH,
format, MM_PTR_FMT_WIDTH - 1, node->backtrace[i]);
}
# endif

syslog(LOG_INFO, "%6d%12zu%12lu%*p%s\n",
node->pid, nodesize, node->seqno,
MM_PTR_FMT_WIDTH,
((FAR char *)node + MM_SIZEOF_ALLOCNODE), buf);
#endif
}
#endif
}
else if (dump->pid == PID_MM_FREE)
{
Expand Down
42 changes: 24 additions & 18 deletions mm/tlsf/mm_tlsf.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,9 @@ static void mallinfo_task_handler(FAR void *ptr, size_t size, int used,
FAR struct memdump_backtrace_s *buf =
ptr + size - sizeof(struct memdump_backtrace_s);

if ((task->pid == buf->pid ||
(task->pid == PID_MM_ALLOC && buf->pid != PID_MM_MEMPOOL) ||
(task->pid == PID_MM_LEAK && buf->pid >= 0 &&
!nxsched_get_tcb(buf->pid))) &&
if ((MM_DUMP_ASSIGN(task->pid, buf->pid) ||
MM_DUMP_ALLOC(task->pid, buf->pid) ||
MM_DUMP_LEAK(task->pid, buf->pid)) &&
buf->seqno >= task->seqmin && buf->seqno <= task->seqmax)
{
info->aordblks++;
Expand Down Expand Up @@ -407,23 +406,32 @@ static void memdump_handler(FAR void *ptr, size_t size, int used,
{
#if CONFIG_MM_BACKTRACE < 0
if (dump->pid == PID_MM_ALLOC)
#else
{
syslog(LOG_INFO, "%12zu%*p\n", size, MM_PTR_FMT_WIDTH, ptr);
}
#elif CONFIG_MM_BACKTRACE == 0
FAR struct memdump_backtrace_s *buf =
ptr + size - sizeof(struct memdump_backtrace_s);

if ((dump->pid == buf->pid ||
(dump->pid == PID_MM_ALLOC && buf->pid != PID_MM_MEMPOOL) ||
(dump->pid == PID_MM_LEAK && buf->pid >= 0 &&
!nxsched_get_tcb(buf->pid))) &&
if ((MM_DUMP_ASSIGN(dump->pid, buf->pid) ||
MM_DUMP_ALLOC(dump->pid, buf->pid) ||
MM_DUMP_LEAK(dump->pid, buf->pid)) &&
buf->seqno >= dump->seqmin && buf->seqno <= dump->seqmax)
#endif
{
#if CONFIG_MM_BACKTRACE < 0
syslog(LOG_INFO, "%12zu%*p\n", size, MM_PTR_FMT_WIDTH, ptr);
syslog(LOG_INFO, "%6d%12zu%12lu%*p\n",
buf->pid, size, buf->seqno, MM_PTR_FMT_WIDTH, ptr);
}
#else
FAR struct memdump_backtrace_s *buf =
ptr + size - sizeof(struct memdump_backtrace_s);

if ((MM_DUMP_ASSIGN(dump->pid, buf->pid) ||
MM_DUMP_ALLOC(dump->pid, buf->pid) ||
MM_DUMP_LEAK(dump->pid, buf->pid)) &&
buf->seqno >= dump->seqmin && buf->seqno <= dump->seqmax)
{
char tmp[CONFIG_MM_BACKTRACE * MM_PTR_FMT_WIDTH + 1] = "";

# if CONFIG_MM_BACKTRACE > 0
FAR const char *format = " %0*p";
int i;

Expand All @@ -433,13 +441,11 @@ static void memdump_handler(FAR void *ptr, size_t size, int used,
sizeof(tmp) - i * MM_PTR_FMT_WIDTH,
format, MM_PTR_FMT_WIDTH - 1, buf->backtrace[i]);
}
# endif

syslog(LOG_INFO, "%6d%12zu%12lu%*p%s\n",
buf->pid, size, buf->seqno, MM_PTR_FMT_WIDTH,
ptr, tmp);
#endif
syslog(LOG_INFO, "%6d%12zu%12lu%*p%s\n",
buf->pid, size, buf->seqno, MM_PTR_FMT_WIDTH, ptr, tmp);
}
#endif
}
else if (dump->pid == PID_MM_FREE)
{
Expand Down

0 comments on commit 13f0051

Please sign in to comment.