Ensure all lines that should be omitted from public includes are marked
[AROS.git] / test / benchmarks / exec / taskswitch2.c
blob818e591054967dd4488664493c049faaa243c741
1 /* $Id$ */
3 #include <stdio.h>
4 #include <sys/time.h>
6 #ifndef __AROS__
7 /*
8 This is a trick to make the timer.device's timeval struct not clash with the
9 sys/time.h's one. I'm using gettymeofday 'cause I'm lazy and don't wanna
10 bother with timer.device myself :)
12 On AROS this is handled automatically (go AROS!)
14 # define timeval timeval_aos
15 #endif
17 #include <exec/tasks.h>
18 #include <proto/exec.h>
19 #include <clib/alib_protos.h>
20 #include <proto/dos.h>
22 #ifndef __AROS__
23 /* The trick is over... */
24 # undef timeval
25 #endif
27 #ifndef __typedef_STACKIPTR
28 /* Normal AmigaOS environments don't have this defined */
29 typedef ULONG STACKIPTR;
30 #endif
32 #ifndef AROS_STACKSIZE
33 # define AROS_STACKSIZE 4096
34 #endif
36 /* Give some prettier names to the standard signals */
37 #define SIGF_STOP SIGBREAKF_CTRL_C
38 #define SIGF_HELLO SIGBREAKF_CTRL_D
39 #define SIGF_BYE SIGBREAKF_CTRL_E
40 #define SIGF_START SIGBREAKF_CTRL_F
42 static unsigned long counter = 0;
43 static struct Task *task1, *task2;
45 static struct timeval start_tv, end_tv;
47 void Task1Entry()
49 for (;;)
51 if (Wait(SIGF_HELLO | SIGF_STOP) == SIGF_HELLO)
52 Signal(task2, SIGF_HELLO);
53 else
55 Wait(SIGF_BYE);
56 return;
61 void Task2Entry()
63 Wait(SIGF_START);
65 for (;;)
67 Signal(task1, SIGF_HELLO);
68 if (Wait(SIGF_HELLO | SIGF_STOP) == SIGF_HELLO)
69 counter++;
70 else
72 Signal(task1, SIGF_BYE);
73 return;
78 int __nocommandline = 1;
80 /*
81 define this to non-zero if you want the benchmark to end automatically
82 when it realizes that there's no need to continue.
84 Beware that it can introduce some overhead (although very little).
86 #define SELF_TIMED_TEST 0
88 int main(void)
90 double elapsed = 0;
92 task1 = CreateTask("Task 1", 0, Task1Entry, AROS_STACKSIZE);
93 task2 = CreateTask("Task 2", 0, Task2Entry, AROS_STACKSIZE);
95 printf
97 "The test is starting.\n"
98 #if !SELF_TIMED_TEST
99 "Press CTRL-C to stop the test and get the results.\n"
100 "Wait a few seconds before doing so, in order to get a more accurate result\n\n"
101 #endif
104 gettimeofday(&start_tv, NULL);
106 Signal(task2, SIGF_START);
108 #if SELF_TIMED_TEST
110 unsigned long i;
111 double oldratio = 0;
113 for (i=4;;i+=2)
115 double ratio;
116 Delay(200);
118 ratio = (double)i/counter;
119 if ((ratio - oldratio) <= .000000001) break;
121 oldratio = ratio;
125 #else
127 Wait(SIGF_STOP);
129 #endif
131 gettimeofday(&end_tv, NULL);
133 Signal(task1, SIGF_STOP);
134 Signal(task2, SIGF_STOP);
136 elapsed = ((double)(((end_tv.tv_sec * 1000000) + end_tv.tv_usec) - ((start_tv.tv_sec * 1000000) + start_tv.tv_usec)))/1000000.;
138 printf
140 "Elapsed time: %f seconds\n"
141 "Number of context switches: %ld\n"
142 "Signal roundtrip time: %.8f\n"
143 "Context switch time: <= %.8f seconds\n",
144 elapsed,
145 counter * 2,
146 elapsed / (double)counter,
147 elapsed / (double)(counter * 2)
150 return 0;