casaroli commented on code in PR #21424:
URL: https://github.com/apache/nuttx/pull/21424#discussion_r4535503364


##########
sched/timer/timer_settime.c:
##########
@@ -74,6 +74,97 @@ static inline void timer_overrun(FAR struct posix_timer_s 
*timer, clock_t n)
     }
 }
 
+#if CONFIG_SCHED_CRITMONITOR_MAXTIME_THREAD >= 0
+/****************************************************************************
+ * Name: timer_cputicks
+ *
+ * Description:
+ *   Read the CPU-time clock of the timer, in ticks rounded down.
+ *
+ ****************************************************************************/
+
+static int timer_cputicks(FAR struct posix_timer_s *timer,
+                          FAR clock_t *ticks)
+{
+  struct timespec ts;
+  int ret;
+
+  ret = nxclock_gettime(timer->pt_clock, &ts);
+  if (ret >= 0)
+    {
+      *ticks = clock_time2ticks_floor(&ts);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: timer_cpustart
+ *
+ * Description:
+ *   Start the watchdog of a CPU-time timer.  The CPU time cannot reach
+ *   pt_expected before the watchdog expires.  timer_timeout() then reads
+ *   the clock again and starts the watchdog again if that is necessary.
+ *
+ ****************************************************************************/
+
+static int timer_cpustart(FAR struct posix_timer_s *timer, clock_t ticks)
+{
+  clock_t delay = timer->pt_expected - ticks;
+
+  if (delay < 0)
+    {
+      delay = 0;
+    }
+
+#ifdef CONFIG_SMP
+  /* The threads of a process can run on all CPUs at the same time */

Review Comment:
   Only the process clock is divided: CLOCK_PROCESS_CPUTIME_ID adds the CPU 
time of all threads of the process, so with N CPUs it can advance N ticks in 
one tick. The watchdog fires after delay/N, and timer_timeout() reads the clock 
again and restarts it if the time is not reached. A thread clock 
(CLOCK_THREAD_CPUTIME_ID) is not divided.



##########
sched/sched/sched_critmonitor.c:
##########
@@ -137,49 +145,27 @@ clock_t g_busywait_total[CONFIG_SMP_NCPUS];
  * Name: nxsched_critmon_cpuload
  *
  * Description:
- *   Update the running time of all running threads when switching threads
+ *   Give the running thread of a CPU the CPU load ticks since the last
+ *   update of that CPU.
  *
  * Input Parameters:
- *   tcb   - The task that we are performing the load operations on.
- *   current - The current time
- *   tick - The ticks that we process in this cpuload.
+ *   tcb     - The thread that runs on the CPU.
+ *   cpu     - The CPU.
+ *   current - The current time.
  *
  * Returned Value:
  *   None
  *
  ****************************************************************************/
 
 #ifdef CONFIG_SCHED_CPULOAD_CRITMONITOR
-static void nxsched_critmon_cpuload(FAR struct tcb_s *tcb, clock_t current,
-                                    clock_t tick)
+static void nxsched_critmon_cpuload(FAR struct tcb_s *tcb, int cpu,
+                                    clock_t current)
 {
-  int i;
-
-  UNUSED(i);
-
-  /* Update the cpuload of the thread ready to be suspended */
-
-  nxsched_process_taskload_ticks(tcb, tick);
+  clock_t elapsed = current - g_cpuload_start[cpu];
 
-  /* Update the cpuload of threads running on other CPUs */
-
-#  ifdef CONFIG_SMP
-  for (i = 0; i < CONFIG_SMP_NCPUS; i++)
-    {
-      FAR struct tcb_s *rtcb = current_task(i);
-
-      if (tcb->cpu == rtcb->cpu)
-        {
-          continue;
-        }
-
-      nxsched_process_taskload_ticks(rtcb, tick);
-
-      /* Update start time, avoid repeated statistics when the next call */
-
-      rtcb->run_start = current;

Review Comment:
   run_start is still used: nxsched_switch_critmon() sets it at each switch, 
and nxsched_critmon_runtime() adds the running slice to run_time with it. Only 
its old use for the CPU load moved to the per-CPU g_cpuload_start.



##########
sched/sched/sched_critmonitor.c:
##########
@@ -110,6 +110,14 @@
 static spinlock_t g_crimonitor_lock = SP_UNLOCKED;
 #endif
 
+/* The time of the last CPU load update of each CPU.  The run time of the
+ * threads (run_start, run_time) is only changed by their own CPU.
+ */
+
+#ifdef CONFIG_SCHED_CPULOAD_CRITMONITOR
+static clock_t g_cpuload_start[CONFIG_SMP_NCPUS];

Review Comment:
   It is the time of the last CPU load update of each CPU, not the start of a 
thread: the thread start is run_start in the TCB.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to