image.c 7.0 KB

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