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 2008 Develer S.r.l. (http://www.develer.com/)
32 * \brief Semaphore test.
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.
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.
45 * It all can be seen looking at the finishing time of the various
46 * processes involved in sem_inv_test (logged).
48 * Notice that priority inheritance makes sense iff priorities
49 * exist, so the whole test depends on CONFIG_KERN_PRI.
51 * \author Daniele Basile <asterix@develer.com>
52 * \author Stefano Fedrigo <aleph@develer.com>
53 * \author Dario Faggioli <raistlin@linux.it>
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
65 #include <cfg/debug.h>
69 #include <kern/proc.h>
72 #include <drv/timer.h>
74 // Global settings for the serialization test.
75 #define MAX_GLOBAL_COUNT 1024
76 #define TEST_TIME_OUT_MS 6000
79 // Settings for the test processes (serialization test).
82 #define DELAY_PROC_T1 INC_PROC_T1*DELAY
85 #define DELAY_PROC_T2 INC_PROC_T2*DELAY
88 #define DELAY_PROC_T3 INC_PROC_T3*DELAY
91 #define DELAY_PROC_T4 INC_PROC_T4*DELAY
93 #define INC_PROC_T5 11
94 #define DELAY_PROC_T5 INC_PROC_T5*DELAY
96 #define INC_PROC_T6 13
97 #define DELAY_PROC_T6 INC_PROC_T6*DELAY
99 #define INC_PROC_T7 17
100 #define DELAY_PROC_T7 INC_PROC_T7*DELAY
102 #define INC_PROC_T8 19
103 #define DELAY_PROC_T8 INC_PROC_T8*DELAY
106 unsigned int global_count = 0;
109 * These macros generate the code needed to create the test process functions.
111 #define PROC_TEST(num) static void proc_semTest##num(void) \
113 unsigned int local_count = 0; \
115 for (int i = 0; i < INC_PROC_T##num; ++i) \
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); \
126 kprintf("> test%d: Relase semaphore.\n", num); \
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);
133 // Define processes for the serialization test.
145 // Global settings for the priority inversion test.
146 // 0.5 secs, enough for seeing the effects
150 unsigned int loops = 0; // For counting iterations
151 int finishing_time[8];
153 enum ProcType {NONE, S1, S2, S1S2};
155 * Macros for the processes of the priority inversion test.
157 #define PROC_INV_TEST(num) static void proc_semInvTest##num(void) \
159 ProcType p_type = (ProcType)((long) proc_currentUserData()); \
160 int mult = p_type == NONE ? 5 : 1; \
161 unsigned int i, local_count = 0; \
164 kprintf("> test%d(%d): Start.\n", num, proc_current()->link.pri); \
165 finishing_time[num-1] = timer_clock(); \
167 if (p_type == S1 || p_type == S1S2) { \
168 kprintf("> test%d(prio=%d): Obtain %p..\n", num, \
169 proc_current()->link.pri, &s1); \
171 kprintf("> test%d(prio=%d): Obtained %p.\n", num, \
172 proc_current()->link.pri, &s1); \
174 if (p_type == S2 || p_type == S1S2) { \
175 kprintf("> test%d(prio=%d): Obtain %p..\n", num, \
176 proc_current()->link.pri, &s2); \
178 kprintf("> test%d(prio=%d): Obtained %p.\n", num, \
179 proc_current()->link.pri, &s2); \
182 start = timer_clock(); \
183 for (i = 0; i < loops * mult && (((unsigned)timer_clock()-start) <= loops*mult); i++) { \
188 global_count += local_count; \
189 kprintf("> test%d(prio=%d): global_count=%u..\n", num, \
190 proc_current()->link.pri, global_count); \
193 if (p_type == S2 || p_type == S1S2) { \
194 kprintf("> test%d(prio=%d): Release %p..\n", num, \
195 proc_current()->link.pri, &s2); \
197 kprintf("> test%d(prio=%d): %p Released.\n", num, \
198 proc_current()->link.pri, &s2); \
200 if (p_type == S1 || p_type == S1S2) { \
201 kprintf("> test%d(prio=%d): Release %p..\n", num, \
202 proc_current()->link.pri, &s1); \
204 kprintf("> test%d(prio=%d): %p Released.\n", num, \
205 proc_current()->link.pri, &s1); \
208 finishing_time[num-1] = timer_clock() - finishing_time[num-1]; \
209 kprintf("> test%d(prio=%d): Exit.\n", num, proc_current()->link.pri); \
212 #define PROC_INV_TEST_INIT(num, pri, type) \
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); \
223 // Define processes for the priority inversion test.
233 #endif /* CONFIG_KERN_PRI */
235 // Define process stacks for both of the tests.
245 int sem_ser_test(void)
247 ticks_t start_time = timer_clock();
252 kprintf("Run semaphore serialization test..\n");
254 // Initialize the processes.
263 kputs("> Main: Processes created\n");
266 * Wait until all processes exit, if something goes wrong we return an
267 * error after timeout_ms.
269 while((timer_clock() - start_time) < ms_to_ticks(TEST_TIME_OUT_MS))
271 if (sem_attempt(&sem))
273 kputs("> Main: Check if test has finished..\n");
274 if(global_count == MAX_GLOBAL_COUNT)
276 kputs("> Main: Test Finished..Ok!\n");
280 kputs("> Main: Test is still running..\n");
285 kputs("Semaphore serialization test failed..\n");
291 int sem_inv_test(void)
293 int i, orig_pri = proc_current()->link.pri;
294 ticks_t fake, start_time;
303 kputs("> Main: calibration for the busy wait cycle..\n");
304 proc_setPri(proc_current(), 10);
306 fake = start_time = timer_clock();
307 while ((fake - start_time) < ms_to_ticks(BASETIME)) {
308 fake = timer_clock();
311 kprintf("> Main: calibration done, %dms equals to %u cycles!\n", BASETIME, loops);
313 kputs("> Main: Run Priority Inversion test...\n");
316 PROC_INV_TEST_INIT(1, 2, S2);
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);
322 // Will block on s1, nothing happens..
323 PROC_INV_TEST_INIT(4, 5, S1);
325 // No semaphore, without PI this will delay everyone!
326 PROC_INV_TEST_INIT(5, 6, NONE);
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);
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");
348 kputs("> Main: Priority Inversion Test failed..\n");
354 void sem_inv_test(void)
358 #endif /* CONFIG_KERN_PRI */
363 int sem_testRun(void)
366 sem_ser_test(); // Serialization
367 sem_inv_test(); // Priority Inversion
372 int sem_testSetup(void)
376 kprintf("Init Timer..");
380 kprintf("Init Process..");
387 int sem_testTearDown(void)
389 kputs("TearDown Semaphore test.\n");