mem0pool.c
Upload User: tsgydb
Upload Date: 2007-04-14
Package Size: 10674k
Code Size: 14k
Category:

MySQL

Development Platform:

Visual C++

  1. /************************************************************************
  2. The lowest-level memory management
  3. (c) 1997 Innobase Oy
  4. Created 5/12/1997 Heikki Tuuri
  5. *************************************************************************/
  6. #include "mem0pool.h"
  7. #ifdef UNIV_NONINL
  8. #include "mem0pool.ic"
  9. #endif
  10. #include "sync0sync.h"
  11. #include "ut0mem.h"
  12. #include "ut0lst.h"
  13. #include "ut0byte.h"
  14. /* We would like to use also the buffer frames to allocate memory. This
  15. would be desirable, because then the memory consumption of the database
  16. would be fixed, and we might even lock the buffer pool to the main memory.
  17. The problem here is that the buffer management routines can themselves call
  18. memory allocation, while the buffer pool mutex is reserved.
  19. The main components of the memory consumption are:
  20. 1. buffer pool,
  21. 2. parsed and optimized SQL statements,
  22. 3. data dictionary cache,
  23. 4. log buffer,
  24. 5. locks for each transaction,
  25. 6. hash table for the adaptive index,
  26. 7. state and buffers for each SQL query currently being executed,
  27. 8. session for each user, and
  28. 9. stack for each OS thread.
  29. Items 1-3 are managed by an LRU algorithm. Items 5 and 6 can potentially
  30. consume very much memory. Items 7 and 8 should consume quite little memory,
  31. and the OS should take care of item 9, which too should consume little memory.
  32. A solution to the memory management:
  33. 1. the buffer pool size is set separately;
  34. 2. log buffer size is set separately;
  35. 3. the common pool size for all the other entries, except 8, is set separately.
  36. Problems: we may waste memory if the common pool is set too big. Another
  37. problem is the locks, which may take very much space in big transactions.
  38. Then the shared pool size should be set very big. We can allow locks to take
  39. space from the buffer pool, but the SQL optimizer is then unaware of the
  40. usable size of the buffer pool. We could also combine the objects in the
  41. common pool and the buffers in the buffer pool into a single LRU list and
  42. manage it uniformly, but this approach does not take into account the parsing
  43. and other costs unique to SQL statements.
  44. So, let the SQL statements and the data dictionary entries form one single
  45. LRU list, let us call it the dictionary LRU list. The locks for a transaction
  46. can be seen as a part of the state of the transaction. Hence, they should be
  47. stored in the common pool. We still have the problem of a very big update
  48. transaction, for example, which will set very many x-locks on rows, and the
  49. locks will consume a lot of memory, say, half of the buffer pool size.
  50. Another problem is what to do if we are not able to malloc a requested
  51. block of memory from the common pool. Then we can truncate the LRU list of
  52. the dictionary cache. If it does not help, a system error results.
  53. Because 5 and 6 may potentially consume very much memory, we let them grow
  54. into the buffer pool. We may let the locks of a transaction take frames
  55. from the buffer pool, when the corresponding memory heap block has grown to
  56. the size of a buffer frame. Similarly for the hash node cells of the locks,
  57. and for the adaptive index. Thus, for each individual transaction, its locks
  58. can occupy at most about the size of the buffer frame of memory in the common
  59. pool, and after that its locks will grow into the buffer pool. */
  60. /* Mask used to extract the free bit from area->size */
  61. #define MEM_AREA_FREE 1
  62. /* The smallest memory area total size */
  63. #define MEM_AREA_MIN_SIZE (2 * sizeof(struct mem_area_struct))
  64. /* Data structure for a memory pool. The space is allocated using the buddy
  65. algorithm, where free list i contains areas of size 2 to power i. */
  66. struct mem_pool_struct{
  67. byte* buf; /* memory pool */
  68. ulint size; /* memory common pool size */
  69. ulint reserved; /* amount of currently allocated
  70. memory */
  71. mutex_t mutex; /* mutex protecting this struct */
  72. UT_LIST_BASE_NODE_T(mem_area_t)
  73. free_list[64]; /* lists of free memory areas: an
  74. area is put to the list whose number
  75. is the 2-logarithm of the area size */
  76. };
  77. /* The common memory pool */
  78. mem_pool_t* mem_comm_pool = NULL;
  79. ulint mem_out_of_mem_err_msg_count = 0;
  80. /************************************************************************
  81. Returns memory area size. */
  82. UNIV_INLINE
  83. ulint
  84. mem_area_get_size(
  85. /*==============*/
  86. /* out: size */
  87. mem_area_t* area) /* in: area */
  88. {
  89. return(area->size_and_free & ~MEM_AREA_FREE);
  90. }
  91. /************************************************************************
  92. Sets memory area size. */
  93. UNIV_INLINE
  94. void
  95. mem_area_set_size(
  96. /*==============*/
  97. mem_area_t* area, /* in: area */
  98. ulint size) /* in: size */
  99. {
  100. area->size_and_free = (area->size_and_free & MEM_AREA_FREE)
  101. | size;
  102. }
  103. /************************************************************************
  104. Returns memory area free bit. */
  105. UNIV_INLINE
  106. ibool
  107. mem_area_get_free(
  108. /*==============*/
  109. /* out: TRUE if free */
  110. mem_area_t* area) /* in: area */
  111. {
  112. ut_ad(TRUE == MEM_AREA_FREE);
  113. return(area->size_and_free & MEM_AREA_FREE);
  114. }
  115. /************************************************************************
  116. Sets memory area free bit. */
  117. UNIV_INLINE
  118. void
  119. mem_area_set_free(
  120. /*==============*/
  121. mem_area_t* area, /* in: area */
  122. ibool free) /* in: free bit value */
  123. {
  124. ut_ad(TRUE == MEM_AREA_FREE);
  125. area->size_and_free = (area->size_and_free & ~MEM_AREA_FREE)
  126. | free;
  127. }
  128. /************************************************************************
  129. Creates a memory pool. */
  130. mem_pool_t*
  131. mem_pool_create(
  132. /*============*/
  133. /* out: memory pool */
  134. ulint size) /* in: pool size in bytes */
  135. {
  136. mem_pool_t* pool;
  137. mem_area_t* area;
  138. ulint i;
  139. ulint used;
  140. ut_a(size > 10000);
  141. pool = ut_malloc(sizeof(mem_pool_t));
  142. pool->buf = ut_malloc(size);
  143. pool->size = size;
  144. mutex_create(&(pool->mutex));
  145. mutex_set_level(&(pool->mutex), SYNC_MEM_POOL);
  146. /* Initialize the free lists */
  147. for (i = 0; i < 64; i++) {
  148. UT_LIST_INIT(pool->free_list[i]);
  149. }
  150. used = 0;
  151. while (size - used >= MEM_AREA_MIN_SIZE) {
  152. i = ut_2_log(size - used);
  153. if (ut_2_exp(i) > size - used) {
  154. /* ut_2_log rounds upward */
  155. i--;
  156. }
  157. area = (mem_area_t*)(pool->buf + used);
  158. mem_area_set_size(area, ut_2_exp(i));
  159. mem_area_set_free(area, TRUE);
  160. UT_LIST_ADD_FIRST(free_list, pool->free_list[i], area);
  161. used = used + ut_2_exp(i);
  162. }
  163. ut_ad(size >= used);
  164. pool->reserved = 0;
  165. return(pool);
  166. }
  167. /************************************************************************
  168. Fills the specified free list. */
  169. static
  170. ibool
  171. mem_pool_fill_free_list(
  172. /*====================*/
  173. /* out: TRUE if we were able to insert a
  174. block to the free list */
  175. ulint i, /* in: free list index */
  176. mem_pool_t* pool) /* in: memory pool */
  177. {
  178. mem_area_t* area;
  179. mem_area_t* area2;
  180. ibool ret;
  181. ut_ad(mutex_own(&(pool->mutex)));
  182. if (i >= 63) {
  183. /* We come here when we have run out of space in the
  184. memory pool: */
  185. if (mem_out_of_mem_err_msg_count % 1000000000 == 0) {
  186. /* We do not print the message every time: */
  187. fprintf(stderr,
  188. "Innobase: Warning: out of memory in additional memory pool.n");
  189. fprintf(stderr,
  190. "Innobase: Innobase will start allocating memory from the OS.n");
  191. fprintf(stderr,
  192. "Innobase: You should restart the database with a bigger value inn");
  193. fprintf(stderr,
  194.      "Innobase: the MySQL .cnf file for innobase_additional_mem_pool_size.n");
  195.       }
  196. mem_out_of_mem_err_msg_count++;
  197.      
  198. return(FALSE);
  199. }
  200. area = UT_LIST_GET_FIRST(pool->free_list[i + 1]);
  201. if (area == NULL) {
  202. ret = mem_pool_fill_free_list(i + 1, pool);
  203. if (ret == FALSE) {
  204. return(FALSE);
  205. }
  206. area = UT_LIST_GET_FIRST(pool->free_list[i + 1]);
  207. }
  208. UT_LIST_REMOVE(free_list, pool->free_list[i + 1], area);
  209. area2 = (mem_area_t*)(((byte*)area) + ut_2_exp(i));
  210. mem_area_set_size(area2, ut_2_exp(i));
  211. mem_area_set_free(area2, TRUE);
  212. UT_LIST_ADD_FIRST(free_list, pool->free_list[i], area2);
  213. mem_area_set_size(area, ut_2_exp(i));
  214. UT_LIST_ADD_FIRST(free_list, pool->free_list[i], area);
  215. return(TRUE);
  216. }
  217. /************************************************************************
  218. Allocates memory from a pool. NOTE: This low-level function should only be
  219. used in mem0mem.*! */
  220. void*
  221. mem_area_alloc(
  222. /*===========*/
  223. /* out, own: allocated memory buffer */
  224. ulint size, /* in: allocated size in bytes; for optimum
  225. space usage, the size should be a power of 2
  226. minus MEM_AREA_EXTRA_SIZE */
  227. mem_pool_t* pool) /* in: memory pool */
  228. {
  229. mem_area_t* area;
  230. ulint n;
  231. ibool ret;
  232. n = ut_2_log(ut_max(size + MEM_AREA_EXTRA_SIZE, MEM_AREA_MIN_SIZE));
  233. mutex_enter(&(pool->mutex));
  234. area = UT_LIST_GET_FIRST(pool->free_list[n]);
  235. if (area == NULL) {
  236. ret = mem_pool_fill_free_list(n, pool);
  237. if (ret == FALSE) {
  238. /* Out of memory in memory pool: we try to allocate
  239. from the operating system with the regular malloc: */
  240. mutex_exit(&(pool->mutex));
  241. return(ut_malloc(size));
  242. }
  243. area = UT_LIST_GET_FIRST(pool->free_list[n]);
  244. }
  245. ut_a(mem_area_get_free(area));
  246. ut_ad(mem_area_get_size(area) == ut_2_exp(n));
  247. mem_area_set_free(area, FALSE);
  248. UT_LIST_REMOVE(free_list, pool->free_list[n], area);
  249. pool->reserved += mem_area_get_size(area);
  250. mutex_exit(&(pool->mutex));
  251. ut_ad(mem_pool_validate(pool));
  252. return((void*)(MEM_AREA_EXTRA_SIZE + ((byte*)area))); 
  253. }
  254. /************************************************************************
  255. Gets the buddy of an area, if it exists in pool. */
  256. UNIV_INLINE
  257. mem_area_t*
  258. mem_area_get_buddy(
  259. /*===============*/
  260. /* out: the buddy, NULL if no buddy in pool */
  261. mem_area_t* area, /* in: memory area */
  262. ulint size, /* in: memory area size */
  263. mem_pool_t* pool) /* in: memory pool */
  264. {
  265. mem_area_t* buddy;
  266. ut_ad(size != 0);
  267. if (((((byte*)area) - pool->buf) % (2 * size)) == 0) {
  268. /* The buddy is in a higher address */
  269. buddy = (mem_area_t*)(((byte*)area) + size);
  270. if ((((byte*)buddy) - pool->buf) + size > pool->size) {
  271. /* The buddy is not wholly contained in the pool:
  272. there is no buddy */
  273. buddy = NULL;
  274. }
  275. } else {
  276. /* The buddy is in a lower address; NOTE that area cannot
  277. be at the pool lower end, because then we would end up to
  278. the upper branch in this if-clause: the remainder would be
  279. 0 */
  280. buddy = (mem_area_t*)(((byte*)area) - size);
  281. }
  282. return(buddy);
  283. }
  284. /************************************************************************
  285. Frees memory to a pool. */
  286. void
  287. mem_area_free(
  288. /*==========*/
  289. void* ptr, /* in, own: pointer to allocated memory
  290. buffer */
  291. mem_pool_t* pool) /* in: memory pool */
  292. {
  293. mem_area_t* area;
  294. mem_area_t* buddy;
  295. void* new_ptr;
  296. ulint size;
  297. ulint n;
  298. if (mem_out_of_mem_err_msg_count > 0) {
  299. /* It may be that the area was really allocated from the
  300. OS with regular malloc: check if ptr points within
  301. our memory pool */
  302. if ((byte*)ptr < pool->buf
  303. || (byte*)ptr >= pool->buf + pool->size) {
  304. ut_free(ptr);
  305. return;
  306. }
  307. }
  308. area = (mem_area_t*) (((byte*)ptr) - MEM_AREA_EXTRA_SIZE);
  309. size = mem_area_get_size(area);
  310. ut_ad(size != 0);
  311. ut_a(!mem_area_get_free(area));
  312. #ifdef UNIV_LIGHT_MEM_DEBUG
  313. if (((byte*)area) + size < pool->buf + pool->size) {
  314. ulint next_size;
  315. next_size = mem_area_get_size(
  316. (mem_area_t*)(((byte*)area) + size));
  317. ut_a(ut_2_power_up(next_size) == next_size);
  318. }
  319. #endif
  320. buddy = mem_area_get_buddy(area, size, pool);
  321. n = ut_2_log(size);
  322. mutex_enter(&(pool->mutex));
  323. if (buddy && mem_area_get_free(buddy)
  324. && (size == mem_area_get_size(buddy))) {
  325. /* The buddy is in a free list */
  326. if ((byte*)buddy < (byte*)area) {
  327. new_ptr = ((byte*)buddy) + MEM_AREA_EXTRA_SIZE;
  328. mem_area_set_size(buddy, 2 * size);
  329. mem_area_set_free(buddy, FALSE);
  330. } else {
  331. new_ptr = ptr;
  332. mem_area_set_size(area, 2 * size);
  333. }
  334. /* Remove the buddy from its free list and merge it to area */
  335. UT_LIST_REMOVE(free_list, pool->free_list[n], buddy);
  336. pool->reserved += ut_2_exp(n);
  337. mutex_exit(&(pool->mutex));
  338. mem_area_free(new_ptr, pool);
  339. return;
  340. } else {
  341. UT_LIST_ADD_FIRST(free_list, pool->free_list[n], area);
  342. mem_area_set_free(area, TRUE);
  343. ut_ad(pool->reserved >= size);
  344. pool->reserved -= size;
  345. }
  346. mutex_exit(&(pool->mutex));
  347. ut_ad(mem_pool_validate(pool));
  348. }
  349. /************************************************************************
  350. Validates a memory pool. */
  351. ibool
  352. mem_pool_validate(
  353. /*==============*/
  354. /* out: TRUE if ok */
  355. mem_pool_t* pool) /* in: memory pool */
  356. {
  357. mem_area_t* area;
  358. mem_area_t* buddy;
  359. ulint free;
  360. ulint i;
  361. mutex_enter(&(pool->mutex));
  362. free = 0;
  363. for (i = 0; i < 64; i++) {
  364. UT_LIST_VALIDATE(free_list, mem_area_t, pool->free_list[i]);
  365. area = UT_LIST_GET_FIRST(pool->free_list[i]);
  366. while (area != NULL) {
  367. ut_a(mem_area_get_free(area));
  368. ut_a(mem_area_get_size(area) == ut_2_exp(i));
  369. buddy = mem_area_get_buddy(area, ut_2_exp(i), pool);
  370. ut_a(!buddy || !mem_area_get_free(buddy)
  371.           || (ut_2_exp(i) != mem_area_get_size(buddy)));
  372. area = UT_LIST_GET_NEXT(free_list, area);
  373. free += ut_2_exp(i);
  374. }
  375. }
  376. ut_a(free + pool->reserved == pool->size
  377. - (pool->size % MEM_AREA_MIN_SIZE));
  378. mutex_exit(&(pool->mutex));
  379. return(TRUE);
  380. }
  381. /************************************************************************
  382. Prints info of a memory pool. */
  383. void
  384. mem_pool_print_info(
  385. /*================*/
  386. FILE*         outfile,/* in: output file to write to */
  387. mem_pool_t* pool) /* in: memory pool */
  388. {
  389. ulint i;
  390. mem_pool_validate(pool);
  391. fprintf(outfile, "INFO OF A MEMORY POOLn");
  392. mutex_enter(&(pool->mutex));
  393. for (i = 0; i < 64; i++) {
  394. if (UT_LIST_GET_LEN(pool->free_list[i]) > 0) {
  395. fprintf(outfile,
  396.   "Free list length %lu for blocks of size %lun",
  397.   UT_LIST_GET_LEN(pool->free_list[i]),
  398.   ut_2_exp(i));
  399. }
  400. }
  401. fprintf(outfile, "Pool size %lu, reserved %lu.n", pool->size,
  402. pool->reserved);
  403. mutex_exit(&(pool->mutex));
  404. }
  405. /************************************************************************
  406. Returns the amount of reserved memory. */
  407. ulint
  408. mem_pool_get_reserved(
  409. /*==================*/
  410. /* out: reserved mmeory in bytes */
  411. mem_pool_t* pool) /* in: memory pool */
  412. {
  413. ulint reserved;
  414. mutex_enter(&(pool->mutex));
  415. reserved = pool->reserved;
  416. mutex_exit(&(pool->mutex));
  417. return(reserved);
  418. }