ad30f711153ca120e232b18336e5afd300555d9f
[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 2008 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  *
33  * \brief Test kernel process.
34  *
35  * \version $Id$
36  * \author Daniele Basile <asterix@develer.com>
37  *
38  * $test$: cp bertos/cfg/cfg_proc.h $cfgdir/
39  * $test$: echo  "#undef CONFIG_KERN" >> $cfgdir/cfg_proc.h
40  * $test$: echo "#define CONFIG_KERN 1" >> $cfgdir/cfg_proc.h
41  * $test$: echo  "#undef CONFIG_KERN_PRI" >> $cfgdir/cfg_proc.h
42  * $test$: echo "#define CONFIG_KERN_PRI 1" >> $cfgdir/cfg_proc.h
43  * $test$: cp bertos/cfg/cfg_monitor.h $cfgdir/
44  * $test$: echo  "#undef CONFIG_KERN_MONITOR" >> $cfgdir/cfg_monitor.h
45  * $test$: echo "#define CONFIG_KERN_MONITOR 1" >> $cfgdir/cfg_monitor.h
46  * $test$: cp bertos/cfg/cfg_signal.h $cfgdir/
47  * $test$: echo  "#undef CONFIG_KERN_SIGNALS" >> $cfgdir/cfg_signal.h
48  * $test$: echo "#define CONFIG_KERN_SIGNALS 1" >> $cfgdir/cfg_signal.h
49  */
50
51 #include <kern/proc.h>
52 #include <kern/irq.h>
53 #include <kern/monitor.h>
54
55 #include <drv/timer.h>
56 #include <cfg/test.h>
57
58
59 // Global settings for the test.
60 #define DELAY                           5
61
62 // Settings for the test process.
63 //Process 1
64 #define INC_PROC_T1                     1
65 #define DELAY_PROC_T1   INC_PROC_T1*DELAY
66 //Process 2
67 #define INC_PROC_T2                     3
68 #define DELAY_PROC_T2   INC_PROC_T2*DELAY
69 //Process 3
70 #define INC_PROC_T3                     5
71 #define DELAY_PROC_T3   INC_PROC_T3*DELAY
72 //Process 4
73 #define INC_PROC_T4                     7
74 #define DELAY_PROC_T4   INC_PROC_T4*DELAY
75 //Process 5
76 #define INC_PROC_T5                    11
77 #define DELAY_PROC_T5   INC_PROC_T5*DELAY
78 //Process 6
79 #define INC_PROC_T6                    13
80 #define DELAY_PROC_T6   INC_PROC_T6*DELAY
81 //Process 7
82 #define INC_PROC_T7                    17
83 #define DELAY_PROC_T7   INC_PROC_T7*DELAY
84 //Process 8
85 #define INC_PROC_T8                    19
86 #define DELAY_PROC_T8   INC_PROC_T8*DELAY
87
88 //Global count for each process.
89 unsigned int t1_count = 0;
90 unsigned int t2_count = 0;
91 unsigned int t3_count = 0;
92 unsigned int t4_count = 0;
93 unsigned int t5_count = 0;
94 unsigned int t6_count = 0;
95 unsigned int t7_count = 0;
96 unsigned int t8_count = 0;
97
98 /*
99  * These macros generate the code needed to create the test process functions.
100  */
101 #define PROC_TEST(num) static void proc_test##num(void) \
102 { \
103         for (int i = 0; i < INC_PROC_T##num; ++i) \
104         { \
105                 t##num##_count++; \
106                 kputs("> Process[" #num "]\n"); \
107                 timer_delay(DELAY_PROC_T##num); \
108         } \
109 }
110
111 #define PROC_TEST_STACK(num)  PROC_DEFINE_STACK(proc_test##num##_stack, KERN_MINSTACKSIZE);
112 #define PROC_TEST_INIT(num)   proc_new(proc_test##num, NULL, sizeof(proc_test##num##_stack), proc_test##num##_stack);
113
114 // Define process
115 PROC_TEST(1)
116 PROC_TEST(2)
117 PROC_TEST(3)
118 PROC_TEST(4)
119 PROC_TEST(5)
120 PROC_TEST(6)
121 PROC_TEST(7)
122 PROC_TEST(8)
123
124 // Define process stacks for test.
125 PROC_TEST_STACK(1)
126 PROC_TEST_STACK(2)
127 PROC_TEST_STACK(3)
128 PROC_TEST_STACK(4)
129 PROC_TEST_STACK(5)
130 PROC_TEST_STACK(6)
131 PROC_TEST_STACK(7)
132 PROC_TEST_STACK(8)
133
134 // Define params to test priority
135 #define PROC_PRI_TEST(num) static void proc_pri_test##num(void) \
136 { \
137         struct Process *main_proc = (struct Process *) proc_currentUserData(); \
138         kputs("> Process: " #num "\n"); \
139         sig_signal(main_proc, SIG_USER##num); \
140 }
141
142 // Default priority is 0
143 #define PROC_PRI_TEST_INIT(num, proc)  \
144 do { \
145         struct Process *p = proc_new(proc_pri_test##num, (proc), sizeof(proc_test##num##_stack), proc_test##num##_stack); \
146         proc_setPri(p, num + 1); \
147 } while (0)
148
149 PROC_TEST_STACK(0)
150 PROC_PRI_TEST(0)
151 PROC_PRI_TEST(1)
152 PROC_PRI_TEST(2)
153
154
155 /**
156  * Process scheduling test
157  */
158 int proc_testRun(void)
159 {
160         int ret_value = 0;
161         kprintf("Run Process test..\n");
162
163         //Init the process tests
164         PROC_TEST_INIT(1)
165         PROC_TEST_INIT(2)
166         PROC_TEST_INIT(3)
167         PROC_TEST_INIT(4)
168         PROC_TEST_INIT(5)
169         PROC_TEST_INIT(6)
170         PROC_TEST_INIT(7)
171         PROC_TEST_INIT(8)
172         kputs("> Main: Processes created\n");
173
174         for (int i = 0; i < 30; ++i)
175         {
176                 kputs("> Main\n");
177                 timer_delay(93);
178                 monitor_report();
179         }
180
181         if( t1_count == INC_PROC_T1 &&
182                 t2_count == INC_PROC_T2 &&
183                 t3_count == INC_PROC_T3 &&
184                 t4_count == INC_PROC_T4 &&
185                 t5_count == INC_PROC_T5 &&
186                 t6_count == INC_PROC_T6 &&
187                 t7_count == INC_PROC_T7 &&
188                 t8_count == INC_PROC_T8)
189         {
190                 kputs("> Main: process test finished..ok!\n");
191                 ret_value = 0;
192         }
193         else
194         {
195                 kputs("> Main: process test..fail!\n");
196                 ret_value = -1;
197         }
198
199 #if CONFIG_KERN_SIGNALS & CONFIG_KERN_PRI
200         // test process priority
201         // main process must have the higher priority to check signals received
202         proc_setPri(proc_current(), 10);
203
204         struct Process *curr = proc_current();
205         // the order in which the processes are created is important!
206         PROC_PRI_TEST_INIT(0, curr);
207         PROC_PRI_TEST_INIT(1, curr);
208         PROC_PRI_TEST_INIT(2, curr);
209
210         // signals must be: USER2, 1, 0 in order
211         sigmask_t signals = sig_wait(SIG_USER0 | SIG_USER1 | SIG_USER2);
212         if (!(signals & SIG_USER2))
213                 goto priority_fail;
214
215         signals = sig_wait(SIG_USER0 | SIG_USER1 | SIG_USER2);
216         if (!(signals & SIG_USER1))
217                 goto priority_fail;
218
219         signals = sig_wait(SIG_USER0 | SIG_USER1 | SIG_USER2);
220         if (!(signals & SIG_USER0))
221                 goto priority_fail;
222
223         // All processes must have quit by now, but just in case...
224         signals = sig_waitTimeout(SIG_USER0 | SIG_USER1 | SIG_USER2, 200);
225         if (signals & (SIG_USER0 | SIG_USER1 | SIG_USER2))
226                 goto priority_fail;
227
228         if (signals & SIG_TIMEOUT)
229         {
230                 kputs("Priority test successfull.\n");
231         }
232
233         return ret_value;
234
235 priority_fail:
236         kputs("Priority test failed.\n");
237         return -1;
238
239 #endif
240
241         return ret_value;
242
243 }
244
245 int proc_testSetup(void)
246 {
247         kdbg_init();
248
249         #if CONFIG_KERN_PREEMPT
250                 kprintf("Init Interrupt (preempt mode)..");
251                 irq_init();
252                 kprintf("Done.\n");
253         #endif
254
255         kprintf("Init Timer..");
256         timer_init();
257         kprintf("Done.\n");
258
259         kprintf("Init Process..");
260         proc_init();
261         kprintf("Done.\n");
262
263         return 0;
264 }
265
266 int proc_testTearDown(void)
267 {
268         kputs("TearDown Process test.\n");
269         return 0;
270 }
271
272 TEST_MAIN(proc);