lumpcache.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #include "stdinc.h"
  2. #include "dat.h"
  3. #include "fns.h"
  4. typedef struct LumpCache LumpCache;
  5. enum
  6. {
  7. HashLog = 9,
  8. HashSize = 1<<HashLog,
  9. HashMask = HashSize - 1,
  10. };
  11. struct LumpCache
  12. {
  13. VtLock *lock;
  14. VtRendez *full;
  15. Lump *free; /* list of available lumps */
  16. u32int allowed; /* total allowable space for packets */
  17. u32int avail; /* remaining space for packets */
  18. u32int now; /* ticks for usage timestamps */
  19. Lump **heads; /* hash table for finding address */
  20. int nheap; /* number of available victims */
  21. Lump **heap; /* heap for locating victims */
  22. int nblocks; /* number of blocks allocated */
  23. Lump *blocks; /* array of block descriptors */
  24. };
  25. static LumpCache lumpCache;
  26. static void delHeap(Lump *db);
  27. static int downHeap(int i, Lump *b);
  28. static void fixHeap(int i, Lump *b);
  29. static int upHeap(int i, Lump *b);
  30. static Lump *bumpLump(void);
  31. void
  32. initLumpCache(u32int size, u32int nblocks)
  33. {
  34. Lump *last, *b;
  35. int i;
  36. lumpCache.lock = vtLockAlloc();
  37. lumpCache.full = vtRendezAlloc(lumpCache.lock);
  38. lumpCache.nblocks = nblocks;
  39. lumpCache.allowed = size;
  40. lumpCache.avail = size;
  41. lumpCache.heads = MKNZ(Lump*, HashSize);
  42. lumpCache.heap = MKNZ(Lump*, nblocks);
  43. lumpCache.blocks = MKNZ(Lump, nblocks);
  44. last = nil;
  45. for(i = 0; i < nblocks; i++){
  46. b = &lumpCache.blocks[i];
  47. b->type = TWID8;
  48. b->heap = TWID32;
  49. b->lock = vtLockAlloc();
  50. b->next = last;
  51. last = b;
  52. }
  53. lumpCache.free = last;
  54. lumpCache.nheap = 0;
  55. }
  56. Lump*
  57. lookupLump(u8int *score, int type)
  58. {
  59. Lump *b;
  60. u32int h;
  61. h = hashBits(score, HashLog);
  62. /*
  63. * look for the block in the cache
  64. */
  65. //checkLumpCache();
  66. vtLock(lumpCache.lock);
  67. again:
  68. for(b = lumpCache.heads[h]; b != nil; b = b->next){
  69. if(scoreEq(score, b->score) && type == b->type){
  70. vtLock(stats.lock);
  71. stats.lumpHit++;
  72. vtUnlock(stats.lock);
  73. goto found;
  74. }
  75. }
  76. /*
  77. * missed: locate the block with the oldest second to last use.
  78. * remove it from the heap, and fix up the heap.
  79. */
  80. while(lumpCache.free == nil){
  81. if(bumpLump() == nil){
  82. logErr(EAdmin, "all lump cache blocks in use");
  83. vtSleep(lumpCache.full);
  84. goto again;
  85. }
  86. }
  87. vtLock(stats.lock);
  88. stats.lumpMiss++;
  89. vtUnlock(stats.lock);
  90. b = lumpCache.free;
  91. lumpCache.free = b->next;
  92. /*
  93. * the new block has no last use, so assume it happens sometime in the middle
  94. ZZZ this is not reasonable
  95. */
  96. b->used = (b->used2 + lumpCache.now) / 2;
  97. /*
  98. * rechain the block on the correct hash chain
  99. */
  100. b->next = lumpCache.heads[h];
  101. lumpCache.heads[h] = b;
  102. if(b->next != nil)
  103. b->next->prev = b;
  104. b->prev = nil;
  105. scoreCp(b->score, score);
  106. b->type = type;
  107. b->size = 0;
  108. b->data = nil;
  109. found:
  110. b->ref++;
  111. b->used2 = b->used;
  112. b->used = lumpCache.now++;
  113. if(b->heap != TWID32)
  114. fixHeap(b->heap, b);
  115. vtUnlock(lumpCache.lock);
  116. //checkLumpCache();
  117. vtLock(b->lock);
  118. return b;
  119. }
  120. void
  121. insertLump(Lump *b, Packet *p)
  122. {
  123. u32int size;
  124. /*
  125. * look for the block in the cache
  126. */
  127. //checkLumpCache();
  128. vtLock(lumpCache.lock);
  129. again:
  130. /*
  131. * missed: locate the block with the oldest second to last use.
  132. * remove it from the heap, and fix up the heap.
  133. */
  134. size = packetAllocatedSize(p);
  135. //ZZZ
  136. while(lumpCache.avail < size){
  137. if(bumpLump() == nil){
  138. logErr(EAdmin, "all lump cache blocks in use");
  139. vtSleep(lumpCache.full);
  140. goto again;
  141. }
  142. }
  143. b->data = p;
  144. b->size = size;
  145. lumpCache.avail -= size;
  146. vtUnlock(lumpCache.lock);
  147. //checkLumpCache();
  148. }
  149. void
  150. putLump(Lump *b)
  151. {
  152. if(b == nil)
  153. return;
  154. vtUnlock(b->lock);
  155. //checkLumpCache();
  156. vtLock(lumpCache.lock);
  157. if(--b->ref == 0){
  158. if(b->heap == TWID32)
  159. upHeap(lumpCache.nheap++, b);
  160. vtWakeup(lumpCache.full);
  161. }
  162. vtUnlock(lumpCache.lock);
  163. //checkLumpCache();
  164. }
  165. /*
  166. * remove some lump from use and update the free list and counters
  167. */
  168. static Lump*
  169. bumpLump(void)
  170. {
  171. Lump *b;
  172. u32int h;
  173. /*
  174. * remove blocks until we find one that is unused
  175. * referenced blocks are left in the heap even though
  176. * they can't be scavenged; this is simple a speed optimization
  177. */
  178. for(;;){
  179. if(lumpCache.nheap == 0)
  180. return nil;
  181. b = lumpCache.heap[0];
  182. delHeap(b);
  183. if(!b->ref){
  184. vtWakeup(lumpCache.full);
  185. break;
  186. }
  187. }
  188. /*
  189. * unchain the block
  190. */
  191. if(b->prev == nil){
  192. h = hashBits(b->score, HashLog);
  193. if(lumpCache.heads[h] != b)
  194. fatal("bad hash chains in lump cache");
  195. lumpCache.heads[h] = b->next;
  196. }else
  197. b->prev->next = b->next;
  198. if(b->next != nil)
  199. b->next->prev = b->prev;
  200. if(b->data != nil){
  201. packetFree(b->data);
  202. b->data = nil;
  203. lumpCache.avail += b->size;
  204. b->size = 0;
  205. }
  206. b->type = TWID8;
  207. b->next = lumpCache.free;
  208. lumpCache.free = b;
  209. return b;
  210. }
  211. /*
  212. * delete an arbitrary block from the heap
  213. */
  214. static void
  215. delHeap(Lump *db)
  216. {
  217. fixHeap(db->heap, lumpCache.heap[--lumpCache.nheap]);
  218. db->heap = TWID32;
  219. }
  220. /*
  221. * push an element up or down to it's correct new location
  222. */
  223. static void
  224. fixHeap(int i, Lump *b)
  225. {
  226. if(upHeap(i, b) == i)
  227. downHeap(i, b);
  228. }
  229. static int
  230. upHeap(int i, Lump *b)
  231. {
  232. Lump *bb;
  233. u32int now;
  234. int p;
  235. now = lumpCache.now;
  236. for(; i != 0; i = p){
  237. p = (i - 1) >> 1;
  238. bb = lumpCache.heap[p];
  239. if(b->used2 - now >= bb->used2 - now)
  240. break;
  241. lumpCache.heap[i] = bb;
  242. bb->heap = i;
  243. }
  244. lumpCache.heap[i] = b;
  245. b->heap = i;
  246. return i;
  247. }
  248. static int
  249. downHeap(int i, Lump *b)
  250. {
  251. Lump *bb;
  252. u32int now;
  253. int k;
  254. now = lumpCache.now;
  255. for(; ; i = k){
  256. k = (i << 1) + 1;
  257. if(k >= lumpCache.nheap)
  258. break;
  259. if(k + 1 < lumpCache.nheap && lumpCache.heap[k]->used2 - now > lumpCache.heap[k + 1]->used2 - now)
  260. k++;
  261. bb = lumpCache.heap[k];
  262. if(b->used2 - now <= bb->used2 - now)
  263. break;
  264. lumpCache.heap[i] = bb;
  265. bb->heap = i;
  266. }
  267. lumpCache.heap[i] = b;
  268. b->heap = i;
  269. return i;
  270. }
  271. static void
  272. findBlock(Lump *bb)
  273. {
  274. Lump *b, *last;
  275. int h;
  276. last = nil;
  277. h = hashBits(bb->score, HashLog);
  278. for(b = lumpCache.heads[h]; b != nil; b = b->next){
  279. if(last != b->prev)
  280. fatal("bad prev link");
  281. if(b == bb)
  282. return;
  283. last = b;
  284. }
  285. fatal("block score=%V type=%#x missing from hash table", bb->score, bb->type);
  286. }
  287. void
  288. checkLumpCache(void)
  289. {
  290. Lump *b;
  291. u32int size, now, nfree;
  292. int i, k, refed;
  293. vtLock(lumpCache.lock);
  294. now = lumpCache.now;
  295. for(i = 0; i < lumpCache.nheap; i++){
  296. if(lumpCache.heap[i]->heap != i)
  297. fatal("lc: mis-heaped at %d: %d", i, lumpCache.heap[i]->heap);
  298. if(i > 0 && lumpCache.heap[(i - 1) >> 1]->used2 - now > lumpCache.heap[i]->used2 - now)
  299. fatal("lc: bad heap ordering");
  300. k = (i << 1) + 1;
  301. if(k < lumpCache.nheap && lumpCache.heap[i]->used2 - now > lumpCache.heap[k]->used2 - now)
  302. fatal("lc: bad heap ordering");
  303. k++;
  304. if(k < lumpCache.nheap && lumpCache.heap[i]->used2 - now > lumpCache.heap[k]->used2 - now)
  305. fatal("lc: bad heap ordering");
  306. }
  307. refed = 0;
  308. size = 0;
  309. for(i = 0; i < lumpCache.nblocks; i++){
  310. b = &lumpCache.blocks[i];
  311. if(b->data == nil && b->size != 0)
  312. fatal("bad size: %d data=%p", b->size, b->data);
  313. if(b->ref && b->heap == TWID32)
  314. refed++;
  315. if(b->type != TWID8){
  316. findBlock(b);
  317. size += b->size;
  318. }
  319. if(b->heap != TWID32
  320. && lumpCache.heap[b->heap] != b)
  321. fatal("lc: spurious heap value");
  322. }
  323. if(lumpCache.avail != lumpCache.allowed - size)
  324. fatal("mismatched available=%d and allowed=%d - used=%d space", lumpCache.avail, lumpCache.allowed, size);
  325. nfree = 0;
  326. for(b = lumpCache.free; b != nil; b = b->next){
  327. if(b->type != TWID8 || b->heap != TWID32)
  328. fatal("lc: bad free list");
  329. nfree++;
  330. }
  331. if(lumpCache.nheap + nfree + refed != lumpCache.nblocks)
  332. fatal("lc: missing blocks: %d %d %d %d", lumpCache.nheap, refed, nfree, lumpCache.nblocks);
  333. vtUnlock(lumpCache.lock);
  334. }