image.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "../port/error.h"
  15. #define NFREECHAN 64
  16. #define IHASHSIZE 64
  17. #define ihash(s) imagealloc.hash[s%IHASHSIZE]
  18. static struct Imagealloc
  19. {
  20. Lock;
  21. Image *mru; /* head of LRU list */
  22. Image *lru; /* tail of LRU list */
  23. Image *hash[IHASHSIZE];
  24. QLock ireclaim; /* mutex on reclaiming free images */
  25. Chan **freechan; /* free image channels */
  26. int nfreechan; /* number of free channels */
  27. int szfreechan; /* size of freechan array */
  28. QLock fcreclaim; /* mutex on reclaiming free channels */
  29. } imagealloc;
  30. static struct {
  31. int calls; /* times imagereclaim was called */
  32. int loops; /* times the main loop was run */
  33. uint64_t ticks; /* total time in the main loop */
  34. uint64_t maxt; /* longest time in main loop */
  35. int noluck; /* # of times we couldn't get one */
  36. int nolock; /* # of times we couldn't get the lock */
  37. } irstats;
  38. static void
  39. dumplru(void)
  40. {
  41. Image *i;
  42. print("lru:");
  43. for(i = imagealloc.mru; i != nil; i = i->next)
  44. print(" %p(c%p,r%d)", i, i->c, i->ref);
  45. print("\n");
  46. }
  47. /*
  48. * imagealloc and i must be locked.
  49. */
  50. static void
  51. imageunused(Image *i)
  52. {
  53. if(i->prev != nil)
  54. i->prev->next = i->next;
  55. else
  56. imagealloc.mru = i->next;
  57. if(i->next != nil)
  58. i->next->prev = i->prev;
  59. else
  60. imagealloc.lru = i->prev;
  61. i->next = i->prev = nil;
  62. }
  63. /*
  64. * imagealloc and i must be locked.
  65. */
  66. static void
  67. imageused(Image *i)
  68. {
  69. imageunused(i);
  70. i->next = imagealloc.mru;
  71. i->next->prev = i;
  72. imagealloc.mru = i;
  73. if(imagealloc.lru == nil)
  74. imagealloc.lru = i;
  75. }
  76. /*
  77. * imagealloc must be locked.
  78. */
  79. static Image*
  80. lruimage(void)
  81. {
  82. Image *i;
  83. for(i = imagealloc.lru; i != nil; i = i->prev)
  84. if(i->c == nil){
  85. /*
  86. * i->c will be set before releasing the
  87. * lock on imagealloc, which means it's in use.
  88. */
  89. return i;
  90. }
  91. return nil;
  92. }
  93. /*
  94. * On clu, set conf.nimages = 10 to exercise reclaiming.
  95. * It won't be able to get through all of cpurc, but will reclaim.
  96. */
  97. void
  98. initimage(void)
  99. {
  100. Image *i, *ie;
  101. DBG("initimage: %uld images\n", conf.nimage);
  102. imagealloc.mru = malloc(conf.nimage*sizeof(Image));
  103. if(imagealloc.mru == nil)
  104. panic("imagealloc: no memory");
  105. ie = &imagealloc.mru[conf.nimage];
  106. for(i = imagealloc.mru; i < ie; i++){
  107. i->c = nil;
  108. i->ref = 0;
  109. i->prev = i-1;
  110. i->next = i+1;
  111. }
  112. imagealloc.mru[0].prev = nil;
  113. imagealloc.mru[conf.nimage-1].next = nil;
  114. imagealloc.lru = &imagealloc.mru[conf.nimage-1];
  115. imagealloc.freechan = malloc(NFREECHAN * sizeof(Chan*));
  116. imagealloc.szfreechan = NFREECHAN;
  117. }
  118. static void
  119. imagereclaim(void)
  120. {
  121. Image *i;
  122. uint64_t ticks0, ticks;
  123. irstats.calls++;
  124. /* Somebody is already cleaning the page cache */
  125. if(!canqlock(&imagealloc.ireclaim))
  126. return;
  127. DBG("imagereclaim maxt %ulld noluck %d nolock %d\n",
  128. irstats.maxt, irstats.noluck, irstats.nolock);
  129. ticks0 = fastticks(nil);
  130. if(!canlock(&imagealloc)){
  131. /* never happen in the experiments I made */
  132. qunlock(&imagealloc.ireclaim);
  133. return;
  134. }
  135. for(i = imagealloc.lru; i != nil; i = i->prev){
  136. if(canlock(i)){
  137. i->ref++; /* make sure it does not go away */
  138. unlock(i);
  139. pagereclaim(i);
  140. lock(i);
  141. DBG("imagereclaim: image %p(c%p, r%d)\n", i, i->c, i->ref);
  142. if(i->ref == 1){ /* no pages referring to it, it's ours */
  143. unlock(i);
  144. unlock(&imagealloc);
  145. putimage(i);
  146. break;
  147. }else
  148. --i->ref;
  149. unlock(i);
  150. }
  151. }
  152. if(i == nil){
  153. irstats.noluck++;
  154. unlock(&imagealloc);
  155. }
  156. irstats.loops++;
  157. ticks = fastticks(nil) - ticks0;
  158. irstats.ticks += ticks;
  159. if(ticks > irstats.maxt)
  160. irstats.maxt = ticks;
  161. //print("T%llud+", ticks);
  162. qunlock(&imagealloc.ireclaim);
  163. }
  164. /*
  165. * since close can block, this has to be called outside of
  166. * spin locks.
  167. */
  168. static void
  169. imagechanreclaim(void)
  170. {
  171. Chan *c;
  172. /* Somebody is already cleaning the image chans */
  173. if(!canqlock(&imagealloc.fcreclaim))
  174. return;
  175. /*
  176. * We don't have to recheck that nfreechan > 0 after we
  177. * acquire the lock, because we're the only ones who decrement
  178. * it (the other lock contender increments it), and there's only
  179. * one of us thanks to the qlock above.
  180. */
  181. while(imagealloc.nfreechan > 0){
  182. lock(&imagealloc);
  183. imagealloc.nfreechan--;
  184. c = imagealloc.freechan[imagealloc.nfreechan];
  185. unlock(&imagealloc);
  186. cclose(c);
  187. }
  188. qunlock(&imagealloc.fcreclaim);
  189. }
  190. Image*
  191. attachimage(int type, Chan *c, int color, uintptr base, usize len)
  192. {
  193. Image *i, **l;
  194. /* reclaim any free channels from reclaimed segments */
  195. if(imagealloc.nfreechan)
  196. imagechanreclaim();
  197. lock(&imagealloc);
  198. /*
  199. * Search the image cache for remains of the text from a previous
  200. * or currently running incarnation
  201. */
  202. for(i = ihash(c->qid.path); i; i = i->hash) {
  203. if(c->qid.path == i->qid.path) {
  204. lock(i);
  205. if(eqqid(c->qid, i->qid) &&
  206. eqqid(c->mqid, i->mqid) &&
  207. c->mchan == i->mchan &&
  208. c->dev->dc == i->dc) {
  209. //subtype
  210. goto found;
  211. }
  212. unlock(i);
  213. }
  214. }
  215. /*
  216. * imagereclaim dumps pages from the free list which are cached by image
  217. * structures. This should free some image structures.
  218. */
  219. while(!(i = lruimage())) {
  220. unlock(&imagealloc);
  221. imagereclaim();
  222. sched();
  223. lock(&imagealloc);
  224. }
  225. lock(i);
  226. incref(c);
  227. i->c = c;
  228. i->dc = c->dev->dc;
  229. //subtype
  230. i->qid = c->qid;
  231. i->mqid = c->mqid;
  232. i->mchan = c->mchan;
  233. i->color = color;
  234. l = &ihash(c->qid.path);
  235. i->hash = *l;
  236. *l = i;
  237. found:
  238. imageused(i);
  239. unlock(&imagealloc);
  240. if(i->s == 0) {
  241. /* Disaster after commit in exec */
  242. if(waserror()) {
  243. unlock(i);
  244. pexit(Enovmem, 1);
  245. }
  246. i->s = newseg(type, base, len);
  247. i->s->image = i;
  248. i->s->color = color;
  249. i->ref++;
  250. poperror();
  251. }
  252. else
  253. incref(i->s);
  254. return i;
  255. }
  256. void
  257. putimage(Image *i)
  258. {
  259. Chan *c, **cp;
  260. Image *f, **l;
  261. if(i->notext)
  262. return;
  263. lock(i);
  264. if(--i->ref == 0) {
  265. l = &ihash(i->qid.path);
  266. mkqid(&i->qid, ~0, ~0, QTFILE);
  267. unlock(i);
  268. c = i->c;
  269. lock(&imagealloc);
  270. for(f = *l; f; f = f->hash) {
  271. if(f == i) {
  272. *l = i->hash;
  273. break;
  274. }
  275. l = &f->hash;
  276. }
  277. /* defer freeing channel till we're out of spin lock's */
  278. if(imagealloc.nfreechan == imagealloc.szfreechan){
  279. imagealloc.szfreechan += NFREECHAN;
  280. cp = malloc(imagealloc.szfreechan*sizeof(Chan*));
  281. if(cp == nil)
  282. panic("putimage");
  283. memmove(cp, imagealloc.freechan, imagealloc.nfreechan*sizeof(Chan*));
  284. free(imagealloc.freechan);
  285. imagealloc.freechan = cp;
  286. }
  287. imagealloc.freechan[imagealloc.nfreechan++] = c;
  288. i->c = nil; /* flag as unused in lru list */
  289. unlock(&imagealloc);
  290. return;
  291. }
  292. unlock(i);
  293. }