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

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


The following commit(s) were added to refs/heads/master by this push:
     new 24062682e7f mm/procfs: refactor mm_notify_pressure to accept heap 
pointer
24062682e7f is described below

commit 24062682e7f38cdc9a231dbaa265f8b6a8ef597e
Author: fangpeina <[email protected]>
AuthorDate: Fri Mar 20 16:09:27 2026 +0800

    mm/procfs: refactor mm_notify_pressure to accept heap pointer
    
    Refactor mm_notify_pressure() to accept a heap pointer instead of
    pre-computed remaining/largest values. Use lockless dq_empty() as
    a fast-path filter to skip when no listeners are registered.
    
    Remove cached g_remaining/g_largest globals and query heap state
    in real-time via mm_heapfree_largest() and mm_heapfree().
    
    Signed-off-by: fangpeina <[email protected]>
    Signed-off-by: jingfei <[email protected]>
---
 fs/procfs/fs_procfspressure.c | 64 +++++++++++++++++++++++++++----------------
 include/nuttx/mm/mm.h         |  4 +--
 mm/umm_heap/umm_calloc.c      |  3 +-
 mm/umm_heap/umm_malloc.c      |  3 +-
 mm/umm_heap/umm_memalign.c    |  3 +-
 mm/umm_heap/umm_realloc.c     |  3 +-
 mm/umm_heap/umm_zalloc.c      |  3 +-
 7 files changed, 47 insertions(+), 36 deletions(-)

diff --git a/fs/procfs/fs_procfspressure.c b/fs/procfs/fs_procfspressure.c
index b94ff7f8f07..86d3ddd5714 100644
--- a/fs/procfs/fs_procfspressure.c
+++ b/fs/procfs/fs_procfspressure.c
@@ -34,6 +34,7 @@
 #include <nuttx/fs/procfs.h>
 #include <nuttx/fs/fs.h>
 #include <nuttx/kmalloc.h>
+#include <nuttx/mm/mm.h>
 #include <nuttx/nuttx.h>
 #include <nuttx/queue.h>
 #include <nuttx/spinlock.h>
@@ -60,8 +61,6 @@ struct pressure_file_s
 
 static dq_queue_t g_pressure_memory_queue;
 static spinlock_t g_pressure_lock;
-static size_t g_remaining;
-static size_t g_largest;
 
 /****************************************************************************
  * Private Function Prototypes
@@ -117,7 +116,7 @@ static int pressure_open(FAR struct file *filep, FAR const 
char *relpath,
                          int oflags, mode_t mode)
 {
   FAR struct pressure_file_s *priv;
-  uint32_t flags;
+  irqstate_t flags;
 
   if (strcmp(relpath, "pressure/memory") != 0)
     {
@@ -146,7 +145,7 @@ static int pressure_open(FAR struct file *filep, FAR const 
char *relpath,
 static int pressure_close(FAR struct file *filep)
 {
   FAR struct pressure_file_s *priv = filep->f_priv;
-  uint32_t flags;
+  irqstate_t flags;
 
   flags = spin_lock_irqsave(&g_pressure_lock);
   dq_rem(&priv->entry, &g_pressure_memory_queue);
@@ -163,16 +162,13 @@ static ssize_t pressure_read(FAR struct file *filep, FAR 
char *buffer,
                              size_t buflen)
 {
   char buf[128];
-  uint32_t flags;
   size_t remain;
   size_t largest;
   off_t offset;
   ssize_t ret;
 
-  flags   = spin_lock_irqsave(&g_pressure_lock);
-  remain  = g_remaining;
-  largest = g_largest;
-  spin_unlock_irqrestore(&g_pressure_lock, flags);
+  largest = mm_heapfree_largest(USR_HEAP);
+  remain  = mm_heapfree(USR_HEAP);
 
   ret = procfs_snprintf(buf, sizeof(buf), "remaining %zu, largest:%zu\n",
                         remain, largest);
@@ -200,7 +196,7 @@ static ssize_t pressure_write(FAR struct file *filep, FAR 
const char *buffer,
   FAR char *endptr;
   size_t threshold;
   clock_t interval;
-  uint32_t flags;
+  irqstate_t flags;
 
   if (buffer == NULL)
     {
@@ -246,7 +242,7 @@ static int pressure_poll(FAR struct file *filep, FAR struct 
pollfd_s *fds,
 {
   FAR struct pressure_file_s *priv = filep->f_priv;
   clock_t current = clock_systime_ticks();
-  uint32_t flags;
+  irqstate_t flags;
 
   flags = spin_lock_irqsave(&g_pressure_lock);
   if (setup)
@@ -259,15 +255,26 @@ static int pressure_poll(FAR struct file *filep, FAR 
struct pollfd_s *fds,
           /* If the remaining memory is less than the threshold and
            * lasttick is CLOCK_MAX, it means the event is triggered for
            * the first time and we should always send a notification.
+           * Query accurate memory state here instead of relying on the
+           * cached values which may not have been updated yet.
            */
 
-          if (g_remaining <= priv->threshold && (priv->lasttick ==
-              CLOCK_MAX || current - priv->lasttick >= priv->interval))
+          if (priv->lasttick == CLOCK_MAX ||
+              current - priv->lasttick >= priv->interval)
             {
-              priv->lasttick = current;
+              size_t largest;
+
               spin_unlock_irqrestore(&g_pressure_lock, flags);
-              poll_notify(&priv->fds, 1, POLLPRI);
-              return OK;
+              largest = mm_heapfree_largest(USR_HEAP);
+              flags   = spin_lock_irqsave(&g_pressure_lock);
+
+              if (largest <= priv->threshold)
+                {
+                  priv->lasttick = current;
+                  spin_unlock_irqrestore(&g_pressure_lock, flags);
+                  poll_notify(&priv->fds, 1, POLLPRI);
+                  return OK;
+                }
             }
         }
       else
