icache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "stdinc.h"
  10. #include "dat.h"
  11. #include "fns.h"
  12. int icacheprefetch = 1;
  13. typedef struct ICache ICache;
  14. typedef struct IHash IHash;
  15. typedef struct ISum ISum;
  16. struct ICache
  17. {
  18. QLock lock;
  19. Rendez full;
  20. IHash *hash;
  21. IEntry *entries;
  22. int nentries;
  23. IEntry free;
  24. IEntry clean;
  25. IEntry dirty;
  26. uint32_t maxdirty;
  27. uint32_t ndirty;
  28. AState as;
  29. ISum **sum;
  30. int nsum;
  31. IHash *shash;
  32. IEntry *sentries;
  33. int nsentries;
  34. };
  35. static ICache icache;
  36. /*
  37. * Hash table of IEntries
  38. */
  39. struct IHash
  40. {
  41. int bits;
  42. uint32_t size;
  43. IEntry **table;
  44. };
  45. static IHash*
  46. mkihash(int size1)
  47. {
  48. uint32_t size;
  49. int bits;
  50. IHash *ih;
  51. bits = 0;
  52. size = 1;
  53. while(size < size1){
  54. bits++;
  55. size <<= 1;
  56. }
  57. ih = vtmallocz(sizeof(IHash)+size*sizeof(ih->table[0]));
  58. ih->table = (IEntry**)(ih+1);
  59. ih->bits = bits;
  60. ih->size = size;
  61. return ih;
  62. }
  63. static IEntry*
  64. ihashlookup(IHash *ih, uint8_t score[VtScoreSize], int type)
  65. {
  66. uint32_t h;
  67. IEntry *ie;
  68. h = hashbits(score, ih->bits);
  69. for(ie=ih->table[h]; ie; ie=ie->nexthash)
  70. if((type == -1 || type == ie->ia.type) && scorecmp(score, ie->score) == 0)
  71. return ie;
  72. return nil;
  73. }
  74. static void
  75. ihashdelete(IHash *ih, IEntry *ie, char *what)
  76. {
  77. uint32_t h;
  78. IEntry **l;
  79. h = hashbits(ie->score, ih->bits);
  80. for(l=&ih->table[h]; *l; l=&(*l)->nexthash)
  81. if(*l == ie){
  82. *l = ie->nexthash;
  83. return;
  84. }
  85. fprint(2, "warning: %s %V not found in ihashdelete\n", what, ie->score);
  86. }
  87. static void
  88. ihashinsert(IHash *ih, IEntry *ie)
  89. {
  90. uint32_t h;
  91. h = hashbits(ie->score, ih->bits);
  92. ie->nexthash = ih->table[h];
  93. ih->table[h] = ie;
  94. }
  95. /*
  96. * IEntry lists.
  97. */
  98. static IEntry*
  99. popout(IEntry *ie)
  100. {
  101. if(ie->prev == nil && ie->next == nil)
  102. return ie;
  103. ie->prev->next = ie->next;
  104. ie->next->prev = ie->prev;
  105. ie->next = nil;
  106. ie->prev = nil;
  107. return ie;
  108. }
  109. static IEntry*
  110. poplast(IEntry *list)
  111. {
  112. if(list->prev == list)
  113. return nil;
  114. return popout(list->prev);
  115. }
  116. static IEntry*
  117. pushfirst(IEntry *list, IEntry *ie)
  118. {
  119. popout(ie);
  120. ie->prev = list;
  121. ie->next = list->next;
  122. ie->prev->next = ie;
  123. ie->next->prev = ie;
  124. return ie;
  125. }
  126. /*
  127. * Arena summary cache.
  128. */
  129. struct ISum
  130. {
  131. QLock lock;
  132. IEntry *entries;
  133. int nentries;
  134. int loaded;
  135. uint64_t addr;
  136. uint64_t limit;
  137. Arena *arena;
  138. int g;
  139. };
  140. static ISum*
  141. scachelookup(uint64_t addr)
  142. {
  143. int i;
  144. ISum *s;
  145. for(i=0; i<icache.nsum; i++){
  146. s = icache.sum[i];
  147. if(s->addr <= addr && addr < s->limit){
  148. if(i > 0){
  149. memmove(icache.sum+1, icache.sum, i*sizeof icache.sum[0]);
  150. icache.sum[0] = s;
  151. }
  152. return s;
  153. }
  154. }
  155. return nil;
  156. }
  157. static void
  158. sumclear(ISum *s)
  159. {
  160. int i;
  161. for(i=0; i<s->nentries; i++)
  162. ihashdelete(icache.shash, &s->entries[i], "scache");
  163. s->nentries = 0;
  164. s->loaded = 0;
  165. s->addr = 0;
  166. s->limit = 0;
  167. s->arena = nil;
  168. s->g = 0;
  169. }
  170. static ISum*
  171. scacheevict(void)
  172. {
  173. ISum *s;
  174. int i;
  175. for(i=icache.nsum-1; i>=0; i--){
  176. s = icache.sum[i];
  177. if(canqlock(&s->lock)){
  178. if(i > 0){
  179. memmove(icache.sum+1, icache.sum, i*sizeof icache.sum[0]);
  180. icache.sum[0] = s;
  181. }
  182. sumclear(s);
  183. return s;
  184. }
  185. }
  186. return nil;
  187. }
  188. static void
  189. scachehit(uint64_t addr)
  190. {
  191. scachelookup(addr); /* for move-to-front */
  192. }
  193. static void
  194. scachesetup(ISum *s, uint64_t addr)
  195. {
  196. uint64_t addr0, limit;
  197. int g;
  198. s->arena = amapitoag(mainindex, addr, &addr0, &limit, &g);
  199. s->addr = addr0;
  200. s->limit = limit;
  201. s->g = g;
  202. }
  203. static void
  204. scacheload(ISum *s)
  205. {
  206. int i, n;
  207. s->loaded = 1;
  208. n = asumload(s->arena, s->g, s->entries, ArenaCIGSize);
  209. /*
  210. * n can be less then ArenaCIGSize, either if the clump group
  211. * is the last in the arena and is only partially filled, or if there
  212. * are corrupt clumps in the group -- those are not returned.
  213. */
  214. for(i=0; i<n; i++){
  215. s->entries[i].ia.addr += s->addr;
  216. ihashinsert(icache.shash, &s->entries[i]);
  217. }
  218. //fprint(2, "%T scacheload %s %d - %d entries\n", s->arena->name, s->g, n);
  219. addstat(StatScachePrefetch, n);
  220. s->nentries = n;
  221. }
  222. static ISum*
  223. scachemiss(uint64_t addr)
  224. {
  225. ISum *s;
  226. if(!icacheprefetch)
  227. return nil;
  228. s = scachelookup(addr);
  229. if(s == nil){
  230. /* first time: make an entry in the cache but don't populate it yet */
  231. s = scacheevict();
  232. if(s == nil)
  233. return nil;
  234. scachesetup(s, addr);
  235. qunlock(&s->lock);
  236. return nil;
  237. }
  238. /* second time: load from disk */
  239. qlock(&s->lock);
  240. if(s->loaded || !icacheprefetch){
  241. qunlock(&s->lock);
  242. return nil;
  243. }
  244. return s; /* locked */
  245. }
  246. /*
  247. * Index cache.
  248. */
  249. void
  250. initicache(uint32_t mem0)
  251. {
  252. uint32_t mem;
  253. int i, entries, scache;
  254. icache.full.l = &icache.lock;
  255. mem = mem0;
  256. entries = mem / (sizeof(IEntry)+sizeof(IEntry*));
  257. scache = (entries/8) / ArenaCIGSize;
  258. entries -= entries/8;
  259. if(scache < 4)
  260. scache = 4;
  261. if(scache > 16)
  262. scache = 16;
  263. if(entries < 1000)
  264. entries = 1000;
  265. fprint(2, "icache %,d bytes = %,d entries; %d scache\n", mem0, entries, scache);
  266. icache.clean.prev = icache.clean.next = &icache.clean;
  267. icache.dirty.prev = icache.dirty.next = &icache.dirty;
  268. icache.free.prev = icache.free.next = &icache.free;
  269. icache.hash = mkihash(entries);
  270. icache.nentries = entries;
  271. setstat(StatIcacheSize, entries);
  272. icache.entries = vtmallocz(entries*sizeof icache.entries[0]);
  273. icache.maxdirty = entries / 2;
  274. for(i=0; i<entries; i++)
  275. pushfirst(&icache.free, &icache.entries[i]);
  276. icache.nsum = scache;
  277. icache.sum = vtmallocz(scache*sizeof icache.sum[0]);
  278. icache.sum[0] = vtmallocz(scache*sizeof icache.sum[0][0]);
  279. icache.nsentries = scache * ArenaCIGSize;
  280. icache.sentries = vtmallocz(scache*ArenaCIGSize*sizeof icache.sentries[0]);
  281. icache.shash = mkihash(scache*ArenaCIGSize);
  282. for(i=0; i<scache; i++){
  283. icache.sum[i] = icache.sum[0] + i;
  284. icache.sum[i]->entries = icache.sentries + i*ArenaCIGSize;
  285. }
  286. }
  287. static IEntry*
  288. evictlru(void)
  289. {
  290. IEntry *ie;
  291. ie = poplast(&icache.clean);
  292. if(ie == nil)
  293. return nil;
  294. ihashdelete(icache.hash, ie, "evictlru");
  295. return ie;
  296. }
  297. static void
  298. icacheinsert(uint8_t score[VtScoreSize], IAddr *ia, int state)
  299. {
  300. IEntry *ie;
  301. if((ie = poplast(&icache.free)) == nil && (ie = evictlru()) == nil){
  302. addstat(StatIcacheStall, 1);
  303. while((ie = poplast(&icache.free)) == nil && (ie = evictlru()) == nil){
  304. // Could safely return here if state == IEClean.
  305. // But if state == IEDirty, have to wait to make
  306. // sure we don't lose an index write.
  307. // Let's wait all the time.
  308. flushdcache();
  309. kickicache();
  310. rsleep(&icache.full);
  311. }
  312. addstat(StatIcacheStall, -1);
  313. }
  314. memmove(ie->score, score, VtScoreSize);
  315. ie->state = state;
  316. ie->ia = *ia;
  317. if(state == IEClean){
  318. addstat(StatIcachePrefetch, 1);
  319. pushfirst(&icache.clean, ie);
  320. }else{
  321. addstat(StatIcacheWrite, 1);
  322. assert(state == IEDirty);
  323. icache.ndirty++;
  324. setstat(StatIcacheDirty, icache.ndirty);
  325. delaykickicache();
  326. pushfirst(&icache.dirty, ie);
  327. }
  328. ihashinsert(icache.hash, ie);
  329. }
  330. int
  331. icachelookup(uint8_t score[VtScoreSize], int type, IAddr *ia)
  332. {
  333. IEntry *ie;
  334. qlock(&icache.lock);
  335. addstat(StatIcacheLookup, 1);
  336. if((ie = ihashlookup(icache.hash, score, type)) != nil){
  337. *ia = ie->ia;
  338. if(ie->state == IEClean)
  339. pushfirst(&icache.clean, ie);
  340. addstat(StatIcacheHit, 1);
  341. qunlock(&icache.lock);
  342. return 0;
  343. }
  344. if((ie = ihashlookup(icache.shash, score, type)) != nil){
  345. *ia = ie->ia;
  346. icacheinsert(score, &ie->ia, IEClean);
  347. scachehit(ie->ia.addr);
  348. addstat(StatScacheHit, 1);
  349. qunlock(&icache.lock);
  350. return 0;
  351. }
  352. addstat(StatIcacheMiss, 1);
  353. qunlock(&icache.lock);
  354. return -1;
  355. }
  356. int
  357. insertscore(uint8_t score[VtScoreSize], IAddr *ia, int state, AState *as)
  358. {
  359. ISum *toload;
  360. qlock(&icache.lock);
  361. icacheinsert(score, ia, state);
  362. if(state == IEClean)
  363. toload = scachemiss(ia->addr);
  364. else{
  365. assert(state == IEDirty);
  366. toload = nil;
  367. if(as == nil)
  368. fprint(2, "%T insertscore IEDirty without as; called from %#p\n",
  369. getcallerpc());
  370. else{
  371. if(icache.as.aa > as->aa)
  372. fprint(2, "%T insertscore: aa moving backward: %#llx -> %#llx\n", icache.as.aa, as->aa);
  373. icache.as = *as;
  374. }
  375. }
  376. qunlock(&icache.lock);
  377. if(toload){
  378. scacheload(toload);
  379. qunlock(&toload->lock);
  380. }
  381. if(icache.ndirty >= icache.maxdirty)
  382. kickicache();
  383. /*
  384. * It's okay not to do this under icache.lock.
  385. * Calling insertscore only happens when we hold
  386. * the lump, meaning any searches for this block
  387. * will hit in the lump cache until after we return.
  388. */
  389. if(state == IEDirty)
  390. markbloomfilter(mainindex->bloom, score);
  391. return 0;
  392. }
  393. int
  394. lookupscore(uint8_t score[VtScoreSize], int type, IAddr *ia)
  395. {
  396. int ms, ret;
  397. IEntry d;
  398. if(icachelookup(score, type, ia) >= 0){
  399. addstat(StatIcacheRead, 1);
  400. return 0;
  401. }
  402. ms = msec();
  403. addstat(StatIcacheFill, 1);
  404. if(loadientry(mainindex, score, type, &d) < 0)
  405. ret = -1;
  406. else{
  407. ret = 0;
  408. insertscore(score, &d.ia, IEClean, nil);
  409. *ia = d.ia;
  410. }
  411. addstat2(StatIcacheRead, 1, StatIcacheReadTime, msec() - ms);
  412. return ret;
  413. }
  414. uint32_t
  415. hashbits(uint8_t *sc, int bits)
  416. {
  417. uint32_t v;
  418. v = (sc[0] << 24) | (sc[1] << 16) | (sc[2] << 8) | sc[3];
  419. if(bits < 32)
  420. v >>= (32 - bits);
  421. return v;
  422. }
  423. uint32_t
  424. icachedirtyfrac(void)
  425. {
  426. return (int64_t)icache.ndirty*IcacheFrac / icache.nentries;
  427. }
  428. /*
  429. * Return a singly-linked list of dirty index entries.
  430. * with 32-bit hash numbers between lo and hi
  431. * and address < limit.
  432. */
  433. IEntry*
  434. icachedirty(uint32_t lo, uint32_t hi, uint64_t limit)
  435. {
  436. uint32_t h;
  437. IEntry *ie, *dirty;
  438. dirty = nil;
  439. trace(TraceProc, "icachedirty enter");
  440. qlock(&icache.lock);
  441. for(ie = icache.dirty.next; ie != &icache.dirty; ie=ie->next){
  442. if(ie->state == IEDirty && ie->ia.addr <= limit){
  443. h = hashbits(ie->score, 32);
  444. if(lo <= h && h <= hi){
  445. ie->nextdirty = dirty;
  446. dirty = ie;
  447. }
  448. }
  449. }
  450. qunlock(&icache.lock);
  451. trace(TraceProc, "icachedirty exit");
  452. if(dirty == nil)
  453. flushdcache();
  454. return dirty;
  455. }
  456. AState
  457. icachestate(void)
  458. {
  459. AState as;
  460. qlock(&icache.lock);
  461. as = icache.as;
  462. qunlock(&icache.lock);
  463. return as;
  464. }
  465. /*
  466. * The singly-linked non-circular list of index entries ie
  467. * has been written to disk. Move them to the clean list.
  468. */
  469. void
  470. icacheclean(IEntry *ie)
  471. {
  472. IEntry *next;
  473. trace(TraceProc, "icacheclean enter");
  474. qlock(&icache.lock);
  475. for(; ie; ie=next){
  476. assert(ie->state == IEDirty);
  477. next = ie->nextdirty;
  478. ie->nextdirty = nil;
  479. popout(ie); /* from icache.dirty */
  480. icache.ndirty--;
  481. ie->state = IEClean;
  482. pushfirst(&icache.clean, ie);
  483. }
  484. setstat(StatIcacheDirty, icache.ndirty);
  485. rwakeupall(&icache.full);
  486. qunlock(&icache.lock);
  487. trace(TraceProc, "icacheclean exit");
  488. }
  489. void
  490. emptyicache(void)
  491. {
  492. int i;
  493. IEntry *ie;
  494. ISum *s;
  495. qlock(&icache.lock);
  496. while((ie = evictlru()) != nil)
  497. pushfirst(&icache.free, ie);
  498. for(i=0; i<icache.nsum; i++){
  499. s = icache.sum[i];
  500. qlock(&s->lock);
  501. sumclear(s);
  502. qunlock(&s->lock);
  503. }
  504. qunlock(&icache.lock);
  505. }