X-Git-Url: https://pd.if.org/git/?p=btree;a=blobdiff_plain;f=threads2h.c;h=7602b1179d54676363846162a930dcf1822fcc4a;hp=4a28044811ef815c490558f5cf66af89d13c29aa;hb=8db938f1d3ce0af4d1b3786d2b02b925a334867d;hpb=0756819e460acb958eeb073f4d5ca9ddf66ec6c7 diff --git a/threads2h.c b/threads2h.c index 4a28044..7602b11 100644 --- a/threads2h.c +++ b/threads2h.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 @@ -372,10 +372,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 @@ -384,9 +384,9 @@ ushort prev; latch->share++; #ifdef unix - __sync_lock_release (&latch->mutex); + __sync_lock_release (latch->mutex); #else - _InterlockedExchange8(&latch->mutex, 0); + _InterlockedExchange8(latch->mutex, 0); #endif if( prev ) @@ -407,10 +407,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) ) @@ -418,9 +418,9 @@ uint prev; else latch->pending = 1; #ifdef unix - __sync_lock_release (&latch->mutex); + __sync_lock_release (latch->mutex); #else - _InterlockedExchange8(&latch->mutex, 0); + _InterlockedExchange8(latch->mutex, 0); #endif if( prev ) return; @@ -441,10 +441,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 @@ -453,9 +453,9 @@ uint prev; latch->exclusive = 1; #ifdef unix - __sync_lock_release (&latch->mutex); + __sync_lock_release (latch->mutex); #else - _InterlockedExchange8(&latch->mutex, 0); + _InterlockedExchange8(latch->mutex, 0); #endif return prev; } @@ -466,17 +466,17 @@ void bt_spinreleasewrite(BtSpinLatch *latch) { // obtain latch mutex #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); + __sync_lock_release (latch->mutex); #else - _InterlockedExchange8(&latch->mutex, 0); + _InterlockedExchange8(latch->mutex, 0); #endif } @@ -485,17 +485,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); + __sync_lock_release (latch->mutex); #else - _InterlockedExchange8(&latch->mutex, 0); + _InterlockedExchange8(latch->mutex, 0); #endif } @@ -612,6 +612,8 @@ ushort hashidx = page_no % bt->mgr->latchmgr->latchhash; ushort slot, avail = 0, victim, idx; BtLatchSet *set; + // try to find existing latch table entry for this page + // obtain read lock on hash table entry bt_spinreadlock(bt->mgr->latchmgr->table[hashidx].latch); @@ -783,14 +785,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 } @@ -800,10 +802,10 @@ uint slot; void bt_close (BtDb *bt) { #ifdef unix - if ( bt->mem ) + if( bt->mem ) free (bt->mem); #else - if ( bt->mem) + if( bt->mem) VirtualFree (bt->mem, 0, MEM_RELEASE); #endif free (bt); @@ -1226,7 +1228,7 @@ uint slot, idx, victim; return pool; } - // upgrade to write lock + // upgrade to write lock bt_spinreleaseread (&bt->mgr->latch[idx]); bt_spinwritelock (&bt->mgr->latch[idx]); @@ -1419,15 +1421,15 @@ int reuse; reuse = 0; } #ifdef unix - if ( pwrite(bt->mgr->idx, page, bt->mgr->page_size, new_page << bt->mgr->page_bits) < bt->mgr->page_size ) + if( pwrite(bt->mgr->idx, page, bt->mgr->page_size, new_page << bt->mgr->page_bits) < bt->mgr->page_size ) return bt->err = BTERR_wrt, 0; // if writing first page of pool block, zero last page in the block - if ( !reuse && bt->mgr->poolmask > 0 && (new_page & bt->mgr->poolmask) == 0 ) + if( !reuse && bt->mgr->poolmask > 0 && (new_page & bt->mgr->poolmask) == 0 ) { // use zero buffer to write zeros - if ( pwrite(bt->mgr->idx,bt->zero, bt->mgr->page_size, (new_page | bt->mgr->poolmask) << bt->mgr->page_bits) < bt->mgr->page_size ) + if( pwrite(bt->mgr->idx,bt->zero, bt->mgr->page_size, (new_page | bt->mgr->poolmask) << bt->mgr->page_bits) < bt->mgr->page_size ) return bt->err = BTERR_wrt, 0; } #else @@ -1535,7 +1537,7 @@ BtPool *prevpool; // re-read and re-lock root after determining actual level of root if( set->page->lvl != drill) { - if ( set->page_no != ROOT_page ) + if( set->page_no != ROOT_page ) return bt->err = BTERR_struct, 0; drill = set->page->lvl; @@ -2082,7 +2084,7 @@ BtKey ptr; ptr = keyptr(set->page, slot); else { - if ( !bt->err ) + if( !bt->err ) bt->err = BTERR_ovflw; return bt->err; } @@ -2225,6 +2227,67 @@ uint bt_tod(BtDb *bt, uint slot) #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 = 0; + + memset (timeconv, 0, sizeof(SYSTEMTIME)); + + switch( type ) { + case 0: + GetSystemTimeAsFileTime (xittime); + FileTimeToSystemTime (xittime, timeconv); + ans = (double)timeconv->wDayOfWeek * 3600 * 24; + break; + case 1: + GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime); + FileTimeToSystemTime (usrtime, timeconv); + break; + case 2: + GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime); + 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]; +struct timeval tv[1]; + + switch( type ) { + case 0: + gettimeofday(tv, NULL); + return (double)tv->tv_sec + (double)tv->tv_usec / 1000000; + + case 1: + getrusage(RUSAGE_SELF, used); + return (double)used->ru_utime.tv_sec + (double)used->ru_utime.tv_usec / 1000000; + + case 2: + getrusage(RUSAGE_SELF, used); + 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; @@ -2454,16 +2517,15 @@ int main (int argc, char **argv) { int idx, cnt, len, slot, err; int segsize, bits = 16; +double start, stop; #ifdef unix pthread_t *threads; -timer start, stop; #else -time_t start[1], stop[1]; HANDLE *threads; #endif -double real_time; ThreadArg *args; uint poolsize = 0; +float elapsed; int num = 0; char key[1]; BtMgr *mgr; @@ -2480,11 +2542,7 @@ BtDb *bt; exit(0); } -#ifdef unix - gettimeofday(&start, NULL); -#else - time(start); -#endif + start = getCpuTime(0); if( argc > 3 ) bits = atoi(argv[3]); @@ -2542,18 +2600,20 @@ BtDb *bt; #ifdef unix for( idx = 0; idx < cnt; idx++ ) pthread_join (threads[idx], NULL); - gettimeofday(&stop, NULL); - real_time = 1000.0 * ( stop.tv_sec - start.tv_sec ) + 0.001 * (stop.tv_usec - start.tv_usec ); #else WaitForMultipleObjects (cnt, threads, TRUE, INFINITE); for( idx = 0; idx < cnt; idx++ ) CloseHandle(threads[idx]); - time (stop); - real_time = 1000 * (*stop - *start); #endif - fprintf(stderr, " Time to complete: %.2f seconds\n", real_time/1000); + elapsed = getCpuTime(0) - start; + 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); }