4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
33 * \brief Test kernel preemption.
35 * This testcase spawns TASKS parallel threads that runs for TIME seconds. They
36 * continuously spin updating a global counter (one counter for each thread).
38 * At exit each thread checks if the others have been che chance to update
39 * their own counter. If not, it means the preemption didn't occur and the
40 * testcase returns an error message.
42 * Otherwise, if all the threads have been able to update their own counter it
43 * means preemption successfully occurs, since there is no active sleep inside
44 * each thread's implementation.
46 * \author Andrea Righi <arighi@develer.com>
48 * $test$: cp bertos/cfg/cfg_proc.h $cfgdir/
49 * $test$: echo "#undef CONFIG_KERN" >> $cfgdir/cfg_proc.h
50 * $test$: echo "#define CONFIG_KERN 1" >> $cfgdir/cfg_proc.h
51 * $test$: echo "#undef CONFIG_KERN_PRI" >> $cfgdir/cfg_proc.h
52 * $test$: echo "#define CONFIG_KERN_PRI 1" >> $cfgdir/cfg_proc.h
53 * $test$: echo "#undef CONFIG_KERN_PREEMPT" >> $cfgdir/cfg_proc.h
54 * $test$: echo "#define CONFIG_KERN_PREEMPT 1" >> $cfgdir/cfg_proc.h
55 * $test$: cp bertos/cfg/cfg_monitor.h $cfgdir/
56 * $test$: sed -i "s/CONFIG_KERN_MONITOR 0/CONFIG_KERN_MONITOR 1/" $cfgdir/cfg_monitor.h
57 * $test$: cp bertos/cfg/cfg_signal.h $cfgdir/
58 * $test$: echo "#undef CONFIG_KERN_SIGNALS" >> $cfgdir/cfg_signal.h
59 * $test$: echo "#define CONFIG_KERN_SIGNALS 1" >> $cfgdir/cfg_signal.h
63 #include <stdio.h> // sprintf
64 #include <string.h> // memset
66 #include <kern/proc.h>
68 #include <kern/monitor.h>
70 #include <drv/timer.h>
72 #include <cfg/cfg_proc.h>
80 /* Number of tasks to spawn */
83 static char name[TASKS][32];
85 static unsigned int done[TASKS];
87 #define WORKER_STACK_SIZE KERN_MINSTACKSIZE * 3
89 /* Base time delay for processes using timer_delay() */
92 // Define process stacks for test.
93 static PROC_DEFINE_STACK(worker_stack, TASKS * WORKER_STACK_SIZE);
95 static int prime_numbers[] =
97 1, 3, 5, 7, 11, 13, 17, 19,
98 23, 29, 31, 37, 41, 43, 47, 53,
101 STATIC_ASSERT(TASKS <= countof(prime_numbers));
103 static void worker(void)
105 ssize_t pid = (ssize_t)proc_currentUserData();
106 long tot = prime_numbers[pid - 1];
107 unsigned int my_count = 0;
110 for (i = 0; i < tot; i++)
113 PROC_ATOMIC(kprintf("> %s[%zd] running\n", __func__, pid));
114 timer_delay(tot * DELAY);
117 PROC_ATOMIC(kprintf("> %s[%zd] completed\n", __func__, pid));
120 static int worker_test(void)
124 // Init the test processes
125 kputs("Run Proc test..\n");
126 for (i = 0; i < TASKS; i++)
129 snprintf(&name[i][0], sizeof(name[i]), "worker_%zd", i + 1);
130 name[i][sizeof(name) - 1] = '\0';
131 proc_new_with_name(name[i], worker, (iptr_t)(i + 1),
133 (cpu_stack_t *)((size_t)&worker_stack + WORKER_STACK_SIZE * i));
135 kputs("> Main: Processes started\n");
138 for (i = 0; i < TASKS; i++)
148 kputs("> Main: process test finished..ok!\n");
152 #if CONFIG_KERN_PREEMPT
153 /* Time to run each preemptible thread (in seconds) */
156 static cpu_atomic_t barrier[TASKS];
157 static cpu_atomic_t main_barrier;
159 static unsigned int preempt_counter[TASKS];
160 static unsigned int preempt_done[TASKS];
162 static void preempt_worker(void)
164 ssize_t pid = (ssize_t)proc_currentUserData();
165 unsigned int *my_count = &preempt_counter[pid - 1];
169 barrier[pid - 1] = 1;
170 /* Synchronize on the main barrier */
171 while (!main_barrier)
173 PROC_ATOMIC(kprintf("> %s[%zd] running\n", __func__, pid));
174 start = timer_clock();
175 stop = ms_to_ticks(TIME * 1000);
176 while (timer_clock() - start < stop)
178 IRQ_ASSERT_ENABLED();
180 /* be sure to wrap to a value different than 0 */
181 if (UNLIKELY(*my_count == (unsigned int)~0))
184 PROC_ATOMIC(kprintf("> %s[%zd] completed: (counter = %d)\n",
185 __func__, pid, *my_count));
186 for (i = 0; i < TASKS; i++)
187 if (!preempt_counter[i])
189 preempt_done[pid - 1] = TEST_FAIL;
192 preempt_done[pid - 1] = TEST_OK;
195 static int preempt_worker_test(void)
197 unsigned long score = 0;
200 // Init the test processes
201 kputs("Run Preemption test..\n");
202 for (i = 0; i < TASKS; i++)
205 snprintf(&name[i][0], sizeof(name[i]),
206 "preempt_worker_%zd", i + 1);
207 name[i][sizeof(name) - 1] = '\0';
208 proc_new_with_name(name[i], preempt_worker, (iptr_t)(i + 1),
210 (cpu_stack_t *)((size_t)&worker_stack + WORKER_STACK_SIZE * i));
212 kputs("> Main: Processes created\n");
213 /* Synchronize on start */
216 for (i = 0; i < TASKS; i++)
223 /* Now all threads have been created, start them all */
226 kputs("> Main: Processes started\n");
229 for (i = 0; i < TASKS; i++)
231 if (!preempt_done[i])
233 else if (preempt_done[i] == TEST_FAIL)
235 kputs("> Main: process test finished..fail!\n");
244 for (i = 0; i < TASKS; i++)
245 score += preempt_counter[i];
246 kputs("> Main: process test finished..ok!\n");
247 kprintf("> Score: %lu\n", score);
250 #endif /* CONFIG_KERN_PREEMPT */
252 #if CONFIG_KERN_SIGNALS & CONFIG_KERN_PRI
254 #define PROC_PRI_TEST_STACK(num) PROC_DEFINE_STACK(proc_test##num##_stack, KERN_MINSTACKSIZE);
256 // Define params to test priority
257 #define PROC_PRI_TEST(num) static void proc_pri_test##num(void) \
259 struct Process *main_proc = (struct Process *) proc_currentUserData(); \
260 kputs("> Process: " #num "\n"); \
261 sig_signal(main_proc, SIG_USER##num); \
264 // Default priority is 0
265 #define PROC_PRI_TEST_INIT(num, proc) \
267 struct Process *p = proc_new(proc_pri_test##num, (proc), \
268 sizeof(proc_test##num##_stack), \
269 proc_test##num##_stack); \
270 proc_setPri(p, num + 1); \
273 PROC_PRI_TEST_STACK(0)
274 PROC_PRI_TEST_STACK(1)
275 PROC_PRI_TEST_STACK(2)
281 static int prio_worker_test(void)
283 struct Process *curr = proc_current();
284 int orig_pri = curr->link.pri;
287 // test process priority
288 // main process must have the higher priority to check signals received
289 proc_setPri(proc_current(), 10);
291 kputs("Run Priority test..\n");
292 // the order in which the processes are created is important!
293 PROC_PRI_TEST_INIT(0, curr);
294 PROC_PRI_TEST_INIT(1, curr);
295 PROC_PRI_TEST_INIT(2, curr);
297 // signals must be: USER2, 1, 0 in order
298 sigmask_t signals = sig_wait(SIG_USER0 | SIG_USER1 | SIG_USER2);
299 if (!(signals & SIG_USER2))
304 signals = sig_wait(SIG_USER0 | SIG_USER1 | SIG_USER2);
305 if (!(signals & SIG_USER1))
310 signals = sig_wait(SIG_USER0 | SIG_USER1 | SIG_USER2);
311 if (!(signals & SIG_USER0))
316 // All processes must have quit by now, but just in case...
317 signals = sig_waitTimeout(SIG_USER0 | SIG_USER1 | SIG_USER2, 200);
318 if (signals & (SIG_USER0 | SIG_USER1 | SIG_USER2))
323 if (signals & SIG_TIMEOUT)
325 kputs("Priority test successfull.\n");
328 proc_setPri(proc_current(), orig_pri);
330 kputs("Priority test failed.\n");
333 #endif /* CONFIG_KERN_SIGNALS & CONFIG_KERN_PRI */
336 * Process scheduling test
338 int proc_testRun(void)
340 #if CONFIG_KERN_PREEMPT
341 // Clear shared data (this is needed when this testcase is embedded in
342 // the demo application).
343 memset(preempt_counter, 0, sizeof(preempt_counter));
344 memset(preempt_done, 0, sizeof(preempt_done));
345 memset(barrier, 0, sizeof(barrier));
347 #endif /* CONFIG_KERN_PREEMPT */
348 memset(done, 0, sizeof(done));
352 #if CONFIG_KERN_PREEMPT
353 preempt_worker_test();
354 #endif /* CONFIG_KERN_PREEMPT */
355 #if CONFIG_KERN_SIGNALS & CONFIG_KERN_PRI
357 #endif /* CONFIG_KERN_SIGNALS & CONFIG_KERN_PRI */
361 int proc_testSetup(void)
365 kprintf("Init Timer..");
369 kprintf("Init Process..");
376 int proc_testTearDown(void)
378 kputs("TearDown Process test.\n");