Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memepool:fix memory consumption double counting issue #9052

Merged
merged 2 commits into from Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions fs/procfs/fs_procfsmeminfo.c
Expand Up @@ -516,11 +516,11 @@ static ssize_t memdump_write(FAR struct file *filep, FAR const char *buffer,
switch (buffer[0])
{
case 'u':
pid = (pid_t)-1;
pid = MM_BACKTRACE_ALLOC_PID;
break;

case 'f':
pid = (pid_t)-2;
pid = MM_BACKTRACE_FREE_PID;
break;
#if CONFIG_MM_BACKTRACE >= 0
default:
Expand Down
6 changes: 1 addition & 5 deletions include/nuttx/mm/mempool.h
Expand Up @@ -90,9 +90,6 @@ struct mempool_s
size_t expandsize; /* The size of expand block every time for mempool */
bool wait; /* The flag of need to wait when mempool is empty */
FAR void *priv; /* This pointer is used to store the user's private data */
bool calibrate; /* The flag is use expend memory calibration
* real memory usage
*/
mempool_alloc_t alloc; /* The alloc function for mempool */
mempool_free_t free; /* The free function for mempool */

Expand Down Expand Up @@ -320,7 +317,6 @@ void mempool_procfs_unregister(FAR struct mempool_procfs_entry_s *entry);
* arg - The alloc & free memory fuctions used arg.
* expandsize - The expend mempry for all pools in multiples pool.
* dict_expendsize - The expend size for multiple dictnoary.
* calibrate - Whether to calibrate when counting memory usage.
* Returned Value:
* Return an initialized multiple pool pointer on success,
* otherwise NULL is returned.
Expand All @@ -335,7 +331,7 @@ mempool_multiple_init(FAR const char *name,
mempool_multiple_alloc_t alloc,
mempool_multiple_free_t free,
FAR void *arg, size_t expandsize,
size_t dict_expendsize, bool calibrate);
size_t dict_expendsize);

/****************************************************************************
* Name: mempool_multiple_alloc
Expand Down
4 changes: 4 additions & 0 deletions include/nuttx/mm/mm.h
Expand Up @@ -98,6 +98,10 @@
# endif
#endif

#define MM_BACKTRACE_MEMPOOL_PID ((pid_t)-3)
#define MM_BACKTRACE_FREE_PID ((pid_t)-2)
#define MM_BACKTRACE_ALLOC_PID ((pid_t)-1)

/****************************************************************************
* Public Types
****************************************************************************/
Expand Down
13 changes: 4 additions & 9 deletions mm/mempool/mempool.c
Expand Up @@ -398,20 +398,15 @@ int mempool_info_task(FAR struct mempool_s *pool,
irqstate_t flags = spin_lock_irqsave(&pool->lock);

DEBUGASSERT(info);
if (info->pid == -2)
if (info->pid == MM_BACKTRACE_FREE_PID)
{
size_t count = mempool_queue_lenth(&pool->queue) +
mempool_queue_lenth(&pool->iqueue);

info->aordblks += count;
info->uordblks += count * pool->blocksize;
if (pool->calibrate)
{
info->aordblks -= pool->nexpend;
info->uordblks -= pool->totalsize;
}
}
else if (info->pid == -1)
else if (info->pid == MM_BACKTRACE_ALLOC_PID)
{
#if CONFIG_MM_BACKTRACE >= 0
size_t count = list_length(&pool->alist);
Expand Down Expand Up @@ -466,7 +461,7 @@ int mempool_info_task(FAR struct mempool_s *pool,

void mempool_memdump(FAR struct mempool_s *pool, pid_t pid)
{
if (pid == -2)
if (pid == MM_BACKTRACE_FREE_PID)
{
FAR sq_entry_t *entry;

Expand All @@ -492,7 +487,7 @@ void mempool_memdump(FAR struct mempool_s *pool, pid_t pid)
list_for_every_entry(&pool->alist, buf, struct mempool_backtrace_s,
node)
{
if (buf->pid == pid || pid == -1)
if (buf->pid == pid || pid == MM_BACKTRACE_ALLOC_PID)
{
# if CONFIG_MM_BACKTRACE > 0
int i;
Expand Down
4 changes: 1 addition & 3 deletions mm/mempool/mempool_multiple.c
Expand Up @@ -257,7 +257,6 @@ mempool_multiple_get_dict(FAR struct mempool_multiple_s *mpool,
* arg - The alloc & free memory fuctions used arg.
* expandsize - The expend mempry for all pools in multiples pool.
* dict_expendsize - The expend size for multiple dictnoary.
* calibrate - Whether to calibrate when counting memory usage.
* Returned Value:
* Return an initialized multiple pool pointer on success,
* otherwise NULL is returned.
Expand All @@ -270,7 +269,7 @@ mempool_multiple_init(FAR const char *name,
mempool_multiple_alloc_t alloc,
mempool_multiple_free_t free,
FAR void *arg, size_t expandsize,
size_t dict_expendsize, bool calibrate)
size_t dict_expendsize)
{
FAR struct mempool_multiple_s *mpool;
FAR struct mempool_s *pools;
Expand Down Expand Up @@ -329,7 +328,6 @@ mempool_multiple_init(FAR const char *name,
pools[i].priv = mpool;
pools[i].alloc = mempool_multiple_alloc_callback;
pools[i].free = mempool_multiple_free_callback;
pools[i].calibrate = calibrate;
#if CONFIG_MM_BACKTRACE >= 0
pools[i].blockalign = mpool->minpoolsize;
#endif
Expand Down
39 changes: 36 additions & 3 deletions mm/mm_heap/mm_initialize.c
Expand Up @@ -41,6 +41,40 @@
# define MEMPOOL_NPOOLS (CONFIG_MM_HEAP_MEMPOOL_THRESHOLD / MM_MIN_CHUNK)
#endif

/****************************************************************************
* Private Functions
****************************************************************************/

#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0 && CONFIG_MM_BACKTRACE >= 0

/****************************************************************************
* Name: mempool_memalign
*
* Description:
* This function call mm_memalign and set mm_backtrace pid to free pid
* avoid repeated calculation.
****************************************************************************/

static FAR void *mempool_memalign(FAR void *arg, size_t alignment,
size_t size)
{
FAR struct mm_allocnode_s *node;
FAR void *ret;

ret = mm_memalign(arg, alignment, size);
if (ret)
{
node = (FAR struct mm_allocnode_s *)
((FAR char *)ret - SIZEOF_MM_ALLOCNODE);
node->pid = MM_BACKTRACE_MEMPOOL_PID;
}

return ret;
}
#else
# define mempool_memalign mm_memalign
#endif

/****************************************************************************
* Public Functions
****************************************************************************/
Expand Down Expand Up @@ -253,11 +287,10 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name,
}

heap->mm_mpool = mempool_multiple_init(name, poolsize, MEMPOOL_NPOOLS,
(mempool_multiple_alloc_t)mm_memalign,
(mempool_multiple_alloc_t)mempool_memalign,
(mempool_multiple_free_t)mm_free, heap,
CONFIG_MM_HEAP_MEMPOOL_EXPAND,
CONFIG_MM_HEAP_MEMPOOL_DICTIONARY_EXPAND,
true);
CONFIG_MM_HEAP_MEMPOOL_DICTIONARY_EXPAND);
#endif

return heap;
Expand Down
6 changes: 3 additions & 3 deletions mm/mm_heap/mm_mallinfo.c
Expand Up @@ -87,16 +87,16 @@ static void mallinfo_task_handler(FAR struct mm_allocnode_s *node,
{
DEBUGASSERT(nodesize >= SIZEOF_MM_ALLOCNODE);
#if CONFIG_MM_BACKTRACE < 0
if (info->pid == -1)
if (info->pid == MM_BACKTRACE_ALLOC_PID)
#else
if (info->pid == -1 || node->pid == info->pid)
if (info->pid == MM_BACKTRACE_ALLOC_PID || node->pid == info->pid)
#endif
{
info->aordblks++;
info->uordblks += nodesize;
}
}
else if (info->pid == -2)
else if (info->pid == MM_BACKTRACE_FREE_PID)
{
info->aordblks++;
info->uordblks += nodesize;
Expand Down
6 changes: 3 additions & 3 deletions mm/mm_heap/mm_memdump.c
Expand Up @@ -60,9 +60,9 @@ static void memdump_handler(FAR struct mm_allocnode_s *node, FAR void *arg)
{
DEBUGASSERT(nodesize >= SIZEOF_MM_ALLOCNODE);
#if CONFIG_MM_BACKTRACE < 0
if (pid == -1)
if (pid == MM_BACKTRACE_ALLOC_PID)
#else
if (pid == -1 || node->pid == pid)
if (pid == MM_BACKTRACE_ALLOC_PID || node->pid == pid)
#endif
{
#if CONFIG_MM_BACKTRACE < 0
Expand Down Expand Up @@ -104,7 +104,7 @@ static void memdump_handler(FAR struct mm_allocnode_s *node, FAR void *arg)
SIZEOF_MM_NODE(fnode->flink) == 0 ||
SIZEOF_MM_NODE(fnode->flink) >= nodesize);

if (pid <= -2)
if (pid <= MM_BACKTRACE_FREE_PID)
{
syslog(LOG_INFO, "%12zu%*p\n",
nodesize, MM_PTR_FMT_WIDTH,
Expand Down
48 changes: 38 additions & 10 deletions mm/tlsf/mm_tlsf.c
Expand Up @@ -220,6 +220,35 @@ static void free_delaylist(FAR struct mm_heap_s *heap)
#endif
}

#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0 && CONFIG_MM_BACKTRACE >= 0

/****************************************************************************
* Name: mempool_memalign
*
* Description:
* This function call mm_memalign and set mm_backtrace pid to free pid
* avoid repeated calculation.
****************************************************************************/

static FAR void *mempool_memalign(FAR void *arg, size_t alignment,
size_t size)
{
FAR struct memdump_backtrace_s *dump;
FAR void *ret;

ret = mm_memalign(arg, alignment, size);
if (ret)
{
dump = ret + mm_malloc_size(arg, ret);
dump->pid = MM_BACKTRACE_MEMPOOL_PID;
}

return ret;
}
#else
# define mempool_memalign mm_memalign
#endif

/****************************************************************************
* Name: mallinfo_handler
****************************************************************************/
Expand Down Expand Up @@ -263,16 +292,16 @@ static void mallinfo_task_handler(FAR void *ptr, size_t size, int used,
if (used)
{
#if CONFIG_MM_BACKTRACE < 0
if (info->pid = -1)
if (info->pid = MM_BACKTRACE_ALLOC_PID)
#else
if (info->pid == -1 || info->pid == dump->pid)
if (info->pid == MM_BACKTRACE_ALLOC_PID || info->pid == dump->pid)
#endif
{
info->aordblks++;
info->uordblks += size;
}
}
else if (info->pid == -2)
else if (info->pid == MM_BACKTRACE_FREE_PID)
{
info->aordblks++;
info->uordblks += size;
Expand Down Expand Up @@ -377,9 +406,9 @@ static void memdump_handler(FAR void *ptr, size_t size, int used,
if (used)
{
#if CONFIG_MM_BACKTRACE < 0
if (pid == -1)
if (pid == MM_BACKTRACE_ALLOC_PID)
#else
if (pid == -1 || dump->pid == pid)
if (pid == MM_BACKTRACE_ALLOC_PID || dump->pid == pid)
#endif
{
#if CONFIG_MM_BACKTRACE < 0
Expand All @@ -406,7 +435,7 @@ static void memdump_handler(FAR void *ptr, size_t size, int used,
#endif
}
}
else if (pid <= -2)
else if (pid <= MM_BACKTRACE_FREE_PID)
{
syslog(LOG_INFO, "%12zu%*p\n", size, MM_PTR_FMT_WIDTH, ptr);
}
Expand Down Expand Up @@ -800,11 +829,10 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name,
}

heap->mm_mpool = mempool_multiple_init(name, poolsize, MEMPOOL_NPOOLS,
(mempool_multiple_alloc_t)mm_memalign,
(mempool_multiple_alloc_t)mempool_algin,
(mempool_multiple_free_t)mm_free, heap,
CONFIG_MM_HEAP_MEMPOOL_EXPAND,
CONFIG_MM_HEAP_MEMPOOL_DICTIONARY_EXPAND,
true);
CONFIG_MM_HEAP_MEMPOOL_DICTIONARY_EXPAND);
#endif

return heap;
Expand Down Expand Up @@ -904,7 +932,7 @@ void mm_memdump(FAR struct mm_heap_s *heap, pid_t pid)
#endif
struct memdump_info_s info;

if (pid >= -1)
if (pid >= MM_BACKTRACE_ALLOC_PID)
{
syslog(LOG_INFO, "Dump all used memory node info:\n");
#if CONFIG_MM_BACKTRACE < 0
Expand Down