Skip to content
Permalink
Browse files

tests: tickless_concept: Fix slicing time measurement

The time measurement based on k_uptime_delta() might be not accurate
for some values of CONFIG_SYS_CLOCK_TICKS_PER_SEC. This commit
introduces measurement based on k_cycle_get_32(), which is more
precise.

Signed-off-by: Piotr Zięcik <piotr.ziecik@nordicsemi.no>
  • Loading branch information...
pizi-nordic authored and nashif committed May 16, 2019
1 parent 86f012d commit 62c71dc4d82f3518ffbf9ce9fe0a629c280dc2aa
Showing with 39 additions and 11 deletions.
  1. +39 −11 tests/kernel/tickless/tickless_concept/src/main.c
@@ -23,9 +23,16 @@ static struct k_thread tdata[NUM_THREAD];

/*slice size is set as half of the sleep duration*/
#define SLICE_SIZE __ticks_to_ms(CONFIG_TICKLESS_IDLE_THRESH >> 1)
#define SLICE_SIZE_CYCLES \
(u32_t)(SLICE_SIZE * sys_clock_hw_cycles_per_sec() / 1000)

/*maximum slice duration accepted by the test*/
#define SLICE_SIZE_LIMIT __ticks_to_ms((CONFIG_TICKLESS_IDLE_THRESH >> 1) + 1)
/*mimimum slice duration accepted by the test: (SLICE_SIZE - 1ms) */
#define SLICE_SIZE_MIN_CYCLES \
(u32_t)(SLICE_SIZE_CYCLES - (sys_clock_hw_cycles_per_sec() / 1000))

/*maximum slice duration accepted by the test: (SLICE_SIZE + 1ms) */
#define SLICE_SIZE_MAX_CYCLES \
(u32_t)(SLICE_SIZE_CYCLES + (sys_clock_hw_cycles_per_sec() / 1000))

/*align to millisecond boundary*/
#if defined(CONFIG_ARCH_POSIX)
@@ -44,19 +51,30 @@ static struct k_thread tdata[NUM_THREAD];
} while (0)
#endif
K_SEM_DEFINE(sema, 0, NUM_THREAD);
static s64_t elapsed_slice;
static u32_t elapsed_slice;

static u32_t cycles_delta(u32_t *reftime)
{
u32_t now, delta;

now = k_cycle_get_32();
delta = now - *reftime;
*reftime = now;

return delta;
}


static void thread_tslice(void *p1, void *p2, void *p3)
{
s64_t t = k_uptime_delta(&elapsed_slice);
u32_t t = cycles_delta(&elapsed_slice);

TC_PRINT("elapsed slice %lld, expected: <%lld, %lld>\n",
t, SLICE_SIZE, SLICE_SIZE_LIMIT);
TC_PRINT("elapsed slice %d, expected: <%d, %d>\n",
t, SLICE_SIZE_MIN_CYCLES, SLICE_SIZE_MAX_CYCLES);

/**TESTPOINT: verify slicing scheduler behaves as expected*/
zassert_true(t >= SLICE_SIZE, NULL);
/*less than one tick delay*/
zassert_true(t <= SLICE_SIZE_LIMIT, NULL);
zassert_true(t >= SLICE_SIZE_MIN_CYCLES, NULL);
zassert_true(t <= SLICE_SIZE_MAX_CYCLES, NULL);

/*keep the current thread busy for more than one slice*/
k_busy_wait(1000 * SLEEP_TICKLESS);
@@ -108,13 +126,23 @@ void test_tickless_slice(void)
/*enable time slice*/
k_sched_time_slice_set(SLICE_SIZE, K_PRIO_PREEMPT(0));

/*synchronize to tick boundary*/
k_sleep(1);

/*create delayed threads with equal preemptive priority*/
for (int i = 0; i < NUM_THREAD; i++) {
tid[i] = k_thread_create(&tdata[i], tstack[i], STACK_SIZE,
thread_tslice, NULL, NULL, NULL,
K_PRIO_PREEMPT(0), 0, SLICE_SIZE);
K_PRIO_PREEMPT(0), 0,
SLICE_SIZE + __ticks_to_ms(1));
}
k_uptime_delta(&elapsed_slice);

/*synchronize to tick boundary.*/
k_sleep(1);

/*set reference time to last tick boundary*/
elapsed_slice = z_tick_get_32() * sys_clock_hw_cycles_per_tick();

/*relinquish CPU and wait for each thread to complete*/
for (int i = 0; i < NUM_THREAD; i++) {
k_sem_take(&sema, K_FOREVER);

0 comments on commit 62c71dc

Please sign in to comment.
You can’t perform that action at this time.