From 52f409d614a07cb84beda8e3c985aac2373abaec Mon Sep 17 00:00:00 2001 From: Jerin Jacob Date: Thu, 23 Apr 2020 00:33:35 +0530 Subject: [PATCH] trace: add memory tracepoints Add the following memory-related tracepoints. - rte_eal_trace_mem_zmalloc() - rte_eal_trace_mem_malloc() - rte_eal_trace_mem_realloc() - rte_eal_trace_mem_free() rte_malloc() and rte_free() has been used in the trace implementation, in order to avoid tracing implementation specific events, added an internal no trace version rte_malloc() and rte_free(). Signed-off-by: Jerin Jacob Acked-by: David Marchand --- lib/librte_eal/common/eal_common_trace.c | 4 +- .../common/eal_common_trace_points.c | 14 +++++ lib/librte_eal/common/eal_private.h | 5 ++ lib/librte_eal/common/rte_malloc.c | 59 ++++++++++++++++--- lib/librte_eal/include/rte_eal_trace.h | 39 ++++++++++++ lib/librte_eal/rte_eal_version.map | 4 ++ 6 files changed, 114 insertions(+), 11 deletions(-) diff --git a/lib/librte_eal/common/eal_common_trace.c b/lib/librte_eal/common/eal_common_trace.c index da269debeb..d69b86f6d0 100644 --- a/lib/librte_eal/common/eal_common_trace.c +++ b/lib/librte_eal/common/eal_common_trace.c @@ -318,7 +318,7 @@ __rte_trace_mem_per_thread_alloc(void) } /* First attempt from huge page */ - header = rte_malloc(NULL, trace_mem_sz(trace->buff_len), 8); + header = eal_malloc_no_trace(NULL, trace_mem_sz(trace->buff_len), 8); if (header) { trace->lcore_meta[count].area = TRACE_AREA_HUGEPAGE; goto found; @@ -371,7 +371,7 @@ trace_mem_per_thread_free(void) for (count = 0; count < trace->nb_trace_mem_list; count++) { mem = trace->lcore_meta[count].mem; if (trace->lcore_meta[count].area == TRACE_AREA_HUGEPAGE) - rte_free(mem); + eal_free_no_trace(mem); else if (trace->lcore_meta[count].area == TRACE_AREA_HEAP) free(mem); } diff --git a/lib/librte_eal/common/eal_common_trace_points.c b/lib/librte_eal/common/eal_common_trace_points.c index 08011c2d4f..f61285ee47 100644 --- a/lib/librte_eal/common/eal_common_trace_points.c +++ b/lib/librte_eal/common/eal_common_trace_points.c @@ -26,6 +26,11 @@ RTE_TRACE_POINT_DEFINE(rte_eal_trace_generic_func); RTE_TRACE_POINT_DEFINE(rte_eal_trace_alarm_set); RTE_TRACE_POINT_DEFINE(rte_eal_trace_alarm_cancel); +RTE_TRACE_POINT_DEFINE(rte_eal_trace_mem_zmalloc); +RTE_TRACE_POINT_DEFINE(rte_eal_trace_mem_malloc); +RTE_TRACE_POINT_DEFINE(rte_eal_trace_mem_realloc); +RTE_TRACE_POINT_DEFINE(rte_eal_trace_mem_free); + RTE_INIT(eal_trace_init) { RTE_TRACE_POINT_REGISTER(rte_eal_trace_generic_void, @@ -65,4 +70,13 @@ RTE_INIT(eal_trace_init) lib.eal.alarm.set); RTE_TRACE_POINT_REGISTER(rte_eal_trace_alarm_cancel, lib.eal.alarm.cancel); + + RTE_TRACE_POINT_REGISTER(rte_eal_trace_mem_zmalloc, + lib.eal.mem.zmalloc); + RTE_TRACE_POINT_REGISTER(rte_eal_trace_mem_malloc, + lib.eal.mem.malloc); + RTE_TRACE_POINT_REGISTER(rte_eal_trace_mem_realloc, + lib.eal.mem.realloc); + RTE_TRACE_POINT_REGISTER(rte_eal_trace_mem_free, + lib.eal.mem.free); } diff --git a/lib/librte_eal/common/eal_private.h b/lib/librte_eal/common/eal_private.h index 8536ed70bb..ecf827914f 100644 --- a/lib/librte_eal/common/eal_private.h +++ b/lib/librte_eal/common/eal_private.h @@ -443,4 +443,9 @@ rte_option_usage(void); uint64_t eal_get_baseaddr(void); +void * +eal_malloc_no_trace(const char *type, size_t size, unsigned int align); + +void eal_free_no_trace(void *addr); + #endif /* _EAL_PRIVATE_H_ */ diff --git a/lib/librte_eal/common/rte_malloc.c b/lib/librte_eal/common/rte_malloc.c index d6026a2b17..f1b73168bd 100644 --- a/lib/librte_eal/common/rte_malloc.c +++ b/lib/librte_eal/common/rte_malloc.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "malloc_elem.h" @@ -30,20 +31,35 @@ /* Free the memory space back to heap */ -void rte_free(void *addr) +static void +mem_free(void *addr, const bool trace_ena) { + if (trace_ena) + rte_eal_trace_mem_free(addr); + if (addr == NULL) return; if (malloc_heap_free(malloc_elem_from_data(addr)) < 0) RTE_LOG(ERR, EAL, "Error: Invalid memory\n"); } -/* - * Allocate memory on specified heap. - */ -void * -rte_malloc_socket(const char *type, size_t size, unsigned int align, - int socket_arg) +void +rte_free(void *addr) { + return mem_free(addr, true); +} + +void +eal_free_no_trace(void *addr) +{ + return mem_free(addr, false); +} + +static void * +malloc_socket(const char *type, size_t size, unsigned int align, + int socket_arg, const bool trace_ena) +{ + void *ptr; + /* return NULL if size is 0 or alignment is not power-of-2 */ if (size == 0 || (align && !rte_is_power_of_2(align))) return NULL; @@ -57,8 +73,28 @@ rte_malloc_socket(const char *type, size_t size, unsigned int align, !rte_eal_has_hugepages()) socket_arg = SOCKET_ID_ANY; - return malloc_heap_alloc(type, size, socket_arg, 0, + ptr = malloc_heap_alloc(type, size, socket_arg, 0, align == 0 ? 1 : align, 0, false); + + if (trace_ena) + rte_eal_trace_mem_malloc(type, size, align, socket_arg, ptr); + return ptr; +} + +/* + * Allocate memory on specified heap. + */ +void * +rte_malloc_socket(const char *type, size_t size, unsigned int align, + int socket_arg) +{ + return malloc_socket(type, size, align, socket_arg, true); +} + +void * +eal_malloc_no_trace(const char *type, size_t size, unsigned int align) +{ + return malloc_socket(type, size, align, SOCKET_ID_ANY, false); } /* @@ -87,6 +123,8 @@ rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket) if (ptr != NULL) memset(ptr, 0, size); #endif + + rte_eal_trace_mem_zmalloc(type, size, align, socket, ptr); return ptr; } @@ -140,8 +178,10 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) if ((socket == SOCKET_ID_ANY || (unsigned int)socket == elem->heap->socket_id) && RTE_PTR_ALIGN(ptr, align) == ptr && - malloc_heap_resize(elem, size) == 0) + malloc_heap_resize(elem, size) == 0) { + rte_eal_trace_mem_realloc(size, align, socket, ptr); return ptr; + } /* either requested socket id doesn't match, alignment is off * or we have no room to expand, @@ -155,6 +195,7 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) rte_memcpy(new_ptr, ptr, old_size < size ? old_size : size); rte_free(ptr); + rte_eal_trace_mem_realloc(size, align, socket, new_ptr); return new_ptr; } diff --git a/lib/librte_eal/include/rte_eal_trace.h b/lib/librte_eal/include/rte_eal_trace.h index ab521d1b20..ebf750c6d0 100644 --- a/lib/librte_eal/include/rte_eal_trace.h +++ b/lib/librte_eal/include/rte_eal_trace.h @@ -136,6 +136,45 @@ RTE_TRACE_POINT( rte_trace_point_emit_int(count); ) +/* Memory */ +RTE_TRACE_POINT( + rte_eal_trace_mem_zmalloc, + RTE_TRACE_POINT_ARGS(const char *type, size_t size, unsigned int align, + int socket, void *ptr), + rte_trace_point_emit_string(type); + rte_trace_point_emit_long(size); + rte_trace_point_emit_u32(align); + rte_trace_point_emit_int(socket); + rte_trace_point_emit_ptr(ptr); +) + +RTE_TRACE_POINT( + rte_eal_trace_mem_malloc, + RTE_TRACE_POINT_ARGS(const char *type, size_t size, unsigned int align, + int socket, void *ptr), + rte_trace_point_emit_string(type); + rte_trace_point_emit_long(size); + rte_trace_point_emit_u32(align); + rte_trace_point_emit_int(socket); + rte_trace_point_emit_ptr(ptr); +) + +RTE_TRACE_POINT( + rte_eal_trace_mem_realloc, + RTE_TRACE_POINT_ARGS(size_t size, unsigned int align, int socket, + void *ptr), + rte_trace_point_emit_long(size); + rte_trace_point_emit_u32(align); + rte_trace_point_emit_int(socket); + rte_trace_point_emit_ptr(ptr); +) + +RTE_TRACE_POINT( + rte_eal_trace_mem_free, + RTE_TRACE_POINT_ARGS(void *ptr), + rte_trace_point_emit_ptr(ptr); +) + #ifdef __cplusplus } #endif diff --git a/lib/librte_eal/rte_eal_version.map b/lib/librte_eal/rte_eal_version.map index 1e016865a2..470cff5dca 100644 --- a/lib/librte_eal/rte_eal_version.map +++ b/lib/librte_eal/rte_eal_version.map @@ -355,6 +355,10 @@ EXPERIMENTAL { __rte_eal_trace_generic_u64; __rte_eal_trace_generic_u8; __rte_eal_trace_generic_void; + __rte_eal_trace_mem_free; + __rte_eal_trace_mem_malloc; + __rte_eal_trace_mem_realloc; + __rte_eal_trace_mem_zmalloc; __rte_trace_mem_per_thread_alloc; __rte_trace_point_emit_field; __rte_trace_point_register; -- 2.20.1