diff --git a/include/zephyr/tracing/tracing.h b/include/zephyr/tracing/tracing.h index f1bbddeec51..dc148302886 100644 --- a/include/zephyr/tracing/tracing.h +++ b/include/zephyr/tracing/tracing.h @@ -1690,6 +1690,25 @@ */ #define sys_port_trace_k_heap_free(h) +/** + * @brief Trace Heap realloc enter + * @param h Heap object + * @param ptr Pointer to reallocate + * @param bytes Bytes to reallocate + * @param timeout Timeout period + */ +#define sys_port_trace_k_heap_realloc_enter(h, ptr, bytes, timeout) + +/** + * @brief Trace Heap realloc exit + * @param h Heap object + * @param ptr Pointer to reallocate + * @param bytes Bytes to reallocate + * @param timeout Timeout period + * @param ret Return value + */ +#define sys_port_trace_k_heap_realloc_exit(h, ptr, bytes, timeout, ret) + /** * @brief Trace System Heap aligned alloc enter * @param heap Heap object @@ -1743,6 +1762,19 @@ */ #define sys_port_trace_k_heap_sys_k_calloc_exit(heap, ret) +/** + * @brief Trace System heap realloc enter + * @param heap + */ +#define sys_port_trace_k_heap_sys_k_realloc_enter(heap, ptr) + +/** + * @brief Trace System heap realloc exit + * @param heap Heap object + * @param ret Return value + */ +#define sys_port_trace_k_heap_sys_k_realloc_exit(heap, ptr, ret) + /** @} */ /* end of subsys_tracing_apis_heap */ /** diff --git a/kernel/kheap.c b/kernel/kheap.c index a452f172a56..55b9feffced 100644 --- a/kernel/kheap.c +++ b/kernel/kheap.c @@ -123,6 +123,8 @@ void *k_heap_realloc(struct k_heap *heap, void *ptr, size_t bytes, k_timeout_t t k_spinlock_key_t key = k_spin_lock(&heap->lock); + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_heap, realloc, heap, ptr, bytes, timeout); + __ASSERT(!arch_is_in_isr() || K_TIMEOUT_EQ(timeout, K_NO_WAIT), ""); while (ret == NULL) { @@ -138,6 +140,8 @@ void *k_heap_realloc(struct k_heap *heap, void *ptr, size_t bytes, k_timeout_t t key = k_spin_lock(&heap->lock); } + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap, realloc, heap, ptr, bytes, timeout, ret); + k_spin_unlock(&heap->lock, key); return ret; } diff --git a/kernel/mempool.c b/kernel/mempool.c index 3d7dba22fac..c8298f0d6fc 100644 --- a/kernel/mempool.c +++ b/kernel/mempool.c @@ -129,15 +129,22 @@ void *k_realloc(void *ptr, size_t size) ptr = --heap_ref; heap = *heap_ref; + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_heap_sys, k_realloc, heap, ptr); + if (size_add_overflow(size, sizeof(heap_ref), &size)) { + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap_sys, k_realloc, heap, ptr, NULL); return NULL; } ret = k_heap_realloc(heap, ptr, size, K_NO_WAIT); + if (ret != NULL) { heap_ref = ret; ret = ++heap_ref; } + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap_sys, k_realloc, heap, ptr, ret); + return ret; } diff --git a/subsys/tracing/ctf/tracing_ctf.h b/subsys/tracing/ctf/tracing_ctf.h index 986a314f915..2c95195f94d 100644 --- a/subsys/tracing/ctf/tracing_ctf.h +++ b/subsys/tracing/ctf/tracing_ctf.h @@ -303,6 +303,8 @@ extern "C" { #define sys_port_trace_k_heap_alloc_enter(heap, timeout) #define sys_port_trace_k_heap_alloc_exit(heap, timeout, ret) #define sys_port_trace_k_heap_free(heap) +#define sys_port_trace_k_heap_realloc_enter(h, ptr, bytes, timeout) +#define sys_port_trace_k_heap_realloc_exit(h, ptr, bytes, timeout, ret) #define sys_port_trace_k_heap_sys_k_aligned_alloc_enter(heap) #define sys_port_trace_k_heap_sys_k_aligned_alloc_exit(heap, ret) #define sys_port_trace_k_heap_sys_k_malloc_enter(heap) @@ -311,6 +313,8 @@ extern "C" { #define sys_port_trace_k_heap_sys_k_free_exit(heap, heap_ref) #define sys_port_trace_k_heap_sys_k_calloc_enter(heap) #define sys_port_trace_k_heap_sys_k_calloc_exit(heap, ret) +#define sys_port_trace_k_heap_sys_k_realloc_enter(heap, ptr) +#define sys_port_trace_k_heap_sys_k_realloc_exit(heap, ptr, ret) #define sys_port_trace_k_mem_slab_init(slab, rc) #define sys_port_trace_k_mem_slab_alloc_enter(slab, timeout) diff --git a/subsys/tracing/sysview/tracing_sysview.h b/subsys/tracing/sysview/tracing_sysview.h index 8d50f02c827..b5255824d16 100644 --- a/subsys/tracing/sysview/tracing_sysview.h +++ b/subsys/tracing/sysview/tracing_sysview.h @@ -566,6 +566,14 @@ void sys_trace_thread_info(struct k_thread *thread); #define sys_port_trace_k_heap_free(heap) \ SEGGER_SYSVIEW_RecordU32(TID_HEAP_FREE, (uint32_t)(uintptr_t)heap) +#define sys_port_trace_k_heap_realloc_enter(heap, ptr, bytes, timeout) \ + SEGGER_SYSVIEW_RecordU32x4(TID_HEAP_REALLOC, (uint32_t)(uintptr_t)heap, \ + (uint32_t)(uintptr_t)ptr, (uint32_t)bytes, \ + (uint32_t)timeout.ticks) + +#define sys_port_trace_k_heap_realloc_exit(heap, ptr, bytes, timeout, ret) \ + SEGGER_SYSVIEW_RecordEndCallU32(TID_HEAP_REALLOC, (uint32_t)ret) + #define sys_port_trace_k_heap_sys_k_aligned_alloc_enter(heap) #define sys_port_trace_k_heap_sys_k_aligned_alloc_exit(heap, ret) #define sys_port_trace_k_heap_sys_k_malloc_enter(heap) @@ -574,6 +582,8 @@ void sys_trace_thread_info(struct k_thread *thread); #define sys_port_trace_k_heap_sys_k_free_exit(heap, heap_ref) #define sys_port_trace_k_heap_sys_k_calloc_enter(heap) #define sys_port_trace_k_heap_sys_k_calloc_exit(heap, ret) +#define sys_port_trace_k_heap_sys_k_realloc_enter(heap, ptr) +#define sys_port_trace_k_heap_sys_k_realloc_exit(heap, ptr, ret) #define sys_port_trace_k_mem_slab_init(slab, rc) \ SEGGER_SYSVIEW_RecordU32(TID_MSLAB_INIT, (uint32_t)(uintptr_t)slab) diff --git a/subsys/tracing/sysview/tracing_sysview_ids.h b/subsys/tracing/sysview/tracing_sysview_ids.h index 4c50130cc6d..3b8cd870175 100644 --- a/subsys/tracing/sysview/tracing_sysview_ids.h +++ b/subsys/tracing/sysview/tracing_sysview_ids.h @@ -71,6 +71,7 @@ extern "C" { #define TID_HEAP_ALLOC (45u + TID_OFFSET) #define TID_HEAP_FREE (46u + TID_OFFSET) #define TID_HEAP_ALIGNED_ALLOC (47u + TID_OFFSET) +#define TID_HEAP_REALLOC (48u + TID_OFFSET) #define TID_MSLAB_INIT (52u + TID_OFFSET) #define TID_MSLAB_ALLOC (53u + TID_OFFSET) diff --git a/subsys/tracing/test/tracing_string_format_test.c b/subsys/tracing/test/tracing_string_format_test.c index 29d781ad1ba..d0e5eb67371 100644 --- a/subsys/tracing/test/tracing_string_format_test.c +++ b/subsys/tracing/test/tracing_string_format_test.c @@ -361,6 +361,16 @@ void sys_trace_k_heap_free(struct k_heap *h, void *mem) TRACING_STRING("%s: %p\n", __func__, h); } +void sys_trace_k_heap_realloc_enter(struct k_heap *h, void *ptr, size_t bytes, k_timeout_t timeout) +{ + TRACING_STRING("%s: %p\n", __func__, h); +} +void sys_trace_k_heap_realloc_exit(struct k_heap *h, void *ptr, size_t bytes, k_timeout_t timeout, + void *ret) +{ + TRACING_STRING("%s: %p\n", __func__, h); +} + void sys_trace_k_heap_aligned_alloc_blocking(struct k_heap *h, size_t bytes, k_timeout_t timeout) { TRACING_STRING("%s: %p\n", __func__, h); diff --git a/subsys/tracing/test/tracing_test.h b/subsys/tracing/test/tracing_test.h index 71b63a762c5..1e40a6831e8 100644 --- a/subsys/tracing/test/tracing_test.h +++ b/subsys/tracing/test/tracing_test.h @@ -379,6 +379,10 @@ #define sys_port_trace_k_heap_alloc_exit(h, timeout, ret) \ sys_trace_k_heap_alloc_exit(h, bytes, timeout, ret) #define sys_port_trace_k_heap_free(h) sys_trace_k_heap_free(h, mem) +#define sys_port_trace_k_heap_realloc_enter(h, ptr, bytes, timeout) \ + sys_trace_k_heap_realloc_enter(h, ptr, bytes, timeout) +#define sys_port_trace_k_heap_realloc_exit(h, ptr, bytes, timeout, ret) \ + sys_trace_k_heap_realloc_exit(h, ptr, bytes, timeout, ret) #define sys_port_trace_k_heap_sys_k_aligned_alloc_enter(heap) \ sys_trace_k_heap_sys_k_aligned_alloc_enter(heap, align, size) #define sys_port_trace_k_heap_sys_k_aligned_alloc_exit(heap, ret) \ @@ -395,6 +399,10 @@ sys_trace_k_heap_sys_k_calloc_enter(heap, nmemb, size) #define sys_port_trace_k_heap_sys_k_calloc_exit(heap, ret) \ sys_trace_k_heap_sys_k_calloc_exit(heap, nmemb, size, ret) +#define sys_port_trace_k_heap_sys_k_realloc_enter(heap, ptr) \ + sys_trace_k_heap_sys_k_realloc_enter(heap, ptr, size) +#define sys_port_trace_k_heap_sys_k_realloc_exit(heap, ptr, ret) \ + sys_trace_k_heap_sys_k_realloc_exit(heap, ptr, size, ret) #define sys_port_trace_k_mem_slab_init(slab, rc) \ sys_trace_k_mem_slab_init(slab, buffer, block_size, num_blocks, rc) @@ -654,6 +662,9 @@ void sys_trace_k_heap_aligned_alloc_blocking(struct k_heap *h, size_t bytes, k_t void sys_trace_k_heap_aligned_alloc_exit(struct k_heap *h, size_t bytes, k_timeout_t timeout, void *ret); void sys_trace_k_heap_free(struct k_heap *h, void *mem); +void sys_trace_k_heap_realloc_enter(struct k_heap *h, void *ptr, size_t bytes, k_timeout_t timeout); +void sys_trace_k_heap_realloc_exit(struct k_heap *h, void *ptr, size_t bytes, k_timeout_t timeout, + void *ret); void sys_trace_k_heap_sys_k_aligned_alloc_enter(struct k_heap *h, size_t align, size_t size); void sys_trace_k_heap_sys_k_aligned_alloc_exit(struct k_heap *h, size_t align, size_t size, void *ret); @@ -663,6 +674,8 @@ void sys_trace_k_heap_sys_k_free_enter(struct k_heap *h, struct k_heap **heap_re void sys_trace_k_heap_sys_k_free_exit(struct k_heap *h, struct k_heap **heap_ref); void sys_trace_k_heap_sys_k_calloc_enter(struct k_heap *h, size_t nmemb, size_t size); void sys_trace_k_heap_sys_k_calloc_exit(struct k_heap *h, size_t nmemb, size_t size, void *ret); +void sys_trace_k_heap_sys_k_realloc_enter(struct k_heap *h, void *ptr, size_t bytes); +void sys_trace_k_heap_sys_k_realloc_exit(struct k_heap *h, void *ptr, size_t bytes, void *ret); void sys_trace_k_mem_slab_init(struct k_mem_slab *slab, void *buffer, size_t block_size, uint32_t num_blocks, int ret); diff --git a/subsys/tracing/user/tracing_user.h b/subsys/tracing/user/tracing_user.h index c4231503d70..4f6f808b99b 100644 --- a/subsys/tracing/user/tracing_user.h +++ b/subsys/tracing/user/tracing_user.h @@ -288,6 +288,8 @@ void sys_trace_idle(void); #define sys_port_trace_k_heap_alloc_enter(heap, timeout) #define sys_port_trace_k_heap_alloc_exit(heap, timeout, ret) #define sys_port_trace_k_heap_free(heap) +#define sys_port_trace_k_heap_realloc_enter(h, ptr, bytes, timeout) +#define sys_port_trace_k_heap_realloc_exit(h, ptr, bytes, timeout, ret) #define sys_port_trace_k_heap_sys_k_aligned_alloc_enter(heap) #define sys_port_trace_k_heap_sys_k_aligned_alloc_exit(heap, ret) #define sys_port_trace_k_heap_sys_k_malloc_enter(heap) @@ -296,6 +298,8 @@ void sys_trace_idle(void); #define sys_port_trace_k_heap_sys_k_free_exit(heap, heap_ref) #define sys_port_trace_k_heap_sys_k_calloc_enter(heap) #define sys_port_trace_k_heap_sys_k_calloc_exit(heap, ret) +#define sys_port_trace_k_heap_sys_k_realloc_enter(heap) +#define sys_port_trace_k_heap_sys_k_realloc_exit(heap, ret) #define sys_port_trace_k_mem_slab_init(slab, rc) #define sys_port_trace_k_mem_slab_alloc_enter(slab, timeout)