X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=btree2u.c;h=2d2a3d0a716149d87a5ab7378a5b6835d8315e36;hb=d6f8fa0131deb92973dbc5c571a1cb612b127ab5;hp=1f7edcbd57d66d3a30b85fd7c8745c20e2864425;hpb=23b96d0575e8f975944f8807c3739e4053356155;p=btree diff --git a/btree2u.c b/btree2u.c index 1f7edcb..2d2a3d0 100644 --- a/btree2u.c +++ b/btree2u.c @@ -43,6 +43,7 @@ REDISTRIBUTION OF THIS SOFTWARE. #include #include #include +#include #endif #include @@ -169,6 +170,7 @@ typedef struct { BtSpinLatch readwr[1]; // read/write page lock BtSpinLatch access[1]; // Access Intent/Page delete BtSpinLatch parent[1]; // Posting of fence key in parent + volatile ushort clock; // accessed since last clock pass volatile uint next; // next entry in hash table chain volatile uint prev; // prev entry in hash table chain volatile uint pin; // number of outstanding pins @@ -473,8 +475,10 @@ BTERR bt_readpage (BtDb *bt, BtPage page, uid page_no) off64_t off = page_no << bt->page_bits; #ifdef unix - if( pread (bt->idx, page, bt->page_size, page_no << bt->page_bits) < bt->page_size ) + if( pread (bt->idx, page, bt->page_size, page_no << bt->page_bits) < bt->page_size ) { + fprintf (stderr, "Unable to read page %.8x errno = %d\n", page_no, errno); return bt->err = BTERR_read; + } #else OVERLAPPED ovl[1]; uint amt[1]; @@ -483,10 +487,14 @@ uint amt[1]; ovl->Offset = off; ovl->OffsetHigh = off >> 32; - if( !ReadFile(bt->idx, page, bt->page_size, amt, ovl)) + if( !ReadFile(bt->idx, page, bt->page_size, amt, ovl)) { + fprintf (stderr, "Unable to read page %.8x GetLastError = %d\n", page_no, GetLastError()); return bt->err = BTERR_read; - if( *amt < bt->page_size ) + } + if( *amt < bt->page_size ) { + fprintf (stderr, "Unable to read page %.8x GetLastError = %d\n", page_no, GetLastError()); return bt->err = BTERR_read; + } #endif return 0; } @@ -525,7 +533,7 @@ uint amt[1]; BTERR bt_latchlink (BtDb *bt, uint hashidx, uint slot, uid page_no) { -BtPage page = (BtPage)(slot * bt->page_size + bt->pagepool); +BtPage page = (BtPage)((uid)slot * bt->page_size + bt->pagepool); BtLatchSet *latch = bt->latchsets + slot; if( latch->next = bt->table[hashidx].slot ) @@ -533,6 +541,7 @@ BtLatchSet *latch = bt->latchsets + slot; bt->table[hashidx].slot = slot; latch->page_no = page_no; + latch->clock = 1; latch->prev = 0; latch->pin = 1; @@ -562,7 +571,7 @@ off64_t off; uint amt[1]; BtPage page; - // try to find unpinned entry + // try to find our entry bt_spinwritelock(bt->table[hashidx].latch); @@ -573,33 +582,12 @@ BtPage page; break; } while( slot = latch->next ); - // found our entry, bring to front of hash chain + // found our entry if( slot ) { latch = bt->latchsets + slot; -#ifdef unix - __sync_fetch_and_add(&latch->pin, 1); -#else - _InterlockedIncrement (&latch->pin); -#endif - // unlink our entry from its hash chain position - - if( latch->prev ) - bt->latchsets[latch->prev].next = latch->next; - else - bt->table[hashidx].slot = latch->next; - - if( latch->next ) - bt->latchsets[latch->next].prev = latch->prev; - - // now link into head of the hash chain - - if( latch->next = bt->table[hashidx].slot ) - bt->latchsets[latch->next].prev = slot; - - bt->table[hashidx].slot = slot; - latch->prev = 0; - + latch->clock = 1; + latch->pin++; bt_spinreleasewrite(bt->table[hashidx].latch); return latch; } @@ -624,40 +612,41 @@ BtPage page; #else _InterlockedDecrement (&bt->latchmgr->latchdeployed); #endif - // find and reuse previous lru lock entry on victim hash chain + // find and reuse previous entry on victim while( 1 ) { #ifdef unix - idx = __sync_fetch_and_add(&bt->latchmgr->latchvictim, 1); + slot = __sync_fetch_and_add(&bt->latchmgr->latchvictim, 1); #else - idx = _InterlockedIncrement (&bt->latchmgr->latchvictim) - 1; + slot = _InterlockedIncrement (&bt->latchmgr->latchvictim) - 1; #endif // try to get write lock on hash chain // skip entry if not obtained - // or has outstanding locks + // or has outstanding pins - idx %= bt->latchmgr->latchhash; + slot %= bt->latchmgr->latchtotal; + latch = bt->latchsets + slot; - if( !bt_spinwritetry (bt->table[idx].latch) ) + idx = latch->page_no % bt->latchmgr->latchhash; + + if( !slot ) continue; - if( slot = bt->table[idx].slot ) - while( 1 ) { - latch = bt->latchsets + slot; - if( !latch->next ) - break; - slot = latch->next; - } + if( !bt_spinwritetry (bt->table[idx].latch) ) + continue; - if( !slot || latch->pin ) { + if( latch->clock ) { + latch->clock = 0; bt_spinreleasewrite (bt->table[idx].latch); continue; } // update permanent page area in btree - page = (BtPage)(slot * bt->page_size + bt->pagepool); - + page = (BtPage)((uid)slot * bt->page_size + bt->pagepool); +#ifdef unix + posix_fadvise (bt->idx, page_no << bt->page_bits, bt->page_size, POSIX_FADV_WILLNEED); +#endif if( page->dirty ) if( bt_writepage (bt, page, latch->page_no) ) return NULL; @@ -744,7 +733,8 @@ struct flock lock[1]; bt = calloc (1, sizeof(BtDb)); bt->idx = open ((char*)name, O_RDWR | O_CREAT, 0666); - + posix_fadvise( bt->idx, 0, 0, POSIX_FADV_RANDOM); + if( bt->idx == -1 ) { fprintf(stderr, "unable to open %s\n", name); return free(bt), NULL; @@ -783,7 +773,7 @@ struct flock lock[1]; #endif #ifdef unix - latchmgr = malloc (BT_maxpage); + latchmgr = valloc (BT_maxpage); *amt = 0; // read minimum page size to get root info @@ -874,13 +864,12 @@ struct flock lock[1]; // clear out buffer pool pages memset(latchmgr, 0, bt->page_size); - last = MIN_lvl; + last = MIN_lvl + nlatchpage; - while( ++last < ((MIN_lvl + 1 + nlatchpage) ) ) - if( bt_writepage (bt, latchmgr->alloc, last) ) { + if( bt_writepage (bt, latchmgr->alloc, last) ) { fprintf (stderr, "Unable to write buffer pool page %.8x\n", last); return bt_close (bt), NULL; - } + } #ifdef unix free (latchmgr); @@ -908,21 +897,22 @@ btlatch: fprintf (stderr, "Unable to mmap page zero, errno = %d", errno); return bt_close (bt), NULL; } - bt->table = (void *)mmap (0, nlatchpage * bt->page_size, flag, MAP_SHARED, bt->idx, LATCH_page * bt->page_size); + bt->table = (void *)mmap (0, (uid)nlatchpage * bt->page_size, flag, MAP_SHARED, bt->idx, LATCH_page * bt->page_size); if( bt->table == MAP_FAILED ) { fprintf (stderr, "Unable to mmap buffer pool, errno = %d", errno); return bt_close (bt), NULL; } + madvise (bt->table, (uid)nlatchpage << bt->page_bits, MADV_RANDOM | MADV_WILLNEED); #else flag = PAGE_READWRITE; - bt->halloc = CreateFileMapping(bt->idx, NULL, flag, 0, (nlatchpage + LATCH_page) * bt->page_size, NULL); + bt->halloc = CreateFileMapping(bt->idx, NULL, flag, 0, ((uid)nlatchpage + LATCH_page) * bt->page_size, NULL); if( !bt->halloc ) { fprintf (stderr, "Unable to create file mapping for buffer pool mgr, GetLastError = %d\n", GetLastError()); return bt_close (bt), NULL; } flag = FILE_MAP_WRITE; - bt->latchmgr = MapViewOfFile(bt->halloc, flag, 0, 0, (nlatchpage + LATCH_page) * bt->page_size); + bt->latchmgr = MapViewOfFile(bt->halloc, flag, 0, 0, ((uid)nlatchpage + LATCH_page) * bt->page_size); if( !bt->latchmgr ) { fprintf (stderr, "Unable to map buffer pool, GetLastError = %d\n", GetLastError()); return bt_close (bt), NULL; @@ -930,11 +920,11 @@ btlatch: bt->table = (void *)((char *)bt->latchmgr + LATCH_page * bt->page_size); #endif - bt->pagepool = (unsigned char *)bt->table + (nlatchpage - bt->latchmgr->latchtotal) * bt->page_size; - bt->latchsets = (BtLatchSet *)(bt->pagepool - bt->latchmgr->latchtotal * sizeof(BtLatchSet)); + bt->pagepool = (unsigned char *)bt->table + (uid)(nlatchpage - bt->latchmgr->latchtotal) * bt->page_size; + bt->latchsets = (BtLatchSet *)(bt->pagepool - (uid)bt->latchmgr->latchtotal * sizeof(BtLatchSet)); #ifdef unix - bt->mem = malloc (2 * bt->page_size); + bt->mem = valloc (2 * bt->page_size); #else bt->mem = VirtualAlloc(NULL, 2 * bt->page_size, MEM_COMMIT, PAGE_READWRITE); #endif @@ -1056,8 +1046,8 @@ int ans; void bt_update (BtDb *bt, BtPage page) { #ifdef unix - msync (page, bt->page_size, MS_ASYNC); -//#else +// msync (page, bt->page_size, MS_ASYNC); +#else // FlushViewOfFile (page, bt->page_size); #endif page->dirty = 1; @@ -1067,7 +1057,7 @@ void bt_update (BtDb *bt, BtPage page) BtPage bt_mappage (BtDb *bt, BtLatchSet *latch) { - return (BtPage)((latch - bt->latchsets) * bt->page_size + bt->pagepool); + return (BtPage)((uid)(latch - bt->latchsets) * bt->page_size + bt->pagepool); } // deallocate a deleted page @@ -1877,12 +1867,14 @@ uint bt_audit (BtDb *bt) uint idx, hashidx; uid next, page_no; BtLatchSet *latch; +uint blks[64]; uint cnt = 0; BtPage page; uint amt[1]; BtKey ptr; -#ifdef unix + memset (blks, 0, sizeof(blks)); + if( *(ushort *)(bt->latchmgr->lock) ) fprintf(stderr, "Alloc page locked\n"); *(ushort *)(bt->latchmgr->lock) = 0; @@ -1905,7 +1897,7 @@ BtKey ptr; fprintf(stderr, "latchset %d pinned for page %.8x\n", idx, latch->page_no); latch->pin = 0; } - page = (BtPage)(idx * bt->page_size + bt->pagepool); + page = (BtPage)((uid)idx * bt->page_size + bt->pagepool); if( page->dirty ) if( bt_writepage (bt, page, latch->page_no) ) @@ -1929,7 +1921,8 @@ BtKey ptr; page_no = LEAF_page; while( page_no < bt_getid(bt->latchmgr->alloc->right) ) { - pread (bt->idx, bt->frame, bt->page_size, page_no << bt->page_bits); + if( bt_readpage (bt, bt->frame, page_no) ) + fprintf(stderr, "page %.8x unreadable\n", page_no); if( !bt->frame->free ) { for( idx = 0; idx++ < bt->frame->cnt - 1; ) { ptr = keyptr(bt->frame, idx+1); @@ -1938,16 +1931,16 @@ BtKey ptr; } if( !bt->frame->lvl ) cnt += bt->frame->act; + blks[bt->frame->lvl]++; } if( page_no > LEAF_page ) next = page_no + 1; page_no = next; } + for( idx = 0; blks[idx]; idx++ ) + fprintf(stderr, "%d lvl %d blocks\n", blks[idx], idx); return cnt - 1; -#else - return 0; -#endif } #ifndef unix @@ -2021,15 +2014,20 @@ int ch, cnt = 0, bits = 12, idx; unsigned char key[256]; double done, start; uid next, page_no; +BtLatchSet *latch; float elapsed; time_t tod[1]; uint scan = 0; uint len = 0; uint map = 0; +BtPage page; BtKey ptr; BtDb *bt; FILE *in; +#ifdef WIN32 + _setmode (1, _O_BINARY); +#endif if( argc < 4 ) { fprintf (stderr, "Usage: %s idx_file src_file Read/Write/Scan/Delete/Find/Count [page_bits mapped_pool_pages start_line_number]\n", argv[0]); fprintf (stderr, " page_bits: size of btree page in bits\n"); @@ -2058,15 +2056,27 @@ FILE *in; switch(argv[3][0]| 0x20) { - case 'a': - fprintf(stderr, "started audit for %s\n", argv[2]); + case 'p': // display page + if( latch = bt_pinlatch (bt, off) ) + page = bt_mappage (bt, latch); + else + fprintf(stderr, "unable to read page %.8x\n", off); + + write (1, page, bt->page_size); + break; + + case 'a': // buffer pool audit + fprintf(stderr, "started audit for %s\n", argv[1]); cnt = bt_audit (bt); - fprintf(stderr, "finished audit for %s, %d keys\n", argv[2], cnt); + fprintf(stderr, "finished audit for %s, %d keys\n", argv[1], cnt); break; - case 'w': + case 'w': // write keys fprintf(stderr, "started indexing for %s\n", argv[2]); - if( argc > 2 && (in = fopen (argv[2], "rb")) ) + if( argc > 2 && (in = fopen (argv[2], "rb")) ) { +#ifdef unix + posix_fadvise( fileno(in), 0, 0, POSIX_FADV_NOREUSE); +#endif while( ch = getc(in), ch != EOF ) if( ch == '\n' ) { @@ -2079,12 +2089,16 @@ FILE *in; } else if( len < 245 ) key[len++] = ch; + } fprintf(stderr, "finished adding keys for %s, %d \n", argv[2], line); break; - case 'd': + case 'd': // delete keys fprintf(stderr, "started deleting keys for %s\n", argv[2]); - if( argc > 2 && (in = fopen (argv[2], "rb")) ) + if( argc > 2 && (in = fopen (argv[2], "rb")) ) { +#ifdef unix + posix_fadvise( fileno(in), 0, 0, POSIX_FADV_NOREUSE); +#endif while( ch = getc(in), ch != EOF ) if( ch == '\n' ) { @@ -2097,12 +2111,16 @@ FILE *in; } else if( len < 245 ) key[len++] = ch; + } fprintf(stderr, "finished deleting keys for %s, %d \n", argv[2], line); break; - case 'f': + case 'f': // find keys fprintf(stderr, "started finding keys for %s\n", argv[2]); - if( argc > 2 && (in = fopen (argv[2], "rb")) ) + if( argc > 2 && (in = fopen (argv[2], "rb")) ) { +#ifdef unix + posix_fadvise( fileno(in), 0, 0, POSIX_FADV_NOREUSE); +#endif while( ch = getc(in), ch != EOF ) if( ch == '\n' ) { @@ -2117,10 +2135,11 @@ FILE *in; } else if( len < 245 ) key[len++] = ch; + } fprintf(stderr, "finished search of %d keys for %s, found %d\n", line, argv[2], found); break; - case 's': + case 's': // scan and print keys fprintf(stderr, "started scaning\n"); cnt = len = key[0] = 0; @@ -2139,7 +2158,7 @@ FILE *in; fprintf(stderr, " Total keys read %d\n", cnt - 1); break; - case 'c': + case 'c': // count keys fprintf(stderr, "started counting\n"); cnt = 0; @@ -2147,8 +2166,6 @@ FILE *in; page_no = LEAF_page; while( page_no < bt_getid(bt->latchmgr->alloc->right) ) { - BtLatchSet *latch; - BtPage page; if( latch = bt_pinlatch (bt, page_no) ) page = bt_mappage (bt, latch); if( !page->free && !page->lvl )