X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/a4175b2e216a20b86cc872dea8a08005c60617a5..8b9d106fcc61fc84712c97d4db060d8302cc63fd:/lock_tester.cc diff --git a/lock_tester.cc b/lock_tester.cc index d063cdc..b2df781 100644 --- a/lock_tester.cc +++ b/lock_tester.cc @@ -2,26 +2,16 @@ // Lock server tester // -#include "lock_protocol.h" #include "lock_client.h" -#include "rpc/rpc.h" #include -#include -#include -#include -#include "lang/verify.h" -#include "lock_client_cache_rsm.h" -#include "tprintf.h" -#include #include -#include "lock.h" -char tprintf_thread_prefix = 'c'; +char log_thread_prefix = 'c'; // must be >= 2 const int nt = 6; //XXX: lab1's rpc handlers are blocking. Since rpcs uses a thread pool of 10 threads, we cannot test more than 10 blocking rpc. -std::string dst; -lock_client_cache_rsm **lc = new lock_client_cache_rsm * [nt]; +string dst; +lock_client **lc = new lock_client * [nt]; lock_protocol::lockid_t a = "1"; lock_protocol::lockid_t b = "2"; lock_protocol::lockid_t c = "3"; @@ -30,37 +20,31 @@ lock_protocol::lockid_t c = "3"; // doesn't grant the same lock to both clients. // it assumes that lock names are distinct in the first byte. int ct[256]; -std::mutex count_mutex; +mutex count_mutex; -void -check_grant(lock_protocol::lockid_t lid) -{ +void check_grant(lock_protocol::lockid_t lid) { lock ml(count_mutex); int x = lid[0] & 0x0f; - if(ct[x] != 0){ - fprintf(stderr, "error: server granted %s twice\n", lid.c_str()); - fprintf(stdout, "error: server granted %s twice\n", lid.c_str()); + if (ct[x] != 0) { + cout << "error: server granted " << lid << " twice" << endl; + cerr << "error: server granted " << lid << " twice" << endl; exit(1); } ct[x] += 1; } -void -check_release(lock_protocol::lockid_t lid) -{ +void check_release(lock_protocol::lockid_t lid) { lock ml(count_mutex); int x = lid[0] & 0x0f; - if(ct[x] != 1){ - fprintf(stderr, "error: client released un-held lock %s\n", lid.c_str()); + if (ct[x] != 1) { + cerr << "error: client released un-held lock " << lid << endl; exit(1); } ct[x] -= 1; } -void -test1(void) -{ - tprintf ("acquire a release a acquire a release a\n"); +void test1(void) { + LOG_NONMEMBER("acquire a release a acquire a release a"); lc[0]->acquire(a); check_grant(a); lc[0]->release(a); @@ -70,7 +54,7 @@ test1(void) lc[0]->release(a); check_release(a); - tprintf ("acquire a acquire b release b release a\n"); + LOG_NONMEMBER("acquire a acquire b release b release a"); lc[0]->acquire(a); check_grant(a); lc[0]->acquire(b); @@ -81,85 +65,65 @@ test1(void) check_release(a); } -void * -test2(void *x) -{ - int i = * (int *) x; - - tprintf ("test2: client %d acquire a release a\n", i); +void test2(int i) { + LOG_NONMEMBER("test2: client " << i << " acquire a release a"); lc[i]->acquire(a); - tprintf ("test2: client %d acquire done\n", i); + LOG_NONMEMBER("test2: client " << i << " acquire done"); check_grant(a); - sleep(1); - tprintf ("test2: client %d release\n", i); + usleep(100000); + LOG_NONMEMBER("test2: client " << i << " release"); check_release(a); lc[i]->release(a); - tprintf ("test2: client %d release done\n", i); - return 0; + LOG_NONMEMBER("test2: client " << i << " release done"); } -void * -test3(void *x) -{ - int i = * (int *) x; - - tprintf ("test3: client %d acquire a release a concurrent\n", i); +void test3(int i) { + LOG_NONMEMBER("test3: client " << i << " acquire a release a concurrent"); for (int j = 0; j < 10; j++) { lc[i]->acquire(a); check_grant(a); - tprintf ("test3: client %d got lock\n", i); + LOG_NONMEMBER("test3: client " << i << " got lock"); check_release(a); lc[i]->release(a); } - return 0; } -void * -test4(void *x) -{ - int i = * (int *) x; - - tprintf ("test4: thread %d acquire a release a concurrent; same clnt\n", i); +void test4(int i) { + LOG_NONMEMBER("test4: thread " << i << " acquire a release a concurrent; same clnt"); for (int j = 0; j < 10; j++) { lc[0]->acquire(a); check_grant(a); - tprintf ("test4: thread %d on client 0 got lock\n", i); + LOG_NONMEMBER("test4: thread " << i << " on client 0 got lock"); check_release(a); lc[0]->release(a); } - return 0; } -void * -test5(void *x) -{ - int i = * (int *) x; - - tprintf ("test5: client %d acquire a release a concurrent; same and diff clnt\n", i); +void test5(int i) { + LOG_NONMEMBER("test5: client " << i << " acquire a release a concurrent; same and diff clnt"); for (int j = 0; j < 10; j++) { if (i < 5) lc[0]->acquire(a); else lc[1]->acquire(a); check_grant(a); - tprintf ("test5: client %d got lock\n", i); + LOG_NONMEMBER("test5: client " << i << " got lock"); check_release(a); if (i < 5) lc[0]->release(a); else lc[1]->release(a); } - return 0; } int main(int argc, char *argv[]) { - std::thread th[nt]; + thread th[nt]; int test = 0; setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); - srandom(getpid()); + srandom((uint32_t)getpid()); - if(argc < 2) { - fprintf(stderr, "Usage: %s [host:]port [test]\n", argv[0]); + if (argc < 2) { + cerr << "Usage: " << argv[0] << " [host:]port [test]" << endl; exit(1); } @@ -167,70 +131,54 @@ main(int argc, char *argv[]) if (argc > 2) { test = atoi(argv[2]); - if(test < 1 || test > 5){ - tprintf("Test number must be between 1 and 5\n"); + if (test < 1 || test > 5) { + LOG_NONMEMBER("Test number must be between 1 and 5"); exit(1); } } - tprintf("cache lock client\n"); - for (int i = 0; i < nt; i++) lc[i] = new lock_client_cache_rsm(dst); + LOG_NONMEMBER("cache lock client"); + for (int i = 0; i < nt; i++) lc[i] = new lock_client(dst); - if(!test || test == 1){ + if (!test || test == 1) { test1(); } - if(!test || test == 2){ + if (!test || test == 2) { // test2 - for (int i = 0; i < nt; i++) { - int *a = new int (i); - th[i] = std::thread(test2, a); - } - for (int i = 0; i < nt; i++) { + for (int i = 0; i < nt; i++) + th[i] = thread(test2, i); + for (int i = 0; i < nt; i++) th[i].join(); - } } - if(!test || test == 3){ - tprintf("test 3\n"); + if (!test || test == 3) { + LOG_NONMEMBER("test 3"); - // test3 - for (int i = 0; i < nt; i++) { - int *a = new int (i); - th[i] = std::thread(test3, a); - } - for (int i = 0; i < nt; i++) { + for (int i = 0; i < nt; i++) + th[i] = thread(test3, i); + for (int i = 0; i < nt; i++) th[i].join(); - } } - if(!test || test == 4){ - tprintf("test 4\n"); + if (!test || test == 4) { + LOG_NONMEMBER("test 4"); - // test 4 - for (int i = 0; i < 2; i++) { - int *a = new int (i); - th[i] = std::thread(test4, a); - } - for (int i = 0; i < 2; i++) { + for (int i = 0; i < 2; i++) + th[i] = thread(test4, i); + for (int i = 0; i < 2; i++) th[i].join(); - } } - if(!test || test == 5){ - tprintf("test 5\n"); - - // test 5 + if (!test || test == 5) { + LOG_NONMEMBER("test 5"); - for (int i = 0; i < nt; i++) { - int *a = new int (i); - th[i] = std::thread(test5, a); - } - for (int i = 0; i < nt; i++) { + for (int i = 0; i < nt; i++) + th[i] = thread(test5, i); + for (int i = 0; i < nt; i++) th[i].join(); - } } - tprintf ("%s: passed all tests successfully\n", argv[0]); + LOG_NONMEMBER(argv[0] << ": passed all tests successfully"); }