[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 | |
---|
[bd37119] | 10 | void ?{}( GreatSuspender & this ) { |
---|
[cf97ccb] | 11 | prime(this); |
---|
| 12 | } |
---|
| 13 | |
---|
[bd37119] | 14 | void main( GreatSuspender & this ) |
---|
[cf97ccb] | 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 ) { |
---|
[bd37119] | 23 | resume( *this ); |
---|
[cf97ccb] | 24 | } |
---|
| 25 | } |
---|
| 26 | |
---|
[bd951f7] | 27 | //----------------------------------------------------------------------------- |
---|
| 28 | // coroutine context switch |
---|
[cf97ccb] | 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 | |
---|
[bd951f7] | 42 | //----------------------------------------------------------------------------- |
---|
| 43 | // thread context switch |
---|
[cf97ccb] | 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 | |
---|
[bd951f7] | 57 | //----------------------------------------------------------------------------- |
---|
| 58 | // single monitor entry |
---|
| 59 | monitor mon_t {}; |
---|
[bd37119] | 60 | void dummy( mon_t & mutex m ) {} |
---|
[bd951f7] | 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 ) { |
---|
[bd37119] | 69 | dummy( mon ); |
---|
[bd951f7] | 70 | } |
---|
| 71 | EndTime = Time(); |
---|
| 72 | |
---|
| 73 | return ( EndTime - StartTime ) / NoOfTimes; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | //----------------------------------------------------------------------------- |
---|
| 77 | // multi monitor entry |
---|
[bd37119] | 78 | void dummy( mon_t & mutex m1, mon_t & mutex m2 ) {} |
---|
[bd951f7] | 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 ) { |
---|
[bd37119] | 87 | dummy( mon1, mon2 ); |
---|
[bd951f7] | 88 | } |
---|
| 89 | EndTime = Time(); |
---|
| 90 | |
---|
| 91 | return ( EndTime - StartTime ) / NoOfTimes; |
---|
| 92 | } |
---|
| 93 | |
---|
[43426d4] | 94 | //----------------------------------------------------------------------------- |
---|
| 95 | // single internal sched entry |
---|
| 96 | mon_t mon1; |
---|
| 97 | |
---|
[7286a40] | 98 | condition cond1a; |
---|
[43426d4] | 99 | condition cond1b; |
---|
| 100 | |
---|
| 101 | thread thrd1a { long long int * out; }; |
---|
| 102 | thread thrd1b {}; |
---|
| 103 | |
---|
[bd37119] | 104 | void ?{}( thrd1a & this, long long int * out ) { |
---|
| 105 | this.out = out; |
---|
[43426d4] | 106 | } |
---|
| 107 | |
---|
[bd37119] | 108 | void side1A( mon_t & mutex a, long long int * out ) { |
---|
[43426d4] | 109 | long long int StartTime, EndTime; |
---|
| 110 | |
---|
| 111 | StartTime = Time(); |
---|
| 112 | for( int i = 0;; i++ ) { |
---|
[4cedd9f] | 113 | signal(cond1a); |
---|
[43426d4] | 114 | if( i > N ) break; |
---|
[4cedd9f] | 115 | wait(cond1b); |
---|
[43426d4] | 116 | } |
---|
| 117 | EndTime = Time(); |
---|
| 118 | |
---|
| 119 | *out = ( EndTime - StartTime ) / N; |
---|
| 120 | } |
---|
| 121 | |
---|
[bd37119] | 122 | void side1B( mon_t & mutex a ) { |
---|
[43426d4] | 123 | for( int i = 0;; i++ ) { |
---|
[4cedd9f] | 124 | signal(cond1b); |
---|
[43426d4] | 125 | if( i > N ) break; |
---|
[4cedd9f] | 126 | wait(cond1a); |
---|
[43426d4] | 127 | } |
---|
| 128 | } |
---|
| 129 | |
---|
[bd37119] | 130 | void main( thrd1a & this ) { side1A( mon1, this.out ); } |
---|
| 131 | void main( thrd1b & this ) { side1B( mon1 ); } |
---|
[43426d4] | 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 | |
---|
[7286a40] | 146 | condition cond2a; |
---|
[43426d4] | 147 | condition cond2b; |
---|
| 148 | |
---|
| 149 | thread thrd2a { long long int * out; }; |
---|
| 150 | thread thrd2b {}; |
---|
| 151 | |
---|
[bd37119] | 152 | void ?{}( thrd2a & this, long long int * out ) { |
---|
| 153 | this.out = out; |
---|
[43426d4] | 154 | } |
---|
| 155 | |
---|
[bd37119] | 156 | void side2A( mon_t & mutex a, mon_t & mutex b, long long int * out ) { |
---|
[43426d4] | 157 | long long int StartTime, EndTime; |
---|
| 158 | |
---|
| 159 | StartTime = Time(); |
---|
| 160 | for( int i = 0;; i++ ) { |
---|
[4cedd9f] | 161 | signal(cond2a); |
---|
[43426d4] | 162 | if( i > N ) break; |
---|
[4cedd9f] | 163 | wait(cond2b); |
---|
[43426d4] | 164 | } |
---|
| 165 | EndTime = Time(); |
---|
| 166 | |
---|
| 167 | *out = ( EndTime - StartTime ) / N; |
---|
| 168 | } |
---|
| 169 | |
---|
[bd37119] | 170 | void side2B( mon_t & mutex a, mon_t & mutex b ) { |
---|
[43426d4] | 171 | for( int i = 0;; i++ ) { |
---|
[4cedd9f] | 172 | signal(cond2b); |
---|
[43426d4] | 173 | if( i > N ) break; |
---|
[4cedd9f] | 174 | wait(cond2a); |
---|
[43426d4] | 175 | } |
---|
| 176 | } |
---|
| 177 | |
---|
[bd37119] | 178 | void main( thrd2a & this ) { side2A( mon1, mon2, this.out ); } |
---|
| 179 | void main( thrd2b & this ) { side2B( mon1, mon2 ); } |
---|
[43426d4] | 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 |
---|
[cf97ccb] | 192 | int main() |
---|
| 193 | { |
---|
[bd951f7] | 194 | sout | time(NULL) | ','; |
---|
| 195 | sout | measure_coroutine() | ','; |
---|
| 196 | sout | measure_thread() | ','; |
---|
| 197 | sout | measure_1_monitor_entry() | ','; |
---|
[43426d4] | 198 | sout | measure_2_monitor_entry() | ','; |
---|
| 199 | sout | measure_1_sched_int() | ','; |
---|
| 200 | sout | measure_2_sched_int() | endl; |
---|
[cf97ccb] | 201 | } |
---|