Skip to content

Commit 02aa980

Browse files
Andy Rossnashif
authored andcommitted
tests: Add kernel/sched/deadline test for EDF validation
Simple test for CONFIG_SCHED_DEADLINE. It creates a bunch of threads at randome deadlines but within the same priority, and validates that they run in the correct order. Signed-off-by: Andy Ross <[email protected]>
1 parent bfca8b6 commit 02aa980

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.8.2)
2+
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
3+
project(NONE)
4+
5+
FILE(GLOB app_sources src/*.c)
6+
target_sources(app PRIVATE ${app_sources})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_MP_NUM_CPUS=1
3+
CONFIG_TEST_RANDOM_GENERATOR=y
4+
CONFIG_SCHED_DEADLINE=y
5+
6+
# Deadline is not compatible with MULTIQ, so we have to pick something
7+
# specific instead of using the board-level default.
8+
CONFIG_SCHED_DUMB=y
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright (c) 2018 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <zephyr.h>
7+
#include <ztest.h>
8+
9+
#define NUM_THREADS 8
10+
#define STACK_SIZE (256 + CONFIG_TEST_EXTRA_STACKSIZE)
11+
12+
struct k_thread worker_threads[NUM_THREADS];
13+
14+
K_THREAD_STACK_ARRAY_DEFINE(worker_stacks, NUM_THREADS, STACK_SIZE);
15+
16+
int thread_deadlines[NUM_THREADS];
17+
18+
/* The number of worker threads that ran, and and array of their
19+
* indices in execution order
20+
*/
21+
int n_exec;
22+
int exec_order[NUM_THREADS];
23+
24+
void worker(void *p1, void *p2, void *p3)
25+
{
26+
int tidx = (int) p1;
27+
28+
ARG_UNUSED(p2);
29+
ARG_UNUSED(p3);
30+
31+
zassert_true(tidx >= 0 && tidx < NUM_THREADS, "");
32+
zassert_true(n_exec >= 0 && n_exec < NUM_THREADS, "");
33+
34+
exec_order[n_exec++] = tidx;
35+
36+
/* Sleep, don't exit. It's not implausible that some
37+
* platforms implement a thread-based cleanup step for threads
38+
* that exit (pthreads does this already) which might muck
39+
* with the scheduling.
40+
*/
41+
while (1) {
42+
k_sleep(1000000);
43+
}
44+
}
45+
46+
void test_deadline(void)
47+
{
48+
int i;
49+
50+
/* Create a bunch of threads at a single lower priority. Give
51+
* them each a random deadline. Sleep, and check that they
52+
* were executed in the right order.
53+
*/
54+
for (i = 0; i < NUM_THREADS; i++) {
55+
k_thread_create(&worker_threads[i],
56+
worker_stacks[i], STACK_SIZE,
57+
worker, (void *)i, NULL, NULL,
58+
K_LOWEST_APPLICATION_THREAD_PRIO,
59+
0, 0);
60+
61+
/* Positive-definite number with the bottom 8 bits
62+
* masked off to prevent aliasing where "very close"
63+
* deadlines end up in the opposite order due to the
64+
* changing "now" between calls to
65+
* k_thread_deadline_set().
66+
*/
67+
thread_deadlines[i] = sys_rand32_get() & 0x7fffff00;
68+
}
69+
70+
zassert_true(n_exec == 0, "threads ran too soon");
71+
72+
/* Similarly do the deadline setting in one quick pass to
73+
* minimize aliasing with "now"
74+
*/
75+
for (i = 0; i < NUM_THREADS; i++) {
76+
k_thread_deadline_set(&worker_threads[i], thread_deadlines[i]);
77+
}
78+
79+
zassert_true(n_exec == 0, "threads ran too soon");
80+
81+
k_sleep(100);
82+
83+
zassert_true(n_exec == NUM_THREADS, "not enough threads ran");
84+
85+
for (i = 1; i < NUM_THREADS; i++) {
86+
int d0 = thread_deadlines[exec_order[i-1]];
87+
int d1 = thread_deadlines[exec_order[i]];
88+
89+
zassert_true(d0 <= d1, "threads ran in wrong order");
90+
}
91+
}
92+
93+
void test_main(void)
94+
{
95+
ztest_test_suite(suite_deadline,
96+
ztest_unit_test(test_deadline));
97+
ztest_run_test_suite(suite_deadline);
98+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
tests:
2+
kernel.sched.deadline:
3+
tags: core

0 commit comments

Comments
 (0)