2009-04-14 20:21:05 +00:00
|
|
|
#include "../check_includes.h"
|
|
|
|
|
2007-06-11 21:36:57 +00:00
|
|
|
#include <stasis/transactional.h>
|
|
|
|
#include <stasis/lockManager.h>
|
2005-02-09 02:53:14 +00:00
|
|
|
|
2009-04-14 20:21:05 +00:00
|
|
|
#include <pthread.h>
|
2005-02-09 02:53:14 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2009-04-14 20:21:05 +00:00
|
|
|
|
2005-02-09 02:53:14 +00:00
|
|
|
#define LOG_NAME "check_lockManager.log"
|
|
|
|
|
|
|
|
/** Needs to be formatted as a floating point */
|
2005-02-10 03:51:09 +00:00
|
|
|
#define NUM_RECORDS 5000000.0
|
2005-02-09 02:53:14 +00:00
|
|
|
#define THREAD_COUNT 100
|
2005-02-10 03:51:09 +00:00
|
|
|
#define RIDS_PER_THREAD 1000
|
2005-02-09 02:53:14 +00:00
|
|
|
|
2005-02-10 03:51:09 +00:00
|
|
|
void * pageWorkerThread(void * j) {
|
|
|
|
int xid = *(int*)j;
|
|
|
|
globalLockManager.begin(xid);
|
|
|
|
recordid rid;
|
|
|
|
rid.page = 0;
|
|
|
|
rid.size = 0;
|
|
|
|
int k;
|
|
|
|
int deadlocks = 0;
|
|
|
|
for(k = 0; k < RIDS_PER_THREAD; k++) {
|
|
|
|
int m = (int) (NUM_RECORDS*random()/(RAND_MAX+1.0));
|
|
|
|
int rw = random() % 2;
|
|
|
|
|
|
|
|
if(rw) {
|
|
|
|
// readlock
|
2005-02-28 00:00:17 +00:00
|
|
|
int locked = 0;
|
2006-10-04 04:29:57 +00:00
|
|
|
if(LLADD_DEADLOCK == globalLockManager.readLockPage(xid, m)) {
|
2009-04-14 20:21:05 +00:00
|
|
|
k = 0;
|
2006-10-04 04:29:57 +00:00
|
|
|
assert(compensation_error() == LLADD_DEADLOCK);
|
|
|
|
compensation_clear_error();
|
|
|
|
globalLockManager.abort(xid);
|
|
|
|
deadlocks++;
|
|
|
|
locked = 1;
|
|
|
|
}
|
2005-02-10 03:51:09 +00:00
|
|
|
} else {
|
2009-04-14 20:21:05 +00:00
|
|
|
|
2005-02-10 03:51:09 +00:00
|
|
|
if(LLADD_DEADLOCK == globalLockManager.writeLockPage(xid, m)) {
|
2009-04-14 20:21:05 +00:00
|
|
|
k = 0;
|
2005-02-10 03:51:09 +00:00
|
|
|
globalLockManager.abort(xid);
|
|
|
|
deadlocks++;
|
2005-02-28 00:00:17 +00:00
|
|
|
int err = compensation_error();
|
|
|
|
assert(err == LLADD_DEADLOCK);
|
|
|
|
compensation_clear_error();
|
2005-02-10 03:51:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-14 20:21:05 +00:00
|
|
|
|
2005-02-10 03:51:09 +00:00
|
|
|
printf("%2d ", deadlocks); fflush(stdout);
|
|
|
|
|
|
|
|
globalLockManager.commit(xid);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
void * ridWorkerThread(void * j) {
|
2005-02-09 02:53:14 +00:00
|
|
|
|
|
|
|
int xid = *(int*)j;
|
2005-02-10 03:51:09 +00:00
|
|
|
globalLockManager.begin(xid);
|
2005-02-09 02:53:14 +00:00
|
|
|
recordid rid;
|
|
|
|
rid.page = 0;
|
|
|
|
rid.size = 0;
|
|
|
|
int k;
|
|
|
|
int deadlocks = 0;
|
|
|
|
for(k = 0; k < RIDS_PER_THREAD; k++) {
|
|
|
|
rid.slot = (int) (NUM_RECORDS*random()/(RAND_MAX+1.0));
|
|
|
|
int rw = random() % 2;
|
|
|
|
|
|
|
|
if(rw) {
|
|
|
|
// readlock
|
2005-02-16 04:11:14 +00:00
|
|
|
|
2006-10-04 04:29:57 +00:00
|
|
|
if(LLADD_DEADLOCK == globalLockManager.readLockRecord(xid, rid)) {
|
|
|
|
k = 0;
|
|
|
|
globalLockManager.abort(xid);
|
|
|
|
deadlocks++;
|
|
|
|
}
|
2009-04-14 20:21:05 +00:00
|
|
|
|
2005-02-09 02:53:14 +00:00
|
|
|
} else {
|
|
|
|
// writelock
|
|
|
|
|
2006-10-04 04:29:57 +00:00
|
|
|
if(LLADD_DEADLOCK == globalLockManager.writeLockRecord(xid, rid)) {
|
|
|
|
k = 0;
|
|
|
|
globalLockManager.abort(xid);
|
|
|
|
deadlocks++;
|
|
|
|
}
|
2005-02-09 02:53:14 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-14 20:21:05 +00:00
|
|
|
|
2005-02-09 02:53:14 +00:00
|
|
|
printf("%2d ", deadlocks); fflush(stdout);
|
|
|
|
|
2005-02-10 03:51:09 +00:00
|
|
|
globalLockManager.commit(xid);
|
2005-02-09 02:53:14 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-02-10 03:51:09 +00:00
|
|
|
START_TEST(recordidLockManagerTest) {
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
|
|
|
|
setupLockManagerCallbacksRecord();
|
|
|
|
|
|
|
|
pthread_t workers[THREAD_COUNT];
|
2009-04-14 20:21:05 +00:00
|
|
|
int i;
|
2006-10-04 04:29:57 +00:00
|
|
|
printf("The following numbers are deadlock counts for each thread.\n");
|
2005-02-10 03:51:09 +00:00
|
|
|
for(i = 0; i < THREAD_COUNT; i++) {
|
|
|
|
int *j = malloc(sizeof(int));
|
|
|
|
*j = i;
|
|
|
|
pthread_create(&workers[i], NULL, ridWorkerThread, j);
|
|
|
|
}
|
|
|
|
for(i = 0; i < THREAD_COUNT; i++) {
|
|
|
|
pthread_join(workers[i], NULL);
|
|
|
|
}
|
|
|
|
} END_TEST
|
|
|
|
|
|
|
|
START_TEST(pageLockManagerTest) {
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
|
|
|
|
setupLockManagerCallbacksPage();
|
2005-02-09 02:53:14 +00:00
|
|
|
|
|
|
|
pthread_t workers[THREAD_COUNT];
|
2009-04-14 20:21:05 +00:00
|
|
|
int i;
|
2006-10-04 04:29:57 +00:00
|
|
|
printf("The following numbers are deadlock counts for each thread.\n");
|
2005-02-09 02:53:14 +00:00
|
|
|
for(i = 0; i < THREAD_COUNT; i++) {
|
|
|
|
int *j = malloc(sizeof(int));
|
|
|
|
*j = i;
|
2005-02-10 03:51:09 +00:00
|
|
|
pthread_create(&workers[i], NULL, pageWorkerThread, j);
|
2005-02-09 02:53:14 +00:00
|
|
|
}
|
|
|
|
for(i = 0; i < THREAD_COUNT; i++) {
|
|
|
|
pthread_join(workers[i], NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
} END_TEST
|
|
|
|
|
|
|
|
Suite * check_suite(void) {
|
|
|
|
Suite *s = suite_create("lockManager");
|
|
|
|
/* Begin a new test */
|
|
|
|
TCase *tc = tcase_create("multithreaded");
|
2005-03-10 03:19:04 +00:00
|
|
|
tcase_set_timeout(tc, 0); // disable timeouts
|
2009-04-14 20:21:05 +00:00
|
|
|
// kinda hacky, but here it is:
|
2005-02-28 00:00:17 +00:00
|
|
|
compensations_init();
|
|
|
|
|
2005-02-09 02:53:14 +00:00
|
|
|
/* Sub tests are added, one per line, here */
|
|
|
|
|
2009-04-14 20:21:05 +00:00
|
|
|
tcase_add_test(tc, recordidLockManagerTest);
|
|
|
|
tcase_add_test(tc, pageLockManagerTest);
|
|
|
|
|
2005-02-09 02:53:14 +00:00
|
|
|
/* --------------------------------------------- */
|
2009-04-14 20:21:05 +00:00
|
|
|
|
2005-02-09 02:53:14 +00:00
|
|
|
tcase_add_checked_fixture(tc, setup, teardown);
|
|
|
|
|
|
|
|
|
|
|
|
suite_add_tcase(s, tc);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "../check_setup.h"
|