@@ -416,19 +423,29 @@ static int pressure_stat(const char *relpath, struct stat 
*buf)
  ****************************************************************************/
 
 /****************************************************************************
- * mm_notify_pressure
+ * Name: mm_notify_pressure
  ****************************************************************************/
 
-void mm_notify_pressure(size_t remaining, size_t largest)
+void mm_notify_pressure(FAR struct mm_heap_s *heap)
 {
-  clock_t current = clock_systime_ticks();
   FAR dq_entry_t *entry;
   FAR dq_entry_t *tmp;
-  uint32_t flags;
+  irqstate_t flags;
+  clock_t current;
+  size_t largest;
+
+  /* First level filter: no listeners, skip everything */
 
-  flags       = spin_lock_irqsave(&g_pressure_lock);
-  g_remaining = remaining;
-  g_largest   = largest;
+  if (dq_empty(&g_pressure_memory_queue))
+    {
+      return;
+    }
+
+  /* Slow path: compute largest block and notify listeners */
+
+  largest = mm_heapfree_largest(heap);
+  current = clock_systime_ticks();
+  flags   = spin_lock_irqsave(&g_pressure_lock);
 
   dq_for_every_safe(&g_pressure_memory_queue, entry, tmp)
     {
@@ -471,4 +488,3 @@ void mm_notify_pressure(size_t remaining, size_t largest)
 
   spin_unlock_irqrestore(&g_pressure_lock, flags);
 }
-
diff --git a/include/nuttx/mm/mm.h b/include/nuttx/mm/mm.h
index 02999ee8aac..ab05a35aff8 100644
--- a/include/nuttx/mm/mm.h
+++ b/include/nuttx/mm/mm.h
@@ -505,9 +505,9 @@ void kmm_checkcorruption(void);
 /* Functions contained in fs_procfspressure.c *******************************/
 
 #ifdef CONFIG_FS_PROCFS_INCLUDE_PRESSURE
-void mm_notify_pressure(size_t remaining, size_t largest);
+void mm_notify_pressure(FAR struct mm_heap_s *heap);
 #else
-#  define mm_notify_pressure(remaining, largest)
+#  define mm_notify_pressure(heap)
 #endif
 
 #ifdef __cplusplus
diff --git a/mm/umm_heap/umm_calloc.c b/mm/umm_heap/umm_calloc.c
index fdfc3f2b9b3..c704d1c2c6f 100644
--- a/mm/umm_heap/umm_calloc.c
+++ b/mm/umm_heap/umm_calloc.c
@@ -85,8 +85,7 @@ FAR void *calloc(size_t n, size_t elem_size)
     }
   else
     {
-      mm_notify_pressure(mm_heapfree(USR_HEAP),
-                         mm_heapfree_largest(USR_HEAP));
+      mm_notify_pressure(USR_HEAP);
     }
 
   return mem;
diff --git a/mm/umm_heap/umm_malloc.c b/mm/umm_heap/umm_malloc.c
index 5977f373550..2543742b1a5 100644
--- a/mm/umm_heap/umm_malloc.c
+++ b/mm/umm_heap/umm_malloc.c
@@ -70,8 +70,7 @@ FAR void *malloc(size_t size)
     }
   else
     {
-      mm_notify_pressure(mm_heapfree(USR_HEAP),
-                         mm_heapfree_largest(USR_HEAP));
+      mm_notify_pressure(USR_HEAP);
     }
 
   return ret;
diff --git a/mm/umm_heap/umm_memalign.c b/mm/umm_heap/umm_memalign.c
index 0ca180bc414..d47d75a69ef 100644
--- a/mm/umm_heap/umm_memalign.c
+++ b/mm/umm_heap/umm_memalign.c
@@ -115,8 +115,7 @@ FAR void *memalign(size_t alignment, size_t size)
     }
   else
     {
-      mm_notify_pressure(mm_heapfree(USR_HEAP),
-                         mm_heapfree_largest(USR_HEAP));
+      mm_notify_pressure(USR_HEAP);
     }
 
   return ret;
diff --git a/mm/umm_heap/umm_realloc.c b/mm/umm_heap/umm_realloc.c
index 44ea27d5ee3..a10cd243917 100644
--- a/mm/umm_heap/umm_realloc.c
+++ b/mm/umm_heap/umm_realloc.c
@@ -117,8 +117,7 @@ FAR void *realloc(FAR void *oldmem, size_t size)
     }
   else
     {
-      mm_notify_pressure(mm_heapfree(USR_HEAP),
-                         mm_heapfree_largest(USR_HEAP));
+      mm_notify_pressure(USR_HEAP);
     }
 
   return ret;
diff --git a/mm/umm_heap/umm_zalloc.c b/mm/umm_heap/umm_zalloc.c
index 8b466a3bc44..30d621f3714 100644
--- a/mm/umm_heap/umm_zalloc.c
+++ b/mm/umm_heap/umm_zalloc.c
@@ -77,8 +77,7 @@ FAR void *zalloc(size_t size)
     }
   else
     {
-      mm_notify_pressure(mm_heapfree(USR_HEAP),
-                         mm_heapfree_largest(USR_HEAP));
+      mm_notify_pressure(USR_HEAP);
     }
 
   return ret;

Reply via email to