You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/03/27 05:21:56 UTC

[incubator-nuttx] branch master updated (42664f4 -> 2ffca6d)

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git.


    from 42664f4  rpmsgfs/rename: fix bug about pathname align with 8bytes
     new 709207b  mm/memdump: dynamic turn on backtrace in heap when enable DEBUG_MM
     new 1ab58af  mm/heap: add MM_BACKTRACE_DEFAULT to config backtrace record by default
     new 2ffca6d  mm/mm_heap: output mallinfo when malloc failed

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 fs/procfs/fs_procfsmeminfo.c | 26 ++++++++++++++++++++++++--
 include/nuttx/fs/procfs.h    |  8 ++++++++
 mm/Kconfig                   |  5 +++++
 mm/mm_heap/mm.h              | 15 +++++++++++----
 mm/mm_heap/mm_initialize.c   |  7 +++++--
 mm/mm_heap/mm_malloc.c       |  9 ++++++++-
 mm/mm_heap/mm_memalign.c     |  2 +-
 mm/mm_heap/mm_realloc.c      |  4 ++--
 8 files changed, 64 insertions(+), 12 deletions(-)

[incubator-nuttx] 01/03: mm/memdump: dynamic turn on backtrace in heap when enable DEBUG_MM

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 709207b8d3b9e019c841b163fe893d8e1c77348a
Author: Jiuzhu Dong <do...@xiaomi.com>
AuthorDate: Thu Mar 17 14:02:37 2022 +0800

    mm/memdump: dynamic turn on backtrace in heap when enable DEBUG_MM
    
    default turn off.
    turn on: echo on > /proc/memdump
    turn off: echo off > proc/memdump
    
    Signed-off-by: Jiuzhu Dong <do...@xiaomi.com>
---
 fs/procfs/fs_procfsmeminfo.c | 26 ++++++++++++++++++++++++--
 include/nuttx/fs/procfs.h    |  8 ++++++++
 mm/mm_heap/mm.h              | 15 +++++++++++----
 mm/mm_heap/mm_initialize.c   |  4 ++--
 mm/mm_heap/mm_malloc.c       |  2 +-
 mm/mm_heap/mm_memalign.c     |  2 +-
 mm/mm_heap/mm_realloc.c      |  4 ++--
 7 files changed, 49 insertions(+), 12 deletions(-)

