tests: kernel: added tests for timeslice reset
Added test cases to verify timeslice reset among thread context switching. Jira: ZEP-948 Signed-off-by: Sharron LIU <sharron.liu@intel.com>
This commit is contained in:
parent
c0549aae75
commit
ab6d4c1a42
5 changed files with 102 additions and 2 deletions
|
@ -1,2 +1,3 @@
|
|||
CONFIG_ZTEST=y
|
||||
CONFIG_IRQ_OFFLOAD=y
|
||||
CONFIG_TIMESLICING=y
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
include $(ZEPHYR_BASE)/tests/Makefile.test
|
||||
|
||||
obj-y = main.o test_sched_priority.o test_sched_timeslice_and_lock.o
|
||||
obj-y += test_sched_is_preempt_thread.o
|
||||
obj-y += test_sched_is_preempt_thread.o test_sched_timeslice_reset.o
|
||||
|
|
|
@ -28,7 +28,8 @@ void test_main(void *p1, void *p2, void *p3)
|
|||
ztest_unit_test(test_time_slicing_disable_preemptible),
|
||||
ztest_unit_test(test_lock_preemptible),
|
||||
ztest_unit_test(test_unlock_preemptible),
|
||||
ztest_unit_test(test_sched_is_preempt_thread)
|
||||
ztest_unit_test(test_sched_is_preempt_thread),
|
||||
ztest_unit_test(test_slice_reset)
|
||||
);
|
||||
ztest_run_test_suite(test_threads_scheduling);
|
||||
}
|
||||
|
|
|
@ -32,5 +32,6 @@ void test_time_slicing_disable_preemptible(void);
|
|||
void test_lock_preemptible(void);
|
||||
void test_unlock_preemptible(void);
|
||||
void test_sched_is_preempt_thread(void);
|
||||
void test_slice_reset(void);
|
||||
|
||||
#endif /* __TEST_SCHED_H__ */
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
* Copyright (c) 2017 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <ztest.h>
|
||||
|
||||
#define STACK_SIZE 512
|
||||
#define NUM_THREAD 3
|
||||
static char __noinit __stack tstack[NUM_THREAD][STACK_SIZE];
|
||||
/* slice size in millisecond*/
|
||||
#define SLICE_SIZE 200
|
||||
/* busy for more than one slice*/
|
||||
#define BUSY_MS (SLICE_SIZE+20)
|
||||
/* a half timeslice*/
|
||||
#define HALF_SLICE_SIZE (SLICE_SIZE >> 1)
|
||||
|
||||
K_SEM_DEFINE(sema, 0, NUM_THREAD);
|
||||
/*elapsed_slice taken by last thread*/
|
||||
static s64_t elapsed_slice;
|
||||
/*expected elapsed duration*/
|
||||
static s64_t expected_slice[NUM_THREAD] = {
|
||||
HALF_SLICE_SIZE,/* the ztest native thread taking a half timeslice*/
|
||||
SLICE_SIZE, /* the spawned thread taking a full timeslice, reset*/
|
||||
SLICE_SIZE /* the spawned thread taking a full timeslice, reset*/
|
||||
};
|
||||
static int thread_idx;
|
||||
|
||||
static void thread_tslice(void *p1, void *p2, void *p3)
|
||||
{
|
||||
s64_t t = k_uptime_delta(&elapsed_slice);
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
TC_PRINT("thread[%d] elapsed slice %lld, ", thread_idx, t);
|
||||
TC_PRINT("expected %lld\n", expected_slice[thread_idx]);
|
||||
#endif
|
||||
/** TESTPOINT: timeslice should be reset for each preemptive thread*/
|
||||
zassert_true(t <= expected_slice[thread_idx], NULL);
|
||||
thread_idx = (thread_idx + 1) % NUM_THREAD;
|
||||
|
||||
u32_t t32 = k_uptime_get_32();
|
||||
|
||||
/* Keep the current thread busy for more than one slice, even though,
|
||||
* when timeslice used up the next thread should be scheduled in.
|
||||
*/
|
||||
while (k_uptime_get_32() - t32 < BUSY_MS)
|
||||
;
|
||||
k_sem_give(&sema);
|
||||
}
|
||||
|
||||
/*test cases*/
|
||||
|
||||
void test_slice_reset(void)
|
||||
{
|
||||
u32_t t32;
|
||||
k_tid_t tid[NUM_THREAD];
|
||||
struct k_thread t[NUM_THREAD];
|
||||
int old_prio = k_thread_priority_get(k_current_get());
|
||||
|
||||
thread_idx = 0;
|
||||
/*disable timeslice*/
|
||||
k_sched_time_slice_set(0, K_PRIO_PREEMPT(0));
|
||||
|
||||
for (int j = 0; j < 2; j++) {
|
||||
k_sem_reset(&sema);
|
||||
/* update priority for current thread*/
|
||||
k_thread_priority_set(k_current_get(), K_PRIO_PREEMPT(j));
|
||||
/* create delayed threads with equal preemptive priority*/
|
||||
for (int i = 0; i < NUM_THREAD; i++) {
|
||||
tid[i] = k_thread_create(&t[i], tstack[i], STACK_SIZE,
|
||||
thread_tslice, NULL, NULL, NULL,
|
||||
K_PRIO_PREEMPT(j), 0, 0);
|
||||
}
|
||||
/* enable time slice*/
|
||||
k_sched_time_slice_set(SLICE_SIZE, K_PRIO_PREEMPT(0));
|
||||
k_uptime_delta(&elapsed_slice);
|
||||
|
||||
/* current thread (ztest native) consumed a half timeslice*/
|
||||
t32 = k_uptime_get_32();
|
||||
while (k_uptime_get_32() - t32 < HALF_SLICE_SIZE)
|
||||
;
|
||||
|
||||
/* relinquish CPU and wait for each thread to complete*/
|
||||
for (int i = 0; i < NUM_THREAD; i++) {
|
||||
k_sem_take(&sema, K_FOREVER);
|
||||
}
|
||||
|
||||
/* test case teardown*/
|
||||
for (int i = 0; i < NUM_THREAD; i++) {
|
||||
k_thread_abort(tid[i]);
|
||||
}
|
||||
/* disable time slice*/
|
||||
k_sched_time_slice_set(0, K_PRIO_PREEMPT(0));
|
||||
}
|
||||
k_thread_priority_set(k_current_get(), old_prio);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue