2 * Write the dirty icache entries to disk. Random seeks are
3 * so expensive that it makes sense to wait until we have
4 * a lot and then just make a sequential pass over the disk.
10 static void icachewriteproc(void*);
11 static void icachewritecoord(void*);
12 static IEntry *iesort(IEntry*);
14 int icachesleeptime = 1000; /* milliseconds */
21 typedef struct IWrite IWrite;
36 initround(&iwrite.round, "icache", 120*60*1000);
38 for(i=0; i<ix->nsects; i++){
39 ix->sects[i]->writechan = chancreate(sizeof(ulong), 1);
40 ix->sects[i]->writedonechan = chancreate(sizeof(ulong), 1);
41 vtproc(icachewriteproc, ix->sects[i]);
43 vtproc(icachewritecoord, nil);
44 vtproc(delaykickroundproc, &iwrite.round);
48 nextchunk(Index *ix, ISect *is, IEntry **pie, u64int *paddr, uint *pnbuf)
53 IEntry *iefirst, *ie, **l;
55 bsize = 1<<is->blocklog;
57 addr = is->blockbase + ((u64int)(hashbits(iefirst->score, 32) / ix->div - is->start) << is->blocklog);
59 for(l=&iefirst->nextdirty; (ie=*l)!=nil; l=&(*l)->nextdirty){
60 naddr = is->blockbase + ((u64int)(hashbits(ie->score, 32) / ix->div - is->start) << is->blocklog);
61 if(naddr - addr >= Bufsize)
75 icachewritesect(Index *ix, ISect *is, u8int *buf)
83 IEntry *ie, *iedirty, **l, *chunk;
85 lo = is->start * ix->div;
86 if(TWID32/ix->div < is->stop)
89 hi = is->stop * ix->div - 1;
91 trace(TraceProc, "icachewritesect enter %ud %ud %llud", lo, hi, iwrite.as.aa);
93 iedirty = icachedirty(lo, hi, iwrite.as.aa);
94 iedirty = iesort(iedirty);
95 bsize = 1<<is->blocklog;
99 sleep(icachesleeptime);
100 trace(TraceProc, "icachewritesect nextchunk");
101 chunk = nextchunk(ix, is, &iedirty, &addr, &nbuf);
103 trace(TraceProc, "icachewritesect readpart 0x%llux+0x%ux", addr, nbuf);
104 if(readpart(is->part, addr, buf, nbuf) < 0){
106 fprint(2, "icachewriteproc readpart: %r\n");
110 trace(TraceProc, "icachewritesect updatebuf");
111 addstat(StatIsectReadBytes, nbuf);
112 addstat(StatIsectRead, 1);
114 for(l=&chunk; (ie=*l)!=nil; l=&ie->nextdirty){
116 naddr = is->blockbase + ((u64int)(hashbits(ie->score, 32) / ix->div - is->start) << is->blocklog);
118 if(off+bsize > nbuf){
119 fprint(2, "whoops! addr=0x%llux nbuf=%ud addr+nbuf=0x%llux naddr=0x%llux\n",
120 addr, nbuf, addr+nbuf, naddr);
121 assert(off+bsize <= nbuf);
123 unpackibucket(&ib, buf+off, is->bucketmagic);
124 if(okibucket(&ib, is) < 0){
125 fprint(2, "bad bucket XXX\n");
128 trace(TraceProc, "icachewritesect add %V at 0x%llux", ie->score, naddr);
129 h = bucklook(ie->score, ie->ia.type, ib.data, ib.n);
132 packientry(ie, &ib.data[h]);
133 }else if(ib.n < is->buckmax){
134 memmove(&ib.data[h+IEntrySize], &ib.data[h], ib.n*IEntrySize - h);
136 packientry(ie, &ib.data[h]);
138 fprint(2, "bucket overflow XXX\n");
148 packibucket(&ib, buf+off, is->bucketmagic);
149 if((b = _getdblock(is->part, naddr, ORDWR, 0)) != nil){
150 memmove(b->data, buf+off, bsize);
155 trace(TraceProc, "icachewritesect writepart", addr, nbuf);
156 if(writepart(is->part, addr, buf, nbuf) < 0){
158 fprint(2, "icachewriteproc writepart: %r\n");
162 addstat(StatIsectWriteBytes, nbuf);
163 addstat(StatIsectWrite, 1);
167 trace(TraceProc, "icachewritesect done");
172 icachewriteproc(void *v)
181 threadsetname("icachewriteproc:%s", is->part->name);
183 bsize = 1<<is->blocklog;
184 buf = emalloc(Bufsize+bsize);
185 buf = (u8int*)(((ulong)buf+bsize-1)&~(ulong)(bsize-1));
188 trace(TraceProc, "icachewriteproc recv");
189 recv(is->writechan, 0);
190 trace(TraceWork, "start");
191 icachewritesect(ix, is, buf);
192 trace(TraceProc, "icachewriteproc send");
193 trace(TraceWork, "finish");
194 send(is->writedonechan, 0);
199 icachewritecoord(void *v)
207 threadsetname("icachewritecoord");
210 iwrite.as = diskstate();
213 trace(TraceProc, "icachewritecoord sleep");
214 waitforkick(&iwrite.round);
215 trace(TraceWork, "start");
217 if(as.arena==iwrite.as.arena && as.aa==iwrite.as.aa){
218 /* will not be able to do anything more than last flush - kick disk */
219 trace(TraceProc, "icachewritecoord flush dcache");
221 trace(TraceProc, "icachewritecoord flushed dcache");
225 trace(TraceProc, "icachewritecoord start flush");
227 for(i=0; i<ix->nsects; i++)
228 send(ix->sects[i]->writechan, 0);
230 send(ix->bloom->writechan, 0);
232 for(i=0; i<ix->nsects; i++)
233 recv(ix->sects[i]->writedonechan, 0);
235 recv(ix->bloom->writedonechan, 0);
237 trace(TraceProc, "icachewritecoord donewrite");
238 setatailstate(&iwrite.as);
240 icacheclean(nil); /* wake up anyone waiting */
241 trace(TraceWork, "finish");
242 addstat(StatIcacheFlush, 1);
249 trace(TraceProc, "flushicache enter");
250 kickround(&iwrite.round, 1);
251 trace(TraceProc, "flushicache exit");
257 kickround(&iwrite.round, 0);
261 delaykickicache(void)
263 delaykickround(&iwrite.round);
271 IEntry *ie1, *ie2, *sorted;
273 if(ie == nil || ie->nextdirty == nil)
276 /* split the lists */
280 ie2 = ie2->nextdirty;
282 ie2 = ie2->nextdirty;
284 ie1 = ie1->nextdirty;
285 ie2 = ie2->nextdirty;
287 ie2 = ie2->nextdirty;
290 ie2 = ie1->nextdirty;
291 ie1->nextdirty = nil;
298 /* merge the lists */
304 cmp = scorecmp(ie1->score, ie2->score);
305 if(ie1==nil || (ie2 && cmp > 0)){
308 ie2 = ie2->nextdirty;
312 ie1 = ie1->nextdirty;