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