Skip to content

Commit 4685175

Browse files
Gary-Hobsonxiaoxiang781216
authored andcommitted
sched: fix the inaccurate cpuload statistics issue
Non-fixed clock source will lead to inaccurate cpuload statistics before: 5 5 253 RR Task - Waiting Signal 0000000000000000 0002000 0000624 31.2% 2.5% cpuload -p 50 6 6 253 RR Task - Waiting Signal 0000000000000000 0002000 0000624 31.2% 0.7% cpuload -p 10 7 7 253 RR Task - Waiting Signal 0000000000000000 0002000 0000624 31.2% 2.2% cpuload -p 20 after: 5 5 253 RR Task - Waiting Signal 0000000000000000 0002000 0000624 31.2% 50.8% cpuload -p 50 6 6 253 RR Task - Waiting Signal 0000000000000000 0002000 0000624 31.2% 10.8% cpuload -p 10 7 7 253 RR Task - Waiting Signal 0000000000000000 0002000 0000624 31.2% 20.0% cpuload -p 20 Signed-off-by: yinshengkai <[email protected]>
1 parent f88afa5 commit 4685175

File tree

6 files changed

+71
-24
lines changed

6 files changed

+71
-24
lines changed

sched/Kconfig

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,6 @@ config SCHED_CPULOAD_NONE
10271027

10281028
config SCHED_CPULOAD_SYSCLK
10291029
bool "Use system clock"
1030-
depends on !SCHED_TICKLESS
10311030
---help---
10321031
If this option is enabled, the system clock is used for cpu load
10331032
measurement by default.
@@ -1077,19 +1076,18 @@ config SCHED_CPULOAD_CRITMONITOR
10771076

10781077
endchoice
10791078

1080-
if SCHED_CPULOAD_EXTCLK
1081-
10821079
config SCHED_CPULOAD_TICKSPERSEC
1083-
int "External clock rate"
1080+
int "CPU load sampling clock frequency(HZ)"
10841081
default 100
10851082
---help---
1086-
If an external clock is used to drive the sampling for the CPU load
1087-
calculations, then this value must be provided. This value provides
1088-
the rate of the external clock interrupts in units of ticks per
1089-
second. The default value of 100 corresponds to a 100Hz clock. NOTE:
1083+
CPU load sampling clock frequency, in HZ. Use sysclk clock source,
1084+
use wdt, EXTCLK clock source, use an external timer.
1085+
The default value of 100 corresponds to a 100Hz clock. NOTE:
10901086
that 100Hz is the default frequency of the system time and, hence,
10911087
the worst possible choice in most cases.
10921088

1089+
if SCHED_CPULOAD_EXTCLK
1090+
10931091
choice
10941092
prompt "Select CPU load timer"
10951093
default CPULOAD_ONESHOT

sched/clock/clock.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,8 @@ void clock_timer(void);
8787

8888
void perf_init(void);
8989

90+
#ifdef CONFIG_SCHED_CPULOAD_SYSCLK
91+
void cpuload_init(void);
92+
#endif
93+
9094
#endif /* __SCHED_CLOCK_CLOCK_H */

sched/clock/clock_initialize.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ void clock_initialize(void)
231231

232232
perf_init();
233233

234+
#ifdef CONFIG_SCHED_CPULOAD_SYSCLK
235+
cpuload_init();
236+
#endif
237+
234238
sched_trace_end();
235239
}
236240

sched/sched/sched_cpuload.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include <nuttx/clock.h>
3333
#include <nuttx/irq.h>
34+
#include <nuttx/wdog.h>
3435

3536
#include "sched/sched.h"
3637

@@ -61,6 +62,11 @@
6162
CONFIG_SCHED_CPULOAD_TIMECONSTANT * \
6263
CPULOAD_TICKSPERSEC)
6364

65+
/* The sampling period in system timer ticks */
66+
67+
#define CPULOAD_SAMPLING_PERIOD \
68+
(TICK_PER_SEC / CONFIG_SCHED_CPULOAD_TICKSPERSEC)
69+
6470
/****************************************************************************
6571
* Public Data
6672
****************************************************************************/
@@ -80,6 +86,34 @@
8086

8187
volatile clock_t g_cpuload_total;
8288

89+
/****************************************************************************
90+
* Private Functions
91+
****************************************************************************/
92+
93+
/****************************************************************************
94+
* Name: cpuload_callback
95+
*
96+
* Description:
97+
* This is the callback function that will be invoked when the watchdog
98+
* timer expires.
99+
*
100+
* Input Parameters:
101+
* argc - the argument passed with the timer when the timer was started.
102+
*
103+
* Returned Value:
104+
* None
105+
*
106+
****************************************************************************/
107+
108+
#ifdef CONFIG_SCHED_CPULOAD_SYSCLK
109+
static void cpuload_callback(wdparm_t arg)
110+
{
111+
FAR struct wdog_s *wdog = (FAR struct wdog_s *)arg;
112+
nxsched_process_cpuload_ticks(CPULOAD_SAMPLING_PERIOD);
113+
wd_start(wdog, CPULOAD_SAMPLING_PERIOD, cpuload_callback, arg);
114+
}
115+
#endif
116+
83117
/****************************************************************************
84118
* Public Functions
85119
****************************************************************************/
@@ -221,3 +255,26 @@ int clock_cpuload(int pid, FAR struct cpuload_s *cpuload)
221255
leave_critical_section(flags);
222256
return ret;
223257
}
258+
259+
/****************************************************************************
260+
* Name: cpuload_init
261+
*
262+
* Description:
263+
* Initialize the CPU load measurement logic.
264+
*
265+
* Input Parameters:
266+
* None
267+
*
268+
* Returned Value:
269+
* None
270+
*
271+
****************************************************************************/
272+
273+
#ifdef CONFIG_SCHED_CPULOAD_SYSCLK
274+
void cpuload_init(void)
275+
{
276+
static struct wdog_s g_cpuload_wdog;
277+
wd_start(&g_cpuload_wdog, CPULOAD_SAMPLING_PERIOD, cpuload_callback,
278+
(wdparm_t)&g_cpuload_wdog);
279+
}
280+
#endif

sched/sched/sched_processtimer.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,6 @@ void nxsched_process_timer(void)
182182

183183
clock_timer();
184184

185-
#ifdef CONFIG_SCHED_CPULOAD_SYSCLK
186-
/* Perform CPU load measurements (before any timer-initiated context
187-
* switches can occur)
188-
*/
189-
190-
nxsched_process_cpuload();
191-
#endif
192-
193185
/* Check if the currently executing task has exceeded its
194186
* timeslice.
195187
*/

sched/sched/sched_timerexpiration.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,6 @@ static clock_t nxsched_timer_process(clock_t ticks, clock_t elapsed,
339339
clock_update_wall_time();
340340
#endif
341341

342-
#ifdef CONFIG_SCHED_CPULOAD_SYSCLK
343-
/* Perform CPU load measurements (before any timer-initiated context
344-
* switches can occur)
345-
*/
346-
347-
nxsched_process_cpuload_ticks(elapsed);
348-
#endif
349-
350342
/* Check for operations specific to scheduling policy of the currently
351343
* active task.
352344
*/

0 commit comments

Comments
 (0)