X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=threads2i.c;h=af913b4cf05b7e69553da0f76eb7df99f0b079f2;hb=5d00eaa431265fef4c583088f21c56f94ce2b141;hp=0c7c41d43896023485e29cab0f96a3ad30b640c9;hpb=471ecef14bab98d48dad385a2d9c3d4f361aa6c6;p=btree diff --git a/threads2i.c b/threads2i.c index 0c7c41d..af913b4 100644 --- a/threads2i.c +++ b/threads2i.c @@ -93,11 +93,11 @@ typedef enum{ // share is count of read accessors // grant write lock when share == 0 -typedef struct { - volatile unsigned char mutex; - volatile unsigned char exclusive:1; - volatile unsigned char pending:1; - volatile ushort share; +volatile typedef struct { + unsigned char mutex[1]; + unsigned char exclusive:1; + unsigned char pending:1; + ushort share; } BtSpinLatch; // hash table entries @@ -162,10 +162,9 @@ typedef struct BtPage_ { uint min; // next key offset unsigned char bits:7; // page size in bits unsigned char free:1; // page is on free chain - unsigned char lvl:5; // level of page + unsigned char lvl:6; // level of page unsigned char kill:1; // page is being deleted unsigned char dirty:1; // page has deleted keys - unsigned char posted:1; // page fence is posted unsigned char right[BtId]; // page number to right } *BtPage; @@ -366,10 +365,10 @@ ushort prev; do { // obtain latch mutex #ifdef unix - if( __sync_lock_test_and_set(&latch->mutex, 1) ) + if( __sync_lock_test_and_set(latch->mutex, 1) ) continue; #else - if( _InterlockedExchange8(&latch->mutex, 1) ) + if( _InterlockedExchange8(latch->mutex, 1) ) continue; #endif // see if exclusive request is granted or pending @@ -378,9 +377,9 @@ ushort prev; latch->share++; #ifdef unix - __sync_lock_release (&latch->mutex); + *latch->mutex = 0; #else - _InterlockedExchange8(&latch->mutex, 0); + _InterlockedExchange8(latch->mutex, 0); #endif if( prev ) @@ -401,10 +400,10 @@ uint prev; do { #ifdef unix - if( __sync_lock_test_and_set(&latch->mutex, 1) ) + if( __sync_lock_test_and_set(latch->mutex, 1) ) continue; #else - if( _InterlockedExchange8(&latch->mutex, 1) ) + if( _InterlockedExchange8(latch->mutex, 1) ) continue; #endif if( prev = !(latch->share | latch->exclusive) ) @@ -412,9 +411,9 @@ uint prev; else latch->pending = 1; #ifdef unix - __sync_lock_release (&latch->mutex); + *latch->mutex = 0; #else - _InterlockedExchange8(&latch->mutex, 0); + _InterlockedExchange8(latch->mutex, 0); #endif if( prev ) return; @@ -435,10 +434,10 @@ int bt_spinwritetry(BtSpinLatch *latch) uint prev; #ifdef unix - if( __sync_lock_test_and_set(&latch->mutex, 1) ) + if( __sync_lock_test_and_set(latch->mutex, 1) ) return 0; #else - if( _InterlockedExchange8(&latch->mutex, 1) ) + if( _InterlockedExchange8(latch->mutex, 1) ) return 0; #endif // take write access if all bits are clear @@ -447,9 +446,9 @@ uint prev; latch->exclusive = 1; #ifdef unix - __sync_lock_release (&latch->mutex); + *latch->mutex = 0; #else - _InterlockedExchange8(&latch->mutex, 0); + _InterlockedExchange8(latch->mutex, 0); #endif return prev; } @@ -459,17 +458,17 @@ uint prev; void bt_spinreleasewrite(BtSpinLatch *latch) { #ifdef unix - while( __sync_lock_test_and_set(&latch->mutex, 1) ) + while( __sync_lock_test_and_set(latch->mutex, 1) ) sched_yield(); #else - while( _InterlockedExchange8(&latch->mutex, 1) ) + while( _InterlockedExchange8(latch->mutex, 1) ) SwitchToThread(); #endif latch->exclusive = 0; #ifdef unix - __sync_lock_release (&latch->mutex); + *latch->mutex = 0; #else - _InterlockedExchange8(&latch->mutex, 0); + _InterlockedExchange8(latch->mutex, 0); #endif } @@ -478,17 +477,17 @@ void bt_spinreleasewrite(BtSpinLatch *latch) void bt_spinreleaseread(BtSpinLatch *latch) { #ifdef unix - while( __sync_lock_test_and_set(&latch->mutex, 1) ) + while( __sync_lock_test_and_set(latch->mutex, 1) ) sched_yield(); #else - while( _InterlockedExchange8(&latch->mutex, 1) ) + while( _InterlockedExchange8(latch->mutex, 1) ) SwitchToThread(); #endif latch->share--; #ifdef unix - __sync_lock_release (&latch->mutex); + *latch->mutex = 0; #else - _InterlockedExchange8(&latch->mutex, 0); + _InterlockedExchange8(latch->mutex, 0); #endif } @@ -697,14 +696,14 @@ uint slot; close (mgr->idx); free (mgr->pool); free (mgr->hash); - free (mgr->latch); + free ((void *)mgr->latch); free (mgr); #else FlushFileBuffers(mgr->idx); CloseHandle(mgr->idx); GlobalFree (mgr->pool); GlobalFree (mgr->hash); - GlobalFree (mgr->latch); + GlobalFree ((void *)mgr->latch); GlobalFree (mgr); #endif } @@ -1878,10 +1877,10 @@ uid left; BTERR bt_splitpage (BtDb *bt, BtPageSet *set) { uint cnt = 0, idx = 0, max, nxt = bt->mgr->page_size; -unsigned char fencekey[256]; +unsigned char fencekey[256], rightkey[256]; uint lvl = set->page->lvl; +BtPageSet right[1]; uint prev; -uid right; BtKey key; // split higher half of keys to bt->frame @@ -1903,6 +1902,10 @@ BtKey key; slotptr(bt->frame, idx)->off = nxt; } + // remember existing fence key for new page to the right + + memcpy (rightkey, key, key->len + 1); + bt->frame->bits = bt->mgr->page_bits; bt->frame->min = nxt; bt->frame->cnt = idx; @@ -1915,7 +1918,7 @@ BtKey key; // get new free page and write higher keys to it. - if( !(right = bt_newpage(bt, bt->frame)) ) + if( !(right->page_no = bt_newpage(bt, bt->frame)) ) return bt->err; // update lower keys to continue in old page @@ -1923,7 +1926,6 @@ BtKey key; memcpy (bt->frame, set->page, bt->mgr->page_size); memset (set->page+1, 0, bt->mgr->page_size - sizeof(*set->page)); nxt = bt->mgr->page_size; - set->page->posted = 0; set->page->dirty = 0; set->page->act = 0; cnt = 0; @@ -1944,65 +1946,42 @@ BtKey key; // remember fence key for smaller page memcpy(fencekey, key, key->len + 1); - bt_putid(set->page->right, right); + + bt_putid(set->page->right, right->page_no); set->page->min = nxt; set->page->cnt = idx; // if current page is the root page, split it if( set->page_no == ROOT_page ) - return bt_splitroot (bt, set, fencekey, right); - - right = 0; + return bt_splitroot (bt, set, fencekey, right->page_no); // insert new fences in their parent pages - while( 1 ) { - bt_lockpage (BtLockParent, set->latch); - - key = keyptr (set->page, set->page->cnt); - memcpy (fencekey, key, key->len + 1); - prev = set->page->posted; - - if( right && prev ) { - bt_unlockpage (BtLockParent, set->latch); - bt_unlockpage (BtLockWrite, set->latch); - bt_unpinlatch (set->latch); - bt_unpinpool (set->pool); - return 0; - } - - right = bt_getid (set->page->right); - set->page->posted = 1; - - bt_unlockpage (BtLockWrite, set->latch); - - // insert new fence for reformulated left block of smaller keys + right->latch = bt_pinlatch (bt, right->page_no); + bt_lockpage (BtLockParent, right->latch); - if( !prev ) - if( bt_insertkey (bt, fencekey+1, *fencekey, lvl+1, set->page_no, time(NULL)) ) - return bt->err; + bt_lockpage (BtLockParent, set->latch); + bt_unlockpage (BtLockWrite, set->latch); - bt_unlockpage (BtLockParent, set->latch); - bt_unpinlatch (set->latch); - bt_unpinpool (set->pool); + // insert new fence for reformulated left block of smaller keys - if( !(set->page_no = right) ) - break; + if( bt_insertkey (bt, fencekey+1, *fencekey, lvl+1, set->page_no, time(NULL)) ) + return bt->err; - set->latch = bt_pinlatch (bt, right); + // switch fence for right block of larger keys to new right page - if( set->pool = bt_pinpool (bt, right) ) - set->page = bt_page (bt, set->pool, right); - else - return bt->err; + if( bt_insertkey (bt, rightkey+1, *rightkey, lvl+1, right->page_no, time(NULL)) ) + return bt->err; - bt_lockpage (BtLockWrite, set->latch); - } + bt_unlockpage (BtLockParent, set->latch); + bt_unpinlatch (set->latch); + bt_unpinpool (set->pool); + bt_unlockpage (BtLockParent, right->latch); + bt_unpinlatch (right->latch); return 0; } - // Insert new key into the btree at given level. BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uint lvl, uid id, uint tod) @@ -2156,51 +2135,81 @@ uint bt_tod(BtDb *bt, uint slot) return slotptr(bt->cursor,slot)->tod; } - #ifdef STANDALONE +#ifndef unix +double getCpuTime(int type) +{ +FILETIME crtime[1]; +FILETIME xittime[1]; +FILETIME systime[1]; +FILETIME usrtime[1]; +SYSTEMTIME timeconv[1]; +double ans; + + GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime); + memset (timeconv, 0, sizeof(SYSTEMTIME)); + + switch( type ) { + case 1: + FileTimeToSystemTime (usrtime, timeconv); + break; + case 2: + FileTimeToSystemTime (systime, timeconv); + break; + } + + ans = (double)timeconv->wHour * 3600; + ans += (double)timeconv->wMinute * 60; + ans += (double)timeconv->wSecond; + ans += (double)timeconv->wMilliseconds / 1000; + return ans; +} +#else +#include +#include + +double getCpuTime(int type) +{ +struct rusage used[1]; + + getrusage(RUSAGE_SELF, used); + switch( type ) { + case 1: + return (double)used->ru_utime.tv_sec + (double)used->ru_utime.tv_usec / 1000000; + + case 2: + return (double)used->ru_stime.tv_sec + (double)used->ru_stime.tv_usec / 1000000; + } + + return 0; +} +#endif + void bt_latchaudit (BtDb *bt) { ushort idx, hashidx; uid next, page_no; -BtLatchSet *latch; -BtPool *pool; -BtPage page; +BtPageSet set[1]; BtKey ptr; #ifdef unix for( idx = 1; idx < bt->mgr->latchmgr->latchdeployed; idx++ ) { - latch = bt->mgr->latchsets + idx; - if( *(ushort *)latch->readwr ) { - fprintf(stderr, "latchset %d r/w locked for page %.8x\n", idx, latch->page_no); - *(ushort *)latch->readwr = 0; - } - if( *(ushort *)latch->access ) { - fprintf(stderr, "latchset %d access locked for page %.8x\n", idx, latch->page_no); - *(ushort *)latch->access = 0; - } - if( *(ushort *)latch->parent ) { - fprintf(stderr, "latchset %d parent locked for page %.8x\n", idx, latch->page_no); - *(ushort *)latch->parent = 0; - } - if( *(ushort *)latch->busy ) { - fprintf(stderr, "latchset %d busy locked for page %.8x\n", idx, latch->page_no); - *(ushort *)latch->parent = 0; - } - if( latch->pin ) { - fprintf(stderr, "latchset %d pinned for page %.8x\n", idx, latch->page_no); - latch->pin = 0; + set->latch = bt->mgr->latchsets + idx; + if( set->latch->pin ) { + fprintf(stderr, "latchset %d pinned for page %.6x\n", idx, set->latch->page_no); + set->latch->pin = 0; } } for( hashidx = 0; hashidx < bt->mgr->latchmgr->latchhash; hashidx++ ) { if( idx = bt->mgr->latchmgr->table[hashidx].slot ) do { - latch = bt->mgr->latchsets + idx; - if( latch->hash != hashidx ) { + set->latch = bt->mgr->latchsets + idx; + if( set->latch->hash != hashidx ) fprintf(stderr, "latchset %d wrong hashidx\n", idx); - latch->hash = hashidx; - } - } while( idx = latch->next ); + if( set->latch->pin ) + fprintf(stderr, "latchset %d pinned for page %.8x\n", idx, set->latch->page_no); + } while( idx = set->latch->next ); } next = bt->mgr->latchmgr->nlatchpage + LATCH_page; @@ -2416,6 +2425,7 @@ HANDLE *threads; double real_time; ThreadArg *args; uint poolsize = 0; +float elapsed; int num = 0; char key[1]; BtMgr *mgr; @@ -2505,7 +2515,13 @@ BtDb *bt; time (stop); real_time = 1000 * (*stop - *start); #endif - fprintf(stderr, " Time to complete: %.2f seconds\n", real_time/1000); + elapsed = real_time / 1000; + fprintf(stderr, " real %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60); + elapsed = getCpuTime(1); + fprintf(stderr, " user %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60); + elapsed = getCpuTime(2); + fprintf(stderr, " sys %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60); + bt_mgrclose (mgr); }