2 * Rebuild the index from scratch, in place.
11 MaxBufSize = 4*1024*1024,
14 typedef struct IEntryBuf IEntryBuf;
21 typedef struct ScoreBuf ScoreBuf;
24 uchar score[100][VtScoreSize];
38 Channel *arenadonechan;
39 Channel *isectdonechan;
46 static int shouldprocess(ISect*);
47 static void isectproc(void*);
48 static void arenapartproc(void*);
53 fprint(2, "usage: buildindex [-bd] [-i isect]... [-M imem] venti.conf\n");
54 threadexitsall("usage");
58 threadmain(int argc, char *argv[])
71 case 'd': /* debugging - make sure to run all 3 passes */
75 isect = vtrealloc(isect, (nisect+1)*sizeof(isect[0]));
76 isect[nisect++] = EARGF(usage());
79 imem = unittoull(EARGF(usage()));
89 if(initventi(argv[0], &conf) < 0)
90 sysfatal("can't init venti: %r");
92 if(nisect == 0 && ix->bloom)
94 if(bloom && ix->bloom && resetbloom(ix->bloom) < 0)
95 sysfatal("loadbloom: %r");
96 if(bloom && !ix->bloom)
97 sysfatal("-b specified but no bloom filter");
100 isectmem = imem/ix->nsects;
103 * safety first - only need read access to arenas
106 for(i=0; i<ix->narenas; i++){
107 if(ix->arenas[i]->part != p){
108 p = ix->arenas[i]->part;
109 if((fd = open(p->filename, OREAD)) < 0)
110 sysfatal("cannot reopen %s: %r", p->filename);
117 * need a block for every arena
119 bcmem = maxblocksize * (mainindex->narenas + 16);
120 if(0) fprint(2, "initialize %d bytes of disk block cache\n", bcmem);
124 for(i=0; i<ix->narenas; i++)
125 totalclumps += ix->arenas[i]->diskstats.clumps;
128 for(i=0; i<ix->nsects; i++)
129 totalbuckets += ix->sects[i]->blocks;
130 fprint(2, "%,lld clumps, %,lld buckets\n", totalclumps, totalbuckets);
132 /* start index procs */
133 fprint(2, "%T read index\n");
134 isectdonechan = chancreate(sizeof(void*), 1);
135 for(i=0; i<ix->nsects; i++){
136 if(shouldprocess(ix->sects[i])){
137 ix->sects[i]->writechan = chancreate(sizeof(IEntryBuf), 1);
138 vtproc(isectproc, ix->sects[i]);
142 for(i=0; i<nisect; i++)
144 fprint(2, "warning: did not find index section %s\n", isect[i]);
146 /* start arena procs */
149 arenadonechan = chancreate(sizeof(void*), 0);
150 for(i=0; i<ix->narenas; i++){
151 if(ix->arenas[i]->part != p){
152 p = ix->arenas[i]->part;
153 vtproc(arenapartproc, p);
158 /* wait for arena procs to finish */
159 for(i=0; i<napart; i++)
160 recvp(arenadonechan);
162 /* tell index procs to finish */
163 for(i=0; i<ix->nsects; i++)
164 if(ix->sects[i]->writechan)
165 send(ix->sects[i]->writechan, nil);
167 /* wait for index procs to finish */
168 for(i=0; i<ix->nsects; i++)
169 if(ix->sects[i]->writechan)
170 recvp(isectdonechan);
172 if(ix->bloom && writebloom(ix->bloom) < 0)
173 fprint(2, "writing bloom filter: %r\n");
175 fprint(2, "%T done arenaentries=%,lld indexed=%,lld (nskip=%,lld)\n",
176 arenaentries, indexentries, skipentries);
181 shouldprocess(ISect *is)
188 for(i=0; i<nisect; i++)
189 if(isect[i] && strcmp(isect[i], is->name) == 0){
197 add(u64int *a, u64int n)
207 * Read through an arena partition and send each of its IEntries
208 * to the appropriate index section. When finished, send on
213 ClumpChunks = 32*1024,
216 arenapartproc(void *v)
218 int i, j, n, nskip, x;
230 threadsetname("arenaproc %s", p->name);
231 buf = MKNZ(IEntryBuf, ix->nsects);
236 cis = MKN(ClumpInfo, ClumpChunks);
237 for(i=0; i<ix->narenas; i++){
241 if(a->memstats.clumps)
242 fprint(2, "%T arena %s: %d entries\n",
243 a->name, a->memstats.clumps);
245 * Running the loop backwards accesses the
246 * clump info blocks forwards, since they are
247 * stored in reverse order at the end of the arena.
248 * This speeds things slightly.
250 addr = ix->amap[i].start + a->memstats.used;
251 for(clump=a->memstats.clumps; clump > 0; clump-=n){
255 if(readclumpinfos(a, clump-n, cis, n) != n){
256 fprint(2, "%T arena %s: directory read: %r\n", a->name);
260 for(j=n-1; j>=0; j--){
262 ie.ia.type = ci->type;
263 ie.ia.size = ci->uncsize;
264 addr -= ci->size + ClumpSize;
266 ie.ia.blocks = (ci->size + ClumpSize + (1<<ABlockLog)-1) >> ABlockLog;
267 scorecp(ie.score, ci->score);
268 if(ci->type == VtCorruptType)
272 x = indexsect(ix, ie.score);
273 assert(0 <= x && x < ix->nsects);
274 if(ix->sects[x]->writechan) {
278 if(b->nie == nelem(b->ie)) {
279 send(ix->sects[x]->writechan, b);
284 score = sb.score[sb.nscore++];
285 scorecp(score, ie.score);
286 if(sb.nscore == nelem(sb.score)) {
287 markbloomfiltern(ix->bloom, sb.score, sb.nscore);
294 if(addr != ix->amap[i].start)
295 fprint(2, "%T arena %s: clump miscalculation %lld != %lld\n", a->name, addr, ix->amap[i].start);
297 add(&arenaentries, tot);
298 add(&skipentries, nskip);
300 for(i=0; i<ix->nsects; i++)
301 if(ix->sects[i]->writechan && buf[i].nie > 0)
302 send(ix->sects[i]->writechan, &buf[i]);
305 if(ix->bloom && sb.nscore > 0)
306 markbloomfiltern(ix->bloom, sb.score, sb.nscore);
307 sendp(arenadonechan, p);
311 * Convert score into relative bucket number in isect.
312 * Can pass a packed ientry instead of score - score is first.
315 score2bucket(ISect *is, uchar *score)
319 b = hashbits(score, 32)/ix->div;
320 if(b < is->start || b >= is->stop){
321 fprint(2, "score2bucket: score=%V div=%d b=%ud start=%ud stop=%ud\n",
322 score, ix->div, b, is->start, is->stop);
324 assert(is->start <= b && b < is->stop);
325 return b - is->start;
329 * Convert offset in index section to bucket number.
332 offset2bucket(ISect *is, u64int offset)
336 assert(is->blockbase <= offset);
337 offset -= is->blockbase;
338 b = offset/is->blocksize;
339 assert(b < is->stop-is->start);
344 * Convert bucket number to offset.
347 bucket2offset(ISect *is, u32int b)
349 assert(b <= is->stop-is->start);
350 return is->blockbase + (u64int)b*is->blocksize;
354 * IEntry buffers to hold initial round of spraying.
356 typedef struct Buf Buf;
359 Part *part; /* partition being written */
360 uchar *bp; /* current block */
361 uchar *ep; /* end of block */
362 uchar *wp; /* write position in block */
363 u64int boffset; /* start offset */
364 u64int woffset; /* next write offset */
365 u64int eoffset; /* end offset */
366 u32int nentry; /* number of entries written */
374 if(buf->woffset >= buf->eoffset)
375 sysfatal("buf index chunk overflow - need bigger index");
376 bufsize = buf->ep - buf->bp;
377 if(writepart(buf->part, buf->woffset, buf->bp, bufsize) < 0){
378 fprint(2, "write %s: %r\n", buf->part->name);
381 buf->woffset += bufsize;
382 memset(buf->bp, 0, bufsize);
387 bwrite(Buf *buf, IEntry *ie)
389 if(buf->wp+IEntrySize > buf->ep)
391 assert(buf->bp <= buf->wp && buf->wp < buf->ep);
392 packientry(ie, buf->wp);
393 buf->wp += IEntrySize;
394 assert(buf->bp <= buf->wp && buf->wp <= buf->ep);
399 * Minibuffer. In-memory data structure holds our place
400 * in the buffer but has no block data. We are writing and
401 * reading the minibuffers at the same time. (Careful!)
403 typedef struct Minibuf Minibuf;
406 u64int boffset; /* start offset */
407 u64int roffset; /* read offset */
408 u64int woffset; /* write offset */
409 u64int eoffset; /* end offset */
410 u32int nentry; /* # entries left to read */
411 u32int nwentry; /* # entries written */
415 * Index entry pool. Used when trying to shuffle around
416 * the entries in a big buffer into the corresponding M minibuffers.
417 * Sized to hold M*EntriesPerBlock entries, so that there will always
418 * either be room in the pool for another block worth of entries
419 * or there will be an entire block worth of sorted entries to
422 typedef struct IEntryLink IEntryLink;
423 typedef struct IPool IPool;
427 uchar ie[IEntrySize]; /* raw IEntry */
428 IEntryLink *next; /* next in chain */
434 u32int buck0; /* first bucket in pool */
435 u32int mbufbuckets; /* buckets per minibuf */
436 IEntryLink *entry; /* all IEntryLinks */
437 u32int nentry; /* # of IEntryLinks */
438 IEntryLink *free; /* free list */
439 u32int nfree; /* # on free list */
440 Minibuf *mbuf; /* all minibufs */
441 u32int nmbuf; /* # of minibufs */
442 IEntryLink **mlist; /* lists for each minibuf */
443 u32int *mcount; /* # on each mlist[i] */
444 u32int bufsize; /* block buffer size */
445 uchar *rbuf; /* read buffer */
446 uchar *wbuf; /* write buffer */
447 u32int epbuf; /* entries per block buffer */
458 for(i=0; i<p->nmbuf; i++)
462 print("free %ud:", p->nfree);
463 for(i=0; i<p->nmbuf; i++)
464 print(" %ud", p->mcount[i]);
465 print(" = %lld nentry: %ud\n", n, p->nentry);
467 return n == p->nentry;
472 mkipool(ISect *isect, Minibuf *mbuf, u32int nmbuf,
473 u32int mbufbuckets, u32int bufsize)
480 nentry = (nmbuf+1)*bufsize / IEntrySize;
481 p = ezmalloc(sizeof(IPool)
482 +nentry*sizeof(IEntry)
483 +nmbuf*sizeof(IEntryLink*)
484 +nmbuf*sizeof(u32int)
488 p->mbufbuckets = mbufbuckets;
489 p->bufsize = bufsize;
490 p->entry = (IEntryLink*)(p+1);
492 p->mlist = (IEntryLink**)(p->entry+nentry);
493 p->mcount = (u32int*)(p->mlist+nmbuf);
496 data = (uchar*)(p->mcount+nmbuf);
497 data += bufsize - (uintptr)data%bufsize;
499 p->wbuf = data+bufsize;
500 p->epbuf = bufsize/IEntrySize;
502 for(i=0; i<p->nentry; i++){
512 * Add the index entry ie to the pool p.
513 * Caller must know there is room.
516 ipoolinsert(IPool *p, uchar *ie)
521 assert(p->free != nil);
523 buck = score2bucket(p->isect, ie);
524 x = (buck-p->buck0) / p->mbufbuckets;
526 fprint(2, "buck=%ud mbufbucket=%ud x=%ud\n",
527 buck, p->mbufbuckets, x);
529 assert(x < p->nmbuf);
534 memmove(l->ie, ie, IEntrySize);
535 l->next = p->mlist[x];
541 * Pull out a block containing as many
542 * entries as possible for minibuffer x.
545 ipoolgetbuf(IPool *p, u32int x)
552 ep = p->wbuf + p->bufsize;
554 assert(x < p->nmbuf);
555 for(wp=bp; wp+IEntrySize<=ep && p->mlist[x]; wp+=IEntrySize){
557 p->mlist[x] = l->next;
559 memmove(wp, l->ie, IEntrySize);
565 memset(wp, 0, ep-wp);
570 * Read a block worth of entries from the minibuf
571 * into the pool. Caller must know there is room.
574 ipoolloadblock(IPool *p, Minibuf *mb)
578 assert(mb->nentry > 0);
579 assert(mb->roffset >= mb->woffset);
580 assert(mb->roffset < mb->eoffset);
582 n = p->bufsize/IEntrySize;
585 if(readpart(p->isect->part, mb->roffset, p->rbuf, p->bufsize) < 0)
586 fprint(2, "readpart %s: %r\n", p->isect->part->name);
589 ipoolinsert(p, p->rbuf+i*IEntrySize);
592 mb->roffset += p->bufsize;
596 * Write out a block worth of entries to minibuffer x.
597 * If necessary, pick up the data there before overwriting it.
600 ipoolflush0(IPool *pool, u32int x)
606 bufsize = pool->bufsize;
607 mb->nwentry += ipoolgetbuf(pool, x);
608 if(mb->nentry > 0 && mb->roffset == mb->woffset){
609 assert(pool->nfree >= pool->bufsize/IEntrySize);
611 * There will be room in the pool -- we just
612 * removed a block worth.
614 ipoolloadblock(pool, mb);
616 if(writepart(pool->isect->part, mb->woffset, pool->wbuf, bufsize) < 0)
617 fprint(2, "writepart %s: %r\n", pool->isect->part->name);
618 mb->woffset += bufsize;
622 * Write out some full block of entries.
623 * (There must be one -- the pool is almost full!)
626 ipoolflush1(IPool *pool)
630 assert(pool->nfree <= pool->epbuf);
632 for(i=0; i<pool->nmbuf; i++){
633 if(pool->mcount[i] >= pool->epbuf){
634 ipoolflush0(pool, i);
638 /* can't be reached - someone must be full */
639 sysfatal("ipoolflush1");
643 * Flush all the entries in the pool out to disk.
644 * Nothing more to read from disk.
647 ipoolflush(IPool *pool)
651 for(i=0; i<pool->nmbuf; i++)
652 while(pool->mlist[i])
653 ipoolflush0(pool, i);
654 assert(pool->nfree == pool->nentry);
658 * Third pass. Pick up each minibuffer from disk into
659 * memory and then write out the buckets.
663 * Compare two packed index entries.
664 * Usual ordering except break ties by putting higher
665 * index addresses first (assumes have duplicates
666 * due to corruption in the lower addresses).
669 ientrycmpaddr(const void *va, const void *vb)
679 return -memcmp(a+IEntryAddrOff, b+IEntryAddrOff, 8);
683 zerorange(Part *p, u64int o, u64int e)
685 static uchar zero[MaxIoSize];
692 if(writepart(p, o, zero, n) < 0)
693 fprint(2, "writepart %s: %r\n", p->name);
698 * Load a minibuffer into memory and write out the
699 * corresponding buckets.
702 sortminibuffer(ISect *is, Minibuf *mb, uchar *buf, u32int nbuf, u32int bufsize)
704 uchar *buckdata, *p, *q, *ep;
705 u32int b, lastb, memsize, n;
711 buckdata = emalloc(is->blocksize);
717 * read entire buffer.
719 assert(mb->nwentry*IEntrySize <= mb->woffset-mb->boffset);
720 assert(mb->woffset-mb->boffset <= nbuf);
721 if(readpart(part, mb->boffset, buf, mb->woffset-mb->boffset) < 0){
722 fprint(2, "readpart %s: %r\n", part->name);
726 assert(*(uint*)buf != 0xa5a5a5a5);
729 * remove fragmentation due to IEntrySize
730 * not evenly dividing Bufsize
732 memsize = (bufsize/IEntrySize)*IEntrySize;
733 for(o=mb->boffset, p=q=buf; o<mb->woffset; o+=bufsize){
734 memmove(p, q, memsize);
738 ep = buf + mb->nwentry*IEntrySize;
739 assert(ep <= buf+nbuf);
744 qsort(buf, mb->nwentry, IEntrySize, ientrycmpaddr);
750 lastb = offset2bucket(is, mb->boffset);
751 for(p=buf; p<ep; p=q){
752 b = score2bucket(is, p);
753 for(q=p; q<ep && score2bucket(is, q)==b; q+=IEntrySize)
755 if(lastb+1 < b && zero)
756 zerorange(part, bucket2offset(is, lastb+1), bucket2offset(is, b));
757 if(IBucketSize+(q-p) > is->blocksize)
758 sysfatal("bucket overflow - make index bigger");
759 memmove(buckdata+IBucketSize, p, q-p);
760 ib.n = (q-p)/IEntrySize;
762 packibucket(&ib, buckdata, is->bucketmagic);
763 if(writepart(part, bucket2offset(is, b), buckdata, is->blocksize) < 0)
764 fprint(2, "write %s: %r\n", part->name);
767 if(lastb+1 < is->stop-is->start && zero)
768 zerorange(part, bucket2offset(is, lastb+1), bucket2offset(is, is->stop - is->start));
771 fprint(2, "sortminibuffer bug: n=%ud nwentry=%ud have=%ld\n", n, mb->nwentry, (ep-buf)/IEntrySize);
779 u32int buck, bufbuckets, bufsize, epbuf, i, j;
780 u32int mbufbuckets, n, nbucket, nn, space;
781 u32int nbuf, nminibuf, xminiclump, prod;
782 u64int blocksize, offset, xclump;
792 blocksize = is->blocksize;
793 nbucket = is->stop - is->start;
797 * pass 1 - write index entries from arenas into
798 * large sequential sections on index disk.
799 * requires nbuf * bufsize memory.
801 * pass 2 - split each section into minibufs.
802 * requires nminibuf * bufsize memory.
804 * pass 3 - read each minibuf into memory and
806 * requires entries/minibuf * IEntrySize memory.
808 * The larger we set bufsize the less seeking hurts us.
810 * The fewer sections and minibufs we have, the less
813 * The fewer sections and minibufs we have, the
814 * more entries we end up with in each minibuf
817 * Shoot for using half our memory to hold each
818 * minibuf. The chance of a random distribution
819 * getting off by 2x is quite low.
821 * Once that is decided, figure out the smallest
822 * nminibuf and nsection/biggest bufsize we can use
823 * and still fit in the memory constraints.
826 /* expected number of clump index entries we'll see */
827 xclump = nbucket * (double)totalclumps/totalbuckets;
829 /* number of clumps we want to see in a minibuf */
830 xminiclump = isectmem/2/IEntrySize;
832 /* total number of minibufs we need */
833 prod = (xclump+xminiclump-1) / xminiclump;
835 /* if possible, skip second pass */
836 if(!dumb && prod*MinBufSize < isectmem){
840 /* otherwise use nsection = sqrt(nmini) */
841 for(nbuf=1; nbuf*nbuf<prod; nbuf++)
843 if(nbuf*MinBufSize > isectmem)
844 sysfatal("not enough memory");
847 /* size buffer to use extra memory */
848 bufsize = MinBufSize;
849 while(bufsize*2*nbuf <= isectmem && bufsize < MaxBufSize)
851 data = emalloc(nbuf*bufsize);
852 epbuf = bufsize/IEntrySize;
853 fprint(2, "%T %s: %,ud buckets, %,ud groups, %,ud minigroups, %,ud buffer\n",
854 is->part->name, nbucket, nbuf, nminibuf, bufsize);
856 * Accept index entries from arena procs.
858 buf = MKNZ(Buf, nbuf);
860 offset = is->blockbase;
861 bufbuckets = (nbucket+nbuf-1)/nbuf;
862 for(i=0; i<nbuf; i++){
863 buf[i].part = is->part;
868 buf[i].boffset = offset;
869 buf[i].woffset = offset;
871 offset += bufbuckets*blocksize;
872 buf[i].eoffset = offset;
874 offset = is->blockbase + nbucket*blocksize;
875 buf[i].eoffset = offset;
878 assert(p == data+nbuf*bufsize);
881 while(recv(is->writechan, &ieb) == 1){
884 for(j=0; j<ieb.nie; j++){
886 buck = score2bucket(is, ie.score);
889 bwrite(&buf[i], &ie);
893 add(&indexentries, n);
896 for(i=0; i<nbuf; i++){
904 fprint(2, "isectproc bug: n=%ud nn=%ud\n", n, nn);
908 fprint(2, "%T %s: reordering\n", is->part->name);
911 * Rearrange entries into minibuffers and then
912 * split each minibuffer into buckets.
913 * The minibuffer must be sized so that it is
914 * a multiple of blocksize -- ipoolloadblock assumes
915 * that each minibuf starts aligned on a blocksize
918 mbuf = MKN(Minibuf, nminibuf);
919 mbufbuckets = (bufbuckets+nminibuf-1)/nminibuf;
920 while(mbufbuckets*blocksize % bufsize)
922 for(i=0; i<nbuf; i++){
924 * Set up descriptors.
928 offset = buf[i].boffset;
929 memset(mbuf, 0, nminibuf*sizeof(mbuf[0]));
930 for(j=0; j<nminibuf; j++){
932 mb->boffset = offset;
933 offset += mbufbuckets*blocksize;
934 if(offset > buf[i].eoffset)
935 offset = buf[i].eoffset;
936 mb->eoffset = offset;
937 mb->roffset = mb->boffset;
938 mb->woffset = mb->boffset;
939 mb->nentry = epbuf * (mb->eoffset - mb->boffset)/bufsize;
940 if(mb->nentry > buf[i].nentry)
941 mb->nentry = buf[i].nentry;
942 buf[i].nentry -= mb->nentry;
946 fprint(2, "isectproc bug2: n=%ud nn=%ud (i=%d)\n", n, nn, i);;
950 if(!dumb && nminibuf == 1){
951 mbuf[0].nwentry = mbuf[0].nentry;
952 mbuf[0].woffset = buf[i].woffset;
954 ipool = mkipool(is, mbuf, nminibuf, mbufbuckets, bufsize);
955 ipool->buck0 = bufbuckets*i;
956 for(j=0; j<nminibuf; j++){
958 while(mb->nentry > 0){
959 if(ipool->nfree < epbuf){
961 /* ipoolflush1 might change mb->nentry */
964 assert(ipool->nfree >= epbuf);
965 ipoolloadblock(ipool, mb);
970 for(j=0; j<nminibuf; j++)
971 nn += mbuf[j].nwentry;
973 fprint(2, "isectproc bug3: n=%ud nn=%ud (i=%d)\n", n, nn, i);
981 for(j=0; j<nminibuf; j++)
982 if(space < mbuf[j].woffset - mbuf[j].boffset)
983 space = mbuf[j].woffset - mbuf[j].boffset;
985 data = emalloc(space);
986 for(j=0; j<nminibuf; j++){
988 sortminibuffer(is, mb, data, space, bufsize);
993 sendp(isectdonechan, is);