00001 #include <stdlib.h>
00002 #include <stdio.h>
00003 #include <time.h>
00004 #include <assert.h>
00005 #include <iostream>
00006 #include <fstream>
00007 #include <assert.h>
00008
00009 #ifndef WIN32
00010 #include <sys/time.h>
00011 #include <sys/resource.h>
00012 #endif
00013
00014 #include "rwlock_tester.h"
00015 #include "MutexRWLock.h"
00016 #include "classic_rwlock.h"
00017 #include "SG_RWLock.h"
00018 #include "rwlock.h"
00019 #include "SimpleRWLock.h"
00020 #include "evg_rwlock.h"
00021 #include "evg_rwlock_1.h"
00022 #include "baseline_lock.h"
00023
00024
00025 #ifdef WIN32
00026 #define PREFIX "Win32_"
00027 #else
00028 #define PREFIX "Linux_"
00029 #ifdef PTHREAD_RWLOCK_INITIALIZER
00030 #include "pthread_rwlock.h"
00031 #endif
00032 #endif
00033
00034 const int gThreadsCount[] = { 1,2,4,8,12,16,20,24,32 };
00035
00036
00037 const size_t gThreadsCountLength = sizeof(gThreadsCount)/sizeof(int);
00038
00039 int main(int argc, char* argv[])
00040 {
00041 int a=0, b=1, c=2, d=3, e=4;
00042
00043 a = atomic_inc_return(&b);
00044 assert(a==2);
00045 assert(b==2);
00046
00047 d = atomic_cmpexch_return(&a, 10, 2);
00048 assert(d==2);
00049 assert(a==10);
00050
00051 d = atomic_cmpexch_return(&a, 11, 1);
00052 assert(d==10);
00053 assert(a==10);
00054
00055 b = atomic_dec_return(&a);
00056 assert(a==9);
00057 assert(b==9);
00058
00059 b = atomic_add_return(&a, 3);
00060 assert(a==12);
00061 assert(b==9);
00062
00063 atomic_dec(&e);
00064 assert(e==3);
00065
00066 atomic_inc(&d);
00067 assert(d==11);
00068
00069 RWLockTester tester;
00070 GraphData graph;
00071
00072 tester.add_lock_test(new RWLockLoopTestImpl<MutexRWLock>(tester.get_ostream()));
00073 tester.add_lock_test(new RWLockLoopTestImpl<ClassicRWLock>(tester.get_ostream()));
00074 #ifndef WIN32
00075 #ifdef PTHREAD_RWLOCK_INITIALIZER
00076 tester.add_lock_test(new RWLockLoopTestImpl<PThreadRWLock>(tester.get_ostream()));
00077 #endif
00078 #endif
00079 tester.add_lock_test(new RWLockLoopTestImpl<NonblockingRWLock>(tester.get_ostream()));
00080 tester.add_lock_test(new RWLockLoopTestImpl<SG_RWLock>(tester.get_ostream()));
00081 tester.add_lock_test(new RWLockLoopTestImpl<SimpleRWLock>(tester.get_ostream()));
00082 tester.add_lock_test(new RWLockLoopTestImpl<SharedCounterRWLock>(tester.get_ostream()));
00083
00084 tester.validate();
00085
00086
00087
00088 tester.add_lock_test(new RWLockLoopTestImpl<BASELINERWLock>(tester.get_ostream(), kPerformanceLoopsNumber, false));
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 #ifndef WIN32
00105 int setprres = setpriority(PRIO_PROCESS, 0, -20);
00106 if(setprres < 0)
00107 {
00108 cerr << "Set Priority Error: " << setprres << " errno:" << errno << std::endl;
00109 }
00110 #else
00111 if(!SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS))
00112 {
00113 LPVOID lpMsgBuf = (LPVOID)"";
00114 FormatMessage(
00115 FORMAT_MESSAGE_ALLOCATE_BUFFER |
00116 FORMAT_MESSAGE_FROM_SYSTEM |
00117 FORMAT_MESSAGE_IGNORE_INSERTS,
00118 NULL,
00119 GetLastError(),
00120 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00121 (LPTSTR) &lpMsgBuf,
00122 0,
00123 NULL );
00124 cerr << "Set Priority Error: " << (char *)lpMsgBuf << std::endl;
00125 }
00126 #endif
00127
00128 graph.init(tester, gThreadsCount, gThreadsCountLength);
00129
00130 tester.get_ostream() << (char*)"\nStarting Sequential Read test (thread(s) with read loop only...).\n";
00131
00132 tester.clear_summary();
00133
00134
00135
00136 for(int i=0; i<gThreadsCountLength; i++)
00137 {
00138 tester.run_test(gThreadsCount[i], kPerformanceLoopsNumber*2, &RWLockLoopTest::run_r);
00139 graph.add_results(tester, i);
00140 }
00141 tester.print_summary();
00142 graph.print_read(PREFIX"read_only_read_thread_time.txt");
00143 graph.print_total_time(PREFIX"read_only_total_time.txt");
00144
00145 tester.get_ostream() << (char*)"\nStarting Sequential Write test (thread(s) with write loop only...).\n";
00146
00147 tester.clear_summary();
00148
00149 for(int i=0; i<gThreadsCountLength; i++)
00150 {
00151 tester.run_test(gThreadsCount[i], kPerformanceLoopsNumber*2, &RWLockLoopTest::run_w);
00152 graph.add_results(tester, i);
00153 }
00154 tester.print_summary();
00155 graph.print_write(PREFIX"write_only_write_thread_time.txt");
00156 graph.print_total_time(PREFIX"write_only_total_time.txt");
00157
00158 tester.get_ostream() << (char*)"\nStarting Sequential Read/Write test (thread(s) with read/write loop executed along with thread(s) with write/read loop...).\n";
00159
00160 tester.clear_summary();
00161
00162 for(int i=0; i<gThreadsCountLength; i++)
00163 {
00164 tester.run_test(gThreadsCount[i], kPerformanceLoopsNumber);
00165 graph.add_results(tester, i);
00166 }
00167 tester.print_summary();
00168 graph.print_read(PREFIX"read_thread_time.txt");
00169 graph.print_write(PREFIX"write_thread_time.txt");
00170 graph.print_summary(PREFIX"summary_thread_time.txt");
00171 graph.print_total_time(PREFIX"total_time.txt");
00172
00173 mixed_start:
00174
00175 tester.get_ostream() << (char*)"\nStarting Mixed Read/Write test (each thread executes alternating read and write locks...).\n";
00176
00177 tester.clear_summary();
00178 for(int i=0; i<gThreadsCountLength; i++)
00179 {
00180 tester.run_test(gThreadsCount[i], kPerformanceLoopsNumber*2, &RWLockLoopTest::run_mixed);
00181 graph.add_results(tester, i);
00182 }
00183 tester.print_summary();
00184 graph.print_summary(PREFIX"mixed_results.txt");
00185 graph.print_total_time(PREFIX"mixed_total_time.txt");
00186 }