You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2023/08/04 17:59:03 UTC

[nuttx] 03/04: sched/cpuload: use perf to implement cpuload without relying on external timers

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

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

commit 996be8f2cfc7545935b5a8da97139083733e57b3
Author: yinshengkai <yi...@xiaomi.com>
AuthorDate: Tue Apr 25 17:41:44 2023 +0800

    sched/cpuload: use perf to implement cpuload without relying on external timers
    
    Need to enable CONFIG_SCHED_CRITMONITOR
    
    Signed-off-by: yinshengkai <yi...@xiaomi.com>
---
 sched/Kconfig                   | 10 ++++++++++
 sched/sched/sched.h             |  3 ++-
 sched/sched/sched_critmonitor.c |  5 +++++
 3 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/sched/Kconfig b/sched/Kconfig
index f309a11673..0b1b25184b 100644
--- a/sched/Kconfig
+++ b/sched/Kconfig
@@ -900,6 +900,7 @@ if SCHED_CPULOAD
 choice
 	prompt "Select CPU load clock source"
 	default SCHED_CPULOAD_EXTCLK if SCHED_TICKLESS
+	default SCHED_CPULOAD_CRITMONITOR if SCHED_CRITMONITOR
 
 config SCHED_CPULOAD_SYSCLK
 	bool "Use system clock"
@@ -941,6 +942,15 @@ config SCHED_CPULOAD_EXTCLK
 		nxsched_process_cpuload_ticks() at each timer expiration with interrupts
 		disabled.
 
+config SCHED_CPULOAD_CRITMONITOR
+	bool "Use critical monitor"
+	depends on SCHED_CRITMONITOR
+	---help---
+		Use the perfcounter in the core of the chip as a counter, no need to
+		use an external timer. Need to depend on SCHED_CRITMONITOR.
+		When the task is suspended, call nxsched_critmon_cpuload_ticks to count
+		the recent running time of the task
+
 endchoice
 
 if SCHED_CPULOAD_EXTCLK
diff --git a/sched/sched/sched.h b/sched/sched/sched.h
index 3b423275fc..0b2dc5f8f6 100644
--- a/sched/sched/sched.h
+++ b/sched/sched/sched.h
@@ -397,7 +397,8 @@ int  nxsched_pause_cpu(FAR struct tcb_s *tcb);
 
 /* CPU load measurement support */
 
-#ifdef CONFIG_SCHED_CPULOAD_SYSCLK
+#if defined(CONFIG_SCHED_CPULOAD_SYSCLK) || \
+    defined (CONFIG_SCHED_CPULOAD_CRITMONITOR)
 void nxsched_process_taskload_ticks(FAR struct tcb_s *tcb, uint32_t ticks);
 void nxsched_process_cpuload_ticks(uint32_t ticks);
 #define nxsched_process_cpuload() nxsched_process_cpuload_ticks(1)
diff --git a/sched/sched/sched_critmonitor.c b/sched/sched/sched_critmonitor.c
index c4560fd0da..f296d75913 100644
--- a/sched/sched/sched_critmonitor.c
+++ b/sched/sched/sched_critmonitor.c
@@ -297,6 +297,11 @@ void nxsched_suspend_critmon(FAR struct tcb_s *tcb)
   unsigned long current = up_perf_gettime();
   unsigned long elapsed = current - tcb->run_start;
 
+#ifdef CONFIG_SCHED_CPULOAD_CRITMONITOR
+  unsigned long tick = elapsed * CLOCKS_PER_SEC / up_perf_getfreq();
+  nxsched_process_taskload_ticks(tcb, tick);
+#endif
+
   tcb->run_time += elapsed;
   if (elapsed > tcb->run_max)
     {