pool.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* $OpenBSD: pool.h,v 1.57 2015/02/10 06:16:13 dlg Exp $ */
  2. /* $NetBSD: pool.h,v 1.27 2001/06/06 22:00:17 rafal Exp $ */
  3. /*-
  4. * Copyright (c) 1997, 1998, 1999, 2000 The NetBSD Foundation, Inc.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to The NetBSD Foundation
  8. * by Paul Kranenburg; by Jason R. Thorpe of the Numerical Aerospace
  9. * Simulation Facility, NASA Ames Research Center.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  24. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #ifndef _SYS_POOL_H_
  33. #define _SYS_POOL_H_
  34. /*
  35. * sysctls.
  36. * kern.pool.npools
  37. * kern.pool.name.<number>
  38. * kern.pool.pool.<number>
  39. */
  40. #define KERN_POOL_NPOOLS 1
  41. #define KERN_POOL_NAME 2
  42. #define KERN_POOL_POOL 3
  43. struct kinfo_pool {
  44. unsigned int pr_size; /* size of a pool item */
  45. unsigned int pr_pgsize; /* size of a "page" */
  46. unsigned int pr_itemsperpage; /* number of items per "page" */
  47. unsigned int pr_minpages; /* same in page units */
  48. unsigned int pr_maxpages; /* maximum # of idle pages to keep */
  49. unsigned int pr_hardlimit; /* hard limit to number of allocated
  50. items */
  51. unsigned int pr_npages; /* # of pages allocated */
  52. unsigned int pr_nout; /* # items currently allocated */
  53. unsigned int pr_nitems; /* # items in the pool */
  54. unsigned long pr_nget; /* # of successful requests */
  55. unsigned long pr_nput; /* # of releases */
  56. unsigned long pr_nfail; /* # of unsuccessful requests */
  57. unsigned long pr_npagealloc; /* # of pages allocated */
  58. unsigned long pr_npagefree; /* # of pages released */
  59. unsigned int pr_hiwat; /* max # of pages in pool */
  60. unsigned long pr_nidle; /* # of idle pages */
  61. };
  62. #if defined(_KERNEL) || defined(_LIBKVM)
  63. #include <sys/queue.h>
  64. #include <sys/tree.h>
  65. #include <sys/mutex.h>
  66. struct pool;
  67. struct pool_request;
  68. TAILQ_HEAD(pool_requests, pool_request);
  69. struct pool_allocator {
  70. void *(*pa_alloc)(struct pool *, int, int *);
  71. void (*pa_free)(struct pool *, void *);
  72. int pa_pagesz;
  73. };
  74. TAILQ_HEAD(pool_pagelist, pool_item_header);
  75. struct pool {
  76. struct mutex pr_mtx;
  77. SIMPLEQ_ENTRY(pool)
  78. pr_poollist;
  79. struct pool_pagelist
  80. pr_emptypages; /* Empty pages */
  81. struct pool_pagelist
  82. pr_fullpages; /* Full pages */
  83. struct pool_pagelist
  84. pr_partpages; /* Partially-allocated pages */
  85. struct pool_item_header *
  86. pr_curpage;
  87. unsigned int pr_size; /* Size of item */
  88. unsigned int pr_minitems; /* minimum # of items to keep */
  89. unsigned int pr_minpages; /* same in page units */
  90. unsigned int pr_maxpages; /* maximum # of idle pages to keep */
  91. unsigned int pr_npages; /* # of pages allocated */
  92. unsigned int pr_itemsperpage;/* # items that fit in a page */
  93. unsigned int pr_slack; /* unused space in a page */
  94. unsigned int pr_nitems; /* number of available items in pool */
  95. unsigned int pr_nout; /* # items currently allocated */
  96. unsigned int pr_hardlimit; /* hard limit to number of allocated
  97. items */
  98. unsigned int pr_serial; /* unique serial number of the pool */
  99. unsigned int pr_pgsize; /* Size of a "page" */
  100. vaddr_t pr_pgmask; /* Mask with an item to get a page */
  101. struct pool_allocator *
  102. pr_alloc; /* backend allocator */
  103. const char * pr_wchan; /* tsleep(9) identifier */
  104. #define PR_WAITOK 0x0001 /* M_WAITOK */
  105. #define PR_NOWAIT 0x0002 /* M_NOWAIT */
  106. #define PR_LIMITFAIL 0x0004 /* M_CANFAIL */
  107. #define PR_ZERO 0x0008 /* M_ZERO */
  108. #define PR_WANTED 0x0100
  109. #define PR_DEBUGCHK 0x1000
  110. int pr_ipl;
  111. RB_HEAD(phtree, pool_item_header)
  112. pr_phtree;
  113. u_int pr_align;
  114. u_int pr_maxcolors; /* Cache coloring */
  115. int pr_phoffset; /* Offset in page of page header */
  116. /*
  117. * Warning message to be issued, and a per-time-delta rate cap,
  118. * if the hard limit is reached.
  119. */
  120. const char *pr_hardlimit_warning;
  121. struct timeval pr_hardlimit_ratecap;
  122. struct timeval pr_hardlimit_warning_last;
  123. /*
  124. * pool item requests queue
  125. */
  126. struct mutex pr_requests_mtx;
  127. struct pool_requests
  128. pr_requests;
  129. unsigned int pr_requesting;
  130. /*
  131. * Instrumentation
  132. */
  133. unsigned long pr_nget; /* # of successful requests */
  134. unsigned long pr_nfail; /* # of unsuccessful requests */
  135. unsigned long pr_nput; /* # of releases */
  136. unsigned long pr_npagealloc; /* # of pages allocated */
  137. unsigned long pr_npagefree; /* # of pages released */
  138. unsigned int pr_hiwat; /* max # of pages in pool */
  139. unsigned long pr_nidle; /* # of idle pages */
  140. /* Physical memory configuration. */
  141. const struct kmem_pa_mode *
  142. pr_crange;
  143. };
  144. #endif /* _KERNEL || _LIBKVM */
  145. #ifdef _KERNEL
  146. extern struct pool_allocator pool_allocator_nointr;
  147. struct pool_request {
  148. TAILQ_ENTRY(pool_request) pr_entry;
  149. void (*pr_handler)(void *, void *);
  150. void *pr_cookie;
  151. void *pr_item;
  152. };
  153. void pool_init(struct pool *, size_t, u_int, u_int, int,
  154. const char *, struct pool_allocator *);
  155. void pool_destroy(struct pool *);
  156. void pool_setipl(struct pool *, int);
  157. void pool_setlowat(struct pool *, int);
  158. void pool_sethiwat(struct pool *, int);
  159. int pool_sethardlimit(struct pool *, u_int, const char *, int);
  160. struct uvm_constraint_range; /* XXX */
  161. void pool_set_constraints(struct pool *,
  162. const struct kmem_pa_mode *mode);
  163. void *pool_get(struct pool *, int) __malloc;
  164. void pool_request_init(struct pool_request *,
  165. void (*)(void *, void *), void *);
  166. void pool_request(struct pool *, struct pool_request *);
  167. void pool_put(struct pool *, void *);
  168. int pool_reclaim(struct pool *);
  169. void pool_reclaim_all(void);
  170. int pool_prime(struct pool *, int);
  171. #ifdef DDB
  172. /*
  173. * Debugging and diagnostic aides.
  174. */
  175. void pool_printit(struct pool *, const char *,
  176. int (*)(const char *, ...));
  177. void pool_walk(struct pool *, int, int (*)(const char *, ...),
  178. void (*)(void *, int, int (*)(const char *, ...)));
  179. #endif
  180. /* the allocator for dma-able memory is a thin layer on top of pool */
  181. void dma_alloc_init(void);
  182. void *dma_alloc(size_t size, int flags);
  183. void dma_free(void *m, size_t size);
  184. #endif /* _KERNEL */
  185. #endif /* _SYS_POOL_H_ */