test_sleep: replacing depreciated APIs with new one.

As part of Zephyr Release 1.9 some APIs will be depreciated.
This patch replaces APIs related to semophore init, kernel sleep,
task spawn etc.

Jira: ZEP-2009

Change-Id: I1fe09e9592f503c3413d51857fd740703173c042
Signed-off-by: Youvedeep Singh <youvedeep.singh@intel.com>
This commit is contained in:
Youvedeep Singh 2017-04-17 17:00:10 +05:30
commit d353800879
3 changed files with 104 additions and 86 deletions

View file

@ -1,15 +1,15 @@
Title: Nanokernel Sleep and Wakeup APIs
Title: cooperative thread Sleep and Wakeup APIs
Description:
This test verifies that the nanokernel sleep and wakeup APIs operate as
This test verifies that cooperative sleep and wakeup APIs operate as
expected.
---------------------------------------------------------------------------
Building and Running Project:
This nanokernel project outputs to the console. It can be built and executed
This project outputs to the console. It can be built and executed
on QEMU as follows:
make qemu
@ -31,15 +31,15 @@ or
Sample Output:
tc_start() - Test Nanokernel Sleep and Wakeup APIs
tc_start() - Test kernel Sleep and Wakeup APIs
Nanokernel objects initialized
Test fiber started: id = 0x001030b8
Helper fiber started: id = 0x001028e8
Testing normal expiration of fiber_sleep()
Testing fiber_sleep() + fiber_fiber_wakeup()
Testing fiber_sleep() + isr_fiber_wakeup()
Testing fiber_sleep() + task_fiber_wakeup()
Testing nanokernel task_sleep()
Kernel objects initialized
Test thread started: id = 0x00103044
Helper thread started: id = 0x00102f44
Testing normal expiration of k_sleep()
Testing: test thread sleep + helper thread wakeup test
Testing: test thread sleep + isr offload wakeup test
Testing: test thread sleep + main wakeup test thread
Testing kernel k_sleep()
===================================================================
PROJECT EXECUTION SUCCESSFUL

View file

@ -1,3 +1 @@
CONFIG_NANO_TIMEOUTS=y
CONFIG_IRQ_OFFLOAD=y
CONFIG_LEGACY_KERNEL=y

View file

