mpool.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*-
  2. * Copyright (c) 1990, 1993, 1994
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the University of
  16. * California, Berkeley and its contributors.
  17. * 4. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #if defined(LIBC_SCCS) && !defined(lint)
  34. static char sccsid[] = "@(#)mpool.c 8.5 (Berkeley) 7/26/94";
  35. #endif /* LIBC_SCCS and not lint */
  36. #include <sys/param.h>
  37. #include <sys/queue.h>
  38. #include <sys/stat.h>
  39. #include <errno.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <unistd.h>
  44. #include "../include/db.h"
  45. #define __MPOOLINTERFACE_PRIVATE
  46. #include <mpool.h>
  47. #undef __APPLE__
  48. #ifndef __APPLE__
  49. #define mpool_open __mpool_open
  50. #define mpool_filter __mpool_filter
  51. #define mpool_new __mpool_new
  52. #define mpool_get __mpool_get
  53. #define mpool_put __mpool_put
  54. #define mpool_sync __mpool_sync
  55. #define mpool_close __mpool_close
  56. #endif
  57. static BKT *mpool_bkt __P((MPOOL *));
  58. static BKT *mpool_look __P((MPOOL *, pgno_t));
  59. static int mpool_write __P((MPOOL *, BKT *));
  60. /*
  61. * mpool_open --
  62. * Initialize a memory pool.
  63. */
  64. MPOOL *
  65. mpool_open(key, fd, pagesize, maxcache)
  66. void *key;
  67. int fd;
  68. pgno_t pagesize, maxcache;
  69. {
  70. struct stat sb;
  71. MPOOL *mp;
  72. int entry;
  73. /*
  74. * Get information about the file.
  75. *
  76. * XXX
  77. * We don't currently handle pipes, although we should.
  78. */
  79. if (fstat(fd, &sb))
  80. return (NULL);
  81. if (!S_ISREG(sb.st_mode)) {
  82. errno = ESPIPE;
  83. return (NULL);
  84. }
  85. /* Allocate and initialize the MPOOL cookie. */
  86. if ((mp = (MPOOL *)calloc(1, sizeof(MPOOL))) == NULL)
  87. return (NULL);
  88. CIRCLEQ_INIT(&mp->lqh);
  89. for (entry = 0; entry < HASHSIZE; ++entry)
  90. CIRCLEQ_INIT(&mp->hqh[entry]);
  91. mp->maxcache = maxcache;
  92. mp->npages = sb.st_size / pagesize;
  93. mp->pagesize = pagesize;
  94. mp->fd = fd;
  95. return (mp);
  96. }
  97. /*
  98. * mpool_filter --
  99. * Initialize input/output filters.
  100. */
  101. void
  102. mpool_filter(mp, pgin, pgout, pgcookie)
  103. MPOOL *mp;
  104. void (*pgin) __P((void *, pgno_t, void *));
  105. void (*pgout) __P((void *, pgno_t, void *));
  106. void *pgcookie;
  107. {
  108. mp->pgin = pgin;
  109. mp->pgout = pgout;
  110. mp->pgcookie = pgcookie;
  111. }
  112. /*
  113. * mpool_new --
  114. * Get a new page of memory.
  115. */
  116. void *
  117. mpool_new(mp, pgnoaddr)
  118. MPOOL *mp;
  119. pgno_t *pgnoaddr;
  120. {
  121. struct _hqh *head;
  122. BKT *bp;
  123. if (mp->npages == MAX_PAGE_NUMBER) {
  124. (void)fprintf(stderr, "mpool_new: page allocation overflow.\n");
  125. abort();
  126. }
  127. #ifdef STATISTICS
  128. ++mp->pagenew;
  129. #endif
  130. /*
  131. * Get a BKT from the cache. Assign a new page number, attach
  132. * it to the head of the hash chain, the tail of the lru chain,
  133. * and return.
  134. */
  135. if ((bp = mpool_bkt(mp)) == NULL)
  136. return (NULL);
  137. *pgnoaddr = bp->pgno = mp->npages++;
  138. bp->flags = MPOOL_PINNED;
  139. head = &mp->hqh[HASHKEY(bp->pgno)];
  140. CIRCLEQ_INSERT_HEAD(head, bp, hq);
  141. CIRCLEQ_INSERT_TAIL(&mp->lqh, bp, q);
  142. return (bp->page);
  143. }
  144. /*
  145. * mpool_get
  146. * Get a page.
  147. */
  148. void *
  149. mpool_get(mp, pgno, flags)
  150. MPOOL *mp;
  151. pgno_t pgno;
  152. u_int flags; /* XXX not used? */
  153. {
  154. struct _hqh *head;
  155. BKT *bp;
  156. off_t off;
  157. int nr;
  158. /* Check for attempt to retrieve a non-existent page. */
  159. if (pgno >= mp->npages) {
  160. errno = EINVAL;
  161. return (NULL);
  162. }
  163. #ifdef STATISTICS
  164. ++mp->pageget;
  165. #endif
  166. /* Check for a page that is cached. */
  167. if ((bp = mpool_look(mp, pgno)) != NULL) {
  168. #ifdef DEBUG
  169. if (bp->flags & MPOOL_PINNED) {
  170. (void)fprintf(stderr,
  171. "mpool_get: page %d already pinned\n", bp->pgno);
  172. abort();
  173. }
  174. #endif
  175. /*
  176. * Move the page to the head of the hash chain and the tail
  177. * of the lru chain.
  178. */
  179. head = &mp->hqh[HASHKEY(bp->pgno)];
  180. CIRCLEQ_REMOVE(head, bp, hq);
  181. CIRCLEQ_INSERT_HEAD(head, bp, hq);
  182. CIRCLEQ_REMOVE(&mp->lqh, bp, q);
  183. CIRCLEQ_INSERT_TAIL(&mp->lqh, bp, q);
  184. /* Return a pinned page. */
  185. bp->flags |= MPOOL_PINNED;
  186. return (bp->page);
  187. }
  188. /* Get a page from the cache. */
  189. if ((bp = mpool_bkt(mp)) == NULL)
  190. return (NULL);
  191. /* Read in the contents. */
  192. #ifdef STATISTICS
  193. ++mp->pageread;
  194. #endif
  195. off = mp->pagesize * pgno;
  196. if (lseek(mp->fd, off, SEEK_SET) != off)
  197. return (NULL);
  198. if ((u_long) (nr = read(mp->fd, bp->page, mp->pagesize))
  199. != mp->pagesize) {
  200. if (nr >= 0)
  201. errno = EFTYPE;
  202. return (NULL);
  203. }
  204. /* Set the page number, pin the page. */
  205. bp->pgno = pgno;
  206. bp->flags = MPOOL_PINNED;
  207. /*
  208. * Add the page to the head of the hash chain and the tail
  209. * of the lru chain.
  210. */
  211. head = &mp->hqh[HASHKEY(bp->pgno)];
  212. CIRCLEQ_INSERT_HEAD(head, bp, hq);
  213. CIRCLEQ_INSERT_TAIL(&mp->lqh, bp, q);
  214. /* Run through the user's filter. */
  215. if (mp->pgin != NULL)
  216. (mp->pgin)(mp->pgcookie, bp->pgno, bp->page);
  217. return (bp->page);
  218. }
  219. /*
  220. * mpool_put
  221. * Return a page.
  222. */
  223. int
  224. mpool_put(mp, page, flags)
  225. MPOOL *mp;
  226. void *page;
  227. u_int flags;
  228. {
  229. BKT *bp;
  230. #ifdef STATISTICS
  231. ++mp->pageput;
  232. #endif
  233. bp = (BKT *)((char *)page - sizeof(BKT));
  234. #ifdef DEBUG
  235. if (!(bp->flags & MPOOL_PINNED)) {
  236. (void)fprintf(stderr,
  237. "mpool_put: page %d not pinned\n", bp->pgno);
  238. abort();
  239. }
  240. #endif
  241. bp->flags &= ~MPOOL_PINNED;
  242. bp->flags |= flags & MPOOL_DIRTY;
  243. return (RET_SUCCESS);
  244. }
  245. /*
  246. * mpool_close
  247. * Close the buffer pool.
  248. */
  249. int
  250. mpool_close(mp)
  251. MPOOL *mp;
  252. {
  253. BKT *bp;
  254. /* Free up any space allocated to the lru pages. */
  255. while ((bp = mp->lqh.cqh_first) != (void *)&mp->lqh) {
  256. CIRCLEQ_REMOVE(&mp->lqh, mp->lqh.cqh_first, q);
  257. free(bp);
  258. }
  259. /* Free the MPOOL cookie. */
  260. free(mp);
  261. return (RET_SUCCESS);
  262. }
  263. /*
  264. * mpool_sync
  265. * Sync the pool to disk.
  266. */
  267. int
  268. mpool_sync(mp)
  269. MPOOL *mp;
  270. {
  271. BKT *bp;
  272. /* Walk the lru chain, flushing any dirty pages to disk. */
  273. for (bp = mp->lqh.cqh_first;
  274. bp != (void *)&mp->lqh; bp = bp->q.cqe_next)
  275. if (bp->flags & MPOOL_DIRTY &&
  276. mpool_write(mp, bp) == RET_ERROR)
  277. return (RET_ERROR);
  278. /* Sync the file descriptor. */
  279. return (fsync(mp->fd) ? RET_ERROR : RET_SUCCESS);
  280. }
  281. #define __APPLE__
  282. #ifndef __APPLE__
  283. #undef mpool_open
  284. #undef mpool_filter
  285. #undef mpool_new
  286. #undef mpool_get
  287. #undef mpool_put
  288. #undef mpool_close
  289. #undef mpool_sync
  290. #define weak_alias(original, alias) \
  291. asm (".weak " #alias "\n" #alias " = " #original);
  292. weak_alias (__mpool_open, mpool_open)
  293. weak_alias (__mpool_filter, mpool_filter)
  294. weak_alias (__mpool_new, mpool_new)
  295. weak_alias (__mpool_get, mpool_get)
  296. weak_alias (__mpool_put, mpool_put)
  297. weak_alias (__mpool_close, mpool_close)
  298. weak_alias (__mpool_sync, mpool_sync)
  299. #endif
  300. /*
  301. * mpool_bkt
  302. * Get a page from the cache (or create one).
  303. */
  304. static BKT *
  305. mpool_bkt(mp)
  306. MPOOL *mp;
  307. {
  308. struct _hqh *head;
  309. BKT *bp;
  310. /* If under the max cached, always create a new page. */
  311. if (mp->curcache < mp->maxcache)
  312. goto new;
  313. /*
  314. * If the cache is max'd out, walk the lru list for a buffer we
  315. * can flush. If we find one, write it (if necessary) and take it
  316. * off any lists. If we don't find anything we grow the cache anyway.
  317. * The cache never shrinks.
  318. */
  319. for (bp = mp->lqh.cqh_first;
  320. bp != (void *)&mp->lqh; bp = bp->q.cqe_next)
  321. if (!(bp->flags & MPOOL_PINNED)) {
  322. /* Flush if dirty. */
  323. if (bp->flags & MPOOL_DIRTY &&
  324. mpool_write(mp, bp) == RET_ERROR)
  325. return (NULL);
  326. #ifdef STATISTICS
  327. ++mp->pageflush;
  328. #endif
  329. /* Remove from the hash and lru queues. */
  330. head = &mp->hqh[HASHKEY(bp->pgno)];
  331. CIRCLEQ_REMOVE(head, bp, hq);
  332. CIRCLEQ_REMOVE(&mp->lqh, bp, q);
  333. #ifdef DEBUG
  334. { void *spage;
  335. spage = bp->page;
  336. memset(bp, 0xff, sizeof(BKT) + mp->pagesize);
  337. bp->page = spage;
  338. }
  339. #endif
  340. return (bp);
  341. }
  342. new: if ((bp = (BKT *) calloc(1, sizeof(BKT) + mp->pagesize)) == NULL)
  343. return (NULL);
  344. #ifdef STATISTICS
  345. ++mp->pagealloc;
  346. #endif
  347. #if defined(DEBUG) || defined(PURIFY)
  348. memset(bp, 0xff, sizeof(BKT) + mp->pagesize);
  349. #endif
  350. bp->page = (char *)bp + sizeof(BKT);
  351. ++mp->curcache;
  352. return (bp);
  353. }
  354. /*
  355. * mpool_write
  356. * Write a page to disk.
  357. */
  358. static int
  359. mpool_write(mp, bp)
  360. MPOOL *mp;
  361. BKT *bp;
  362. {
  363. off_t off;
  364. #ifdef STATISTICS
  365. ++mp->pagewrite;
  366. #endif
  367. /* Run through the user's filter. */
  368. if (mp->pgout)
  369. (mp->pgout)(mp->pgcookie, bp->pgno, bp->page);
  370. off = mp->pagesize * bp->pgno;
  371. if (lseek(mp->fd, off, SEEK_SET) != off)
  372. return (RET_ERROR);
  373. if ((u_long) write(mp->fd, bp->page, mp->pagesize) != mp->pagesize)
  374. return (RET_ERROR);
  375. bp->flags &= ~MPOOL_DIRTY;
  376. return (RET_SUCCESS);
  377. }
  378. /*
  379. * mpool_look
  380. * Lookup a page in the cache.
  381. */
  382. static BKT *
  383. mpool_look(mp, pgno)
  384. MPOOL *mp;
  385. pgno_t pgno;
  386. {
  387. struct _hqh *head;
  388. BKT *bp;
  389. head = &mp->hqh[HASHKEY(pgno)];
  390. for (bp = head->cqh_first; bp != (void *)head; bp = bp->hq.cqe_next)
  391. if (bp->pgno == pgno) {
  392. #ifdef STATISTICS
  393. ++mp->cachehit;
  394. #endif
  395. return (bp);
  396. }
  397. #ifdef STATISTICS
  398. ++mp->cachemiss;
  399. #endif
  400. return (NULL);
  401. }
  402. #ifdef STATISTICS
  403. /*
  404. * mpool_stat
  405. * Print out cache statistics.
  406. */
  407. void
  408. mpool_stat(mp)
  409. MPOOL *mp;
  410. {
  411. BKT *bp;
  412. int cnt;
  413. char *sep;
  414. (void)fprintf(stderr, "%lu pages in the file\n", mp->npages);
  415. (void)fprintf(stderr,
  416. "page size %lu, cacheing %lu pages of %lu page max cache\n",
  417. mp->pagesize, mp->curcache, mp->maxcache);
  418. (void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n",
  419. mp->pageput, mp->pageget, mp->pagenew);
  420. (void)fprintf(stderr, "%lu page allocs, %lu page flushes\n",
  421. mp->pagealloc, mp->pageflush);
  422. if (mp->cachehit + mp->cachemiss)
  423. (void)fprintf(stderr,
  424. "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
  425. ((double)mp->cachehit / (mp->cachehit + mp->cachemiss))
  426. * 100, mp->cachehit, mp->cachemiss);
  427. (void)fprintf(stderr, "%lu page reads, %lu page writes\n",
  428. mp->pageread, mp->pagewrite);
  429. sep = "";
  430. cnt = 0;
  431. for (bp = mp->lqh.cqh_first;
  432. bp != (void *)&mp->lqh; bp = bp->q.cqe_next) {
  433. (void)fprintf(stderr, "%s%d", sep, bp->pgno);
  434. if (bp->flags & MPOOL_DIRTY)
  435. (void)fprintf(stderr, "d");
  436. if (bp->flags & MPOOL_PINNED)
  437. (void)fprintf(stderr, "P");
  438. if (++cnt == 10) {
  439. sep = "\n";
  440. cnt = 0;
  441. } else
  442. sep = ", ";
  443. }
  444. (void)fprintf(stderr, "\n");
  445. }
  446. #endif