Skip to content

Commit

Permalink
mempool:Add mail_info support for multiple pools
Browse files Browse the repository at this point in the history
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
  • Loading branch information
anjiahao1 committed Jun 7, 2023
1 parent 0a034ec commit 459bcdf
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 52 deletions.
10 changes: 6 additions & 4 deletions arch/sim/src/sim/sim_heap.c
Expand Up @@ -381,11 +381,13 @@ void mm_extend(struct mm_heap_s *heap, void *mem, size_t size,
*
****************************************************************************/

int mm_mallinfo(struct mm_heap_s *heap, struct mallinfo *info)
struct mallinfo mm_mallinfo(struct mm_heap_s *heap)
{
memset(info, 0, sizeof(struct mallinfo));
host_mallinfo(&info->aordblks, &info->uordblks);
return 0;
struct mallinfo info;

memset(&info, 0, sizeof(struct mallinfo));
host_mallinfo(&info.aordblks, &info.uordblks);
return info;
}

/****************************************************************************
Expand Down
4 changes: 2 additions & 2 deletions arch/xtensa/src/esp32/esp32_iramheap.c
Expand Up @@ -175,7 +175,7 @@ bool esp32_iramheap_heapmember(void *mem)
*
****************************************************************************/

int esp32_iramheap_mallinfo(struct mallinfo *info)
struct mallinfo esp32_iramheap_mallinfo(void)
{
return mm_mallinfo(g_iramheap, info);
return mm_mallinfo(g_iramheap);
}
2 changes: 1 addition & 1 deletion arch/xtensa/src/esp32/esp32_iramheap.h
Expand Up @@ -136,7 +136,7 @@ bool esp32_iramheap_heapmember(void *mem);
*
****************************************************************************/

int esp32_iramheap_mallinfo(struct mallinfo *info);
struct mallinfo esp32_iramheap_mallinfo(void);

#undef EXTERN
#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion fs/procfs/fs_procfsmeminfo.c
Expand Up @@ -313,7 +313,7 @@ static ssize_t meminfo_read(FAR struct file *filep, FAR char *buffer,

/* Show heap information */

mm_mallinfo(entry->heap, &minfo);
minfo = mm_mallinfo(entry->heap);
linesize = procfs_snprintf(procfile->line, MEMINFO_LINELEN,
"%12s:%11lu%11lu%11lu%11lu%7lu%7lu\n",
entry->name,
Expand Down
13 changes: 13 additions & 0 deletions include/nuttx/mm/mempool.h
Expand Up @@ -60,6 +60,8 @@ typedef CODE FAR void *(*mempool_multiple_alloc_t)(FAR void *arg,
size_t alignment,
size_t size);
typedef CODE void (*mempool_multiple_free_t)(FAR void *arg, FAR void *addr);
typedef CODE size_t (*mempool_multiple_alloc_size_t)(FAR void *arg,
FAR void *addr);

typedef CODE void (mempool_multiple_foreach_t)(FAR struct mempool_s *pool,
FAR void *arg);
Expand Down Expand Up @@ -320,6 +322,7 @@ void mempool_procfs_unregister(FAR struct mempool_procfs_entry_s *entry);
* poolsize - The block size array for pools in multiples pool.
* npools - How many pools in multiples pool.
* alloc - The alloc memory function for multiples pool.
* alloc_size - Get the address size of the alloc function.
* free - The free memory function for multiples pool.
* arg - The alloc & free memory fuctions used arg.
* chunksize - The multiples pool chunk size.
Expand All @@ -337,6 +340,7 @@ FAR struct mempool_multiple_s *
mempool_multiple_init(FAR const char *name,
FAR size_t *poolsize, size_t npools,
mempool_multiple_alloc_t alloc,
mempool_multiple_alloc_size_t alloc_size,
mempool_multiple_free_t free, FAR void *arg,
size_t chunksize, size_t expandsize,
size_t dict_expendsize);
Expand Down Expand Up @@ -492,6 +496,15 @@ void mempool_multiple_foreach(FAR struct mempool_multiple_s *mpool,
mempool_multiple_foreach_t handle,
FAR void *arg);

/****************************************************************************
* Name: mempool_multiple_mallinfo
* Description:
* mallinfo returns a copy of updated current multiples pool information.
****************************************************************************/

struct mallinfo
mempool_multiple_mallinfo(FAR struct mempool_multiple_s *mpool);

/****************************************************************************
* Name: mempool_multiple_info_task
* Description:
Expand Down
2 changes: 1 addition & 1 deletion include/nuttx/mm/mm.h
Expand Up @@ -315,7 +315,7 @@ void kmm_extend(FAR void *mem, size_t size, int region);
/* Functions contained in mm_mallinfo.c *************************************/

struct mallinfo; /* Forward reference */
int mm_mallinfo(FAR struct mm_heap_s *heap, FAR struct mallinfo *info);
struct mallinfo mm_mallinfo(FAR struct mm_heap_s *heap);
struct mallinfo_task; /* Forward reference */
struct mallinfo_task mm_mallinfo_task(FAR struct mm_heap_s *heap,
FAR const struct mm_memdump_s *dump);
Expand Down
4 changes: 1 addition & 3 deletions mm/kmm_heap/kmm_mallinfo.c
Expand Up @@ -45,9 +45,7 @@

struct mallinfo kmm_mallinfo(void)
{
struct mallinfo info;
mm_mallinfo(g_kmmheap, &info);
return info;
return mm_mallinfo(g_kmmheap);
}

/****************************************************************************
Expand Down
99 changes: 79 additions & 20 deletions mm/mempool/mempool_multiple.c
Expand Up @@ -63,17 +63,21 @@ struct mpool_chunk_s

struct mempool_multiple_s
{
FAR struct mempool_s *pools; /* The memory pool array */
size_t npools; /* The number of memory pool array elements */
size_t expandsize; /* The number not will use it to init erery
* pool expandsize
*/
size_t minpoolsize; /* The number is align for each memory pool */
FAR void *arg; /* This pointer is used to store the user's
* private data
*/
mempool_multiple_alloc_t alloc; /* The alloc function for mempool */
mempool_multiple_free_t free; /* The free function for mempool */
FAR struct mempool_s *pools; /* The memory pool array */
size_t npools; /* The number of memory pool array elements */
size_t expandsize; /* The number not will use it to init erery
* pool expandsize
*/
size_t minpoolsize; /* The number is align for each memory pool */
FAR void *arg; /* This pointer is used to store the user's
* private data
*/
mempool_multiple_alloc_t alloc; /* The alloc function for mempool */
mempool_multiple_alloc_size_t alloc_size; /* Get the address size of the
* alloc function
*/
mempool_multiple_free_t free; /* The free function for mempool */
size_t alloced; /* Total size of alloc */

/* This delta describes the relationship between the block size of each
* mempool in multiple mempool by user initialized. It is automatically
Expand All @@ -82,19 +86,19 @@ struct mempool_multiple_s
* arithmetic progressions, otherwise it is an increasing progressions.
*/

size_t delta;
size_t delta;

/* It is used to record the information recorded by the mempool during
* expansion, and find the mempool by adding an index
*/

mutex_t lock;
sq_queue_t chunk_queue;
size_t chunk_size;
size_t dict_used;
size_t dict_col_num_log2;
size_t dict_row_num;
FAR struct mpool_dict_s **dict;
mutex_t lock;
sq_queue_t chunk_queue;
size_t chunk_size;
size_t dict_used;
size_t dict_col_num_log2;
size_t dict_row_num;
FAR struct mpool_dict_s **dict;
};

/****************************************************************************
Expand Down Expand Up @@ -157,7 +161,13 @@ mempool_multiple_alloc_chunk(FAR struct mempool_multiple_s *mpool,

if (mpool->chunk_size < mpool->expandsize)
{
return mpool->alloc(mpool->arg, align, size);
ret = mpool->alloc(mpool->arg, align, size);
if (ret)
{
mpool->alloced += mpool->alloc_size(mpool->arg, ret);
}

return ret;
}

chunk = (FAR struct mpool_chunk_s *)sq_peek(&mpool->chunk_queue);
Expand All @@ -173,6 +183,7 @@ mempool_multiple_alloc_chunk(FAR struct mempool_multiple_s *mpool,
return NULL;
}

mpool->alloced += mpool->alloc_size(mpool->arg, tmp);
chunk = (FAR struct mpool_chunk_s *)(tmp + mpool->chunk_size);
chunk->end = tmp + mpool->chunk_size;
chunk->start = tmp;
Expand Down Expand Up @@ -345,6 +356,7 @@ mempool_multiple_get_dict(FAR struct mempool_multiple_s *mpool,
* poolsize - The block size array for pools in multiples pool.
* npools - How many pools in multiples pool.
* alloc - The alloc memory function for multiples pool.
* alloc_size - Get the address size of the alloc function.
* free - The free memory function for multiples pool.
* arg - The alloc & free memory fuctions used arg.
* chunksize - The multiples pool chunk size.
Expand All @@ -360,6 +372,7 @@ FAR struct mempool_multiple_s *
mempool_multiple_init(FAR const char *name,
FAR size_t *poolsize, size_t npools,
mempool_multiple_alloc_t alloc,
mempool_multiple_alloc_size_t alloc_size,
mempool_multiple_free_t free, FAR void *arg,
size_t chunksize, size_t expandsize,
size_t dict_expendsize)
Expand Down Expand Up @@ -397,11 +410,13 @@ mempool_multiple_init(FAR const char *name,
return NULL;
}

mpool->alloc_size = alloc_size;
mpool->expandsize = expandsize;
mpool->chunk_size = chunksize;
mpool->alloc = alloc;
mpool->free = free;
mpool->arg = arg;
mpool->alloced = alloc_size(arg, mpool);
sq_init(&mpool->chunk_queue);
pools = mempool_multiple_alloc_chunk(mpool, sizeof(uintptr_t),
npools * sizeof(struct mempool_s));
Expand Down Expand Up @@ -704,6 +719,50 @@ void mempool_multiple_foreach(FAR struct mempool_multiple_s *mpool,
}
}

/****************************************************************************
* Name: mempool_multiple_mallinfo
****************************************************************************/

struct mallinfo
mempool_multiple_mallinfo(FAR struct mempool_multiple_s *mpool)
{
struct mallinfo info;
size_t i;

memset(&info, 0, sizeof(struct mallinfo));

nxmutex_lock(&mpool->lock);
info.arena = mpool->alloced;

if (mpool->chunk_size >= mpool->expandsize)
{
FAR struct mpool_chunk_s *chunk;

chunk = (FAR struct mpool_chunk_s *)sq_peek(&mpool->chunk_queue);
info.fordblks += chunk->end - chunk->next;
}

nxmutex_unlock(&mpool->lock);

for (i = 0; i < mpool->npools; i++)
{
struct mempoolinfo_s poolinfo;

mempool_info(mpool->pools + i, &poolinfo);
info.fordblks += (poolinfo.ordblks + poolinfo.iordblks)
* poolinfo.sizeblks;
info.ordblks += poolinfo.ordblks + poolinfo.iordblks;
info.aordblks += poolinfo.aordblks;
if (info.mxordblk < poolinfo.sizeblks)
{
info.mxordblk = poolinfo.sizeblks;
}
}

info.uordblks += mpool->alloced - info.fordblks;
return info;
}

/****************************************************************************
* Name: mempool_multiple_info_task
****************************************************************************/
Expand Down
1 change: 1 addition & 0 deletions mm/mm_heap/mm_initialize.c
Expand Up @@ -288,6 +288,7 @@ 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)mempool_memalign,
(mempool_multiple_alloc_size_t)mm_malloc_size,
(mempool_multiple_free_t)mm_free, heap,
CONFIG_MM_HEAP_MEMPOOL_CHUNK_SIZE,
CONFIG_MM_HEAP_MEMPOOL_EXPAND_SIZE,
Expand Down
24 changes: 16 additions & 8 deletions mm/mm_heap/mm_mallinfo.c
Expand Up @@ -136,20 +136,28 @@ static void mallinfo_task_handler(FAR struct mm_allocnode_s *node,
*
****************************************************************************/

int mm_mallinfo(FAR struct mm_heap_s *heap, FAR struct mallinfo *info)
struct mallinfo mm_mallinfo(FAR struct mm_heap_s *heap)
{
struct mallinfo info;
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
struct mallinfo poolinfo;
#endif
#if CONFIG_MM_REGIONS > 1
int region = heap->mm_nregions;
#else
# define region 1
#endif

DEBUGASSERT(info);
memset(&info, 0, sizeof(info));
mm_foreach(heap, mallinfo_handler, &info);
info.arena = heap->mm_heapsize;

memset(info, 0, sizeof(*info));
mm_foreach(heap, mallinfo_handler, info);
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
poolinfo = mempool_multiple_mallinfo(heap->mm_mpool);

info->arena = heap->mm_heapsize;
info.uordblks -= poolinfo.fordblks;
info.fordblks += poolinfo.fordblks;
#endif

/* Account for the heap->mm_heapend[region] node overhead and the
* heap->mm_heapstart[region]->preceding:
Expand All @@ -158,11 +166,11 @@ int mm_mallinfo(FAR struct mm_heap_s *heap, FAR struct mallinfo *info)
* and SIZEOF_MM_ALLOCNODE = OVERHEAD_MM_ALLOCNODE + sizeof(mmsize_t).
*/

info->uordblks += region * SIZEOF_MM_ALLOCNODE;
info.uordblks += region * SIZEOF_MM_ALLOCNODE;

DEBUGASSERT((size_t)info->uordblks + info->fordblks == heap->mm_heapsize);
DEBUGASSERT((size_t)info.uordblks + info.fordblks == heap->mm_heapsize);

return OK;
return info;
}

/****************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion mm/mm_heap/mm_malloc.c
Expand Up @@ -282,7 +282,7 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size)

mwarn("WARNING: Allocation failed, size %zu\n", alignsize);
#ifdef CONFIG_MM_DUMP_ON_FAILURE
mm_mallinfo(heap, &minfo);
minfo = mm_mallinfo(heap);
mwarn("Total:%d, used:%d, free:%d, largest:%d, nused:%d, nfree:%d\n",
minfo.arena, minfo.uordblks, minfo.fordblks,
minfo.mxordblk, minfo.aordblks, minfo.ordblks);
Expand Down
26 changes: 18 additions & 8 deletions mm/tlsf/mm_tlsf.c
Expand Up @@ -845,6 +845,7 @@ 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)mempool_memalign,
(mempool_multiple_alloc_size_t)mm_malloc_size,
(mempool_multiple_free_t)mm_free, heap,
CONFIG_MM_HEAP_MEMPOOL_CHUNK_SIZE,
CONFIG_MM_HEAP_MEMPOOL_EXPAND_SIZE,
Expand All @@ -862,17 +863,19 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name,
*
****************************************************************************/

int mm_mallinfo(FAR struct mm_heap_s *heap, FAR struct mallinfo *info)
struct mallinfo mm_mallinfo(FAR struct mm_heap_s *heap)
{
struct mallinfo info;
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
struct mallinfo poolinfo;
#endif
#if CONFIG_MM_REGIONS > 1
int region;
#else
# define region 0
#endif

DEBUGASSERT(info);

memset(info, 0, sizeof(struct mallinfo));
memset(&info, 0, sizeof(struct mallinfo));

/* Visit each region */

Expand All @@ -884,15 +887,22 @@ int mm_mallinfo(FAR struct mm_heap_s *heap, FAR struct mallinfo *info)

DEBUGVERIFY(mm_lock(heap));
tlsf_walk_pool(heap->mm_heapstart[region],
mallinfo_handler, info);
mallinfo_handler, &info);
mm_unlock(heap);
}
#undef region

info->arena = heap->mm_heapsize;
info->uordblks = info->arena - info->fordblks;
info.arena = heap->mm_heapsize;
info.uordblks = info.arena - info.fordblks;

#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
poolinfo = mempool_multiple_mallinfo(heap->mm_mpool);

info.uordblks -= poolinfo.fordblks;
info.fordblks += poolinfo.fordblks;
#endif

return OK;
return info;
}

struct mallinfo_task mm_mallinfo_task(FAR struct mm_heap_s *heap,
Expand Down

0 comments on commit 459bcdf

Please sign in to comment.