#include <rwlock_tester.h>
Public Member Functions | |
| RWLockTester (int loops=1000000, int threadNum=1, SyncOStream &os=stdSyncOStream) | |
| ~RWLockTester () | |
| void | add_lock_test (RWLockLoopTest *test) |
| void | validate (void) |
| void | run_test (int thread_num=1, int loops=kPerformanceLoopsNumber, RWLockLoopTestRunFunction tf=&RWLockLoopTest::run, int rwratio=1) |
| void | set_loops (int loops) |
| void | set_threads (int tcount) |
| int | get_loops (int loops) |
| int | get_threads (int tcount) |
| SyncOStream & | get_ostream (void) |
| void | print_summary (void) |
| void | clear_summary (void) |
| std::vector< RWLockLoopTest * > ::const_iterator | begin () const |
| std::vector< RWLockLoopTest * > ::const_iterator | end () const |
| const RWLockLoopTest * | operator[] (size_t indx) const |
| size_t | size () const |
Private Member Functions | |
| void | set_tests (int threads_num, int loops, int rwratio, RWLockLoopTestRunFunction testf) |
| void | clearStartFlag (void) |
| void | signalStart (void) |
| void | waitForStart (void) |
Static Private Member Functions | |
| static void * | thread_function (void *) |
| static void * | thread_function_read (void *) |
| static void * | thread_function_write (void *) |
Private Attributes | |
| volatile int | startFlag |
| int | repeat |
| int | threads |
| int | rw_ratio |
| RWLockLoopTestRunFunction | testfptr |
| RWLockLoopTest * | curr |
| std::vector< RWLockLoopTest * > | lock_tests |
| volatile int | current_threads |
| volatile int | current_active_threads |
| volatile int | current_read_threads |
| volatile int | current_write_threads |
| volatile int | current_rw_threads |
| volatile int | current_wr_threads |
| SyncOStream & | out |
Static Private Attributes | |
| static SyncOStream | stdSyncOStream |
Definition at line 155 of file rwlock_tester.h.
| RWLockTester::RWLockTester | ( | int | loops = 1000000, |
|
| int | threadNum = 1, |
|||
| SyncOStream & | os = stdSyncOStream | |||
| ) | [inline] |
Definition at line 158 of file rwlock_tester.h.
00158 : 00159 repeat(loops), threads(threadNum), out(os), 00160 current_threads(0), current_active_threads(0), 00161 current_read_threads(0), current_write_threads(0) 00162 { 00163 assert(CLOCKS_PER_SEC >= CLOCK_T(1000)); 00164 assert(CLOCKS_PER_SEC % CLOCK_T(1000) == 0); 00165 }
| RWLockTester::~RWLockTester | ( | ) | [inline] |
Definition at line 167 of file rwlock_tester.h.
References lock_tests.
00168 { 00169 std::vector<RWLockLoopTest *>::iterator i; 00170 for(i=lock_tests.begin(); i!=lock_tests.end(); i++) 00171 { 00172 delete *i; 00173 } 00174 }
| void RWLockTester::add_lock_test | ( | RWLockLoopTest * | test | ) | [inline] |
Definition at line 176 of file rwlock_tester.h.
References lock_tests.
Referenced by main().
00176 { lock_tests.push_back(test); }
| void RWLockTester::validate | ( | void | ) |
Definition at line 135 of file rwlock_tester.cc.
References atomic_inc(), curr, current_read_threads, current_threads, current_write_threads, RWLockLoopTest::enter_read(), RWLockLoopTest::enter_write(), RWLockLoopTest::leave_read(), RWLockLoopTest::leave_write(), SyncOStream::lock(), lock_tests, RWLockLoopTest::name(), out, signalStart(), SystemSleep(), thread_function_read(), thread_function_write(), thread_yield(), SyncOStream::unlock(), and vthreads.
Referenced by main().
00136 { 00137 SystemThread **thread_objs = new SystemThread * [vthreads]; 00138 SystemThread **thread_objs2 = new SystemThread * [vthreads]; 00139 int t; 00140 00141 for(std::vector<RWLockLoopTest *>::iterator i=lock_tests.begin(); i != lock_tests.end(); i++) 00142 { 00143 curr = *i; 00144 00145 out.lock(); 00146 out << "Validating " << curr->name() << ":\n"; 00147 out.unlock(); 00148 00149 current_threads = 0; 00150 00151 signalStart(); 00152 00153 curr->enter_write(); // to put all read threads in a waiting state 00154 for(t=0; t<vthreads; t++) 00155 { 00156 atomic_inc(¤t_threads); 00157 thread_objs[t] = new SystemThread(&thread_function_read, (void*)this); 00158 } 00159 while(current_read_threads != vthreads) SystemSleep(100); 00160 if(current_threads != vthreads) 00161 { 00162 out.lock(); 00163 out << "Failed writer test, need to increase loops/threads number.\n"; 00164 out.unlock(); 00165 00166 exit(-1001); 00167 } 00168 curr->leave_write(); 00169 thread_yield(); 00170 for(t=0; t<vthreads; t++) 00171 { 00172 atomic_inc(¤t_threads); 00173 thread_objs2[t] = new SystemThread(&thread_function_read, (void*)this); 00174 } 00175 curr->enter_write(); 00176 if(current_threads <= 0) 00177 { 00178 out << "Failed writer starvation test.\n"; 00179 } 00180 else 00181 { 00182 out << "Writer starvation test passed - remaining readers: " << 00183 current_threads << " out of " << vthreads*2 << ".\n"; 00184 } 00185 curr->leave_write(); 00186 00187 SystemSleep(100); 00188 00189 for(t=0; t<vthreads; t++) 00190 { 00191 delete thread_objs[t]; 00192 delete thread_objs2[t]; 00193 } 00194 00195 if(current_threads != 0) 00196 { 00197 out.lock(); 00198 out << "Failed the framework test, current_threads= " << current_threads << "\n"; 00199 out.unlock(); 00200 exit(-1003); 00201 } 00202 00203 curr->enter_read(); 00204 for(t=0; t<vthreads; t++) 00205 { 00206 atomic_inc(¤t_threads); 00207 thread_objs[t] = new SystemThread(&thread_function_write, (void*)this); 00208 } 00209 while(current_write_threads != vthreads) SystemSleep(100); 00210 if(current_threads != vthreads) 00211 { 00212 out.lock(); 00213 out << "Failed reader test, need to increase loops/threads number.\n"; 00214 out.unlock(); 00215 } 00216 curr->leave_read(); 00217 thread_yield(); 00218 for(t=0; t<vthreads; t++) 00219 { 00220 atomic_inc(¤t_threads); 00221 thread_objs2[t] = new SystemThread(&thread_function_write, (void*)this); 00222 } 00223 00224 curr->enter_read(); 00225 if(current_threads <= 0) 00226 { 00227 out << "Failed reader starvation test.\n"; 00228 } 00229 else 00230 { 00231 out << "Reader starvation test passed - remaining writers: " << 00232 current_threads << " out of " << vthreads*2 << ".\n"; 00233 } 00234 curr->leave_read(); 00235 SystemSleep(100); 00236 for(t=0; t<vthreads; t++) 00237 { 00238 delete thread_objs[t]; 00239 delete thread_objs2[t]; 00240 } 00241 if(current_threads != 0) 00242 { 00243 out.lock(); 00244 out << "Failed the framework test 2, current_threads= " << current_threads << "\n"; 00245 out.unlock(); 00246 exit(-1013); 00247 } 00248 } 00249 delete [] thread_objs; 00250 delete [] thread_objs2; 00251 }
| void RWLockTester::run_test | ( | int | thread_num = 1, |
|
| int | loops = kPerformanceLoopsNumber, |
|||
| RWLockLoopTestRunFunction | tf = &RWLockLoopTest::run, |
|||
| int | rwratio = 1 | |||
| ) |
Definition at line 253 of file rwlock_tester.cc.
References atomic_inc(), RWLockLoopTest::clear_thread_times(), clearStartFlag(), curr, current_active_threads, current_threads, SystemThread::join(), SyncOStream::lock(), lock_tests, RWLockLoopTest::mark_test_end(), RWLockLoopTest::mark_test_start(), RWLockLoopTest::name(), out, RWLockLoopTest::print_mixed_total(), RWLockLoopTest::print_thread_times(), RWLockLoopTest::run_mixed(), set_tests(), signalStart(), SystemSleep(), testfptr, thread_function(), thread_yield(), threads, and SyncOStream::unlock().
Referenced by main().
00254 { 00255 int t; 00256 SystemThread **thread_objs = new SystemThread * [threads_num]; 00257 set_tests(threads_num, loops, rwratio, testf); 00258 00259 out.lock(); 00260 out << "\n" << threads << " thread test.\n"; 00261 out.unlock(); 00262 00263 for(std::vector<RWLockLoopTest *>::iterator i=lock_tests.begin(); i != lock_tests.end(); i++) 00264 { 00265 curr = *i; 00266 out.lock(); 00267 out << " " << curr->name() << ":\n"; 00268 out.unlock(); 00269 current_threads = 0; 00270 00271 clearStartFlag(); 00272 00273 for(t=0; t<threads_num; t++) 00274 { 00275 atomic_inc(¤t_threads); 00276 thread_objs[t] = new SystemThread(&thread_function, (void*)this); 00277 thread_yield(); 00278 } 00279 00280 while(current_threads > current_active_threads) thread_yield(); 00281 00282 SystemSleep(2000); 00283 00284 curr->clear_thread_times(); 00285 curr->mark_test_start(); 00286 00287 signalStart(); 00288 00289 while(current_threads > 0) thread_yield(); 00290 00291 curr->mark_test_end(); 00292 00293 for(t=0; t<threads_num; t++) 00294 { 00295 thread_objs[t]->join(); 00296 delete thread_objs[t]; 00297 thread_objs[t] = NULL; 00298 thread_yield(); 00299 } 00300 00301 if(testfptr == &RWLockLoopTest::run_mixed) 00302 curr->print_mixed_total(); 00303 else 00304 curr->print_thread_times(); 00305 } 00306 delete [] thread_objs; 00307 }
| void RWLockTester::set_loops | ( | int | loops | ) | [inline] |
Definition at line 183 of file rwlock_tester.h.
References repeat.
00183 { if(loops>0) repeat = loops; }
| void RWLockTester::set_threads | ( | int | tcount | ) | [inline] |
Definition at line 184 of file rwlock_tester.h.
References threads.
00184 { if(tcount>0) threads = tcount; }
| int RWLockTester::get_loops | ( | int | loops | ) | [inline] |
| int RWLockTester::get_threads | ( | int | tcount | ) | [inline] |
| SyncOStream& RWLockTester::get_ostream | ( | void | ) | [inline] |
Definition at line 187 of file rwlock_tester.h.
References out.
Referenced by main().
00187 { return out; }
| void RWLockTester::print_summary | ( | void | ) |
Definition at line 362 of file rwlock_tester.cc.
References RWLockLoopTest::get_total_read_thread_time(), RWLockLoopTest::get_total_write_thread_time(), lock_tests, RWLockLoopTest::name(), and out.
Referenced by main().
00363 { 00364 for(std::vector<RWLockLoopTest *>::iterator i=lock_tests.begin(); i != lock_tests.end(); i++) 00365 { 00366 RWLockLoopTest *ct = *i; 00367 out << "\nTest Summary for " << ct->name() 00368 << ":\n\tTotal read thread time=" 00369 << (unsigned long)ct->get_total_read_thread_time() 00370 << "\n\tTotal write thread time=" 00371 << (unsigned long)ct->get_total_write_thread_time() 00372 << "\n\tTotal R/W thread time=" 00373 << (unsigned long)(ct->get_total_read_thread_time() + ct->get_total_write_thread_time()) << "\n"; 00374 } 00375 }
| void RWLockTester::clear_summary | ( | void | ) |
Definition at line 356 of file rwlock_tester.cc.
References lock_tests.
Referenced by main().
00357 { 00358 for(std::vector<RWLockLoopTest *>::iterator i=lock_tests.begin(); i != lock_tests.end(); i++) 00359 (*i)->clear_total_thread_time(); 00360 }
| std::vector<RWLockLoopTest *>::const_iterator RWLockTester::begin | ( | ) | const [inline] |
Definition at line 191 of file rwlock_tester.h.
References lock_tests.
Referenced by GraphData::init().
00191 { return lock_tests.begin(); }
| std::vector<RWLockLoopTest *>::const_iterator RWLockTester::end | ( | ) | const [inline] |
Definition at line 192 of file rwlock_tester.h.
References lock_tests.
Referenced by GraphData::init().
00192 { return lock_tests.end(); }
| const RWLockLoopTest* RWLockTester::operator[] | ( | size_t | indx | ) | const [inline] |
Definition at line 193 of file rwlock_tester.h.
References lock_tests.
00193 { return lock_tests[indx]; }
| size_t RWLockTester::size | ( | ) | const [inline] |
Definition at line 194 of file rwlock_tester.h.
References lock_tests.
Referenced by GraphData::add_results().
00194 { return lock_tests.size(); }
| void * RWLockTester::thread_function | ( | void * | ptr | ) | [static, private] |
Definition at line 341 of file rwlock_tester.cc.
References atomic_dec(), atomic_inc(), curr, current_active_threads, current_threads, repeat, rw_ratio, testfptr, and waitForStart().
Referenced by run_test().
00342 { 00343 RWLockTester *thisPtr = (RWLockTester *)ptr; 00344 atomic_inc(&thisPtr->current_active_threads); 00345 00346 thisPtr->waitForStart(); 00347 00348 (thisPtr->curr->*thisPtr->testfptr)(thisPtr->repeat, thisPtr->rw_ratio); 00349 00350 atomic_dec(&thisPtr->current_active_threads); 00351 00352 atomic_dec(&thisPtr->current_threads); 00353 return NULL; 00354 }
| void * RWLockTester::thread_function_read | ( | void * | ptr | ) | [static, private] |
Definition at line 309 of file rwlock_tester.cc.
References atomic_dec(), atomic_inc(), curr, current_active_threads, current_read_threads, current_threads, RWLockLoopTest::read_test_loop(), vloops, and waitForStart().
Referenced by validate().
00310 { 00311 RWLockTester *thisPtr = (RWLockTester *)ptr; 00312 atomic_inc(&thisPtr->current_active_threads); 00313 atomic_inc(&thisPtr->current_read_threads); 00314 00315 thisPtr->waitForStart(); 00316 00317 thisPtr->curr->read_test_loop(vloops); 00318 00319 atomic_dec(&thisPtr->current_read_threads); 00320 atomic_dec(&thisPtr->current_active_threads); 00321 atomic_dec(&thisPtr->current_threads); 00322 return NULL; 00323 }
| void * RWLockTester::thread_function_write | ( | void * | ptr | ) | [static, private] |
Definition at line 325 of file rwlock_tester.cc.
References atomic_dec(), atomic_inc(), curr, current_active_threads, current_threads, current_write_threads, vloops, waitForStart(), and RWLockLoopTest::write_test_loop().
Referenced by validate().
00326 { 00327 RWLockTester *thisPtr = (RWLockTester *)ptr; 00328 atomic_inc(&thisPtr->current_active_threads); 00329 atomic_inc(&thisPtr->current_write_threads); 00330 00331 thisPtr->waitForStart(); 00332 00333 thisPtr->curr->write_test_loop(vloops); 00334 00335 atomic_dec(&thisPtr->current_write_threads); 00336 atomic_dec(&thisPtr->current_active_threads); 00337 atomic_dec(&thisPtr->current_threads); 00338 return NULL; 00339 }
| void RWLockTester::set_tests | ( | int | threads_num, | |
| int | loops, | |||
| int | rwratio, | |||
| RWLockLoopTestRunFunction | testf | |||
| ) | [inline, private] |
| void RWLockTester::clearStartFlag | ( | void | ) | [inline, private] |
Definition at line 209 of file rwlock_tester.h.
References startFlag.
Referenced by run_test().
00209 { startFlag = 0; }
| void RWLockTester::signalStart | ( | void | ) | [inline, private] |
Definition at line 210 of file rwlock_tester.h.
References startFlag.
Referenced by run_test(), and validate().
00210 { startFlag = 1; }
| void RWLockTester::waitForStart | ( | void | ) | [inline, private] |
Definition at line 211 of file rwlock_tester.h.
References startFlag, and thread_yield().
Referenced by thread_function(), thread_function_read(), and thread_function_write().
00211 { while(startFlag == 0) thread_yield(); }
volatile int RWLockTester::startFlag [private] |
Definition at line 213 of file rwlock_tester.h.
Referenced by clearStartFlag(), signalStart(), and waitForStart().
int RWLockTester::repeat [private] |
Definition at line 214 of file rwlock_tester.h.
Referenced by get_loops(), set_loops(), set_tests(), and thread_function().
int RWLockTester::threads [private] |
Definition at line 215 of file rwlock_tester.h.
Referenced by get_threads(), run_test(), set_tests(), and set_threads().
int RWLockTester::rw_ratio [private] |
Definition at line 217 of file rwlock_tester.h.
Referenced by run_test(), set_tests(), and thread_function().
RWLockLoopTest* RWLockTester::curr [private] |
Definition at line 218 of file rwlock_tester.h.
Referenced by run_test(), thread_function(), thread_function_read(), thread_function_write(), and validate().
std::vector<RWLockLoopTest *> RWLockTester::lock_tests [private] |
Definition at line 220 of file rwlock_tester.h.
Referenced by add_lock_test(), begin(), clear_summary(), end(), operator[](), print_summary(), run_test(), size(), validate(), and ~RWLockTester().
volatile int RWLockTester::current_threads [private] |
Definition at line 222 of file rwlock_tester.h.
Referenced by run_test(), thread_function(), thread_function_read(), thread_function_write(), and validate().
volatile int RWLockTester::current_active_threads [private] |
Definition at line 223 of file rwlock_tester.h.
Referenced by run_test(), thread_function(), thread_function_read(), and thread_function_write().
volatile int RWLockTester::current_read_threads [private] |
Definition at line 224 of file rwlock_tester.h.
Referenced by thread_function_read(), and validate().
volatile int RWLockTester::current_write_threads [private] |
Definition at line 225 of file rwlock_tester.h.
Referenced by thread_function_write(), and validate().
volatile int RWLockTester::current_rw_threads [private] |
Definition at line 226 of file rwlock_tester.h.
volatile int RWLockTester::current_wr_threads [private] |
Definition at line 227 of file rwlock_tester.h.
SyncOStream& RWLockTester::out [private] |
Definition at line 228 of file rwlock_tester.h.
Referenced by get_ostream(), print_summary(), run_test(), and validate().
SyncOStream RWLockTester::stdSyncOStream [static, private] |
Definition at line 230 of file rwlock_tester.h.
1.5.5