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

ligd 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 2c8c35431d fs/procfs: fix the issue of /proc/cpuload in SMP
2c8c35431d is described below

commit 2c8c35431db8e76255c8b16f3d5a41de212921b7
Author: fangxiang <[email protected]>
AuthorDate: Tue Jun 14 13:49:18 2022 +0800

    fs/procfs: fix the issue of /proc/cpuload in SMP
    
    Signed-off-by: ligd <[email protected]>
---
 fs/procfs/fs_procfscpuload.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/fs/procfs/fs_procfscpuload.c b/fs/procfs/fs_procfscpuload.c
index b6850f21a8..39376e0bc4 100644
--- a/fs/procfs/fs_procfscpuload.c
+++ b/fs/procfs/fs_procfscpuload.c
@@ -201,7 +201,8 @@ static ssize_t cpuload_read(FAR struct file *filep, FAR 
char *buffer,
 
   if (filep->f_pos == 0)
     {
-      struct cpuload_s cpuload;
+      uint32_t total = 0;
+      uint32_t active = 0;
       uint32_t intpart;
       uint32_t fracpart;
 
@@ -209,18 +210,39 @@ static ssize_t cpuload_read(FAR struct file *filep, FAR 
char *buffer,
        * fail if the PID is not valid.  This, however, should never happen
        * for the IDLE thread.
        */
+#ifdef CONFIG_SMP
+      struct cpuload_s cpuloads[CONFIG_SMP_NCPUS];
+      uint32_t i;
+
+      for (i = 0; i < CONFIG_SMP_NCPUS; i++)
+        {
+          DEBUGVERIFY(clock_cpuload(i, &cpuloads[i]));
+          active += cpuloads[i].active;
+        }
+
+      total = cpuloads[0].total;
+#else
+      struct cpuload_s cpuload;
 
       DEBUGVERIFY(clock_cpuload(0, &cpuload));
+      active = cpuload.active;
+      total = cpuload.total;
+#endif
+
+      if (active > total)
+        {
+          active = total;
+        }
 
       /* On the simulator, you may hit cpuload.total == 0, but probably never
        * on real hardware.
        */
 
-      if (cpuload.total > 0)
+      if (total > 0)
         {
           uint32_t tmp;
 
-          tmp      = 1000 - (1000 * cpuload.active) / cpuload.total;
+          tmp      = 1000 - (1000 * active) / total;
           intpart  = tmp / 10;
           fracpart = tmp - 10 * intpart;
         }

Reply via email to