1 | #include <fstream> |
---|
2 | #include <monitor> |
---|
3 | #include <stdlib> |
---|
4 | #include <thread> |
---|
5 | |
---|
6 | #include "bench.h" |
---|
7 | |
---|
8 | coroutine GreatSuspender {}; |
---|
9 | |
---|
10 | void ?{}( GreatSuspender * this ) { |
---|
11 | prime(this); |
---|
12 | } |
---|
13 | |
---|
14 | void main( GreatSuspender * this ) |
---|
15 | { |
---|
16 | while( true ) { |
---|
17 | suspend(); |
---|
18 | } |
---|
19 | } |
---|
20 | |
---|
21 | void resumer( GreatSuspender * this, const unsigned int NoOfTimes ) { |
---|
22 | for ( volatile unsigned int i = 0; i < NoOfTimes; i += 1 ) { |
---|
23 | resume( this ); |
---|
24 | } |
---|
25 | } |
---|
26 | |
---|
27 | //----------------------------------------------------------------------------- |
---|
28 | // coroutine context switch |
---|
29 | long long int measure_coroutine() { |
---|
30 | const unsigned int NoOfTimes = N; |
---|
31 | long long int StartTime, EndTime; |
---|
32 | |
---|
33 | GreatSuspender s; |
---|
34 | |
---|
35 | StartTime = Time(); |
---|
36 | resumer( &s, NoOfTimes ); |
---|
37 | EndTime = Time(); |
---|
38 | |
---|
39 | return ( EndTime - StartTime ) / NoOfTimes; |
---|
40 | } |
---|
41 | |
---|
42 | //----------------------------------------------------------------------------- |
---|
43 | // thread context switch |
---|
44 | long long int measure_thread() { |
---|
45 | const unsigned int NoOfTimes = N; |
---|
46 | long long int StartTime, EndTime; |
---|
47 | |
---|
48 | StartTime = Time(); |
---|
49 | for ( volatile unsigned int i = 0; i < NoOfTimes; i += 1 ) { |
---|
50 | yield(); |
---|
51 | } |
---|
52 | EndTime = Time(); |
---|
53 | |
---|
54 | return ( EndTime - StartTime ) / NoOfTimes; |
---|
55 | } |
---|
56 | |
---|
57 | //----------------------------------------------------------------------------- |
---|
58 | // single monitor entry |
---|
59 | monitor mon_t {}; |
---|
60 | void dummy( mon_t * mutex m ) {} |
---|
61 | |
---|
62 | long long int measure_1_monitor_entry() { |
---|
63 | const unsigned int NoOfTimes = N; |
---|
64 | long long int StartTime, EndTime; |
---|
65 | mon_t mon; |
---|
66 | |
---|
67 | StartTime = Time(); |
---|
68 | for ( volatile unsigned int i = 0; i < NoOfTimes; i += 1 ) { |
---|
69 | dummy( &mon ); |
---|
70 | } |
---|
71 | EndTime = Time(); |
---|
72 | |
---|
73 | return ( EndTime - StartTime ) / NoOfTimes; |
---|
74 | } |
---|
75 | |
---|
76 | //----------------------------------------------------------------------------- |
---|
77 | // multi monitor entry |
---|
78 | void dummy( mon_t * mutex m1, mon_t * mutex m2 ) {} |
---|
79 | |
---|
80 | long long int measure_2_monitor_entry() { |
---|
81 | const unsigned int NoOfTimes = N; |
---|
82 | long long int StartTime, EndTime; |
---|
83 | mon_t mon1, mon2; |
---|
84 | |
---|
85 | StartTime = Time(); |
---|
86 | for ( volatile unsigned int i = 0; i < NoOfTimes; i += 1 ) { |
---|
87 | dummy( &mon1, &mon2 ); |
---|
88 | } |
---|
89 | EndTime = Time(); |
---|
90 | |
---|
91 | return ( EndTime - StartTime ) / NoOfTimes; |
---|
92 | } |
---|
93 | |
---|
94 | //----------------------------------------------------------------------------- |
---|
95 | // single internal sched entry |
---|
96 | mon_t mon1; |
---|
97 | |
---|
98 | condition cond1a; |
---|
99 | condition cond1b; |
---|
100 | |
---|
101 | thread thrd1a { long long int * out; }; |
---|
102 | thread thrd1b {}; |
---|
103 | |
---|
104 | void ?{}( thrd1a * this, long long int * out ) { |
---|
105 | this->out = out; |
---|
106 | } |
---|
107 | |
---|
108 | void side1A( mon_t * mutex a, long long int * out ) { |
---|
109 | long long int StartTime, EndTime; |
---|
110 | |
---|
111 | StartTime = Time(); |
---|
112 | for( int i = 0;; i++ ) { |
---|
113 | signal(&cond1a); |
---|
114 | if( i > N ) break; |
---|
115 | wait(&cond1b); |
---|
116 | } |
---|
117 | EndTime = Time(); |
---|
118 | |
---|
119 | *out = ( EndTime - StartTime ) / N; |
---|
120 | } |
---|
121 | |
---|
122 | void side1B( mon_t * mutex a ) { |
---|
123 | for( int i = 0;; i++ ) { |
---|
124 | signal(&cond1b); |
---|
125 | if( i > N ) break; |
---|
126 | wait(&cond1a); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | void main( thrd1a * this ) { side1A( &mon1, this->out ); } |
---|
131 | void main( thrd1b * this ) { side1B( &mon1 ); } |
---|
132 | |
---|
133 | long long int measure_1_sched_int() { |
---|
134 | long long int t; |
---|
135 | { |
---|
136 | thrd1a a = { &t }; |
---|
137 | thrd1b b; |
---|
138 | } |
---|
139 | return t; |
---|
140 | } |
---|
141 | |
---|
142 | //----------------------------------------------------------------------------- |
---|
143 | // multi internal sched entry |
---|
144 | mon_t mon2; |
---|
145 | |
---|
146 | condition cond2a; |
---|
147 | condition cond2b; |
---|
148 | |
---|
149 | thread thrd2a { long long int * out; }; |
---|
150 | thread thrd2b {}; |
---|
151 | |
---|
152 | void ?{}( thrd2a * this, long long int * out ) { |
---|
153 | this->out = out; |
---|
154 | } |
---|
155 | |
---|
156 | void side2A( mon_t * mutex a, mon_t * mutex b, long long int * out ) { |
---|
157 | long long int StartTime, EndTime; |
---|
158 | |
---|
159 | StartTime = Time(); |
---|
160 | for( int i = 0;; i++ ) { |
---|
161 | signal(&cond2a); |
---|
162 | if( i > N ) break; |
---|
163 | wait(&cond2b); |
---|
164 | } |
---|
165 | EndTime = Time(); |
---|
166 | |
---|
167 | *out = ( EndTime - StartTime ) / N; |
---|
168 | } |
---|
169 | |
---|
170 | void side2B( mon_t * mutex a, mon_t * mutex b ) { |
---|
171 | for( int i = 0;; i++ ) { |
---|
172 | signal(&cond2b); |
---|
173 | if( i > N ) break; |
---|
174 | wait(&cond2a); |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | void main( thrd2a * this ) { side2A( &mon1, &mon2, this->out ); } |
---|
179 | void main( thrd2b * this ) { side2B( &mon1, &mon2 ); } |
---|
180 | |
---|
181 | long long int measure_2_sched_int() { |
---|
182 | long long int t; |
---|
183 | { |
---|
184 | thrd2a a = { &t }; |
---|
185 | thrd2b b; |
---|
186 | } |
---|
187 | return t; |
---|
188 | } |
---|
189 | |
---|
190 | //----------------------------------------------------------------------------- |
---|
191 | // main loop |
---|
192 | int main() |
---|
193 | { |
---|
194 | sout | time(NULL) | ','; |
---|
195 | sout | measure_coroutine() | ','; |
---|
196 | sout | measure_thread() | ','; |
---|
197 | sout | measure_1_monitor_entry() | ','; |
---|
198 | sout | measure_2_monitor_entry() | ','; |
---|
199 | sout | measure_1_sched_int() | ','; |
---|
200 | sout | measure_2_sched_int() | endl; |
---|
201 | } |
---|