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

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

commit f305cefe82f897f4131bc1f6fc946d9246cb990c
Author: YAMAMOTO Takashi <[email protected]>
AuthorDate: Fri Feb 12 13:50:12 2021 +0900

    procfs: Add procfs_register_meminfo
    
    This allows subsystems to register their mallinfo-like functions
    to be shown in /proc/meminfo.
---
 fs/procfs/fs_procfsmeminfo.c | 46 ++++++++++++++++++++++++++++++++++++++++++++
 include/nuttx/fs/procfs.h    | 24 +++++++++++++++++++++++
 2 files changed, 70 insertions(+)

diff --git a/fs/procfs/fs_procfsmeminfo.c b/fs/procfs/fs_procfsmeminfo.c
index 661d655..ec000b6 100644
--- a/fs/procfs/fs_procfsmeminfo.c
+++ b/fs/procfs/fs_procfsmeminfo.c
@@ -122,6 +122,8 @@ const struct procfs_operations meminfo_operations =
   meminfo_stat    /* stat */
 };
 
+FAR struct procfs_meminfo_entry_s *g_procfs_meminfo = NULL;
+
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
@@ -291,6 +293,33 @@ static ssize_t meminfo_read(FAR struct file *filep, FAR 
char *buffer,
 
   /* Followed by information about the memory resources */
 
+  FAR const struct procfs_meminfo_entry_s *entry;
+
+  for (entry = g_procfs_meminfo; entry != NULL; entry = entry->next)
+    {
+      if (totalsize < buflen)
+        {
+          struct mallinfo minfo;
+
+          buffer    += copysize;
+          buflen    -= copysize;
+
+          /* Show heap information */
+
+          entry->mallinfo(entry->user_data, &minfo);
+          linesize   = snprintf(procfile->line, MEMINFO_LINELEN,
+                                "%4s:  %11lu%11lu%11lu%11lu\n",
+                                entry->name,
+                                (unsigned long)minfo.arena,
+                                (unsigned long)minfo.uordblks,
+                                (unsigned long)minfo.fordblks,
+                                (unsigned long)minfo.mxordblk);
+          copysize   = procfs_memcpy(procfile->line, linesize, buffer,
+                                     buflen, &offset);
+          totalsize += copysize;
+        }
+    }
+
 #ifdef CONFIG_MM_KERNEL_HEAP
   if (totalsize < buflen)
     {
@@ -462,4 +491,21 @@ static int meminfo_stat(FAR const char *relpath, FAR 
struct stat *buf)
  * Public Functions
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: procfs_register_meminfo
+ *
+ * Description:
+ *   Add a new meminfo entry to the procfs file system.
+ *
+ * Input Parameters:
+ *   entry - Describes the entry to be registered.
+ *
+ ****************************************************************************/
+
+void procfs_register_meminfo(FAR struct procfs_meminfo_entry_s *entry)
+{
+  entry->next = g_procfs_meminfo;
+  g_procfs_meminfo = entry;
+}
+
 #endif /* !CONFIG_FS_PROCFS_EXCLUDE_MEMINFO */
diff --git a/include/nuttx/fs/procfs.h b/include/nuttx/fs/procfs.h
index 9595e2e..e6cf97b 100644
--- a/include/nuttx/fs/procfs.h
+++ b/include/nuttx/fs/procfs.h
@@ -140,6 +140,17 @@ struct procfs_dir_priv_s
   FAR const struct procfs_entry_s *procfsentry; /* Pointer to procfs handler 
entry */
 };
 
+/* An entry for procfs_register_meminfo */
+
+struct procfs_meminfo_entry_s
+{
+  FAR const char *name;
+  CODE void (*mallinfo)(FAR void *user_data, FAR struct mallinfo *);
+  FAR void *user_data;
+
+  struct procfs_meminfo_entry_s *next;
+};
+
 /****************************************************************************
  * Public Function Prototypes
  ****************************************************************************/
@@ -220,6 +231,19 @@ size_t procfs_memcpy(FAR const char *src, size_t srclen,
 int procfs_register(FAR const struct procfs_entry_s *entry);
 #endif
 
+/****************************************************************************
+ * Name: procfs_register_meminfo
+ *
+ * Description:
+ *   Add a new meminfo entry to the procfs file system.
+ *
+ * Input Parameters:
+ *   entry - Describes the entry to be registered.
+ *
+ ****************************************************************************/
+
+void procfs_register_meminfo(FAR struct procfs_meminfo_entry_s *entry);
+
 #undef EXTERN
 #ifdef __cplusplus
 }

Reply via email to