diff --git a/fs/procfs/fs_procfsmeminfo.c b/fs/procfs/fs_procfsmeminfo.c
index 29f3ff3..50b3ac4 100644
--- a/fs/procfs/fs_procfsmeminfo.c
+++ b/fs/procfs/fs_procfsmeminfo.c
@@ -417,7 +417,8 @@ static ssize_t memdump_read(FAR struct file *filep, FAR char *buffer,
 
 #ifdef CONFIG_DEBUG_MM
   linesize  = procfs_snprintf(procfile->line, MEMINFO_LINELEN,
-                              "usage: <pid/used/free>\n"
+                              "usage: <pid/used/free/on/off>\n"
+                              "on/off backtrace\n"
                               "pid: dump pid allocated node\n");
 #else
   linesize  = procfs_snprintf(procfile->line, MEMINFO_LINELEN,
@@ -448,7 +449,7 @@ static ssize_t memdump_read(FAR struct file *filep, FAR char *buffer,
 static ssize_t memdump_write(FAR struct file *filep, FAR const char *buffer,
                              size_t buflen)
 {
-  FAR const struct procfs_meminfo_entry_s *entry;
+  FAR struct procfs_meminfo_entry_s *entry;
   FAR struct meminfo_file_s *procfile;
   pid_t pid = INVALID_PROCESS_ID;
 
@@ -459,6 +460,27 @@ static ssize_t memdump_write(FAR struct file *filep, FAR const char *buffer,
   procfile = filep->f_priv;
   DEBUGASSERT(procfile);
 
+#ifdef CONFIG_DEBUG_MM
+  if (strcmp(buffer, "on") == 0)
+    {
+      for (entry = g_procfs_meminfo; entry != NULL; entry = entry->next)
+        {
+          entry->backtrace = true;
+        }
+
+      return buflen;
+    }
+  else if (strcmp(buffer, "off") == 0)
+    {
+      for (entry = g_procfs_meminfo; entry != NULL; entry = entry->next)
+        {
+          entry->backtrace = false;
+        }
+
+      return buflen;
+    }
+#endif
+
   switch (buffer[0])
     {
       case 'u':
diff --git a/include/nuttx/fs/procfs.h b/include/nuttx/fs/procfs.h
index b68a2b7..97bd245 100644
--- a/include/nuttx/fs/procfs.h
+++ b/include/nuttx/fs/procfs.h
@@ -133,6 +133,14 @@ struct procfs_meminfo_entry_s
   FAR const char *name;
   FAR struct mm_heap_s *heap;
   struct procfs_meminfo_entry_s *next;
+#if defined(CONFIG_DEBUG_MM)
+
+  /* This is dynamic control flag whether to turn on backtrace in the heap,
+   * you can set it by /proc/memdump.
+   */
+
+  bool backtrace;
+#endif
 };
 
 /****************************************************************************
diff --git a/mm/mm_heap/mm.h b/mm/mm_heap/mm.h
index c4c6a9d..af89268 100644
--- a/mm/mm_heap/mm.h
+++ b/mm/mm_heap/mm.h
@@ -93,17 +93,24 @@
 #ifdef CONFIG_DEBUG_MM
 #  define MM_MIN_SHIFT       (MM_MIN_SHIFT_ + 2)
 #  define MM_BACKTRACE_DEPTH 8
-#  define MM_ADD_BACKTRACE(ptr) \
+#  define MM_ADD_BACKTRACE(heap, ptr) \
      do \
        { \
          FAR struct mm_allocnode_s *tmp = (FAR struct mm_allocnode_s *)(ptr); \
          tmp->pid = getpid(); \
-         memset(tmp->backtrace, 0, sizeof(tmp->backtrace)); \
-         backtrace(tmp->backtrace, MM_BACKTRACE_DEPTH); \
+         if ((heap)->mm_procfs.backtrace) \
+           { \
+             memset(tmp->backtrace, 0, sizeof(tmp->backtrace)); \
+             backtrace(tmp->backtrace, MM_BACKTRACE_DEPTH); \
+           } \
+         else \
+           { \
+             tmp->backtrace[0] = 0; \
+           } \
        } \
      while (0)
 #else
-#  define MM_ADD_BACKTRACE(ptr)
+#  define MM_ADD_BACKTRACE(heap, ptr)
 #  define MM_MIN_SHIFT MM_MIN_SHIFT_
 #endif
 
diff --git a/mm/mm_heap/mm_initialize.c b/mm/mm_heap/mm_initialize.c
index d13c45c..096ebff 100644
--- a/mm/mm_heap/mm_initialize.c
+++ b/mm/mm_heap/mm_initialize.c
@@ -117,7 +117,7 @@ void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart,
 
   heap->mm_heapstart[IDX]            = (FAR struct mm_allocnode_s *)
                                        heapbase;
-  MM_ADD_BACKTRACE(heap->mm_heapstart[IDX]);
+  MM_ADD_BACKTRACE(heap, heap->mm_heapstart[IDX]);
   heap->mm_heapstart[IDX]->size      = SIZEOF_MM_ALLOCNODE;
   heap->mm_heapstart[IDX]->preceding = MM_ALLOC_BIT;
   node                               = (FAR struct mm_freenode_s *)
@@ -128,7 +128,7 @@ void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart,
                                        (heapend - SIZEOF_MM_ALLOCNODE);
   heap->mm_heapend[IDX]->size        = SIZEOF_MM_ALLOCNODE;
   heap->mm_heapend[IDX]->preceding   = node->size | MM_ALLOC_BIT;
-  MM_ADD_BACKTRACE(heap->mm_heapend[IDX]);
+  MM_ADD_BACKTRACE(heap, heap->mm_heapend[IDX]);
 
 #undef IDX
 
diff --git a/mm/mm_heap/mm_malloc.c b/mm/mm_heap/mm_malloc.c
index d392c2a..51c789d 100644
--- a/mm/mm_heap/mm_malloc.c
+++ b/mm/mm_heap/mm_malloc.c
@@ -223,7 +223,7 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size)
       /* Handle the case of an exact size match */
 
       node->preceding |= MM_ALLOC_BIT;
-      MM_ADD_BACKTRACE(node);
+      MM_ADD_BACKTRACE(heap, node);
       ret = (FAR void *)((FAR char *)node + SIZEOF_MM_ALLOCNODE);
     }
 
diff --git a/mm/mm_heap/mm_memalign.c b/mm/mm_heap/mm_memalign.c
index 0fa3968..d3681dd 100644
--- a/mm/mm_heap/mm_memalign.c
+++ b/mm/mm_heap/mm_memalign.c
@@ -178,7 +178,7 @@ FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t alignment,
 
       newnode->size = (size_t)next - (size_t)newnode;
       newnode->preceding = precedingsize | MM_ALLOC_BIT;
-      MM_ADD_BACKTRACE(newnode);
+      MM_ADD_BACKTRACE(heap, newnode);
 
       /* Reduce the size of the original chunk and mark it not allocated, */
 
diff --git a/mm/mm_heap/mm_realloc.c b/mm/mm_heap/mm_realloc.c
index 366cbfe..7b9abe8 100644
--- a/mm/mm_heap/mm_realloc.c
+++ b/mm/mm_heap/mm_realloc.c
@@ -128,7 +128,7 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
                        oldsize - oldnode->size);
         }
 
-      MM_ADD_BACKTRACE(oldnode);
+      MM_ADD_BACKTRACE(heap, oldnode);
 
       /* Then return the original address */
 
@@ -334,7 +334,7 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
             }
         }
 
