vppinfra: expose raw memory traces 80/41280/2
authorMatus Fabian <matfabia@cisco.com>
Fri, 19 Jul 2024 13:24:10 +0000 (15:24 +0200)
committerFlorin Coras <florin.coras@gmail.com>
Mon, 22 Jul 2024 15:50:22 +0000 (15:50 +0000)
Add clib_mem_trace_dup which return copy of memory traces and
can be used to save memory trace in different formats.

Type: improvement

Change-Id: I731860cdc65e6c5255620a6778ce6c893a493b1d
Signed-off-by: Matus Fabian <matfabia@cisco.com>
src/vppinfra/mem.h
src/vppinfra/mem_dlmalloc.c

index 75015d5..ab9c5da 100644 (file)
@@ -299,10 +299,27 @@ void *clib_mem_init_thread_safe (void *memory, uword memory_size);
 
 void clib_mem_exit (void);
 
+typedef struct
+{
+  /* Address of callers: outer first, inner last. */
+  uword callers[12];
+
+  /* Count of allocations with this traceback. */
+  u32 n_allocations;
+
+  /* Count of bytes allocated with this traceback. */
+  u32 n_bytes;
+
+  /* Offset of this item */
+  uword offset;
+} mheap_trace_t;
+
 void clib_mem_trace (int enable);
 
 int clib_mem_is_traced (void);
 
+mheap_trace_t *clib_mem_trace_dup (clib_mem_heap_t *heap);
+
 typedef struct
 {
   /* Total number of objects allocated. */
index e98687f..d5ff21e 100644 (file)
 #include <vppinfra/elf_clib.h>
 #include <vppinfra/stack.h>
 
-typedef struct
-{
-  /* Address of callers: outer first, inner last. */
-  uword callers[12];
-
-  /* Count of allocations with this traceback. */
-  u32 n_allocations;
-
-  /* Count of bytes allocated with this traceback. */
-  u32 n_bytes;
-
-  /* Offset of this item */
-  uword offset;
-} mheap_trace_t;
-
 typedef struct
 {
   clib_spinlock_t lock;
@@ -574,6 +559,23 @@ clib_mem_trace_enable_disable (uword enable)
   return rv;
 }
 
+__clib_export mheap_trace_t *
+clib_mem_trace_dup (clib_mem_heap_t *heap)
+{
+  mheap_trace_main_t *tm = &mheap_trace_main;
+  mheap_trace_t *traces_copy = 0;
+
+  clib_spinlock_lock (&tm->lock);
+  if (vec_len (tm->traces) > 0 && heap == tm->current_traced_mheap)
+    {
+      traces_copy = vec_dup (tm->traces);
+      qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]),
+            mheap_trace_sort);
+    }
+  clib_spinlock_unlock (&tm->lock);
+  return traces_copy;
+}
+
 __clib_export clib_mem_heap_t *
 clib_mem_create_heap (void *base, uword size, int is_locked, char *fmt, ...)
 {