From 7e89ff5d117a070ed378be75bfd05e9c865f1853 Mon Sep 17 00:00:00 2001 From: xuxingliang Date: Wed, 3 Jul 2024 13:28:42 +0800 Subject: [PATCH] sched/note: add note when mm add new region Signed-off-by: xuxingliang Signed-off-by: Neo Xu --- arch/sim/src/sim/sim_heap.c | 5 +++++ drivers/note/noteram_driver.c | 11 +++++++---- include/nuttx/sched_note.h | 2 ++ mm/mm_heap/mm_initialize.c | 6 ++++++ mm/tlsf/mm_tlsf.c | 5 +++++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/arch/sim/src/sim/sim_heap.c b/arch/sim/src/sim/sim_heap.c index b435f6c5e8..0dc48e87f4 100644 --- a/arch/sim/src/sim/sim_heap.c +++ b/arch/sim/src/sim/sim_heap.c @@ -230,6 +230,7 @@ struct mm_heap_s *mm_initialize(const char *name, procfs_register_meminfo(&heap->mm_procfs); #endif + sched_note_heap(NOTE_HEAP_ADD, heap, heap_start, heap_size); return heap; } @@ -251,6 +252,10 @@ struct mm_heap_s *mm_initialize(const char *name, void mm_uninitialize(struct mm_heap_s *heap) { + sched_note_heap(NOTE_HEAP_REMOVE, heap, heap_start, + (uintptr_t)heap->mm_heapend[0] - + (uintptr_t)heap->mm_heapstart[0]); + #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMINFO) procfs_unregister_meminfo(&heap->mm_procfs); #endif diff --git a/drivers/note/noteram_driver.c b/drivers/note/noteram_driver.c index 754f804486..0a1c63a9f5 100644 --- a/drivers/note/noteram_driver.c +++ b/drivers/note/noteram_driver.c @@ -1086,6 +1086,8 @@ static int noteram_dump_one(FAR uint8_t *p, FAR struct lib_outstream_s *s, break; #endif #ifdef CONFIG_SCHED_INSTRUMENTATION_HEAP + case NOTE_HEAP_ADD: + case NOTE_HEAP_REMOVE: case NOTE_HEAP_ALLOC: case NOTE_HEAP_FREE: { @@ -1094,21 +1096,22 @@ static int noteram_dump_one(FAR uint8_t *p, FAR struct lib_outstream_s *s, int used = 0; FAR const char *name[] = { - "malloc", "free" + "add", "remove", "malloc", "free" }; tctx = noteram_dump_find_task_context(ctx, pid); if (tctx != NULL) { - tctx->mm_used += note->nc_type == NOTE_HEAP_FREE ? - -nmm->size : nmm->size; + tctx->mm_used += note->nc_type == NOTE_HEAP_FREE ? -nmm->size + : note->nc_type == NOTE_HEAP_ALLOC ? nmm->size + : 0; used = tctx->mm_used; } ret += noteram_dump_header(s, &nmm->nmm_cmn, ctx); ret += lib_sprintf(s, "tracing_mark_write: C|%d|Heap Usage|%d|%s" ": heap: %p size:%" PRIiPTR ", address: %p\n", - pid, used, name[note->nc_type - NOTE_HEAP_ALLOC], + pid, used, name[note->nc_type - NOTE_HEAP_ADD], nmm->heap, nmm->size, nmm->mem); } break; diff --git a/include/nuttx/sched_note.h b/include/nuttx/sched_note.h index 44947751dd..2b9725de47 100644 --- a/include/nuttx/sched_note.h +++ b/include/nuttx/sched_note.h @@ -186,6 +186,8 @@ enum note_type_e NOTE_SYSCALL_LEAVE, NOTE_IRQ_ENTER, NOTE_IRQ_LEAVE, + NOTE_HEAP_ADD, + NOTE_HEAP_REMOVE, NOTE_HEAP_ALLOC, NOTE_HEAP_FREE, NOTE_DUMP_STRING, diff --git a/mm/mm_heap/mm_initialize.c b/mm/mm_heap/mm_initialize.c index 6852b439ad..d35604cd7b 100644 --- a/mm/mm_heap/mm_initialize.c +++ b/mm/mm_heap/mm_initialize.c @@ -30,6 +30,7 @@ #include #include +#include #include #include @@ -201,6 +202,8 @@ void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart, mm_addfreechunk(heap, node); heap->mm_curused += 2 * MM_SIZEOF_ALLOCNODE; mm_unlock(heap); + + sched_note_heap(NOTE_HEAP_ADD, heap, heapstart, heapsize); } /**************************************************************************** @@ -366,6 +369,9 @@ void mm_uninitialize(FAR struct mm_heap_s *heap) for (i = 0; i < CONFIG_MM_REGIONS; i++) { kasan_unregister(heap->mm_heapstart[i]); + sched_note_heap(NOTE_HEAP_REMOVE, heap, heap->mm_heapstart[i], + (uintptr_t)heap->mm_heapend[i] - + (uintptr_t)heap->mm_heapstart[i]); } #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMINFO) diff --git a/mm/tlsf/mm_tlsf.c b/mm/tlsf/mm_tlsf.c index 51d351651e..811cb47217 100644 --- a/mm/tlsf/mm_tlsf.c +++ b/mm/tlsf/mm_tlsf.c @@ -600,6 +600,8 @@ void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart, tlsf_add_pool(heap->mm_tlsf, heapstart, heapsize); mm_unlock(heap); + + sched_note_heap(NOTE_HEAP_ADD, heap, heapstart, heapsize); } /**************************************************************************** @@ -1439,6 +1441,9 @@ void mm_uninitialize(FAR struct mm_heap_s *heap) for (i = 0; i < CONFIG_MM_REGIONS; i++) { kasan_unregister(heap->mm_heapstart[i]); + sched_note_heap(NOTE_HEAP_REMOVE, heap, heap->mm_heapstart[i], + (uintptr_t)heap->mm_heapend[i] - + (uintptr_t)heap->mm_heapstart[i]); } #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMINFO)