Skip to content

Commit 4a39b9e

Browse files
pizi-nordicnashif
authored andcommitted
kernel: sched: Use ticks as time unit in time slicing.
The time slicing settings was kept in milliseconds while all related operations was based on ticks. Continuous back and forth conversion between ticks and milliseconds introduced an accumulating error due to rounding in _ms_to_ticks() and __ticks_to_ms(). As result configured time slice duration was not achieved. This commit removes excessive ticks <-> ms conversion by using ticks as time unit for all operations related to time slicing. Also, it fixes #8896 as well as #8897. Signed-off-by: Piotr Zięcik <[email protected]>
1 parent ee9a061 commit 4a39b9e

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

kernel/sched.c

+10-6
Original file line numberDiff line numberDiff line change
@@ -591,16 +591,16 @@ struct k_thread *_priq_mq_best(struct _priq_mq *pq)
591591
}
592592

593593
#ifdef CONFIG_TIMESLICING
594-
extern s32_t _time_slice_duration; /* Measured in ms */
595-
extern s32_t _time_slice_elapsed; /* Measured in ms */
594+
extern s32_t _time_slice_duration; /* Measured in ticks */
595+
extern s32_t _time_slice_elapsed; /* Measured in ticks */
596596
extern int _time_slice_prio_ceiling;
597597

598598
void k_sched_time_slice_set(s32_t duration_in_ms, int prio)
599599
{
600600
__ASSERT(duration_in_ms >= 0, "");
601601
__ASSERT((prio >= 0) && (prio < CONFIG_NUM_PREEMPT_PRIORITIES), "");
602602

603-
_time_slice_duration = duration_in_ms;
603+
_time_slice_duration = _ms_to_ticks(duration_in_ms);
604604
_time_slice_elapsed = 0;
605605
_time_slice_prio_ceiling = prio;
606606
}
@@ -641,10 +641,9 @@ void _update_time_slice_before_swap(void)
641641
}
642642

643643
u32_t remaining = _get_remaining_program_time();
644-
u32_t time_slice_ticks = _ms_to_ticks(_time_slice_duration);
645644

646-
if (!remaining || (_time_slice_ticks < remaining)) {
647-
_set_time(_time_slice_ticks);
645+
if (!remaining || (_time_slice_duration < remaining)) {
646+
_set_time(_time_slice_duration);
648647
} else {
649648
/* Account previous elapsed time and reprogram
650649
* timer with remaining time
@@ -691,6 +690,11 @@ void _sched_init(void)
691690
sys_dlist_init(&_kernel.ready_q.runq.queues[i]);
692691
}
693692
#endif
693+
694+
#ifdef CONFIG_TIMESLICING
695+
k_sched_time_slice_set(CONFIG_TIMESLICE_SIZE,
696+
CONFIG_TIMESLICE_PRIORITY);
697+
#endif
694698
}
695699

696700
int _impl_k_thread_priority_get(k_tid_t thread)

kernel/sys_clock.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ static inline void handle_timeouts(s32_t ticks)
263263

264264
#ifdef CONFIG_TIMESLICING
265265
s32_t _time_slice_elapsed;
266-
s32_t _time_slice_duration = CONFIG_TIMESLICE_SIZE;
267-
int _time_slice_prio_ceiling = CONFIG_TIMESLICE_PRIORITY;
266+
s32_t _time_slice_duration;
267+
int _time_slice_prio_ceiling;
268268

269269
/*
270270
* Always called from interrupt level, and always only from the system clock
@@ -285,7 +285,7 @@ static void handle_time_slicing(s32_t ticks)
285285
return;
286286
}
287287

288-
_time_slice_elapsed += __ticks_to_ms(ticks);
288+
_time_slice_elapsed += ticks;
289289
if (_time_slice_elapsed >= _time_slice_duration) {
290290

291291
unsigned int key;
@@ -297,8 +297,7 @@ static void handle_time_slicing(s32_t ticks)
297297
irq_unlock(key);
298298
}
299299
#ifdef CONFIG_TICKLESS_KERNEL
300-
next_ts =
301-
_ms_to_ticks(_time_slice_duration - _time_slice_elapsed);
300+
next_ts = _time_slice_duration - _time_slice_elapsed;
302301
#endif
303302
}
304303
#else

0 commit comments

Comments
 (0)