From bc087b3014da5f7682a330fedb3f42b036ee46c6 Mon Sep 17 00:00:00 2001 From: Vinayak Kariappa Chettimada Date: Fri, 31 Aug 2018 08:58:59 +0200 Subject: [PATCH 1/2] kernel: Improve precision of ticks and ms conversions The following 2 improvements are contained in this patch: - When converting from ms to ticks, instead of using hardware cycles per tick, use hardware cycles per second. This ensures that the multiplication is done before the division, increasing precision. - When converting from ticks to ms, instead of using cycles per tick and cycles per sec, use ticks per sec. This too increases the precision. The concept is to make the dividend as large as possible compared to the divisor in order to lose as little precision as possible. Fixes #8898 Fixes #9459 Fixes #9466 Fixes #9468 Signed-off-by: Vinayak Kariappa Chettimada Signed-off-by: Carles Cufi --- include/kernel.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/kernel.h b/include/kernel.h index d2003d06095db..26b701918b607 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -1363,7 +1363,8 @@ static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms) /* use 64-bit math to keep precision */ return (s32_t)ceiling_fraction( (s64_t)ms * sys_clock_hw_cycles_per_sec, - (s64_t)MSEC_PER_SEC * sys_clock_hw_cycles_per_tick); + ((s64_t)MSEC_PER_SEC * sys_clock_hw_cycles_per_sec) / + sys_clock_ticks_per_sec); #else /* simple division keeps precision */ s32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec; @@ -1383,8 +1384,7 @@ static inline s64_t __ticks_to_ms(s64_t ticks) #ifdef _NEED_PRECISE_TICK_MS_CONVERSION /* use 64-bit math to keep precision */ - return (u64_t)ticks * sys_clock_hw_cycles_per_tick * MSEC_PER_SEC / - sys_clock_hw_cycles_per_sec; + return (u64_t)ticks * MSEC_PER_SEC / sys_clock_ticks_per_sec; #else /* simple multiplication keeps precision */ u32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec; From fbba8ecda78de66f3712d2cd6bb6ae47786c6a95 Mon Sep 17 00:00:00 2001 From: Anas Nashif Date: Fri, 31 Aug 2018 10:17:46 -0400 Subject: [PATCH 2/2] tests: preempt: increase stack size for test Failed with: Running test suite suite_preempt =================================================================== starting test - test_preempt ***** Stack Check Fail! ***** Current thread ID = 0x0040019c eax: 0x00400254, ebx: 0x00400254, ecx: 0x004002b0, edx: 0x00000001 esi: 0x004001f8, edi: 0x00401080, ebp: 0x00408024, esp: 0x00408000 eflags: 0x00000046 cs: 0x0008 call trace: eip: 0x00002115 0x000021b8 (0x40019c) 0x00002685 (0x4001f8) 0x00004f65 (0x401120) 0x00005187 (0x401120) 0x000051c2 (0x40019c) 0x000052be (0xffffffff) 0x00005bab (0x246) 0x000019c4 (0x400078) Fatal fault in thread 0x0040019c! Aborting. Signed-off-by: Anas Nashif --- tests/kernel/sched/preempt/prj.conf | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/kernel/sched/preempt/prj.conf b/tests/kernel/sched/preempt/prj.conf index 7a3e5084084ab..2272f88f7059e 100644 --- a/tests/kernel/sched/preempt/prj.conf +++ b/tests/kernel/sched/preempt/prj.conf @@ -2,3 +2,4 @@ CONFIG_ZTEST=y CONFIG_MP_NUM_CPUS=1 CONFIG_NUM_METAIRQ_PRIORITIES=1 CONFIG_IRQ_OFFLOAD=y +CONFIG_TEST_EXTRA_STACKSIZE=256