[2/2] malloc: malloc_stats now also print heap info

Message ID 1572072726-2197-2-git-send-email-liusirui@huawei.com
State Superseded
Headers

Commit Message

l00420122 Oct. 26, 2019, 6:52 a.m. UTC
  From: liusirui <liusirui@huawei.com>

Before, malloc_stats only print heaps info of arenas except
main_arena, now it can alos prints heap info of main_arena.

---
 malloc/arena.c  | 28 ++++++++++++++++++++++------
 malloc/malloc.c |  2 ++
 2 files changed, 24 insertions(+), 6 deletions(-)
  

Patch

diff --git a/malloc/arena.c b/malloc/arena.c
index be5c9f9..a9361c6 100644
--- a/malloc/arena.c
+++ b/malloc/arena.c
@@ -411,16 +411,32 @@  dump_heap (heap_info *heap)
 {
   char *ptr;
   mchunkptr p;
+  mchunkptr top;
+
+  if ((mstate)(heap + 1) != &main_arena)
+  {
+    fprintf (stderr, "Heap %p, size %10lx:\n", heap, (long) heap->size);
+    ptr = (heap->ar_ptr != (mstate) (heap + 1)) ?
+          (char *) (heap + 1) : (char *) (heap + 1) + sizeof (struct malloc_state);
+    top = heap->ar_ptr->top;
+    p = (mchunkptr) (((unsigned long) ptr + MALLOC_ALIGN_MASK) &
+                     ~MALLOC_ALIGN_MASK);
+  }
+  else
+  {
+    if (((mstate) (heap + 1))->max_system_mem == 0)
+      return;
+    top = ((mstate) (heap + 1))->top;
+    ptr = (char*)top - (((mstate) (heap + 1))->max_system_mem - chunksize(top));
+    p = (mchunkptr) (((unsigned long) ptr + MALLOC_ALIGN_MASK) &
+                     ~MALLOC_ALIGN_MASK);
+    fprintf (stderr, "Heap %p, size %10lx:\n", p, (long)(((mstate) (heap + 1))->max_system_mem));
+  }
 
-  fprintf (stderr, "Heap %p, size %10lx:\n", heap, (long) heap->size);
-  ptr = (heap->ar_ptr != (mstate) (heap + 1)) ?
-        (char *) (heap + 1) : (char *) (heap + 1) + sizeof (struct malloc_state);
-  p = (mchunkptr) (((unsigned long) ptr + MALLOC_ALIGN_MASK) &
-                   ~MALLOC_ALIGN_MASK);
   for (;; )
     {
       fprintf (stderr, "chunk %p size %10lx", p, (long) chunksize_nomask(p));
-      if (p == top (heap->ar_ptr))
+      if (p == top)
         {
           fprintf (stderr, " (top)\n");
           break;
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 351e95f..16a012e 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -4992,6 +4992,8 @@  __malloc_stats (void)
         }
 	dump_heap (heap_ptr);
       }
+      else
+        dump_heap (((heap_info*)ar_ptr) - 1);
 #endif
       system_b += mi.arena;
       in_use_b += mi.uordblks;