mirror of
https://github.com/apache/nuttx.git
synced 2026-06-04 23:03:27 +08:00
mm: record the maximum system memory usage
Add the usmblks field to mallinfo to record the maximum space allocated historically in the system https://man7.org/linux/man-pages/man3/mallinfo.3.html#:~:text=mmap(2).-,usmblks,-This%20field%20is Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
This commit is contained in:
@@ -230,6 +230,14 @@ struct mm_heap_s
|
||||
|
||||
size_t mm_heapsize;
|
||||
|
||||
/* This is the heap maximum used memory size */
|
||||
|
||||
size_t mm_maxused;
|
||||
|
||||
/* This is the current used size of the heap */
|
||||
|
||||
size_t mm_curused;
|
||||
|
||||
/* This is the first and last nodes of the heap */
|
||||
|
||||
FAR struct mm_allocnode_s *mm_heapstart[CONFIG_MM_REGIONS];
|
||||
|
||||
@@ -121,6 +121,10 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
|
||||
|
||||
node->size &= ~MM_ALLOC_BIT;
|
||||
|
||||
/* Update heap statistics */
|
||||
|
||||
heap->mm_curused -= nodesize;
|
||||
|
||||
/* Check if the following node is free and, if so, merge it */
|
||||
|
||||
next = (FAR struct mm_freenode_s *)((FAR char *)node + nodesize);
|
||||
|
||||
@@ -145,6 +145,7 @@ struct mallinfo mm_mallinfo(FAR struct mm_heap_s *heap)
|
||||
info.arena = heap->mm_heapsize;
|
||||
info.arena += sizeof(struct mm_heap_s);
|
||||
info.uordblks += sizeof(struct mm_heap_s);
|
||||
info.usmblks = heap->mm_maxused + sizeof(struct mm_heap_s);
|
||||
|
||||
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
|
||||
poolinfo = mempool_multiple_mallinfo(heap->mm_mpool);
|
||||
|
||||
@@ -251,6 +251,14 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size)
|
||||
next->size &= ~MM_PREVFREE_BIT;
|
||||
}
|
||||
|
||||
/* Update heap statistics */
|
||||
|
||||
heap->mm_curused += MM_SIZEOF_NODE(node);
|
||||
if (heap->mm_curused > heap->mm_maxused)
|
||||
{
|
||||
heap->mm_maxused = heap->mm_curused;
|
||||
}
|
||||
|
||||
/* Handle the case of an exact size match */
|
||||
|
||||
node->size |= MM_ALLOC_BIT;
|
||||
|
||||
@@ -259,6 +259,14 @@ FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t alignment,
|
||||
mm_shrinkchunk(heap, node, size);
|
||||
}
|
||||
|
||||
/* Update heap statistics */
|
||||
|
||||
heap->mm_curused += MM_SIZEOF_NODE(node);
|
||||
if (heap->mm_curused > heap->mm_maxused)
|
||||
{
|
||||
heap->mm_maxused = heap->mm_curused;
|
||||
}
|
||||
|
||||
mm_unlock(heap);
|
||||
|
||||
MM_ADD_BACKTRACE(heap, node);
|
||||
|
||||
@@ -365,6 +365,14 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
|
||||
}
|
||||
}
|
||||
|
||||
/* Update heap statistics */
|
||||
|
||||
heap->mm_curused += newsize - oldsize;
|
||||
if (heap->mm_curused > heap->mm_maxused)
|
||||
{
|
||||
heap->mm_maxused = heap->mm_curused;
|
||||
}
|
||||
|
||||
mm_unlock(heap);
|
||||
MM_ADD_BACKTRACE(heap, (FAR char *)newmem - MM_SIZEOF_ALLOCNODE);
|
||||
|
||||
|
||||
@@ -78,6 +78,14 @@ struct mm_heap_s
|
||||
|
||||
size_t mm_heapsize;
|
||||
|
||||
/* This is the heap maximum used memory size */
|
||||
|
||||
size_t mm_maxused;
|
||||
|
||||
/* This is the current used size of the heap */
|
||||
|
||||
size_t mm_curused;
|
||||
|
||||
/* This is the first and last of the heap */
|
||||
|
||||
FAR void *mm_heapstart[CONFIG_MM_REGIONS];
|
||||
@@ -698,6 +706,10 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
|
||||
|
||||
kasan_poison(mem, mm_malloc_size(heap, mem));
|
||||
|
||||
/* Update heap statistics */
|
||||
|
||||
heap->mm_curused -= mm_malloc_size(heap, mem);
|
||||
|
||||
/* Pass, return to the tlsf pool */
|
||||
|
||||
tlsf_free(heap->mm_tlsf, mem);
|
||||
@@ -891,6 +903,7 @@ struct mallinfo mm_mallinfo(FAR struct mm_heap_s *heap)
|
||||
|
||||
info.arena = heap->mm_heapsize;
|
||||
info.uordblks = info.arena - info.fordblks;
|
||||
info.usmblks = heap->mm_maxused;
|
||||
|
||||
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
|
||||
poolinfo = mempool_multiple_mallinfo(heap->mm_mpool);
|
||||
@@ -1061,6 +1074,12 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size)
|
||||
ret = tlsf_malloc(heap->mm_tlsf, size);
|
||||
#endif
|
||||
|
||||
heap->mm_curused += mm_malloc_size(heap, ret);
|
||||
if (heap->mm_curused > heap->mm_maxused)
|
||||
{
|
||||
heap->mm_maxused = heap->mm_curused;
|
||||
}
|
||||
|
||||
mm_unlock(heap);
|
||||
|
||||
if (ret)
|
||||
@@ -1119,6 +1138,13 @@ FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t alignment,
|
||||
#else
|
||||
ret = tlsf_memalign(heap->mm_tlsf, alignment, size);
|
||||
#endif
|
||||
|
||||
heap->mm_curused += mm_malloc_size(heap, ret);
|
||||
if (heap->mm_curused > heap->mm_maxused)
|
||||
{
|
||||
heap->mm_maxused = heap->mm_curused;
|
||||
}
|
||||
|
||||
mm_unlock(heap);
|
||||
|
||||
if (ret)
|
||||
@@ -1217,12 +1243,20 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
|
||||
/* Allocate from the tlsf pool */
|
||||
|
||||
DEBUGVERIFY(mm_lock(heap));
|
||||
heap->mm_curused -= mm_malloc_size(heap, oldmem);
|
||||
#if CONFIG_MM_BACKTRACE >= 0
|
||||
newmem = tlsf_realloc(heap->mm_tlsf, oldmem, size +
|
||||
sizeof(struct memdump_backtrace_s));
|
||||
#else
|
||||
newmem = tlsf_realloc(heap->mm_tlsf, oldmem, size);
|
||||
#endif
|
||||
|
||||
heap->mm_curused += mm_malloc_size(heap, newmem ? newmem : oldmem);
|
||||
if (heap->mm_curused > heap->mm_maxused)
|
||||
{
|
||||
heap->mm_maxused = heap->mm_curused;
|
||||
}
|
||||
|
||||
mm_unlock(heap);
|
||||
|
||||
#if CONFIG_MM_BACKTRACE >= 0
|
||||
|
||||
Reference in New Issue
Block a user