Add a priority inversion test for Semaphores.
[bertos.git] / bertos / kern / sem_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  * \brief Semaphore test.
33  *
34  * For testing priority inversion (avoidance) a set of processes
35  * interacting among each others by means of two semaphores are
36  * disturbed by an unrelated process, i.e., a process not using
37  * any semaphore at all.
38  *
39  * In case of priority inversion, high priority processes
40  * are affected (delayed!) by such process, even it has lower
41  * priority, because of semaphores. On the other hand, when priority
42  * inheritance is enabled, non interacting low priority processes
43  * can't affect the execution of high priority ones.
44  *
45  * It all can be seen looking at the finishing time of the various
46  * processes involved in sem_inv_test (logged).
47  *
48  * Notice that priority inheritance makes sense iff priorities
49  * exist, so the whole test depends on CONFIG_KERN_PRI.
50  *
51  * \author Daniele Basile <asterix@develer.com>
52  * \author Stefano Fedrigo <aleph@develer.com>
53  * \author Dario Faggioli <raistlin@linux.it>
54  *
55  * $test$: cp bertos/cfg/cfg_proc.h $cfgdir/
56  * $test$: echo  "#undef CONFIG_KERN" >> $cfgdir/cfg_proc.h
57  * $test$: echo "#define CONFIG_KERN 1" >> $cfgdir/cfg_proc.h
58  * $test$: echo  "#undef CONFIG_KERN_PRI" >> $cfgdir/cfg_proc.h
59  * $test$: echo "#define CONFIG_KERN_PRI 1" >> $cfgdir/cfg_proc.h
60  * $test$: cp bertos/cfg/cfg_sem.h $cfgdir/
61  * $test$: echo  "#undef CONFIG_KERN_SEMAPHORES" >> $cfgdir/cfg_sem.h
62  * $test$: echo "#define CONFIG_KERN_SEMAPHORES 1" >> $cfgdir/cfg_sem.h
63  */
64
65 #include <cfg/debug.h>
66 #include <cfg/test.h>
67
68 #include <kern/sem.h>
69 #include <kern/proc.h>
70 #include <kern/irq.h>
71
72 #include <drv/timer.h>
73
74 // Global settings for the serialization test.
75 #define MAX_GLOBAL_COUNT             1024
76 #define TEST_TIME_OUT_MS             6000
77 #define DELAY                           5
78
79 // Settings for the test processes (serialization test).
80 //Process 1
81 #define INC_PROC_T1                     1
82 #define DELAY_PROC_T1   INC_PROC_T1*DELAY
83 //Process 2
84 #define INC_PROC_T2                     3
85 #define DELAY_PROC_T2   INC_PROC_T2*DELAY
86 //Process 3
87 #define INC_PROC_T3                     5
88 #define DELAY_PROC_T3   INC_PROC_T3*DELAY
89 //Process 4
90 #define INC_PROC_T4                     7
91 #define DELAY_PROC_T4   INC_PROC_T4*DELAY
92 //Process 5
93 #define INC_PROC_T5                    11
94 #define DELAY_PROC_T5   INC_PROC_T5*DELAY
95 //Process 6
96 #define INC_PROC_T6                    13
97 #define DELAY_PROC_T6   INC_PROC_T6*DELAY
98 //Process 7
99 #define INC_PROC_T7                    17
100 #define DELAY_PROC_T7   INC_PROC_T7*DELAY
101 //Process 8
102 #define INC_PROC_T8                    19
103 #define DELAY_PROC_T8   INC_PROC_T8*DELAY
104
105 Semaphore sem;
106 unsigned int global_count = 0;
107
108 /*
109  * These macros generate the code needed to create the test process functions.
110  */
111 #define PROC_TEST(num) static void proc_semTest##num(void) \
112 { \
113         unsigned int local_count = 0; \
114         \
115         for (int i = 0; i < INC_PROC_T##num; ++i) \
116         { \
117                 sem_obtain(&sem); \
118                 kprintf("> test%d: Obtain semaphore.\n", num); \
119                 local_count = global_count; \
120                 kprintf("> test%d: Read global count [%d]\n", num, local_count); \
121                 timer_delay(DELAY_PROC_T##num); \
122                 local_count += INC_PROC_T##num; \
123                 global_count = local_count; \
124                 kprintf("> test%d: Update count g[%d] l[%d]\n", num, global_count, local_count); \
125                 sem_release(&sem); \
126                 kprintf("> test%d: Relase semaphore.\n", num); \
127         } \
128 } \
129
130 #define PROC_TEST_STACK(num)  PROC_DEFINE_STACK(proc_sem_test##num##_stack, KERN_MINSTACKSIZE * 2)
131 #define PROC_TEST_INIT(num)   proc_new(proc_semTest##num, NULL, sizeof(proc_sem_test##num##_stack), proc_sem_test##num##_stack);
132
133 // Define processes for the serialization test.
134 PROC_TEST(1)
135 PROC_TEST(2)
136 PROC_TEST(3)
137 PROC_TEST(4)
138 PROC_TEST(5)
139 PROC_TEST(6)
140 PROC_TEST(7)
141 PROC_TEST(8)
142
143 #if CONFIG_KERN_PRI
144
145 // Global settings for the priority inversion test.
146 // 0.5 secs, enough for seeing the effects
147 #define BASETIME 500
148
149 Semaphore s1, s2;
150 unsigned int loops = 0; // For counting iterations
151 int finishing_time[8];
152
153 enum ProcType {NONE, S1, S2, S1S2};
154 /*
155  * Macros for the processes of the priority inversion test.
156  */
157 #define PROC_INV_TEST(num) static void proc_semInvTest##num(void) \
158 { \
159         ProcType p_type = (ProcType)((long) proc_currentUserData()); \
160         int mult = p_type == NONE ? 5 : 1; \
161         unsigned int i, local_count = 0; \
162         ticks_t start; \
163         \
164         kprintf("> test%d(%d): Start.\n", num, proc_current()->link.pri); \
165         finishing_time[num-1] = timer_clock(); \
166         \
167         if (p_type == S1 || p_type == S1S2) { \
168                 kprintf("> test%d(prio=%d): Obtain %p..\n", num, \
169                                 proc_current()->link.pri, &s1); \
170                 sem_obtain(&s1); \
171                 kprintf("> test%d(prio=%d): Obtained %p.\n", num, \
172                                 proc_current()->link.pri, &s1); \
173         } \
174         if (p_type == S2 || p_type == S1S2) { \
175                 kprintf("> test%d(prio=%d): Obtain %p..\n", num, \
176                                 proc_current()->link.pri, &s2); \
177                 sem_obtain(&s2); \
178                 kprintf("> test%d(prio=%d): Obtained %p.\n", num, \
179                                 proc_current()->link.pri, &s2); \
180         } \
181         \
182         start = timer_clock(); \
183         for (i = 0; i < loops * mult && (((unsigned)timer_clock()-start) <= loops*mult); i++) { \
184                 local_count++; \
185         } \
186         \
187         sem_obtain(&sem); \
188         global_count += local_count; \
189         kprintf("> test%d(prio=%d): global_count=%u..\n", num, \
190                         proc_current()->link.pri, global_count); \
191         sem_release(&sem); \
192         \
193         if (p_type == S2 || p_type == S1S2) { \
194                 kprintf("> test%d(prio=%d): Release %p..\n", num, \
195                                 proc_current()->link.pri, &s2); \
196                 sem_release(&s2); \
197                 kprintf("> test%d(prio=%d): %p Released.\n", num, \
198                                 proc_current()->link.pri, &s2); \
199         } \
200         if (p_type == S1 || p_type == S1S2) { \
201                 kprintf("> test%d(prio=%d): Release %p..\n", num, \
202                                 proc_current()->link.pri, &s1); \
203                 sem_release(&s1); \
204                 kprintf("> test%d(prio=%d): %p Released.\n", num, \
205                                 proc_current()->link.pri, &s1); \
206         } \
207         \
208         finishing_time[num-1] = timer_clock() - finishing_time[num-1]; \
209         kprintf("> test%d(prio=%d): Exit.\n", num, proc_current()->link.pri); \
210 } \
211
212 #define PROC_INV_TEST_INIT(num, pri, type) \
213 do { \
214         struct Process *p; \
215         \
216         timer_delay(10); \
217         p = proc_new(proc_semInvTest##num, \
218                         ((void*)type), sizeof(proc_sem_test##num##_stack), \
219                         proc_sem_test##num##_stack); \
220         proc_setPri(p, pri); \
221 } while (0) \
222
223 // Define processes for the priority inversion test.
224 PROC_INV_TEST(1)
225 PROC_INV_TEST(2)
226 PROC_INV_TEST(3)
227 PROC_INV_TEST(4)
228 PROC_INV_TEST(5)
229 PROC_INV_TEST(6)
230 PROC_INV_TEST(7)
231 PROC_INV_TEST(8)
232
233 #endif /* CONFIG_KERN_PRI */
234
235 // Define process stacks for both of the tests.
236 PROC_TEST_STACK(1)
237 PROC_TEST_STACK(2)
238 PROC_TEST_STACK(3)
239 PROC_TEST_STACK(4)
240 PROC_TEST_STACK(5)
241 PROC_TEST_STACK(6)
242 PROC_TEST_STACK(7)
243 PROC_TEST_STACK(8)
244
245 int sem_ser_test(void)
246 {
247         ticks_t start_time = timer_clock();
248
249         sem_init(&sem);
250         global_count = 0;
251
252         kprintf("Run semaphore serialization test..\n");
253
254         // Initialize the processes.
255         PROC_TEST_INIT(1)
256         PROC_TEST_INIT(2)
257         PROC_TEST_INIT(3)
258         PROC_TEST_INIT(4)
259         PROC_TEST_INIT(5)
260         PROC_TEST_INIT(6)
261         PROC_TEST_INIT(7)
262         PROC_TEST_INIT(8)
263         kputs("> Main: Processes created\n");
264
265         /*
266          * Wait until all processes exit, if something goes wrong we return an
267          * error after timeout_ms.
268          */
269         while((timer_clock() - start_time) < ms_to_ticks(TEST_TIME_OUT_MS))
270         {
271                 if (sem_attempt(&sem))
272                 {
273                         kputs("> Main: Check if test has finished..\n");
274                         if(global_count == MAX_GLOBAL_COUNT)
275                         {
276                                 kputs("> Main: Test Finished..Ok!\n");
277                                 return 0;
278                         }
279                         sem_release(&sem);
280                         kputs("> Main: Test is still running..\n");
281                 }
282                 proc_yield();
283         }
284
285         kputs("Semaphore serialization test failed..\n");
286         return -1;
287 }
288
289 #if CONFIG_KERN_PRI
290
291 int sem_inv_test(void)
292 {
293         int i, orig_pri = proc_current()->link.pri;
294         ticks_t fake, start_time;
295
296         sem_init(&sem);
297         global_count = 0;
298         loops = 0;
299
300         sem_init(&s1);
301         sem_init(&s2);
302
303         kputs("> Main: calibration for the busy wait cycle..\n");
304         proc_setPri(proc_current(), 10);
305
306         fake = start_time = timer_clock();
307         while ((fake - start_time) < ms_to_ticks(BASETIME)) {
308                 fake = timer_clock();
309                 loops++;
310         }
311         kprintf("> Main: calibration done, %dms equals to %u cycles!\n", BASETIME, loops);
312
313         kputs("> Main: Run Priority Inversion test...\n");
314
315         // Will take s2
316         PROC_INV_TEST_INIT(1, 2, S2);
317
318         // 2 will block on s2; 3 will take s2 and still block on s2
319         PROC_INV_TEST_INIT(2, 3, S2);
320         PROC_INV_TEST_INIT(3, 4, S1S2);
321
322         // Will block on s1, nothing happens..
323         PROC_INV_TEST_INIT(4, 5, S1);
324
325         // No semaphore, without PI this will delay everyone!
326         PROC_INV_TEST_INIT(5, 6, NONE);
327
328         // Will block on s1 and boost
329         PROC_INV_TEST_INIT(6, 7, S1);
330         PROC_INV_TEST_INIT(7, 8, S1);
331         PROC_INV_TEST_INIT(8, 9, S1);
332
333         // All processes created, let them run.
334         proc_setPri(proc_current(), orig_pri);
335         while ((timer_clock() - start_time) < ms_to_ticks(TEST_TIME_OUT_MS*2)) {
336                 if (sem_attempt(&sem)) {
337                         if (global_count >= loops*7 + loops*5) {
338                                 for (i = 0; i < 8; i++)
339                                         kprintf("> Main: I-O latency of %d = %dms\n", i+1, ms_to_ticks(finishing_time[i]));
340                                 kputs("> Main: Test Finished..Ok!\n");
341                                 return 0;
342                         }
343                         sem_release(&sem);
344                 }
345                 proc_yield();
346         }
347
348         kputs("> Main: Priority Inversion Test failed..\n");
349         return -1;
350 }
351
352 #else
353
354 void sem_inv_test(void)
355 {
356 }
357
358 #endif /* CONFIG_KERN_PRI */
359
360 /**
361  * Run semaphore test
362  */
363 int sem_testRun(void)
364 {
365         /* Start tests */
366         sem_ser_test();         // Serialization
367         sem_inv_test();         // Priority Inversion
368
369         return 0;
370 }
371
372 int sem_testSetup(void)
373 {
374         kdbg_init();
375
376         kprintf("Init Timer..");
377         timer_init();
378         kprintf("Done.\n");
379
380         kprintf("Init Process..");
381         proc_init();
382         kprintf("Done.\n");
383
384         return 0;
385 }
386
387 int sem_testTearDown(void)
388 {
389         kputs("TearDown Semaphore test.\n");
390         return 0;
391 }
392
393 TEST_MAIN(sem);