2007-03-10 01:29:43 +00:00
|
|
|
#include <check.h>
|
|
|
|
|
|
|
|
#include <config.h>
|
2007-06-11 21:36:57 +00:00
|
|
|
#include <stasis/transactional.h>
|
|
|
|
#include <stasis/replacementPolicy.h>
|
|
|
|
#include <stasis/doubleLinkedList.h>
|
2007-03-10 01:29:43 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include "../check_includes.h"
|
|
|
|
|
|
|
|
#define LOG_NAME "check_replacementPolicy.log"
|
|
|
|
|
|
|
|
long myrandom(long x) {
|
|
|
|
double xx = x;
|
|
|
|
double r = random();
|
|
|
|
double max = ((uint64_t)RAND_MAX)+1;
|
|
|
|
max /= xx;
|
|
|
|
return (long)((r/max));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define OBJECT_COUNT 1000
|
|
|
|
#define OP_COUNT 10000000
|
|
|
|
|
2007-03-11 07:53:32 +00:00
|
|
|
typedef struct LL_ENTRY(node_t) node_t;
|
|
|
|
|
2007-03-10 01:29:43 +00:00
|
|
|
typedef struct tracker {
|
2007-03-13 09:56:39 +00:00
|
|
|
long val;
|
2007-03-11 07:53:32 +00:00
|
|
|
intptr_t key;
|
2007-03-10 01:29:43 +00:00
|
|
|
int inCache;
|
|
|
|
} tracker;
|
2007-03-11 07:53:32 +00:00
|
|
|
|
|
|
|
static node_t * getKey(void * page, void * ignore) {
|
|
|
|
tracker * p = page;
|
|
|
|
return (node_t*)p->key;
|
|
|
|
}
|
|
|
|
static void setKey(void * page, node_t * n, void * ignore) {
|
|
|
|
tracker * p = page;
|
|
|
|
p->key = (intptr_t) n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void randomTest(replacementPolicy * lru) {
|
2007-03-10 01:29:43 +00:00
|
|
|
time_t seed = time(0);
|
|
|
|
printf("\nSeed = %ld\n", seed);
|
|
|
|
srandom(seed);
|
|
|
|
|
|
|
|
int cachedCount = 0;
|
|
|
|
|
|
|
|
tracker * t = calloc(OBJECT_COUNT, sizeof(tracker));
|
|
|
|
for(int i = 0; i < OBJECT_COUNT; i++) {
|
2007-03-13 09:56:39 +00:00
|
|
|
t[i].val = i;
|
2007-03-10 01:29:43 +00:00
|
|
|
}
|
|
|
|
for(unsigned long j = 0; j < 100000000UL; j++) {
|
|
|
|
int op = myrandom(100);
|
|
|
|
|
|
|
|
int i = myrandom(OBJECT_COUNT);
|
|
|
|
if(op < 10) {
|
|
|
|
// TOGGLE IN CACHE
|
|
|
|
if(!t[i].inCache) {
|
2007-03-11 07:53:32 +00:00
|
|
|
lru->insert(lru, &t[i]);
|
2007-03-10 01:29:43 +00:00
|
|
|
t[i].inCache = 1;
|
|
|
|
cachedCount ++;
|
|
|
|
} else {
|
2007-03-11 07:53:32 +00:00
|
|
|
void * v = lru->remove(lru, &t[i]);
|
2007-03-10 01:29:43 +00:00
|
|
|
assert(v == &t[i]);
|
|
|
|
t[i].inCache = 0;
|
|
|
|
cachedCount --;
|
|
|
|
}
|
|
|
|
} else if(op < 30) {
|
|
|
|
// Get stale + remove
|
|
|
|
tracker * tr = lru->getStale(lru);
|
|
|
|
if( tr ) {
|
|
|
|
assert(tr->inCache);
|
2007-03-13 09:56:39 +00:00
|
|
|
assert(tr == &t[tr->val]);
|
2007-03-11 07:53:32 +00:00
|
|
|
tr = lru->remove(lru, tr);
|
2007-03-13 09:56:39 +00:00
|
|
|
assert(tr == &t[tr->val]);
|
2007-03-10 01:29:43 +00:00
|
|
|
tr->inCache = 0;
|
|
|
|
assert(cachedCount != 0);
|
|
|
|
cachedCount --;
|
|
|
|
} else {
|
|
|
|
assert(cachedCount == 0);
|
|
|
|
}
|
|
|
|
} else if(op < 50) {
|
|
|
|
// Get stale
|
|
|
|
tracker * tr = lru->getStale(lru);
|
|
|
|
if(tr) {
|
|
|
|
assert(tr->inCache);
|
2007-03-13 09:56:39 +00:00
|
|
|
assert(tr == &t[tr->val]);
|
2007-03-10 01:29:43 +00:00
|
|
|
assert(cachedCount != 0);
|
|
|
|
} else {
|
|
|
|
assert(cachedCount == 0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Hit
|
2007-03-11 07:53:32 +00:00
|
|
|
if(t[i].inCache) lru->hit(lru, &t[i]);
|
2007-03-10 01:29:43 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-11 07:53:32 +00:00
|
|
|
}
|
|
|
|
START_TEST(replacementPolicyLRURandomTest) {
|
|
|
|
replacementPolicy * lru = lruFastInit(getKey, setKey, 0);
|
|
|
|
randomTest(lru);
|
|
|
|
} END_TEST
|
|
|
|
START_TEST(replacementPolicyLRUFastRandomTest) {
|
|
|
|
replacementPolicy * lru = lruFastInit(getKey, setKey, 0);
|
|
|
|
randomTest(lru);
|
2007-03-10 01:29:43 +00:00
|
|
|
} END_TEST
|
|
|
|
|
|
|
|
Suite * check_suite(void) {
|
|
|
|
Suite *s = suite_create("replacemenPolicy");
|
|
|
|
/* 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 */
|
2007-03-11 07:53:32 +00:00
|
|
|
tcase_add_test(tc, replacementPolicyLRURandomTest);
|
|
|
|
tcase_add_test(tc, replacementPolicyLRUFastRandomTest);
|
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"
|