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


##########
sched/timer/timer_create.c:
##########
@@ -107,6 +107,38 @@ static FAR struct posix_timer_s *timer_allocate(void)
   return ret;
 }
 
+/****************************************************************************
+ * Name: timer_cpuclock
+ *
+ * Description:
+ *   Check a CPU-time clock of a new timer.  Give a clock of the calling
+ *   thread its thread ID: the timer reads the clock in interrupt handlers.
+ *
+ ****************************************************************************/
+
+#if CONFIG_SCHED_CRITMONITOR_MAXTIME_THREAD >= 0
+static bool timer_cpuclock(FAR clockid_t *clockid)

Review Comment:
   timer_cpu_getclockid



##########
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,

Review Comment:
   timer_cpu_getticks



##########
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:
   should we remove run_start from tcb_s?



##########
sched/timer/timer.h:
##########
@@ -45,6 +46,14 @@
 
 #define PT_FLAGS_PREALLOCATED 0x01u /* Timer comes from a pool of preallocated 
timers */
 
+/* Timers on the CPU-time clocks need the run time of the threads */
+
+#if CONFIG_SCHED_CRITMONITOR_MAXTIME_THREAD >= 0
+#  define timer_iscputime(t) \

Review Comment:
   timer_is_cputime?



##########
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 */
+
+  if ((timer->pt_clock & CLOCK_MASK) == CLOCK_PROCESS_CPUTIME_ID)
+    {
+      delay = div_round_up(delay, CONFIG_SMP_NCPUS);
+    }
+#endif
+
+  return wd_start(&timer->pt_wdog, delay, timer_timeout, (wdparm_t)timer);
+}
+
+/****************************************************************************
+ * Name: timer_cpusettime
+ *
+ * Description:
+ *   Arm a CPU-time timer.  pt_expected is a time on the CPU-time clock.
+ *
+ ****************************************************************************/
+
+static int timer_cpusettime(FAR struct posix_timer_s *timer, int flags,

Review Comment:
   timer_cpu_settime



##########
sched/timer/timer_gettime.c:
##########
@@ -38,6 +38,41 @@
  * Public Functions
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: timer_cpuremaining
+ *
+ * Description:
+ *   Return the CPU time until a CPU-time timer expires, or zero if the
+ *   timer is disarmed.
+ *
+ ****************************************************************************/
+
+#if CONFIG_SCHED_CRITMONITOR_MAXTIME_THREAD >= 0
+void timer_cpuremaining(FAR struct posix_timer_s *timer,

Review Comment:
   timer_cpu_gettime?



##########
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:
   g_thread_start?



##########
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:
   but one thread can't run on the different cpu at the same time, why do we 
need divide timeout by cpu number?



##########
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)

Review Comment:
   timer_cpu_start[time]?



-- 
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