grub2: bring back build of aros-side grub2 tools
[AROS.git] / test / benchmarks / exec / taskswitch2.c
blobe43d5d5c5a50eb3569ad37dabcc5882c5323e578
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <stdio.h>
7 #include <sys/time.h>
9 #ifndef __AROS__
10 /*
11 This is a trick to make the timer.device's timeval struct not clash with the
12 sys/time.h's one. I'm using gettymeofday 'cause I'm lazy and don't wanna
13 bother with timer.device myself :)
15 On AROS this is handled automatically (go AROS!)
17 # define timeval timeval_aos
18 #endif
20 #include <exec/tasks.h>
21 #include <proto/exec.h>
22 #include <clib/alib_protos.h>
23 #include <proto/dos.h>
25 #ifndef __AROS__
26 /* The trick is over... */
27 # undef timeval
28 #endif
30 #ifndef __typedef_STACKIPTR
31 /* Normal AmigaOS environments don't have this defined */
32 typedef ULONG STACKIPTR;
33 #endif
35 #ifndef AROS_STACKSIZE
36 # define AROS_STACKSIZE 4096
37 #endif
39 /* Give some prettier names to the standard signals */
40 #define SIGF_STOP SIGBREAKF_CTRL_C
41 #define SIGF_HELLO SIGBREAKF_CTRL_D
42 #define SIGF_BYE SIGBREAKF_CTRL_E
43 #define SIGF_START SIGBREAKF_CTRL_F
45 static unsigned long counter = 0;
46 static struct Task *task1, *task2;
48 static struct timeval start_tv, end_tv;
50 void Task1Entry()
52 for (;;)
54 if (Wait(SIGF_HELLO | SIGF_STOP) == SIGF_HELLO)
55 Signal(task2, SIGF_HELLO);
56 else
58 Wait(SIGF_BYE);
59 return;
64 void Task2Entry()
66 Wait(SIGF_START);
68 for (;;)
70 Signal(task1, SIGF_HELLO);
71 if (Wait(SIGF_HELLO | SIGF_STOP) == SIGF_HELLO)
72 counter++;
73 else
75 Signal(task1, SIGF_BYE);
76 return;
81 int __nocommandline = 1;
83 /*
84 define this to non-zero if you want the benchmark to end automatically
85 when it realizes that there's no need to continue.
87 Beware that it can introduce some overhead (although very little).
89 #define SELF_TIMED_TEST 0
91 int main(void)
93 double elapsed = 0;
95 task1 = CreateTask("Task 1", 0, Task1Entry, AROS_STACKSIZE);
96 task2 = CreateTask("Task 2", 0, Task2Entry, AROS_STACKSIZE);
98 printf
100 "The test is starting.\n"
101 #if !SELF_TIMED_TEST
102 "Press CTRL-C to stop the test and get the results.\n"
103 "Wait a few seconds before doing so, in order to get a more accurate result\n\n"
104 #endif
107 gettimeofday(&start_tv, NULL);
109 Signal(task2, SIGF_START);
111 #if SELF_TIMED_TEST
113 unsigned long i;
114 double oldratio = 0;
116 for (i=4;;i+=2)
118 double ratio;
119 Delay(200);
121 ratio = (double)i/counter;
122 if ((ratio - oldratio) <= .000000001) break;
124 oldratio = ratio;
128 #else
130 Wait(SIGF_STOP);
132 #endif
134 gettimeofday(&end_tv, NULL);
136 Signal(task1, SIGF_STOP);
137 Signal(task2, SIGF_STOP);
139 elapsed = ((double)(((end_tv.tv_sec * 1000000) + end_tv.tv_usec) - ((start_tv.tv_sec * 1000000) + start_tv.tv_usec)))/1000000.;
141 printf
143 "Elapsed time: %f seconds\n"
144 "Number of context switches: %ld\n"
145 "Signal roundtrip time: %.8f\n"
146 "Context switch time: <= %.8f seconds\n",
147 elapsed,
148 counter * 2,
149 elapsed / (double)counter,
150 elapsed / (double)(counter * 2)
153 return 0;