mm: rewrite the memdump code for more readable

Signed-off-by: ligd <liguiding1@xiaomi.com>
This commit is contained in:
ligd 2023-09-11 17:52:17 +08:00 committed by Xiang Xiao
parent dcb8188f07
commit 13f0051747
5 changed files with 59 additions and 38 deletions

View File

@ -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
****************************************************************************/

View File

@ -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++;
@ -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] = "";

View File

@ -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++;

View File

@ -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;
@ -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)
{

View File

@ -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++;
@ -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)
{
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 ((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)
{
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 ((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);
#else
char tmp[CONFIG_MM_BACKTRACE * MM_PTR_FMT_WIDTH + 1] = "";
# if CONFIG_MM_BACKTRACE > 0
FAR const char *format = " %0*p";
int i;
@ -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)
{