From c2bcc56546bedce43a25751e7c9438675a0b3d5d Mon Sep 17 00:00:00 2001 From: xuxingliang Date: Tue, 9 Jul 2024 14:35:01 +0800 Subject: [PATCH] sched/note: specify note event for heap instrumentation 1. Add NOTE_HEAP_ prefix for heap note event. 2. Use note type as heap instrumentation parameter. Signed-off-by: xuxingliang Signed-off-by: Neo Xu --- arch/sim/src/sim/sim_heap.c | 8 ++++---- drivers/note/note_driver.c | 8 ++++---- drivers/note/noteram_driver.c | 8 ++++---- include/nuttx/note/note_driver.h | 2 +- include/nuttx/sched_note.h | 26 +++++++++++++------------- mm/mm_heap/mm_free.c | 2 +- mm/mm_heap/mm_malloc.c | 2 +- mm/mm_heap/mm_memalign.c | 3 ++- mm/mm_heap/mm_realloc.c | 4 ++-- mm/tlsf/mm_tlsf.c | 12 ++++++------ sched/Kconfig | 2 +- 11 files changed, 39 insertions(+), 38 deletions(-) diff --git a/arch/sim/src/sim/sim_heap.c b/arch/sim/src/sim/sim_heap.c index ba3c266035..b435f6c5e8 100644 --- a/arch/sim/src/sim/sim_heap.c +++ b/arch/sim/src/sim/sim_heap.c @@ -186,7 +186,7 @@ static void mm_delayfree(struct mm_heap_s *heap, void *mem, bool delay) int size = host_mallocsize(mem); atomic_fetch_sub(&heap->aordblks, 1); atomic_fetch_sub(&heap->uordblks, size); - sched_note_heap(false, heap, mem, size); + sched_note_heap(NOTE_HEAP_FREE, heap, mem, size); host_free(mem); } } @@ -388,10 +388,10 @@ void *mm_realloc(struct mm_heap_s *heap, void *oldmem, { if (oldmem != NULL) { - sched_note_heap(false, heap, oldmem, oldsize); + sched_note_heap(NOTE_HEAP_FREE, heap, oldmem, oldsize); } - sched_note_heap(true, heap, mem, newsize); + sched_note_heap(NOTE_HEAP_ALLOC, heap, mem, newsize); } do @@ -483,7 +483,7 @@ void *mm_memalign(struct mm_heap_s *heap, size_t alignment, size_t size) } size = host_mallocsize(mem); - sched_note_heap(true, heap, mem, size); + sched_note_heap(NOTE_HEAP_ALLOC, heap, mem, size); atomic_fetch_add(&heap->aordblks, 1); atomic_fetch_add(&heap->uordblks, size); usmblks = atomic_load(&heap->usmblks); diff --git a/drivers/note/note_driver.c b/drivers/note/note_driver.c index 96cac54ab7..232efde066 100644 --- a/drivers/note/note_driver.c +++ b/drivers/note/note_driver.c @@ -1355,7 +1355,8 @@ void sched_note_irqhandler(int irq, FAR void *handler, bool enter) #endif #ifdef CONFIG_SCHED_INSTRUMENTATION_HEAP -void sched_note_heap(bool alloc, FAR void *heap, FAR void *mem, size_t size) +void sched_note_heap(uint8_t event, FAR void *heap, FAR void *mem, + size_t size) { FAR struct note_driver_s **driver; struct note_heap_s note; @@ -1371,7 +1372,7 @@ void sched_note_heap(bool alloc, FAR void *heap, FAR void *mem, size_t size) for (driver = g_note_drivers; *driver; driver++) { - if (note_heap(*driver, alloc, heap, mem, size)) + if (note_heap(*driver, event, heap, mem, size)) { continue; } @@ -1383,9 +1384,8 @@ void sched_note_heap(bool alloc, FAR void *heap, FAR void *mem, size_t size) if (!formatted) { - enum note_type_e type = alloc ? NOTE_ALLOC : NOTE_FREE; formatted = true; - note_common(tcb, ¬e.nmm_cmn, sizeof(note), type); + note_common(tcb, ¬e.nmm_cmn, sizeof(note), event); note.heap = heap; note.mem = mem; note.size = size; diff --git a/drivers/note/noteram_driver.c b/drivers/note/noteram_driver.c index f6ba276e3f..754f804486 100644 --- a/drivers/note/noteram_driver.c +++ b/drivers/note/noteram_driver.c @@ -1086,8 +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_ALLOC: - case NOTE_FREE: + case NOTE_HEAP_ALLOC: + case NOTE_HEAP_FREE: { FAR struct note_heap_s *nmm = (FAR struct note_heap_s *)p; FAR struct noteram_dump_task_context_s *tctx; @@ -1100,7 +1100,7 @@ static int noteram_dump_one(FAR uint8_t *p, FAR struct lib_outstream_s *s, tctx = noteram_dump_find_task_context(ctx, pid); if (tctx != NULL) { - tctx->mm_used += note->nc_type == NOTE_FREE ? + tctx->mm_used += note->nc_type == NOTE_HEAP_FREE ? -nmm->size : nmm->size; used = tctx->mm_used; } @@ -1108,7 +1108,7 @@ static int noteram_dump_one(FAR uint8_t *p, FAR struct lib_outstream_s *s, 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_ALLOC], + pid, used, name[note->nc_type - NOTE_HEAP_ALLOC], nmm->heap, nmm->size, nmm->mem); } break; diff --git a/include/nuttx/note/note_driver.h b/include/nuttx/note/note_driver.h index a36da233b4..5372e14643 100644 --- a/include/nuttx/note/note_driver.h +++ b/include/nuttx/note/note_driver.h @@ -90,7 +90,7 @@ struct note_driver_ops_s FAR void *handler, bool enter); #endif #ifdef CONFIG_SCHED_INSTRUMENTATION_HEAP - CODE void (*heap)(FAR struct note_driver_s *drv, bool alloc, + CODE void (*heap)(FAR struct note_driver_s *drv, uint8_t event, FAR void *heap, FAR void *mem, size_t size); #endif #ifdef CONFIG_SCHED_INSTRUMENTATION_DUMP diff --git a/include/nuttx/sched_note.h b/include/nuttx/sched_note.h index cdc0145b22..44947751dd 100644 --- a/include/nuttx/sched_note.h +++ b/include/nuttx/sched_note.h @@ -186,9 +186,8 @@ enum note_type_e NOTE_SYSCALL_LEAVE, NOTE_IRQ_ENTER, NOTE_IRQ_LEAVE, - NOTE_ALLOC, - NOTE_FREE, - NOTE_REALLOC, + NOTE_HEAP_ALLOC, + NOTE_HEAP_FREE, NOTE_DUMP_STRING, NOTE_DUMP_BINARY, NOTE_DUMP_BEGIN, @@ -383,6 +382,14 @@ struct note_irqhandler_s uint8_t nih_irq; /* IRQ number */ }; +struct note_heap_s +{ + struct note_common_s nmm_cmn; /* Common note parameters */ + FAR void *heap; + FAR void *mem; + size_t size; +}; + struct note_string_s { struct note_common_s nst_cmn; /* Common note parameters */ @@ -403,14 +410,6 @@ struct note_binary_s #define SIZEOF_NOTE_BINARY(n) (sizeof(struct note_binary_s) + \ ((n) - 1) * sizeof(uint8_t)) -struct note_heap_s -{ - struct note_common_s nmm_cmn; /* Common note parameters */ - FAR void *heap; - FAR void *mem; - size_t size; -}; - struct note_counter_s { long int value; @@ -554,9 +553,10 @@ void sched_note_irqhandler(int irq, FAR void *handler, bool enter); #endif #ifdef CONFIG_SCHED_INSTRUMENTATION_HEAP -void sched_note_heap(bool alloc, FAR void *heap, FAR void *mem, size_t size); +void sched_note_heap(uint8_t event, FAR void *heap, FAR void *mem, + size_t size); #else -# define sched_note_heap(a,h,m,s) +# define sched_note_heap(e,h,m,s) #endif #ifdef CONFIG_SCHED_INSTRUMENTATION_DUMP diff --git a/mm/mm_heap/mm_free.c b/mm/mm_heap/mm_free.c index 7afac84668..5a1dcb7989 100644 --- a/mm/mm_heap/mm_free.c +++ b/mm/mm_heap/mm_free.c @@ -128,7 +128,7 @@ void mm_delayfree(FAR struct mm_heap_s *heap, FAR void *mem, bool delay) /* Update heap statistics */ heap->mm_curused -= nodesize; - sched_note_heap(false, heap, mem, nodesize); + sched_note_heap(NOTE_HEAP_FREE, heap, mem, nodesize); /* Check if the following node is free and, if so, merge it */ diff --git a/mm/mm_heap/mm_malloc.c b/mm/mm_heap/mm_malloc.c index fd72eeb007..4345fc1f4b 100644 --- a/mm/mm_heap/mm_malloc.c +++ b/mm/mm_heap/mm_malloc.c @@ -327,7 +327,7 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size) { MM_ADD_BACKTRACE(heap, node); ret = kasan_unpoison(ret, nodesize - MM_ALLOCNODE_OVERHEAD); - sched_note_heap(true, heap, ret, nodesize); + sched_note_heap(NOTE_HEAP_ALLOC, heap, ret, nodesize); #ifdef CONFIG_MM_FILL_ALLOCATIONS memset(ret, MM_ALLOC_MAGIC, alignsize - MM_ALLOCNODE_OVERHEAD); #endif diff --git a/mm/mm_heap/mm_memalign.c b/mm/mm_heap/mm_memalign.c index 1d963055b2..d446c600bc 100644 --- a/mm/mm_heap/mm_memalign.c +++ b/mm/mm_heap/mm_memalign.c @@ -281,7 +281,8 @@ FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t alignment, alignedchunk = (uintptr_t)kasan_unpoison((FAR const void *)alignedchunk, size - MM_ALLOCNODE_OVERHEAD); - sched_note_heap(true, heap, (FAR void *)alignedchunk, size); + sched_note_heap(NOTE_HEAP_ALLOC, heap, (FAR void *)alignedchunk, size); + DEBUGASSERT(alignedchunk % alignment == 0); return (FAR void *)alignedchunk; } diff --git a/mm/mm_heap/mm_realloc.c b/mm/mm_heap/mm_realloc.c index f1f1284b55..a6e67d8ce2 100644 --- a/mm/mm_heap/mm_realloc.c +++ b/mm/mm_heap/mm_realloc.c @@ -383,8 +383,8 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem, heap->mm_maxused = heap->mm_curused; } - sched_note_heap(false, heap, oldmem, oldsize); - sched_note_heap(true, heap, newmem, newsize); + sched_note_heap(NOTE_HEAP_FREE, heap, oldmem, oldsize); + sched_note_heap(NOTE_HEAP_ALLOC, heap, newmem, newsize); mm_unlock(heap); MM_ADD_BACKTRACE(heap, (FAR char *)newmem - MM_SIZEOF_ALLOCNODE); diff --git a/mm/tlsf/mm_tlsf.c b/mm/tlsf/mm_tlsf.c index 6962eed550..51d351651e 100644 --- a/mm/tlsf/mm_tlsf.c +++ b/mm/tlsf/mm_tlsf.c @@ -510,8 +510,8 @@ static void mm_delayfree(FAR struct mm_heap_s *heap, FAR void *mem, { /* Update heap statistics */ - heap->mm_curused -= mm_malloc_size(heap, mem); - sched_note_heap(false, heap, mem, size); + heap->mm_curused -= size; + sched_note_heap(NOTE_HEAP_FREE, heap, mem, size); tlsf_free(heap->mm_tlsf, mem); } @@ -1189,7 +1189,7 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size) #endif ret = kasan_unpoison(ret, nodesize); - sched_note_heap(true, heap, ret, nodesize); + sched_note_heap(NOTE_HEAP_ALLOC, heap, ret, nodesize); #ifdef CONFIG_MM_FILL_ALLOCATIONS memset(ret, 0xaa, nodesize); @@ -1269,7 +1269,7 @@ FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t alignment, memdump_backtrace(heap, buf); #endif ret = kasan_unpoison(ret, nodesize); - sched_note_heap(true, heap, ret, nodesize); + sched_note_heap(NOTE_HEAP_ALLOC, heap, ret, nodesize); } #if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0 @@ -1397,8 +1397,8 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem, memdump_backtrace(heap, buf); #endif - sched_note_heap(false, heap, oldmem, oldsize); - sched_note_heap(true, heap, newmem, newsize); + sched_note_heap(NOTE_HEAP_FREE, heap, oldmem, oldsize); + sched_note_heap(NOTE_HEAP_ALLOC, heap, newmem, newsize); } #if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0 diff --git a/sched/Kconfig b/sched/Kconfig index 06fd953df0..63b27f3752 100644 --- a/sched/Kconfig +++ b/sched/Kconfig @@ -1290,7 +1290,7 @@ config SCHED_INSTRUMENTATION_HEAP ---help--- Enables additional hooks for heap allocation. - void sched_note_heap(bool alloc, FAR void* heap, FAR void *mem, size_t size) + void sched_note_heap(uint8_t event, FAR void* heap, FAR void *mem, size_t size); config SCHED_INSTRUMENTATION_DUMP bool "Use note dump for instrumentation"