2009-04-14 20:21:05 +00:00
|
|
|
#include "../check_includes.h"
|
2007-03-10 01:29:43 +00:00
|
|
|
|
2007-06-11 21:36:57 +00:00
|
|
|
#include <stasis/transactional.h>
|
|
|
|
#include <stasis/replacementPolicy.h>
|
2011-08-31 19:25:09 +00:00
|
|
|
#include <stasis/util/random.h>
|
2009-04-14 20:21:05 +00:00
|
|
|
|
2007-03-10 01:29:43 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#define LOG_NAME "check_replacementPolicy.log"
|
|
|
|
|
2011-08-23 18:25:26 +00:00
|
|
|
#define OBJECT_COUNT 100
|
2007-03-10 01:29:43 +00:00
|
|
|
#define OP_COUNT 10000000
|
|
|
|
|
2010-03-31 22:37:42 +00:00
|
|
|
#define LONG_COUNT 100000000UL
|
|
|
|
#define SHORT_COUNT 10000000UL
|
|
|
|
|
|
|
|
#define THREAD_COUNT 10
|
|
|
|
|
2009-04-14 20:21:05 +00:00
|
|
|
typedef struct tracker {
|
2007-03-10 01:29:43 +00:00
|
|
|
int inCache;
|
2010-03-31 22:37:42 +00:00
|
|
|
pthread_mutex_t mut;
|
2007-03-10 01:29:43 +00:00
|
|
|
} tracker;
|
2011-04-20 20:25:17 +00:00
|
|
|
|
2010-03-31 22:37:42 +00:00
|
|
|
static pthread_mutex_t cached_count_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static int threaded = 0;
|
|
|
|
static int cachedCount = 0;
|
2007-03-11 07:53:32 +00:00
|
|
|
|
2010-03-31 22:37:42 +00:00
|
|
|
tracker * t;
|
2011-08-23 18:25:26 +00:00
|
|
|
Page* pages;
|
2012-12-05 02:17:23 +00:00
|
|
|
void randomSetup(void) {
|
2007-03-10 01:29:43 +00:00
|
|
|
time_t seed = time(0);
|
|
|
|
printf("\nSeed = %ld\n", seed);
|
|
|
|
srandom(seed);
|
|
|
|
|
2010-03-31 22:37:42 +00:00
|
|
|
cachedCount = 0;
|
2009-04-14 20:21:05 +00:00
|
|
|
|
2012-12-01 02:29:28 +00:00
|
|
|
t = stasis_calloc(OBJECT_COUNT, tracker);
|
|
|
|
pages = stasis_calloc(OBJECT_COUNT, Page);
|
2009-04-14 20:21:05 +00:00
|
|
|
for(int i = 0; i < OBJECT_COUNT; i++) {
|
2011-08-23 18:25:26 +00:00
|
|
|
pages[i].id = i;
|
|
|
|
pages[i].pinCount = 1;
|
|
|
|
pages[i].next = 0;
|
2007-03-10 01:29:43 +00:00
|
|
|
}
|
2010-03-31 22:37:42 +00:00
|
|
|
|
|
|
|
}
|
2012-12-05 02:17:23 +00:00
|
|
|
void randomTeardown(void) {
|
2010-03-31 22:37:42 +00:00
|
|
|
free(t);
|
2011-08-23 18:25:26 +00:00
|
|
|
free(pages);
|
2010-03-31 22:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void randomTest(replacementPolicy * lru, unsigned long count) {
|
|
|
|
|
|
|
|
unsigned long progress_indicator = count / 10;
|
|
|
|
|
|
|
|
for(unsigned long j = 0; j < count /*100000000UL*/; j++) {
|
|
|
|
if(0 == (j % progress_indicator)) { printf("."); fflush(stdout); }
|
2011-08-31 19:25:09 +00:00
|
|
|
int op = stasis_util_random64(100);
|
2007-03-10 01:29:43 +00:00
|
|
|
|
2011-08-31 19:25:09 +00:00
|
|
|
int i = stasis_util_random64(OBJECT_COUNT);
|
2009-04-14 20:21:05 +00:00
|
|
|
if(op < 10) {
|
2007-03-10 01:29:43 +00:00
|
|
|
// TOGGLE IN CACHE
|
2010-03-31 22:37:42 +00:00
|
|
|
pthread_mutex_lock(&t[i].mut);
|
2009-04-14 20:21:05 +00:00
|
|
|
if(!t[i].inCache) {
|
2011-08-23 18:25:26 +00:00
|
|
|
lru->insert(lru, &pages[i]);
|
2010-03-31 22:37:42 +00:00
|
|
|
t[i].inCache = 1;
|
|
|
|
pthread_mutex_lock(&cached_count_mutex);
|
|
|
|
cachedCount ++;
|
|
|
|
pthread_mutex_unlock(&cached_count_mutex);
|
2009-04-14 20:21:05 +00:00
|
|
|
} else {
|
2011-08-23 18:25:26 +00:00
|
|
|
Page *p = lru->remove(lru, &pages[i]);
|
|
|
|
assert(p == &pages[i]);
|
2010-03-31 22:37:42 +00:00
|
|
|
t[i].inCache = 0;
|
|
|
|
pthread_mutex_lock(&cached_count_mutex);
|
|
|
|
cachedCount --;
|
|
|
|
pthread_mutex_unlock(&cached_count_mutex);
|
2007-03-10 01:29:43 +00:00
|
|
|
}
|
2010-03-31 22:37:42 +00:00
|
|
|
pthread_mutex_unlock(&t[i].mut);
|
2009-04-14 20:21:05 +00:00
|
|
|
} else if(op < 30) {
|
2007-03-10 01:29:43 +00:00
|
|
|
// Get stale + remove
|
2011-08-23 18:25:26 +00:00
|
|
|
Page *p = lru->getStale(lru);
|
|
|
|
if( p ) {
|
|
|
|
pthread_mutex_lock(&t[p->id].mut);
|
|
|
|
if(t[p->id].inCache) {
|
|
|
|
assert(p == &pages[p->id]);
|
|
|
|
p = lru->remove(lru, p);
|
|
|
|
assert(p == &pages[p->id]);
|
|
|
|
t[p->id].inCache = 0;
|
2010-03-31 22:37:42 +00:00
|
|
|
pthread_mutex_lock(&cached_count_mutex);
|
|
|
|
if(!threaded) assert(cachedCount != 0);
|
|
|
|
cachedCount --;
|
|
|
|
pthread_mutex_unlock(&cached_count_mutex);
|
|
|
|
}
|
2011-08-23 18:25:26 +00:00
|
|
|
pthread_mutex_unlock(&t[p->id].mut);
|
2009-04-14 20:21:05 +00:00
|
|
|
} else {
|
2010-03-31 22:37:42 +00:00
|
|
|
if(!threaded) {
|
|
|
|
assert(cachedCount == 0);
|
|
|
|
}
|
2007-03-10 01:29:43 +00:00
|
|
|
}
|
2009-04-14 20:21:05 +00:00
|
|
|
} else if(op < 50) {
|
2007-03-10 01:29:43 +00:00
|
|
|
// Get stale
|
2011-08-23 18:25:26 +00:00
|
|
|
Page * p = lru->getStale(lru);
|
|
|
|
if(p) {
|
|
|
|
pthread_mutex_lock(&t[p->id].mut);
|
|
|
|
if(!threaded) assert(t[p->id].inCache);
|
|
|
|
assert(p == &pages[p->id]);
|
2010-03-31 22:37:42 +00:00
|
|
|
if(!threaded) assert(cachedCount != 0);
|
2011-08-23 18:25:26 +00:00
|
|
|
pthread_mutex_unlock(&t[p->id].mut);
|
2009-04-14 20:21:05 +00:00
|
|
|
} else {
|
2010-03-31 22:37:42 +00:00
|
|
|
if(!threaded) assert(cachedCount == 0);
|
2007-03-10 01:29:43 +00:00
|
|
|
}
|
2009-04-14 20:21:05 +00:00
|
|
|
} else {
|
2007-03-10 01:29:43 +00:00
|
|
|
// Hit
|
2010-03-31 22:37:42 +00:00
|
|
|
pthread_mutex_lock(&t[i].mut);
|
2011-08-23 18:25:26 +00:00
|
|
|
if(t[i].inCache) lru->hit(lru, &pages[i]);
|
2010-03-31 22:37:42 +00:00
|
|
|
pthread_mutex_unlock(&t[i].mut);
|
2007-03-10 01:29:43 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-11 07:53:32 +00:00
|
|
|
}
|
2010-04-21 21:34:00 +00:00
|
|
|
|
|
|
|
void fillThenEmptyTest(replacementPolicy *lru) {
|
|
|
|
for(int i = 0; i < OBJECT_COUNT; i++) {
|
2011-08-23 18:25:26 +00:00
|
|
|
lru->insert(lru, &pages[i]);
|
2010-04-21 21:34:00 +00:00
|
|
|
}
|
|
|
|
int j = 0;
|
|
|
|
while(lru->getStaleAndRemove(lru)) {
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
assert(0 == lru->getStaleAndRemove(lru));
|
|
|
|
assert(0 == lru->getStale(lru));
|
|
|
|
assert(j == OBJECT_COUNT);
|
|
|
|
for(int i = 0; i < OBJECT_COUNT; i++) {
|
2011-08-23 18:25:26 +00:00
|
|
|
lru->insert(lru, &pages[i]);
|
|
|
|
lru->remove(lru, &pages[i]);
|
2010-04-21 21:34:00 +00:00
|
|
|
}
|
|
|
|
j = 0;
|
|
|
|
while(lru->getStaleAndRemove(lru)) {
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
assert(j == 0);
|
|
|
|
}
|
|
|
|
|
2007-03-11 07:53:32 +00:00
|
|
|
START_TEST(replacementPolicyLRURandomTest) {
|
2011-04-20 20:25:17 +00:00
|
|
|
replacementPolicy * lru = lruFastInit();
|
2010-03-31 22:37:42 +00:00
|
|
|
threaded = 0;
|
|
|
|
randomSetup();
|
|
|
|
randomTest(lru, LONG_COUNT);
|
|
|
|
lru->deinit(lru);
|
|
|
|
randomTeardown();
|
2007-03-11 07:53:32 +00:00
|
|
|
} END_TEST
|
|
|
|
START_TEST(replacementPolicyLRUFastRandomTest) {
|
2011-04-20 20:25:17 +00:00
|
|
|
replacementPolicy * lru = lruFastInit();
|
2010-03-31 22:37:42 +00:00
|
|
|
threaded = 0;
|
|
|
|
randomSetup();
|
|
|
|
randomTest(lru, LONG_COUNT);
|
|
|
|
lru->deinit(lru);
|
|
|
|
randomTeardown();
|
|
|
|
} END_TEST
|
|
|
|
START_TEST(replacementPolicyThreadsafeRandomTest) {
|
2011-04-20 20:25:17 +00:00
|
|
|
replacementPolicy * lru = lruFastInit();
|
2010-03-31 22:37:42 +00:00
|
|
|
replacementPolicy * tsLru = replacementPolicyThreadsafeWrapperInit(lru);
|
|
|
|
threaded = 0;
|
|
|
|
randomSetup();
|
|
|
|
randomTest(tsLru, LONG_COUNT);
|
|
|
|
tsLru->deinit(tsLru);
|
|
|
|
randomTeardown();
|
|
|
|
|
|
|
|
} END_TEST
|
|
|
|
START_TEST(replacementPolicyConcurrentRandomTest) {
|
|
|
|
int LRU_COUNT = OBJECT_COUNT / 51;
|
2013-02-13 21:27:51 +00:00
|
|
|
replacementPolicy** lru = stasis_alloca(LRU_COUNT, replacementPolicy*);
|
2010-03-31 22:37:42 +00:00
|
|
|
for(int i = 0; i < LRU_COUNT; i++) {
|
2011-04-20 20:25:17 +00:00
|
|
|
lru[i] = lruFastInit();
|
2010-03-31 22:37:42 +00:00
|
|
|
}
|
|
|
|
threaded = 0;
|
|
|
|
replacementPolicy * cwLru = replacementPolicyConcurrentWrapperInit(lru, LRU_COUNT);
|
|
|
|
randomSetup();
|
|
|
|
randomTest(cwLru, SHORT_COUNT);
|
|
|
|
cwLru->deinit(cwLru);
|
|
|
|
randomTeardown();
|
|
|
|
} END_TEST
|
2011-08-23 18:25:26 +00:00
|
|
|
START_TEST(replacementPolicyClockRandomTest) {
|
|
|
|
threaded = 0;
|
|
|
|
randomSetup();
|
|
|
|
replacementPolicy * lru = replacementPolicyClockInit(pages, OBJECT_COUNT);
|
|
|
|
randomTest(lru, LONG_COUNT);
|
|
|
|
lru->deinit(lru);
|
|
|
|
randomTeardown();
|
|
|
|
} END_TEST
|
2010-03-31 22:37:42 +00:00
|
|
|
replacementPolicy * worker_lru;
|
|
|
|
unsigned long worker_count;
|
|
|
|
void * randomTestWorker(void * arg) {
|
|
|
|
randomTest(worker_lru, worker_count);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
START_TEST(replacementPolicyThreadsafeThreadTest) {
|
2011-04-20 20:25:17 +00:00
|
|
|
replacementPolicy * lru = lruFastInit();
|
2010-03-31 22:37:42 +00:00
|
|
|
replacementPolicy * tsLru = replacementPolicyThreadsafeWrapperInit(lru);
|
|
|
|
threaded = 1;
|
|
|
|
worker_lru = tsLru;
|
|
|
|
worker_count = LONG_COUNT / THREAD_COUNT;
|
|
|
|
pthread_t threads[THREAD_COUNT];
|
|
|
|
randomSetup();
|
|
|
|
for(int i = 0; i < THREAD_COUNT; i++) {
|
|
|
|
pthread_create(&threads[i], 0, randomTestWorker, 0);
|
|
|
|
}
|
|
|
|
for(int i = 0; i < THREAD_COUNT; i++) {
|
|
|
|
pthread_join(threads[i], 0);
|
|
|
|
}
|
|
|
|
tsLru->deinit(tsLru);
|
|
|
|
randomTeardown();
|
|
|
|
} END_TEST
|
|
|
|
START_TEST(replacementPolicyConcurrentThreadTest) {
|
|
|
|
int LRU_COUNT = OBJECT_COUNT / 51;
|
2013-02-13 21:27:51 +00:00
|
|
|
replacementPolicy ** lru = stasis_alloca(LRU_COUNT,replacementPolicy*);
|
2010-03-31 22:37:42 +00:00
|
|
|
for(int i = 0; i < LRU_COUNT; i++) {
|
2011-04-20 20:25:17 +00:00
|
|
|
lru[i] = lruFastInit();
|
2010-03-31 22:37:42 +00:00
|
|
|
}
|
2011-08-23 18:25:26 +00:00
|
|
|
replacementPolicy * cwLru = replacementPolicyConcurrentWrapperInit(lru, LRU_COUNT);
|
2010-03-31 22:37:42 +00:00
|
|
|
threaded = 1;
|
|
|
|
worker_lru = cwLru;
|
|
|
|
worker_count = LONG_COUNT / THREAD_COUNT;
|
2013-02-13 21:27:51 +00:00
|
|
|
pthread_t *threads = stasis_alloca(THREAD_COUNT, pthread_t);
|
2010-03-31 22:37:42 +00:00
|
|
|
randomSetup();
|
|
|
|
for(int i = 0; i < THREAD_COUNT; i++) {
|
|
|
|
pthread_create(&threads[i], 0, randomTestWorker, 0);
|
|
|
|
}
|
|
|
|
for(int i = 0; i < THREAD_COUNT; i++) {
|
|
|
|
pthread_join(threads[i], 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
cwLru->deinit(cwLru);
|
|
|
|
randomTeardown();
|
2007-03-10 01:29:43 +00:00
|
|
|
} END_TEST
|
2011-08-23 18:25:26 +00:00
|
|
|
|
|
|
|
START_TEST(replacementPolicyClockThreadTest) {
|
|
|
|
replacementPolicy * clockLru = replacementPolicyClockInit(pages, OBJECT_COUNT);
|
|
|
|
threaded = 1;
|
|
|
|
worker_lru = clockLru;
|
|
|
|
worker_count = LONG_COUNT / THREAD_COUNT;
|
|
|
|
pthread_t threads[THREAD_COUNT];
|
|
|
|
randomSetup();
|
|
|
|
for(int i = 0; i < THREAD_COUNT; i++) {
|
|
|
|
pthread_create(&threads[i], 0, randomTestWorker, 0);
|
|
|
|
}
|
|
|
|
for(int i = 0; i < THREAD_COUNT; i++) {
|
|
|
|
pthread_join(threads[i], 0);
|
|
|
|
}
|
|
|
|
clockLru->deinit(clockLru);
|
|
|
|
randomTeardown();
|
|
|
|
} END_TEST
|
|
|
|
|
|
|
|
|
2010-04-21 21:34:00 +00:00
|
|
|
START_TEST(replacementPolicyEmptyFastLRUTest) {
|
|
|
|
randomSetup();
|
2011-04-20 20:25:17 +00:00
|
|
|
replacementPolicy *rp = lruFastInit();
|
2010-04-21 21:34:00 +00:00
|
|
|
fillThenEmptyTest(rp);
|
|
|
|
rp->deinit(rp);
|
|
|
|
randomTeardown();
|
|
|
|
} END_TEST
|
|
|
|
START_TEST(replacementPolicyEmptyThreadsafeTest) {
|
|
|
|
randomSetup();
|
2011-04-20 20:25:17 +00:00
|
|
|
replacementPolicy *rpA = lruFastInit();
|
2010-04-21 21:34:00 +00:00
|
|
|
replacementPolicy *rp = replacementPolicyThreadsafeWrapperInit(rpA);
|
|
|
|
fillThenEmptyTest(rp);
|
|
|
|
rp->deinit(rp);
|
|
|
|
randomTeardown();
|
|
|
|
} END_TEST
|
|
|
|
START_TEST(replacementPolicyEmptyConcurrentTest) {
|
|
|
|
randomSetup();
|
|
|
|
replacementPolicy *rpA[THREAD_COUNT];
|
|
|
|
for(int i = 0; i < THREAD_COUNT; i++) {
|
2011-04-20 20:25:17 +00:00
|
|
|
rpA[i] = lruFastInit();
|
2010-04-21 21:34:00 +00:00
|
|
|
}
|
|
|
|
replacementPolicy *rp
|
|
|
|
= replacementPolicyConcurrentWrapperInit(rpA, THREAD_COUNT);
|
|
|
|
fillThenEmptyTest(rp);
|
|
|
|
rp->deinit(rp);
|
|
|
|
randomTeardown();
|
|
|
|
} END_TEST
|
2011-08-23 18:25:26 +00:00
|
|
|
START_TEST(replacementPolicyEmptyClockTest) {
|
|
|
|
randomSetup();
|
|
|
|
replacementPolicy *rp = replacementPolicyClockInit(pages, OBJECT_COUNT);
|
|
|
|
fillThenEmptyTest(rp);
|
|
|
|
rp->deinit(rp);
|
|
|
|
randomTeardown();
|
|
|
|
} END_TEST
|
2007-03-10 01:29:43 +00:00
|
|
|
Suite * check_suite(void) {
|
2010-03-31 22:37:42 +00:00
|
|
|
Suite *s = suite_create("replacementPolicy");
|
2007-03-10 01:29:43 +00:00
|
|
|
/* Begin a new test */
|
|
|
|
TCase *tc = tcase_create("multithreaded");
|
|
|
|
tcase_set_timeout(tc, 1200); // twenty minute timeout
|
|
|
|
/* Sub tests are added, one per line, here */
|
2010-04-21 21:34:00 +00:00
|
|
|
tcase_add_test(tc, replacementPolicyEmptyFastLRUTest);
|
|
|
|
tcase_add_test(tc, replacementPolicyEmptyThreadsafeTest);
|
|
|
|
tcase_add_test(tc, replacementPolicyEmptyConcurrentTest);
|
2011-08-23 18:25:26 +00:00
|
|
|
tcase_add_test(tc, replacementPolicyEmptyClockTest);
|
2009-04-14 20:21:05 +00:00
|
|
|
tcase_add_test(tc, replacementPolicyLRURandomTest);
|
|
|
|
tcase_add_test(tc, replacementPolicyLRUFastRandomTest);
|
2010-03-31 22:37:42 +00:00
|
|
|
tcase_add_test(tc, replacementPolicyThreadsafeRandomTest);
|
|
|
|
tcase_add_test(tc, replacementPolicyConcurrentRandomTest);
|
2011-08-23 18:25:26 +00:00
|
|
|
tcase_add_test(tc, replacementPolicyClockRandomTest);
|
2010-03-31 22:37:42 +00:00
|
|
|
tcase_add_test(tc, replacementPolicyThreadsafeThreadTest);
|
|
|
|
tcase_add_test(tc, replacementPolicyConcurrentThreadTest);
|
2011-08-23 18:25:26 +00:00
|
|
|
tcase_add_test(tc, replacementPolicyClockThreadTest);
|
2010-03-31 22:37:42 +00:00
|
|
|
|
2007-03-10 01:29:43 +00:00
|
|
|
|
|
|
|
/* --------------------------------------------- */
|
2009-04-14 20:21:05 +00:00
|
|
|
|
2007-03-10 01:29:43 +00:00
|
|
|
tcase_add_checked_fixture(tc, setup, teardown);
|
|
|
|
|
|
|
|
|
|
|
|
suite_add_tcase(s, tc);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "../check_setup.h"
|