Merge branch "preempt" in "trunk".
[bertos.git] / bertos / kern / proc_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  *
33  * \brief Test kernel preemption.
34  *
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).
37  *
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.
41  *
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.
45  *
46  * \author Andrea Righi <arighi@develer.com>
47  *
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
60  *
61  */
62
63 #include <stdio.h> // sprintf
64 #include <string.h> // memset
65
66 #include <kern/proc.h>
67 #include <kern/irq.h>
68 #include <kern/monitor.h>
69
70 #include <drv/timer.h>
71 #include <cfg/test.h>
72 #include <cfg/cfg_proc.h>
73
74 enum
75 {
76         TEST_OK = 1,
77         TEST_FAIL = 2,
78 };
79
80 /* Number of tasks to spawn */
81 #define TASKS   8
82
83 static char name[TASKS][32];
84
85 static unsigned int done[TASKS];
86
87 #define WORKER_STACK_SIZE KERN_MINSTACKSIZE * 3
88
89 /* Base time delay for processes using timer_delay() */
90 #define DELAY   5
91
92 // Define process stacks for test.
93 static cpu_stack_t worker_stack[TASKS][WORKER_STACK_SIZE / sizeof(cpu_stack_t)];
94
95 static int prime_numbers[] =
96 {
97         1, 3, 5, 7, 11, 13, 17, 19,
98         23, 29, 31, 37, 41, 43, 47, 53,
99 };
100
101 STATIC_ASSERT(TASKS <= countof(prime_numbers));
102
103 static void worker(void)
104 {
105         long pid = (long)proc_currentUserData();
106         long tot = prime_numbers[pid - 1];
107         unsigned int my_count = 0;
108         int i;
109
110         for (i = 0; i < tot; i++)
111         {
112                 my_count++;
113                 PROC_ATOMIC(kprintf("> %s[%ld] running\n", __func__, pid));
114                 timer_delay(tot * DELAY);
115         }
116         done[pid - 1] = 1;
117         PROC_ATOMIC(kprintf("> %s[%ld] completed\n", __func__, pid));
118 }
119
120 static int worker_test(void)
121 {
122         long i;
123
124         // Init the test processes
125         kputs("Run Proc test..\n");
126         for (i = 0; i < TASKS; i++)
127         {
128                 sprintf(&name[i][0], "worker_%ld", i + 1);
129                 proc_new_with_name(name[i], worker, (iptr_t)(i + 1),
130                                 WORKER_STACK_SIZE, &worker_stack[i][0]);
131         }
132         kputs("> Main: Processes started\n");
133         while (1)
134         {
135                 for (i = 0; i < TASKS; i++)
136                 {
137                         if (!done[i])
138                                 break;
139                 }
140                 if (i == TASKS)
141                         break;
142                 monitor_report();
143                 timer_delay(93);
144         }
145         kputs("> Main: process test finished..ok!\n");
146         return 0;
147 }
148
149 #if CONFIG_KERN_PREEMPT
150 /* Time to run each preemptible thread (in seconds) */
151 #define TIME    10
152
153 static char preempt_name[TASKS][32];
154
155 static cpu_atomic_t barrier[TASKS];
156 static cpu_atomic_t main_barrier;
157
158 static unsigned int preempt_counter[TASKS];
159 static unsigned int preempt_done[TASKS];
160
161 static cpu_stack_t preempt_worker_stack[TASKS][WORKER_STACK_SIZE / sizeof(cpu_stack_t)];
162
163 static void preempt_worker(void)
164 {
165         long pid = (long)proc_currentUserData();
166         unsigned int *my_count = &preempt_counter[pid - 1];
167         ticks_t start, stop;
168         int i;
169
170         barrier[pid - 1] = 1;
171         /* Synchronize on the main barrier */
172         while (!main_barrier)
173                 proc_yield();
174         PROC_ATOMIC(kprintf("> %s[%ld] running\n", __func__, pid));
175         start = timer_clock();
176         stop  = ms_to_ticks(TIME * 1000);
177         while (timer_clock() - start < stop)
178         {
179                 IRQ_ASSERT_ENABLED();
180                 (*my_count)++;
181                 /* be sure to wrap to a value different than 0 */
182                 if (UNLIKELY(*my_count == (unsigned int)~0))
183                         *my_count = 1;
184         }
185         PROC_ATOMIC(kprintf("> %s[%ld] completed: (counter = %d)\n",
186                                 __func__, pid, *my_count));
187         for (i = 0; i < TASKS; i++)
188                 if (!preempt_counter[i])
189                 {
190                         preempt_done[pid - 1] = TEST_FAIL;
191                         return;
192                 }
193         preempt_done[pid - 1] = TEST_OK;
194 }
195
196 static int preempt_worker_test(void)
197 {
198         unsigned long score = 0;
199         long i;
200
201         // Init the test processes
202         kputs("Run Preemption test..\n");
203         for (i = 0; i < TASKS; i++)
204         {
205                 sprintf(&preempt_name[i][0], "preempt_worker_%ld", i + 1);
206                 proc_new_with_name(preempt_name[i], preempt_worker, (iptr_t)(i + 1),
207                                 WORKER_STACK_SIZE, &preempt_worker_stack[i][0]);
208         }
209         kputs("> Main: Processes created\n");
210         /* Synchronize on start */
211         while (1)
212         {
213                 for (i = 0; i < TASKS; i++)
214                         if (!barrier[i])
215                                 break;
216                 if (i == TASKS)
217                         break;
218                 proc_yield();
219         }
220         /* Now all threads have been created, start them all */
221         main_barrier = 1;
222         MEMORY_BARRIER;
223         kputs("> Main: Processes started\n");
224         while (1)
225         {
226                 for (i = 0; i < TASKS; i++)
227                 {
228                         if (!preempt_done[i])
229                                 break;
230                         else if (preempt_done[i] == TEST_FAIL)
231                         {
232                                 kputs("> Main: process test finished..fail!\n");
233                                 return -1;
234                         }
235                 }
236                 if (i == TASKS)
237                         break;
238                 monitor_report();
239                 timer_delay(1000);
240         }
241         for (i = 0; i < TASKS; i++)
242                 score += preempt_counter[i];
243         kputs("> Main: process test finished..ok!\n");
244         kprintf("> Score: %lu\n", score);
245         return 0;
246 }
247 #endif /* CONFIG_KERN_PREEMPT */
248
249 #if CONFIG_KERN_SIGNALS & CONFIG_KERN_PRI
250
251 #define PROC_PRI_TEST_STACK(num)  PROC_DEFINE_STACK(proc_test##num##_stack, KERN_MINSTACKSIZE);
252
253 // Define params to test priority
254 #define PROC_PRI_TEST(num) static void proc_pri_test##num(void) \
255 { \
256         struct Process *main_proc = (struct Process *) proc_currentUserData(); \
257         kputs("> Process: " #num "\n"); \
258         sig_signal(main_proc, SIG_USER##num); \
259 }
260
261 // Default priority is 0
262 #define PROC_PRI_TEST_INIT(num, proc)                                   \
263 do {                                                                    \
264         struct Process *p = proc_new(proc_pri_test##num, (proc),        \
265                                         sizeof(proc_test##num##_stack), \
266                                         proc_test##num##_stack);        \
267         proc_setPri(p, num + 1);                                        \
268 } while (0)
269
270 PROC_PRI_TEST_STACK(0)
271 PROC_PRI_TEST_STACK(1)
272 PROC_PRI_TEST_STACK(2)
273
274 PROC_PRI_TEST(0)
275 PROC_PRI_TEST(1)
276 PROC_PRI_TEST(2)
277
278 static int prio_worker_test(void)
279 {
280         struct Process *curr = proc_current();
281         int orig_pri = curr->link.pri;
282         int ret = 0;
283
284         // test process priority
285         // main process must have the higher priority to check signals received
286         proc_setPri(proc_current(), 10);
287
288         kputs("Run Priority test..\n");
289         // the order in which the processes are created is important!
290         PROC_PRI_TEST_INIT(0, curr);
291         PROC_PRI_TEST_INIT(1, curr);
292         PROC_PRI_TEST_INIT(2, curr);
293
294         // signals must be: USER2, 1, 0 in order
295         sigmask_t signals = sig_wait(SIG_USER0 | SIG_USER1 | SIG_USER2);
296         if (!(signals & SIG_USER2))
297         {
298                 ret = -1;
299                 goto out;
300         }
301         signals = sig_wait(SIG_USER0 | SIG_USER1 | SIG_USER2);
302         if (!(signals & SIG_USER1))
303         {
304                 ret = -1;
305                 goto out;
306         }
307         signals = sig_wait(SIG_USER0 | SIG_USER1 | SIG_USER2);
308         if (!(signals & SIG_USER0))
309         {
310                 ret = -1;
311                 goto out;
312         }
313         // All processes must have quit by now, but just in case...
314         signals = sig_waitTimeout(SIG_USER0 | SIG_USER1 | SIG_USER2, 200);
315         if (signals & (SIG_USER0 | SIG_USER1 | SIG_USER2))
316         {
317                 ret = -1;
318                 goto out;
319         }
320         if (signals & SIG_TIMEOUT)
321         {
322                 kputs("Priority test successfull.\n");
323         }
324 out:
325         proc_setPri(proc_current(), orig_pri);
326         if (ret != 0)
327                 kputs("Priority test failed.\n");
328         return ret;
329 }
330 #endif /* CONFIG_KERN_SIGNALS & CONFIG_KERN_PRI */
331
332 /**
333  * Process scheduling test
334  */
335 int proc_testRun(void)
336 {
337 #if CONFIG_KERN_PREEMPT
338         // Clear shared data (this is needed when this testcase is embedded in
339         // the demo application).
340         memset(preempt_counter, 0, sizeof(preempt_counter));
341         memset(preempt_done, 0, sizeof(preempt_done));
342         memset(barrier, 0, sizeof(barrier));
343         main_barrier = 0;
344 #endif /* CONFIG_KERN_PREEMPT */
345         memset(done, 0, sizeof(done));
346
347         /* Start tests */
348         worker_test();
349 #if CONFIG_KERN_PREEMPT
350         preempt_worker_test();
351 #endif /* CONFIG_KERN_PREEMPT */
352 #if CONFIG_KERN_SIGNALS & CONFIG_KERN_PRI
353         prio_worker_test();
354 #endif /* CONFIG_KERN_SIGNALS & CONFIG_KERN_PRI */
355         return 0;
356 }
357
358 int proc_testSetup(void)
359 {
360         kdbg_init();
361
362         kprintf("Init Timer..");
363         timer_init();
364         kprintf("Done.\n");
365
366         kprintf("Init Process..");
367         proc_init();
368         kprintf("Done.\n");
369
370         return 0;
371 }
372
373 int proc_testTearDown(void)
374 {
375         kputs("TearDown Process test.\n");
376         return 0;
377 }
378
379 TEST_MAIN(proc);