2 a0d146ed 2005-07-12 devnull * Write the dirty icache entries to disk. Random seeks are
3 a0d146ed 2005-07-12 devnull * so expensive that it makes sense to wait until we have
4 a0d146ed 2005-07-12 devnull * a lot and then just make a sequential pass over the disk.
6 a0d146ed 2005-07-12 devnull #include "stdinc.h"
7 a0d146ed 2005-07-12 devnull #include "dat.h"
8 a0d146ed 2005-07-12 devnull #include "fns.h"
10 a0d146ed 2005-07-12 devnull static void icachewriteproc(void*);
11 a0d146ed 2005-07-12 devnull static void icachewritecoord(void*);
12 a0d146ed 2005-07-12 devnull static IEntry *iesort(IEntry*);
14 a0d146ed 2005-07-12 devnull int icachesleeptime = 1000; /* milliseconds */
18 a0d146ed 2005-07-12 devnull Bufsize = 8*1024*1024
21 a0d146ed 2005-07-12 devnull typedef struct IWrite IWrite;
22 a0d146ed 2005-07-12 devnull struct IWrite
24 a0d146ed 2005-07-12 devnull Round round;
25 a0d146ed 2005-07-12 devnull AState as;
28 a0d146ed 2005-07-12 devnull static IWrite iwrite;
31 a0d146ed 2005-07-12 devnull initicachewrite(void)
34 a0d146ed 2005-07-12 devnull Index *ix;
36 a0d146ed 2005-07-12 devnull initround(&iwrite.round, "icache", 120*60*1000);
37 a0d146ed 2005-07-12 devnull ix = mainindex;
38 a0d146ed 2005-07-12 devnull for(i=0; i<ix->nsects; i++){
39 a0d146ed 2005-07-12 devnull ix->sects[i]->writechan = chancreate(sizeof(ulong), 1);
40 a0d146ed 2005-07-12 devnull ix->sects[i]->writedonechan = chancreate(sizeof(ulong), 1);
41 a0d146ed 2005-07-12 devnull vtproc(icachewriteproc, ix->sects[i]);
43 a0d146ed 2005-07-12 devnull vtproc(icachewritecoord, nil);
44 a0d146ed 2005-07-12 devnull vtproc(delaykickroundproc, &iwrite.round);
47 a0d146ed 2005-07-12 devnull static IEntry*
48 a0d146ed 2005-07-12 devnull nextchunk(Index *ix, ISect *is, IEntry **pie, u64int *paddr, uint *pnbuf)
50 a0d146ed 2005-07-12 devnull u64int addr, naddr;
51 a0d146ed 2005-07-12 devnull uint nbuf;
52 a0d146ed 2005-07-12 devnull int bsize;
53 a0d146ed 2005-07-12 devnull IEntry *iefirst, *ie, **l;
55 a0d146ed 2005-07-12 devnull bsize = 1<<is->blocklog;
56 a0d146ed 2005-07-12 devnull iefirst = *pie;
57 a0d146ed 2005-07-12 devnull addr = is->blockbase + ((u64int)(hashbits(iefirst->score, 32) / ix->div - is->start) << is->blocklog);
58 a0d146ed 2005-07-12 devnull nbuf = 0;
59 a0d146ed 2005-07-12 devnull for(l=&iefirst->nextdirty; (ie=*l)!=nil; l=&(*l)->nextdirty){
60 a0d146ed 2005-07-12 devnull naddr = is->blockbase + ((u64int)(hashbits(ie->score, 32) / ix->div - is->start) << is->blocklog);
61 a0d146ed 2005-07-12 devnull if(naddr - addr >= Bufsize)
63 a0d146ed 2005-07-12 devnull nbuf = naddr-addr;
65 a0d146ed 2005-07-12 devnull nbuf += bsize;
67 a0d146ed 2005-07-12 devnull *l = nil;
68 a0d146ed 2005-07-12 devnull *pie = ie;
69 a0d146ed 2005-07-12 devnull *paddr = addr;
70 a0d146ed 2005-07-12 devnull *pnbuf = nbuf;
71 a0d146ed 2005-07-12 devnull return iefirst;
74 a0d146ed 2005-07-12 devnull static int
75 a0d146ed 2005-07-12 devnull icachewritesect(Index *ix, ISect *is, u8int *buf)
77 a0d146ed 2005-07-12 devnull int err, h, bsize;
78 a0d146ed 2005-07-12 devnull u32int lo, hi;
79 a0d146ed 2005-07-12 devnull u64int addr, naddr;
80 a0d146ed 2005-07-12 devnull uint nbuf, off;
81 a0d146ed 2005-07-12 devnull DBlock *b;
82 a0d146ed 2005-07-12 devnull IBucket ib;
83 a0d146ed 2005-07-12 devnull IEntry *ie, *iedirty, **l, *chunk;
85 a0d146ed 2005-07-12 devnull lo = is->start * ix->div;
86 a0d146ed 2005-07-12 devnull if(TWID32/ix->div < is->stop)
87 a0d146ed 2005-07-12 devnull hi = TWID32;
89 a0d146ed 2005-07-12 devnull hi = is->stop * ix->div - 1;
91 a0d146ed 2005-07-12 devnull trace(TraceProc, "icachewritesect enter %ud %ud %llud", lo, hi, iwrite.as.aa);
93 a0d146ed 2005-07-12 devnull iedirty = icachedirty(lo, hi, iwrite.as.aa);
94 a0d146ed 2005-07-12 devnull iedirty = iesort(iedirty);
95 a0d146ed 2005-07-12 devnull bsize = 1<<is->blocklog;
98 a0d146ed 2005-07-12 devnull while(iedirty){
99 a0d146ed 2005-07-12 devnull sleep(icachesleeptime);
100 a0d146ed 2005-07-12 devnull trace(TraceProc, "icachewritesect nextchunk");
101 a0d146ed 2005-07-12 devnull chunk = nextchunk(ix, is, &iedirty, &addr, &nbuf);
103 a0d146ed 2005-07-12 devnull trace(TraceProc, "icachewritesect readpart 0x%llux+0x%ux", addr, nbuf);
104 a0d146ed 2005-07-12 devnull if(readpart(is->part, addr, buf, nbuf) < 0){
106 a0d146ed 2005-07-12 devnull fprint(2, "icachewriteproc readpart: %r\n");
107 a0d146ed 2005-07-12 devnull err = -1;
108 a0d146ed 2005-07-12 devnull continue;
110 a0d146ed 2005-07-12 devnull trace(TraceProc, "icachewritesect updatebuf");
111 a0d146ed 2005-07-12 devnull addstat(StatIsectReadBytes, nbuf);
112 a0d146ed 2005-07-12 devnull addstat(StatIsectRead, 1);
114 a0d146ed 2005-07-12 devnull for(l=&chunk; (ie=*l)!=nil; l=&ie->nextdirty){
116 a0d146ed 2005-07-12 devnull naddr = is->blockbase + ((u64int)(hashbits(ie->score, 32) / ix->div - is->start) << is->blocklog);
117 a0d146ed 2005-07-12 devnull off = naddr - addr;
118 a0d146ed 2005-07-12 devnull if(off+bsize > nbuf){
119 a0d146ed 2005-07-12 devnull fprint(2, "whoops! addr=0x%llux nbuf=%ud addr+nbuf=0x%llux naddr=0x%llux\n",
120 a0d146ed 2005-07-12 devnull addr, nbuf, addr+nbuf, naddr);
121 a0d146ed 2005-07-12 devnull assert(off+bsize <= nbuf);
123 a0d146ed 2005-07-12 devnull unpackibucket(&ib, buf+off, is->bucketmagic);
124 a0d146ed 2005-07-12 devnull if(okibucket(&ib, is) < 0){
125 a0d146ed 2005-07-12 devnull fprint(2, "bad bucket XXX\n");
126 a0d146ed 2005-07-12 devnull goto skipit;
128 a0d146ed 2005-07-12 devnull trace(TraceProc, "icachewritesect add %V at 0x%llux", ie->score, naddr);
129 a0d146ed 2005-07-12 devnull h = bucklook(ie->score, ie->ia.type, ib.data, ib.n);
130 a0d146ed 2005-07-12 devnull if(h & 1){
132 a0d146ed 2005-07-12 devnull packientry(ie, &ib.data[h]);
133 a0d146ed 2005-07-12 devnull }else if(ib.n < is->buckmax){
134 a0d146ed 2005-07-12 devnull memmove(&ib.data[h+IEntrySize], &ib.data[h], ib.n*IEntrySize - h);
136 a0d146ed 2005-07-12 devnull packientry(ie, &ib.data[h]);
138 a0d146ed 2005-07-12 devnull fprint(2, "bucket overflow XXX\n");
140 a0d146ed 2005-07-12 devnull err = -1;
141 a0d146ed 2005-07-12 devnull *l = ie->nextdirty;
142 a0d146ed 2005-07-12 devnull ie = *l;
144 a0d146ed 2005-07-12 devnull goto again;
148 a0d146ed 2005-07-12 devnull packibucket(&ib, buf+off, is->bucketmagic);
149 a0d146ed 2005-07-12 devnull if((b = _getdblock(is->part, naddr, ORDWR, 0)) != nil){
150 a0d146ed 2005-07-12 devnull memmove(b->data, buf+off, bsize);
151 a0d146ed 2005-07-12 devnull putdblock(b);
155 a0d146ed 2005-07-12 devnull trace(TraceProc, "icachewritesect writepart", addr, nbuf);
156 a0d146ed 2005-07-12 devnull if(writepart(is->part, addr, buf, nbuf) < 0){
158 a0d146ed 2005-07-12 devnull fprint(2, "icachewriteproc writepart: %r\n");
159 a0d146ed 2005-07-12 devnull err = -1;
160 a0d146ed 2005-07-12 devnull continue;
162 a0d146ed 2005-07-12 devnull addstat(StatIsectWriteBytes, nbuf);
163 a0d146ed 2005-07-12 devnull addstat(StatIsectWrite, 1);
164 a0d146ed 2005-07-12 devnull icacheclean(chunk);
167 a0d146ed 2005-07-12 devnull trace(TraceProc, "icachewritesect done");
168 a0d146ed 2005-07-12 devnull return err;
171 a0d146ed 2005-07-12 devnull static void
172 a0d146ed 2005-07-12 devnull icachewriteproc(void *v)
174 a0d146ed 2005-07-12 devnull uint bsize;
175 a0d146ed 2005-07-12 devnull ISect *is;
176 a0d146ed 2005-07-12 devnull Index *ix;
177 a0d146ed 2005-07-12 devnull u8int *buf;
179 a0d146ed 2005-07-12 devnull ix = mainindex;
181 a0d146ed 2005-07-12 devnull threadsetname("icachewriteproc:%s", is->part->name);
183 a0d146ed 2005-07-12 devnull bsize = 1<<is->blocklog;
184 a0d146ed 2005-07-12 devnull buf = emalloc(Bufsize+bsize);
185 a0d146ed 2005-07-12 devnull buf = (u8int*)(((ulong)buf+bsize-1)&~(ulong)(bsize-1));
187 a0d146ed 2005-07-12 devnull for(;;){
188 a0d146ed 2005-07-12 devnull trace(TraceProc, "icachewriteproc recv");
189 a0d146ed 2005-07-12 devnull recv(is->writechan, 0);
190 a0d146ed 2005-07-12 devnull trace(TraceWork, "start");
191 a0d146ed 2005-07-12 devnull icachewritesect(ix, is, buf);
192 a0d146ed 2005-07-12 devnull trace(TraceProc, "icachewriteproc send");
193 a0d146ed 2005-07-12 devnull trace(TraceWork, "finish");
194 a0d146ed 2005-07-12 devnull send(is->writedonechan, 0);
198 a0d146ed 2005-07-12 devnull static void
199 a0d146ed 2005-07-12 devnull icachewritecoord(void *v)
202 a0d146ed 2005-07-12 devnull Index *ix;
203 a0d146ed 2005-07-12 devnull AState as;
205 a0d146ed 2005-07-12 devnull USED(v);
207 a0d146ed 2005-07-12 devnull threadsetname("icachewritecoord");
209 a0d146ed 2005-07-12 devnull ix = mainindex;
210 a0d146ed 2005-07-12 devnull iwrite.as = diskstate();
212 a0d146ed 2005-07-12 devnull for(;;){
213 a0d146ed 2005-07-12 devnull trace(TraceProc, "icachewritecoord sleep");
214 a0d146ed 2005-07-12 devnull waitforkick(&iwrite.round);
215 a0d146ed 2005-07-12 devnull trace(TraceWork, "start");
216 a0d146ed 2005-07-12 devnull as = diskstate();
217 a0d146ed 2005-07-12 devnull if(as.arena==iwrite.as.arena && as.aa==iwrite.as.aa){
218 a0d146ed 2005-07-12 devnull /* will not be able to do anything more than last flush - kick disk */
219 a0d146ed 2005-07-12 devnull trace(TraceProc, "icachewritecoord flush dcache");
220 a0d146ed 2005-07-12 devnull kickdcache();
221 a0d146ed 2005-07-12 devnull trace(TraceProc, "icachewritecoord flushed dcache");
223 a0d146ed 2005-07-12 devnull iwrite.as = as;
225 a0d146ed 2005-07-12 devnull trace(TraceProc, "icachewritecoord start flush");
226 a0d146ed 2005-07-12 devnull if(iwrite.as.arena){
227 a0d146ed 2005-07-12 devnull for(i=0; i<ix->nsects; i++)
228 a0d146ed 2005-07-12 devnull send(ix->sects[i]->writechan, 0);
229 a0d146ed 2005-07-12 devnull if(ix->bloom)
230 a0d146ed 2005-07-12 devnull send(ix->bloom->writechan, 0);
232 a0d146ed 2005-07-12 devnull for(i=0; i<ix->nsects; i++)
233 a0d146ed 2005-07-12 devnull recv(ix->sects[i]->writedonechan, 0);
234 a0d146ed 2005-07-12 devnull if(ix->bloom)
235 a0d146ed 2005-07-12 devnull recv(ix->bloom->writedonechan, 0);
237 a0d146ed 2005-07-12 devnull trace(TraceProc, "icachewritecoord donewrite");
238 a0d146ed 2005-07-12 devnull setatailstate(&iwrite.as);
240 a0d146ed 2005-07-12 devnull icacheclean(nil); /* wake up anyone waiting */
241 a0d146ed 2005-07-12 devnull trace(TraceWork, "finish");
242 a0d146ed 2005-07-12 devnull addstat(StatIcacheFlush, 1);
247 a0d146ed 2005-07-12 devnull flushicache(void)
249 a0d146ed 2005-07-12 devnull trace(TraceProc, "flushicache enter");
250 a0d146ed 2005-07-12 devnull kickround(&iwrite.round, 1);
251 a0d146ed 2005-07-12 devnull trace(TraceProc, "flushicache exit");
255 a0d146ed 2005-07-12 devnull kickicache(void)
257 a0d146ed 2005-07-12 devnull kickround(&iwrite.round, 0);
261 a0d146ed 2005-07-12 devnull delaykickicache(void)
263 a0d146ed 2005-07-12 devnull delaykickround(&iwrite.round);
266 a0d146ed 2005-07-12 devnull static IEntry*
267 a0d146ed 2005-07-12 devnull iesort(IEntry *ie)
269 a0d146ed 2005-07-12 devnull int cmp;
270 a0d146ed 2005-07-12 devnull IEntry **l;
271 a0d146ed 2005-07-12 devnull IEntry *ie1, *ie2, *sorted;
273 a0d146ed 2005-07-12 devnull if(ie == nil || ie->nextdirty == nil)
274 a0d146ed 2005-07-12 devnull return ie;
276 a0d146ed 2005-07-12 devnull /* split the lists */
277 a0d146ed 2005-07-12 devnull ie1 = ie;
278 a0d146ed 2005-07-12 devnull ie2 = ie;
280 a0d146ed 2005-07-12 devnull ie2 = ie2->nextdirty;
282 a0d146ed 2005-07-12 devnull ie2 = ie2->nextdirty;
283 a0d146ed 2005-07-12 devnull while(ie1 && ie2){
284 a0d146ed 2005-07-12 devnull ie1 = ie1->nextdirty;
285 a0d146ed 2005-07-12 devnull ie2 = ie2->nextdirty;
287 a0d146ed 2005-07-12 devnull ie2 = ie2->nextdirty;
289 a0d146ed 2005-07-12 devnull if(ie1){
290 a0d146ed 2005-07-12 devnull ie2 = ie1->nextdirty;
291 a0d146ed 2005-07-12 devnull ie1->nextdirty = nil;
294 a0d146ed 2005-07-12 devnull /* sort the lists */
295 a0d146ed 2005-07-12 devnull ie1 = iesort(ie);
296 a0d146ed 2005-07-12 devnull ie2 = iesort(ie2);
298 a0d146ed 2005-07-12 devnull /* merge the lists */
299 a0d146ed 2005-07-12 devnull sorted = nil;
300 a0d146ed 2005-07-12 devnull l = &sorted;
301 a0d146ed 2005-07-12 devnull cmp = 0;
302 a0d146ed 2005-07-12 devnull while(ie1 || ie2){
303 a0d146ed 2005-07-12 devnull if(ie1 && ie2)
304 a0d146ed 2005-07-12 devnull cmp = scorecmp(ie1->score, ie2->score);
305 a0d146ed 2005-07-12 devnull if(ie1==nil || (ie2 && cmp > 0)){
306 a0d146ed 2005-07-12 devnull *l = ie2;
307 a0d146ed 2005-07-12 devnull l = &ie2->nextdirty;
308 a0d146ed 2005-07-12 devnull ie2 = ie2->nextdirty;
310 a0d146ed 2005-07-12 devnull *l = ie1;
311 a0d146ed 2005-07-12 devnull l = &ie1->nextdirty;
312 a0d146ed 2005-07-12 devnull ie1 = ie1->nextdirty;
315 a0d146ed 2005-07-12 devnull *l = nil;
316 a0d146ed 2005-07-12 devnull return sorted;