X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/5fd8cc8409d0efadc07dfe8d6774ad9ff477663d..f0dcb6b97d6d40f67698d1f71ac26970f1776f82:/lock_tester.cc diff --git a/lock_tester.cc b/lock_tester.cc index dd7c07b..f535d8f 100644 --- a/lock_tester.cc +++ b/lock_tester.cc @@ -2,63 +2,48 @@ // Lock server tester // -#include "lock_protocol.h" #include "lock_client.h" -#include "rpc.h" -#include "jsl_log.h" #include -#include -#include -#include -#include "lang/verify.h" -#include "lock_client_cache_rsm.h" -#include "tprintf.h" +#include -char tprintf_thread_prefix = 'c'; +char log_thread_prefix = 'c'; // must be >= 2 -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]; -lock_protocol::lockid_t a = 1; -lock_protocol::lockid_t b = 2; -lock_protocol::lockid_t c = 3; +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. +static string dst; +static lock_client **lc = new lock_client * [nt]; +static lock_protocol::lockid_t a = "1"; +static lock_protocol::lockid_t b = "2"; +static lock_protocol::lockid_t c = "3"; // check_grant() and check_release() check that the lock server // doesn't grant the same lock to both clients. // it assumes that lock names are distinct in the first byte. -int ct[256]; -pthread_mutex_t count_mutex; - -void -check_grant(lock_protocol::lockid_t lid) -{ - ScopedLock ml(&count_mutex); - int x = lid & 0xff; - if(ct[x] != 0){ - fprintf(stderr, "error: server granted %016llx twice\n", lid); - fprintf(stdout, "error: server granted %016llx twice\n", lid); - exit(1); - } - ct[x] += 1; +static int ct[256]; +static mutex count_mutex; + +void check_grant(lock_protocol::lockid_t lid) { + lock ml(count_mutex); + int x = lid[0] & 0x0f; + if (ct[x] != 0) { + LOG_NONMEMBER("error: server granted " << lid << " twice"); + exit(1); + } + ct[x] += 1; } -void -check_release(lock_protocol::lockid_t lid) -{ - ScopedLock ml(&count_mutex); - int x = lid & 0xff; - if(ct[x] != 1){ - fprintf(stderr, "error: client released un-held lock %016llx\n", lid); - exit(1); - } - ct[x] -= 1; +void check_release(lock_protocol::lockid_t lid) { + lock ml(count_mutex); + int x = lid[0] & 0x0f; + if (ct[x] != 1) { + LOG_NONMEMBER("error: client released un-held lock " << lid); + 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); @@ -68,7 +53,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); @@ -79,164 +64,120 @@ test1(void) check_release(a); } -void * -test2(void *x) -{ - int i = * (int *) x; - - tprintf ("test2: client %d acquire a release a\n", i); - lc[i]->acquire(a); - tprintf ("test2: client %d acquire done\n", i); - check_grant(a); - sleep(1); - tprintf ("test2: client %d release\n", i); - check_release(a); - lc[i]->release(a); - tprintf ("test2: client %d release done\n", i); - return 0; -} - -void * -test3(void *x) -{ - int i = * (int *) x; - - tprintf ("test3: client %d acquire a release a concurrent\n", i); - for (int j = 0; j < 10; j++) { +void test2(int i) { + LOG_NONMEMBER("test2: client " << i << " acquire a release a"); lc[i]->acquire(a); + LOG_NONMEMBER("test2: client " << i << " acquire done"); check_grant(a); - tprintf ("test3: client %d got lock\n", i); + usleep(100000); + LOG_NONMEMBER("test2: client " << i << " release"); check_release(a); lc[i]->release(a); - } - return 0; + LOG_NONMEMBER("test2: client " << i << " release done"); } -void * -test4(void *x) -{ - int i = * (int *) x; - - tprintf ("test4: thread %d acquire a release a concurrent; same clnt\n", i); - 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); - check_release(a); - lc[0]->release(a); - } - return 0; +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); + LOG_NONMEMBER("test3: client " << i << " got lock"); + check_release(a); + lc[i]->release(a); + } } -void * -test5(void *x) -{ - int i = * (int *) x; +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); + LOG_NONMEMBER("test4: thread " << i << " on client 0 got lock"); + check_release(a); + lc[0]->release(a); + } +} - tprintf ("test5: client %d acquire a release a concurrent; same and diff clnt\n", i); - 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); - check_release(a); - if (i < 5) lc[0]->release(a); - else lc[1]->release(a); - } - return 0; +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); + LOG_NONMEMBER("test5: client " << i << " got lock"); + check_release(a); + if (i < 5) lc[0]->release(a); + else lc[1]->release(a); + } } int main(int argc, char *argv[]) { - int r; - pthread_t th[nt]; + thread th[nt]; int test = 0; setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); - srandom(getpid()); + srandom((uint32_t)getpid()); - //jsl_set_debug(2); - - if(argc < 2) { - fprintf(stderr, "Usage: %s [host:]port [test]\n", argv[0]); - exit(1); + if (argc < 2) { + LOG_NONMEMBER("Usage: " << argv[0] << " [host:]port [test]"); + exit(1); } dst = argv[1]; if (argc > 2) { - test = atoi(argv[2]); - if(test < 1 || test > 5){ - tprintf("Test number must be between 1 and 5\n"); - exit(1); - } + test = atoi(argv[2]); + if (test < 1 || test > 5) { + LOG_NONMEMBER("Test number must be between 1 and 5"); + exit(1); + } } - VERIFY(pthread_mutex_init(&count_mutex, NULL) == 0); - 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){ - test1(); + if (!test || test == 1) { + test1(); } - if(!test || test == 2){ - // test2 - for (int i = 0; i < nt; i++) { - int *a = new int (i); - r = pthread_create(&th[i], NULL, test2, (void *) a); - VERIFY (r == 0); - } - for (int i = 0; i < nt; i++) { - pthread_join(th[i], NULL); - } + if (!test || test == 2) { + // test2 + 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"); - - // test3 - for (int i = 0; i < nt; i++) { - int *a = new int (i); - r = pthread_create(&th[i], NULL, test3, (void *) a); - VERIFY (r == 0); - } - for (int i = 0; i < nt; i++) { - pthread_join(th[i], NULL); - } + if (!test || test == 3) { + LOG_NONMEMBER("test 3"); + + 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"); - - // test 4 - for (int i = 0; i < 2; i++) { - int *a = new int (i); - r = pthread_create(&th[i], NULL, test4, (void *) a); - VERIFY (r == 0); - } - for (int i = 0; i < 2; i++) { - pthread_join(th[i], NULL); - } + if (!test || test == 4) { + LOG_NONMEMBER("test 4"); + + 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 - - for (int i = 0; i < nt; i++) { - int *a = new int (i); - r = pthread_create(&th[i], NULL, test5, (void *) a); - VERIFY (r == 0); - } - for (int i = 0; i < nt; i++) { - pthread_join(th[i], NULL); - } + if (!test || test == 5) { + LOG_NONMEMBER("test 5"); + + 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"); }