Skip to content

Commit

Permalink
memepool:fix memory consumption double counting issue
Browse files Browse the repository at this point in the history
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
  • Loading branch information
anjiahao1 authored and xiaoxiang781216 committed Apr 23, 2023
1 parent 49cd7a7 commit 781a34d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 21 deletions.
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
5 changes: 3 additions & 2 deletions include/nuttx/mm/mm.h
Expand Up @@ -98,8 +98,9 @@
# endif
#endif

#define MM_BACKTRACE_FREE_PID ((pid_t)-2)
#define MM_BACKTRACE_ALLOC_PID ((pid_t)-1)
#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
5 changes: 0 additions & 5 deletions mm/mempool/mempool.c
Expand Up @@ -405,11 +405,6 @@ int mempool_info_task(FAR struct mempool_s *pool,

info->aordblks += count;
info->uordblks += count * pool->blocksize;
if (pool->calibrate)
{
info->aordblks -= pool->nexpend;
info->uordblks -= pool->totalsize;
}
}
else if (info->pid == MM_BACKTRACE_ALLOC_PID)
{
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
34 changes: 31 additions & 3 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 @@ -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

0 comments on commit 781a34d

Please sign in to comment.