-      MM_ADD_BACKTRACE((FAR char *)newmem - SIZEOF_MM_ALLOCNODE);
+      MM_ADD_BACKTRACE(heap, (FAR char *)newmem - SIZEOF_MM_ALLOCNODE);
 
       mm_givesemaphore(heap);
 

[incubator-nuttx] 03/03: mm/mm_heap: output mallinfo when malloc failed

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 2ffca6d16dec2c976fdaa406a7c46ab816425620
Author: Jiuzhu Dong <do...@xiaomi.com>
AuthorDate: Sat Mar 26 23:27:21 2022 +0800

    mm/mm_heap: output mallinfo when malloc failed
    
    Signed-off-by: Jiuzhu Dong <do...@xiaomi.com>
---
 mm/mm_heap/mm_malloc.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/mm/mm_heap/mm_malloc.c b/mm/mm_heap/mm_malloc.c
index 51c789d..135e1f3 100644
--- a/mm/mm_heap/mm_malloc.c
+++ b/mm/mm_heap/mm_malloc.c
@@ -27,6 +27,7 @@
 #include <assert.h>
 #include <debug.h>
 #include <string.h>
+#include <malloc.h>
 
 #include <nuttx/arch.h>
 #include <nuttx/mm/mm.h>
@@ -243,7 +244,13 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size)
 #ifdef CONFIG_DEBUG_MM
   else
     {
+      struct mallinfo minfo;
+
       mwarn("WARNING: Allocation failed, size %zu\n", alignsize);
+      mm_mallinfo(heap, &minfo);
+      mwarn("Total:%d, used:%d, free:%d, largest:%d, nused:%d, nfree:%d\n",
+            minfo.arena, minfo.uordblks, minfo.fordblks,
+            minfo.mxordblk, minfo.aordblks, minfo.ordblks);
       mm_memdump(heap, -1);
       DEBUGASSERT(false);
     }

[incubator-nuttx] 02/03: mm/heap: add MM_BACKTRACE_DEFAULT to config backtrace record by default

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 1ab58aff5594fe017cde73d852b6cb79f48120d0
Author: Jiuzhu Dong <do...@xiaomi.com>
AuthorDate: Tue Mar 22 10:37:49 2022 +0800

    mm/heap: add MM_BACKTRACE_DEFAULT to config backtrace record by default
    
    Signed-off-by: Jiuzhu Dong <do...@xiaomi.com>
---
 mm/Kconfig                 | 5 +++++
 mm/mm_heap/mm_initialize.c | 3 +++
 2 files changed, 8 insertions(+)

diff --git a/mm/Kconfig b/mm/Kconfig
index 8746a20..c4f506a 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -186,4 +186,9 @@ config MM_KASAN
 		bugs in native code. After turn on this option, Please
 		add -fsanitize=kernel-address to CFLAGS/CXXFLAGS too.
 
+config MM_BACKTRACE_DEFAULT
+	bool "Enable the backtrace record by default"
+	default n
+	depends on DEBUG_MM
+
 source "mm/iob/Kconfig"
diff --git a/mm/mm_heap/mm_initialize.c b/mm/mm_heap/mm_initialize.c
index 096ebff..e32c63c 100644
--- a/mm/mm_heap/mm_initialize.c
+++ b/mm/mm_heap/mm_initialize.c
@@ -213,6 +213,9 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name,
 #if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
   heap->mm_procfs.name = name;
   heap->mm_procfs.heap = heap;
+#if defined (CONFIG_DEBUG_MM) && defined(CONFIG_MM_BACKTRACE_DEFAULT)
+  heap->mm_procfs.backtrace = true;
+#endif
   procfs_register_meminfo(&heap->mm_procfs);
 #endif
 #endif