@ -9,11 +9,11 @@
* @brief Test nanokernel sleep and wakeup APIs
*
* This module tests the following sleep and wakeup scenarios:
* 1. fiber_sleep() without cancellation
* 2. fiber_sleep() cancelled via fiber_fiber_wakeup()
* 3. fiber_sleep() cancelled via isr_fiber_wakeup()
* 4. fiber_sleep() cancelled via task_fiber_wakeup()
* 5. task_sleep() - no cancellation exists
* 1. k_sleep() without cancellation
* 2. k_sleep() cancelled via k_wakeup()
* 3. k_sleep() cancelled via k_wakeup()
* 4. k_sleep() cancelled via k_wakeup()
* 5. k_sleep() - no cancellation exists
*/
#include <tc_util.h>
@ -25,45 +25,47 @@
#include <util_test_common.h>
#if defined(CONFIG_ASSERT) && defined(CONFIG_DEBUG)
#define FIBER_STACKSIZE (384 + CONFIG_TEST_EXTRA_STACKSIZE)
#define THREAD_STACK (384 + CONFIG_TEST_EXTRA_STACKSIZE)
#else
#define FIBER_STACKSIZE (256 + CONFIG_TEST_EXTRA_STACKSIZE)
#define THREAD_STACK (256 + CONFIG_TEST_EXTRA_STACKSIZE)
#endif
#define TEST_FIBER_PRIORITY 4
#define HELPER_FIBER_PRIORITY 10
#define TEST_THREAD_PRIORITY -4
#define HELPER_THREAD_PRIORITY -10
#define ONE_SECOND (sys_clock_ticks_per_sec)
#define ONE_SECOND (MSEC_PER_SEC)
#define TICKS_PER_MS MSEC_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC
static struct nano_sem test_fiber_sem;
static struct nano_sem helper_fiber_sem;
static struct nano_sem task_sem;
static struct k_sem test_thread_sem;
static struct k_sem helper_thread_sem;
static struct k_sem task_sem;
static char __stack test_fiber_stack[FIBER_STACKSIZE];
static char __stack helper_fiber_stack[FIBER_STACKSIZE];
static char __stack test_thread_stack[THREAD_STACK];
static char __stack helper_thread_stack[THREAD_STACK];
static nano_thread_id_t test_fiber_id;
static nano_thread_id_t helper_fiber_id;
static k_tid_t test_thread_id;
static k_tid_t helper_thread_id;
static bool test_failure = true; /* Assume the test will fail */
static void test_objects_init(void)
{
nano_sem_init(&test_fiber_sem);
nano_sem_init(&helper_fiber_sem);
nano_sem_init(&task_sem);
k_sem_init(&test_thread_sem, 0, UINT_MAX);
k_sem_init(&helper_thread_sem, 0, UINT_MAX);
k_sem_init(&task_sem, 0, UINT_MAX);
TC_PRINT("Nanokernel objects initialized\n");
TC_PRINT("Kernel objects initialized\n");
}
static void align_to_tick_boundary(void)
{
uint32_t tick;
tick = sys_tick_get_32();
while (sys_tick_get_32() == tick) {
tick = k_uptime_get_32();
while (k_uptime_get_32() == tick) {
/* Busy wait to align to tick boundary */
}
}
/* Shouldn't ever sleep for less than requested time, but allow for 1
@ -78,65 +80,75 @@ static int sleep_time_valid(uint32_t start, uint32_t end, uint32_t dur)
return dt >= dur && dt <= (dur + 1);
}
static void test_fiber(int arg1, int arg2)
static void test_thread(int arg1, int arg2)
{
uint32_t start_tick;
uint32_t end_tick;
nano_fiber_sem_take(&test_fiber_sem, TICKS_UNLIMITED);
k_sem_take(&test_thread_sem, K_FOREVER);
TC_PRINT("Testing normal expiration of fiber_sleep()\n");
TC_PRINT("Testing normal expiration of k_sleep()\n");
align_to_tick_boundary();
start_tick = sys_tick_get_32();
fiber_sleep(ONE_SECOND);
end_tick = sys_tick_get_32();
start_tick = k_uptime_get_32();
/* FIXME: one tick less to account for
* one extra tick for _TICK_ALIGN in k_sleep*/
k_sleep(ONE_SECOND - TICKS_PER_MS);
end_tick = k_uptime_get_32();
if (!sleep_time_valid(start_tick, end_tick, ONE_SECOND)) {
TC_ERROR(" *** fiber_sleep() slept for %d ticks not %d.",
TC_ERROR(" *** k_sleep() slept for %d ticks not %d.",
end_tick - start_tick, ONE_SECOND);
return;
}
TC_PRINT("Testing fiber_sleep() + fiber_fiber_wakeup()\n");
nano_fiber_sem_give(&helper_fiber_sem); /* Activate helper fiber */
TC_PRINT("Testing: test thread sleep + helper thread wakeup test\n");
k_sem_give(&helper_thread_sem); /* Activate helper fiber */
align_to_tick_boundary();
start_tick = sys_tick_get_32();
fiber_sleep(ONE_SECOND);
end_tick = sys_tick_get_32();
start_tick = k_uptime_get_32();
/* FIXME: one tick less to account for
* one extra tick for _TICK_ALIGN in k_sleep*/
k_sleep(ONE_SECOND - TICKS_PER_MS);
end_tick = k_uptime_get_32();
if (end_tick - start_tick > 1) {
TC_ERROR(" *** fiber_fiber_wakeup() took too long (%d ticks)\n",
TC_ERROR(" *** k_wakeup() took too long (%d ticks) \n",
end_tick - start_tick);
return;
}
TC_PRINT("Testing fiber_sleep() + isr_fiber_wakeup()\n");
nano_fiber_sem_give(&helper_fiber_sem); /* Activate helper fiber */
TC_PRINT("Testing: test thread sleep + isr offload wakeup test\n");
k_sem_give(&helper_thread_sem); /* Activate helper fiber */
align_to_tick_boundary();
start_tick = sys_tick_get_32();
fiber_sleep(ONE_SECOND);
end_tick = sys_tick_get_32();
start_tick = k_uptime_get_32();
/* FIXME: one tick less to account for
* one extra tick for _TICK_ALIGN in k_sleep*/
k_sleep(ONE_SECOND - TICKS_PER_MS);
end_tick = k_uptime_get_32();
if (end_tick - start_tick > 1) {
TC_ERROR(" *** isr_fiber_wakeup() took too long (%d ticks)\n",
TC_ERROR(" *** k_wakeup() took too long (%d ticks)\n",
end_tick - start_tick);
return;
}
TC_PRINT("Testing fiber_sleep() + task_fiber_wakeup()\n");
nano_task_sem_give(&task_sem); /* Activate task */
TC_PRINT("Testing: test thread sleep + main wakeup test thread\n");
k_sem_give(&task_sem); /* Activate task */
align_to_tick_boundary();
start_tick = sys_tick_get_32();
fiber_sleep(ONE_SECOND); /* Task will execute */
end_tick = sys_tick_get_32();
start_tick = k_uptime_get_32();
/* FIXME: one tick less to account for
* one extra tick for _TICK_ALIGN in k_sleep*/
k_sleep(ONE_SECOND - TICKS_PER_MS); /* Task will execute */
end_tick = k_uptime_get_32();
if (end_tick - start_tick > 1) {
TC_ERROR(" *** task_fiber_wakeup() took too long (%d ticks)\n",
TC_ERROR(" *** k_wakeup() took too long (%d ticks) at LAST\n",
end_tick - start_tick);
return;
}
@ -146,19 +158,21 @@ static void test_fiber(int arg1, int arg2)
static void irq_offload_isr(void *arg)
{
isr_fiber_wakeup((nano_thread_id_t) arg);
k_wakeup((k_tid_t) arg);
}
static void helper_fiber(int arg1, int arg2)
static void helper_thread(int arg1, int arg2)
{
nano_fiber_sem_take(&helper_fiber_sem, TICKS_UNLIMITED);
k_sem_take(&helper_thread_sem, K_FOREVER);
/* Wake the test fiber */
fiber_fiber_wakeup(test_fiber_id);
nano_fiber_sem_take(&helper_fiber_sem, TICKS_UNLIMITED);
k_wakeup(test_thread_id);
k_sem_take(&helper_thread_sem, K_FOREVER);
/* Wake the test fiber from an ISR */
irq_offload(irq_offload_isr, (void *)test_fiber_id);
irq_offload(irq_offload_isr, (void *)test_thread_id);
}
void main(void)
@ -167,40 +181,46 @@ void main(void)
uint32_t start_tick;
uint32_t end_tick;
TC_START("Test Nanokernel Sleep and Wakeup APIs\n");
TC_START("Test kernel Sleep and Wakeup APIs\n");
test_objects_init();
test_fiber_id = task_fiber_start(test_fiber_stack, FIBER_STACKSIZE,
test_fiber, 0, 0, TEST_FIBER_PRIORITY, 0);
TC_PRINT("Test fiber started: id = %p\n", test_fiber_id);
test_thread_id = k_thread_spawn(test_thread_stack, THREAD_STACK,
(k_thread_entry_t) test_thread, 0, 0, NULL,
TEST_THREAD_PRIORITY, 0, 0);
helper_fiber_id = task_fiber_start(helper_fiber_stack, FIBER_STACKSIZE,
helper_fiber, 0, 0, HELPER_FIBER_PRIORITY, 0);
TC_PRINT("Helper fiber started: id = %p\n", helper_fiber_id);
TC_PRINT("Test thread started: id = %p\n", test_thread_id);
/* Activate test_fiber */
nano_task_sem_give(&test_fiber_sem);
helper_thread_id = k_thread_spawn(helper_thread_stack, THREAD_STACK,
(k_thread_entry_t) helper_thread, 0, 0, NULL,
HELPER_THREAD_PRIORITY, 0, 0);
/* Wait for test_fiber to activate us */
nano_task_sem_take(&task_sem, TICKS_UNLIMITED);
TC_PRINT("Helper thread started: id = %p\n", helper_thread_id);
/* Activate test_thread */
k_sem_give(&test_thread_sem);
/* Wait for test_thread to activate us */
k_sem_take(&task_sem, K_FOREVER);
/* Wake the test fiber */
task_fiber_wakeup(test_fiber_id);
k_wakeup(test_thread_id);
if (test_failure) {
goto done_tests;
}
TC_PRINT("Testing nanokernel task_sleep()\n");
TC_PRINT("Testing kernel k_sleep()\n");
align_to_tick_boundary();
start_tick = sys_tick_get_32();
task_sleep(ONE_SECOND);
end_tick = sys_tick_get_32();
start_tick = k_uptime_get_32();
/* FIXME: one tick less to account for
* one extra tick for _TICK_ALIGN in k_sleep*/
k_sleep(ONE_SECOND - TICKS_PER_MS);
end_tick = k_uptime_get_32();
if (!sleep_time_valid(start_tick, end_tick, ONE_SECOND)) {
TC_ERROR("task_sleep() slept for %d ticks, not %d\n",
end_tick - start_tick, ONE_SECOND);
TC_ERROR("k_sleep() slept for %d ticks, not %d\n",
end_tick - start_tick, ONE_SECOND);
goto done_tests;
}