/* -*- mode: C; c-basic-offset: 4 -*- */ #ident "Copyright (c) 2007, 2008 Tokutek Inc. All rights reserved." #include "includes.h" #include "test.h" // this mutex is used by some of the tests to serialize access to some // global data, especially between the test thread and the cachetable // writeback threads toku_pthread_mutex_t test_mutex; static inline void test_mutex_init(void) { int r = toku_pthread_mutex_init(&test_mutex, 0); assert(r == 0); } static inline void test_mutex_destroy(void) { int r = toku_pthread_mutex_destroy(&test_mutex); assert(r == 0); } static inline void test_mutex_lock(void) { int r = toku_pthread_mutex_lock(&test_mutex); assert(r == 0); } static inline void test_mutex_unlock(void) { int r = toku_pthread_mutex_unlock(&test_mutex); assert(r == 0); } static void maybe_flush(CACHETABLE t) { toku_cachetable_maybe_flush_some(t); } static const int test_object_size = 1; static CACHETABLE ct; enum { N_PRESENT_LIMIT = 4, TRIALS=20000, N_FILES=2 }; static int n_present=0; static struct present_items { CACHEKEY key; CACHEFILE cf; } present_items[N_PRESENT_LIMIT]; static void print_ints(void) __attribute__((__unused__)); static void print_ints(void) { int i; for (i=0; i= N_PRESENT_LIMIT) { test_mutex_unlock(); toku_pthread_yield(); maybe_flush(thect); test_mutex_lock(); } assert(n_present0 && r0) { // First touch some random ones test_mutex_lock(); int whichone = random()%n_present; CACHEFILE whichcf = present_items[whichone].cf; CACHEKEY whichkey = present_items[whichone].key; test_mutex_unlock(); void *value; //printf("Touching %d (%lld, %p)\n", whichone, whichkey, whichcf); u_int32_t fhash = toku_cachetable_hash(whichcf, whichkey); r = toku_cachetable_get_and_pin(whichcf, whichkey, fhash, &value, NULL, flush_forchain, fetch_forchain, pe_callback, (void*)(long)whichkey.b ); assert(r==0); r = toku_cachetable_unpin(whichcf, whichkey, fhash, CACHETABLE_CLEAN, test_object_size); assert(r==0); } i += 1+ random()%100; int fnum = i%N_FILES; // i is always incrementing, so we need not worry about inserting a duplicate // if i is a duplicate, cachetable_put will return -1 // printf("%s:%d Add {%ld,%p}\n", __FILE__, __LINE__, i, f[fnum]); u_int32_t fhash = toku_cachetable_hash(f[fnum], make_blocknum(i)); r = toku_cachetable_put(f[fnum], make_blocknum(i), fhash, (void*)i, test_object_size, flush_forchain, fetch_forchain, pe_callback, (void*)i); assert(r==0 || r==-1); if (r==0) { item_becomes_present(ct, f[fnum], make_blocknum(i)); //print_ints(); //cachetable_print_state(ct); } r = toku_cachetable_unpin(f[fnum], make_blocknum(i), fhash, CACHETABLE_CLEAN, test_object_size); assert(r==0); long long pinned; r = toku_cachetable_get_key_state(ct, make_blocknum(i), f[fnum], 0, 0, &pinned, 0); assert(r==0); assert(pinned == 0); verify_cachetable_against_present(); if (random()%10==0) { i = random()%N_FILES; //printf("Close %d (%p), now n_present=%d\n", i, f[i], n_present); //print_ints(); CACHEFILE oldcf=f[i]; r = toku_cachefile_close(&f[i], 0, FALSE, ZERO_LSN); assert(r==0); file_is_not_present(oldcf); r = toku_cachetable_openf(&f[i], ct, fname[i], O_RDWR, S_IRWXU|S_IRWXG|S_IRWXO); assert(r==0); } } for (i=0; i