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

commit f0e12a983d60985e0e2ceefc8023fe8703bd046c
Author: ligd <liguidi...@xiaomi.com>
AuthorDate: Thu Dec 28 23:10:43 2023 +0800

    cpuload: add nxsched_update_critmon() to handle thread busyloop
    
    Signed-off-by: ligd <liguidi...@xiaomi.com>
    Signed-off-by: lipengfei28 <lipengfe...@xiaomi.com>
---
 sched/sched/sched.h             |  1 +
 sched/sched/sched_cpuload.c     |  6 ++++++
 sched/sched/sched_critmonitor.c | 24 ++++++++++++++++++++++++
 3 files changed, 31 insertions(+)

diff --git a/sched/sched/sched.h b/sched/sched/sched.h
index edc287f75f..d8ffdf2acc 100644
--- a/sched/sched/sched.h
+++ b/sched/sched/sched.h
@@ -415,6 +415,7 @@ void nxsched_process_cpuload_ticks(clock_t ticks);
 #ifdef CONFIG_SCHED_CRITMONITOR
 void nxsched_resume_critmon(FAR struct tcb_s *tcb);
 void nxsched_suspend_critmon(FAR struct tcb_s *tcb);
+void nxsched_update_critmon(FAR struct tcb_s *tcb);
 #endif
 
 #if CONFIG_SCHED_CRITMONITOR_MAXTIME_PREEMPTION >= 0
diff --git a/sched/sched/sched_cpuload.c b/sched/sched/sched_cpuload.c
index 1044fbd2b7..a5b5b81eaa 100644
--- a/sched/sched/sched_cpuload.c
+++ b/sched/sched/sched_cpuload.c
@@ -224,6 +224,12 @@ int clock_cpuload(int pid, FAR struct cpuload_s *cpuload)
 
   DEBUGASSERT(cpuload);
 
+#ifdef CONFIG_SCHED_CPULOAD_CRITMONITOR
+  /* Update critmon in case of the target thread busyloop */
+
+  nxsched_update_critmon(nxsched_get_tcb(pid));
+#endif
+
   /* Momentarily disable interrupts.  We need (1) the task to stay valid
    * while we are doing these operations and (2) the tick counts to be
    * synchronized when read.
diff --git a/sched/sched/sched_critmonitor.c b/sched/sched/sched_critmonitor.c
index 15a5d479c8..b7a59d3ed5 100644
--- a/sched/sched/sched_critmonitor.c
+++ b/sched/sched/sched_critmonitor.c
@@ -445,3 +445,27 @@ void nxsched_suspend_critmon(FAR struct tcb_s *tcb)
     }
 #endif /* CONFIG_SCHED_CRITMONITOR_MAXTIME_CSECTION */
 }
+
+void nxsched_update_critmon(FAR struct tcb_s *tcb)
+{
+  clock_t current = perf_gettime();
+  clock_t elapsed = current - tcb->run_start;
+
+  if (tcb->task_state != TSTATE_TASK_RUNNING)
+    {
+      return;
+    }
+
+#ifdef CONFIG_SCHED_CPULOAD_CRITMONITOR
+  clock_t tick = elapsed * CLOCKS_PER_SEC / perf_getfreq();
+  nxsched_process_taskload_ticks(tcb, tick);
+#endif
+
+  tcb->run_start = current;
+  tcb->run_time += elapsed;
+  if (elapsed > tcb->run_max)
+    {
+      tcb->run_max = elapsed;
+      CHECK_THREAD(tcb->pid, elapsed);
+    }
+}

Reply via email to