1 /* Innobase relational database engine; Copyright (C) 2001 Innobase Oy
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License 2
5 as published by the Free Software Foundation in June 1991.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License 2
13 along with this program (in file COPYING); if not, write to the Free
14 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
15 /******************************************************
16 The database buffer pool high-level routines
20 Created 11/5/1995 Heikki Tuuri
21 *******************************************************/
28 #include "mtr0types.h"
29 #include "buf0types.h"
31 #include "hash0hash.h"
35 /* Flags for flush types */
36 #define BUF_FLUSH_LRU 1
37 #define BUF_FLUSH_SINGLE_PAGE 2
38 #define BUF_FLUSH_LIST 3 /* An array in the pool struct
39 has size BUF_FLUSH_LIST + 1: if you
40 add more flush types, put them in
42 /* Modes for buf_page_get_gen */
43 #define BUF_GET 10 /* get always */
44 #define BUF_GET_IF_IN_POOL 11 /* get if in pool */
45 #define BUF_GET_NOWAIT 12 /* get if can set the latch without
47 #define BUF_GET_NO_LATCH 14 /* get and bufferfix, but set no latch;
48 we have separated this case, because
49 it is error-prone programming not to
50 set a latch, and it should be used
52 /* Modes for buf_page_get_known_nowait */
53 #define BUF_MAKE_YOUNG 51
54 #define BUF_KEEP_OLD 52
55 /* Magic value to use instead of checksums when they are disabled */
56 #define BUF_NO_CHECKSUM_MAGIC 0xDEADBEEFUL
58 extern buf_pool_t
* buf_pool
; /* The buffer pool of the database */
60 extern ibool buf_debug_prints
;/* If this is set TRUE, the program
61 prints info whenever read or flush
63 #endif /* UNIV_DEBUG */
64 extern ulint srv_buf_pool_write_requests
; /* variable to count write request
67 /************************************************************************
68 Creates the buffer pool. */
73 /* out, own: buf_pool object, NULL if not
74 enough memory or error */
75 ulint max_size
, /* in: maximum size of the buf_pool in
77 ulint curr_size
, /* in: current size to use, must be <=
78 max_size, currently must be equal to
80 ulint n_frames
); /* in: number of frames; if AWE is used,
81 this is the size of the address space window
82 where physical memory pages are mapped; if
83 AWE is not used then this must be the same
85 /*************************************************************************
86 Gets the current size of buffer buf_pool in bytes. In the case of AWE, the
87 size of AWE window (= the frames). */
90 buf_pool_get_curr_size(void);
91 /*========================*/
92 /* out: size in bytes */
93 /*************************************************************************
94 Gets the maximum size of buffer pool in bytes. In the case of AWE, the
95 size of AWE window (= the frames). */
98 buf_pool_get_max_size(void);
99 /*=======================*/
100 /* out: size in bytes */
101 /************************************************************************
102 Gets the smallest oldest_modification lsn for any page in the pool. Returns
103 ut_dulint_zero if all modified pages have been flushed to disk. */
106 buf_pool_get_oldest_modification(void);
107 /*==================================*/
108 /* out: oldest modification in pool,
109 ut_dulint_zero if none */
110 /*************************************************************************
111 Allocates a buffer frame. */
114 buf_frame_alloc(void);
115 /*==================*/
116 /* out: buffer frame */
117 /*************************************************************************
118 Frees a buffer frame which does not contain a file page. */
123 buf_frame_t
* frame
); /* in: buffer frame */
124 /*************************************************************************
125 Copies contents of a buffer frame to a given buffer. */
131 byte
* buf
, /* in: buffer to copy to */
132 buf_frame_t
* frame
); /* in: buffer frame */
133 /******************************************************************
134 NOTE! The following macros should be used instead of buf_page_get_gen,
135 to improve debugging. Only values RW_S_LATCH and RW_X_LATCH are allowed
137 #define buf_page_get(SP, OF, LA, MTR) buf_page_get_gen(\
139 BUF_GET, __FILE__, __LINE__, MTR)
140 /******************************************************************
141 Use these macros to bufferfix a page with no latching. Remember not to
142 read the contents of the page unless you know it is safe. Do not modify
143 the contents of the page! We have separated this case, because it is
144 error-prone programming not to set a latch, and it should be used
146 #define buf_page_get_with_no_latch(SP, OF, MTR) buf_page_get_gen(\
147 SP, OF, RW_NO_LATCH, NULL,\
148 BUF_GET_NO_LATCH, __FILE__, __LINE__, MTR)
149 /******************************************************************
150 NOTE! The following macros should be used instead of buf_page_get_gen, to
151 improve debugging. Only values RW_S_LATCH and RW_X_LATCH are allowed as LA! */
152 #define buf_page_get_nowait(SP, OF, LA, MTR) buf_page_get_gen(\
154 BUF_GET_NOWAIT, __FILE__, __LINE__, MTR)
155 /******************************************************************
156 NOTE! The following macros should be used instead of
157 buf_page_optimistic_get_func, to improve debugging. Only values RW_S_LATCH and
158 RW_X_LATCH are allowed as LA! */
159 #define buf_page_optimistic_get(LA, BL, G, MC, MTR) \
160 buf_page_optimistic_get_func(LA, BL, G, MC, __FILE__, __LINE__, MTR)
161 /************************************************************************
162 This is the general function used to get optimistic access to a database
166 buf_page_optimistic_get_func(
167 /*=========================*/
168 /* out: TRUE if success */
169 ulint rw_latch
,/* in: RW_S_LATCH, RW_X_LATCH */
170 buf_block_t
* block
, /* in: guessed block */
171 buf_frame_t
* guess
, /* in: guessed frame; note that AWE may move
173 dulint modify_clock
,/* in: modify clock value if mode is
174 ..._GUESS_ON_CLOCK */
175 const char* file
, /* in: file name */
176 ulint line
, /* in: line where called */
177 mtr_t
* mtr
); /* in: mini-transaction */
178 /************************************************************************
179 This is used to get access to a known database page, when no waiting can be
183 buf_page_get_known_nowait(
184 /*======================*/
185 /* out: TRUE if success */
186 ulint rw_latch
,/* in: RW_S_LATCH, RW_X_LATCH */
187 buf_frame_t
* guess
, /* in: the known page frame */
188 ulint mode
, /* in: BUF_MAKE_YOUNG or BUF_KEEP_OLD */
189 const char* file
, /* in: file name */
190 ulint line
, /* in: line where called */
191 mtr_t
* mtr
); /* in: mini-transaction */
192 /************************************************************************
193 This is the general function used to get access to a database page. */
198 /* out: pointer to the frame or NULL */
199 ulint space
, /* in: space id */
200 ulint offset
, /* in: page number */
201 ulint rw_latch
,/* in: RW_S_LATCH, RW_X_LATCH, RW_NO_LATCH */
202 buf_frame_t
* guess
, /* in: guessed frame or NULL */
203 ulint mode
, /* in: BUF_GET, BUF_GET_IF_IN_POOL,
205 const char* file
, /* in: file name */
206 ulint line
, /* in: line where called */
207 mtr_t
* mtr
); /* in: mini-transaction */
208 /************************************************************************
209 Initializes a page to the buffer buf_pool. The page is usually not read
210 from a file even if it cannot be found in the buffer buf_pool. This is one
211 of the functions which perform to a block a state transition NOT_USED =>
212 FILE_PAGE (the other is buf_page_init_for_read above). */
217 /* out: pointer to the frame, page bufferfixed */
218 ulint space
, /* in: space id */
219 ulint offset
, /* in: offset of the page within space in units of
221 mtr_t
* mtr
); /* in: mini-transaction handle */
222 /************************************************************************
223 Inits a page to the buffer buf_pool, for use in ibbackup --restore. */
226 buf_page_init_for_backup_restore(
227 /*=============================*/
228 ulint space
, /* in: space id */
229 ulint offset
, /* in: offset of the page within space
230 in units of a page */
231 buf_block_t
* block
); /* in: block to init */
232 /************************************************************************
233 Decrements the bufferfix count of a buffer control block and releases
234 a latch, if specified. */
239 buf_block_t
* block
, /* in: buffer block */
240 ulint rw_latch
, /* in: RW_S_LATCH, RW_X_LATCH,
242 mtr_t
* mtr
); /* in: mtr */
243 /************************************************************************
244 Moves a page to the start of the buffer pool LRU list. This high-level
245 function can be used to prevent an important page from from slipping out of
251 buf_frame_t
* frame
); /* in: buffer frame of a file page */
252 /************************************************************************
253 Returns TRUE if the page can be found in the buffer pool hash table. NOTE
254 that it is possible that the page is not yet read from disk, though. */
259 /* out: TRUE if found from page hash table,
260 NOTE that the page is not necessarily yet read
262 ulint space
, /* in: space id */
263 ulint offset
);/* in: page number */
264 /************************************************************************
265 Returns the buffer control block if the page can be found in the buffer
266 pool. NOTE that it is possible that the page is not yet read
267 from disk, though. This is a very low-level function: use with care! */
272 /* out: control block if found from page hash table,
273 otherwise NULL; NOTE that the page is not necessarily
274 yet read from disk! */
275 ulint space
, /* in: space id */
276 ulint offset
);/* in: page number */
277 /************************************************************************
278 Sets file_page_was_freed TRUE if the page is found in the buffer pool.
279 This function should be called when we free a file page and want the
280 debug version to check that it is not accessed any more unless
284 buf_page_set_file_page_was_freed(
285 /*=============================*/
286 /* out: control block if found from page hash table,
288 ulint space
, /* in: space id */
289 ulint offset
); /* in: page number */
290 /************************************************************************
291 Sets file_page_was_freed FALSE if the page is found in the buffer pool.
292 This function should be called when we free a file page and want the
293 debug version to check that it is not accessed any more unless
297 buf_page_reset_file_page_was_freed(
298 /*===============================*/
299 /* out: control block if found from page hash table,
301 ulint space
, /* in: space id */
302 ulint offset
); /* in: page number */
303 /************************************************************************
304 Recommends a move of a block to the start of the LRU list if there is danger
305 of dropping from the buffer pool. NOTE: does not reserve the buffer pool
309 buf_block_peek_if_too_old(
310 /*======================*/
311 /* out: TRUE if should be made younger */
312 buf_block_t
* block
); /* in: block to make younger */
313 /************************************************************************
314 Returns the current state of is_hashed of a page. FALSE if the page is
315 not in the pool. NOTE that this operation does not fix the page in the
316 pool if it is found there. */
319 buf_page_peek_if_search_hashed(
320 /*===========================*/
321 /* out: TRUE if page hash index is built in search
323 ulint space
, /* in: space id */
324 ulint offset
);/* in: page number */
325 /************************************************************************
326 Gets the youngest modification log sequence number for a frame.
327 Returns zero if not file page or no modification occurred yet. */
330 buf_frame_get_newest_modification(
331 /*==============================*/
332 /* out: newest modification to page */
333 buf_frame_t
* frame
); /* in: pointer to a frame */
334 /************************************************************************
335 Increments the modify clock of a frame by 1. The caller must (1) own the
336 pool mutex and block bufferfix count has to be zero, (2) or own an x-lock
340 buf_frame_modify_clock_inc(
341 /*=======================*/
343 buf_frame_t
* frame
); /* in: pointer to a frame */
344 /************************************************************************
345 Increments the modify clock of a frame by 1. The caller must (1) own the
346 buf_pool mutex and block bufferfix count has to be zero, (2) or own an x-lock
350 buf_block_modify_clock_inc(
351 /*=======================*/
353 buf_block_t
* block
); /* in: block */
354 /************************************************************************
355 Returns the value of the modify clock. The caller must have an s-lock
356 or x-lock on the block. */
359 buf_block_get_modify_clock(
360 /*=======================*/
362 buf_block_t
* block
); /* in: block */
363 /************************************************************************
364 Calculates a page checksum which is stored to the page when it is written
365 to a file. Note that we must be careful to calculate the same value
366 on 32-bit and 64-bit architectures. */
369 buf_calc_page_new_checksum(
370 /*=======================*/
372 byte
* page
); /* in: buffer page */
373 /************************************************************************
374 In versions < 4.0.14 and < 4.1.1 there was a bug that the checksum only
375 looked at the first few bytes of the page. This calculates that old
377 NOTE: we must first store the new formula checksum to
378 FIL_PAGE_SPACE_OR_CHKSUM before calculating and storing this old checksum
379 because this takes that field as an input! */
382 buf_calc_page_old_checksum(
383 /*=======================*/
385 byte
* page
); /* in: buffer page */
386 /************************************************************************
387 Checks if a page is corrupt. */
390 buf_page_is_corrupted(
391 /*==================*/
392 /* out: TRUE if corrupted */
393 byte
* read_buf
); /* in: a database page */
394 /**************************************************************************
395 Gets the page number of a pointer pointing within a buffer frame containing
399 buf_frame_get_page_no(
400 /*==================*/
401 /* out: page number */
402 byte
* ptr
); /* in: pointer to within a buffer frame */
403 /**************************************************************************
404 Gets the space id of a pointer pointing within a buffer frame containing a
408 buf_frame_get_space_id(
409 /*===================*/
411 byte
* ptr
); /* in: pointer to within a buffer frame */
412 /**************************************************************************
413 Gets the space id, page offset, and byte offset within page of a
414 pointer pointing to a buffer frame containing a file page. */
417 buf_ptr_get_fsp_addr(
418 /*=================*/
419 byte
* ptr
, /* in: pointer to a buffer frame */
420 ulint
* space
, /* out: space id */
421 fil_addr_t
* addr
); /* out: page offset and byte offset */
422 /**************************************************************************
423 Gets the hash value of the page the pointer is pointing to. This can be used
424 in searches in the lock hash table. */
427 buf_frame_get_lock_hash_val(
428 /*========================*/
429 /* out: lock hash value */
430 byte
* ptr
); /* in: pointer to within a buffer frame */
431 /**************************************************************************
432 Gets the mutex number protecting the page record lock hash chain in the lock
439 byte
* ptr
); /* in: pointer to within a buffer frame */
440 /***********************************************************************
441 Gets the frame the pointer is pointing to. */
446 /* out: pointer to frame */
447 byte
* ptr
); /* in: pointer to a frame */
448 /***********************************************************************
449 Checks if a pointer points to the block array of the buffer pool (blocks, not
455 /* out: TRUE if pointer to block */
456 void* ptr
); /* in: pointer to memory */
458 /*************************************************************************
459 Validates the buffer pool data structure. */
464 /*************************************************************************
465 Prints info of the buffer pool data structure. */
471 /*************************************************************************
472 Returns the number of latched pages in the buffer pool. */
475 buf_get_latched_pages_number(void);
476 /*==============================*/
477 #endif /* UNIV_DEBUG */
479 /************************************************************************
480 Prints a page to stderr. */
485 byte
* read_buf
); /* in: a database page */
487 /*************************************************************************
488 Returns the number of pending buf pool ios. */
491 buf_get_n_pending_ios(void);
492 /*=======================*/
493 /*************************************************************************
494 Prints info of the buffer i/o. */
499 FILE* file
); /* in: file where to print */
500 /*************************************************************************
501 Returns the ratio in percents of modified pages in the buffer pool /
502 database pages in the buffer pool. */
505 buf_get_modified_ratio_pct(void);
506 /*============================*/
507 /**************************************************************************
508 Refreshes the statistics used to print per-second averages. */
511 buf_refresh_io_stats(void);
512 /*======================*/
513 /*************************************************************************
514 Checks that all file pages in the buffer are in a replaceable state. */
519 /*************************************************************************
520 Checks that there currently are no pending i/o-operations for the buffer
524 buf_pool_check_no_pending_io(void);
525 /*==============================*/
526 /* out: TRUE if there is no pending i/o */
527 /*************************************************************************
528 Invalidates the file pages in the buffer pool when an archive recovery is
529 completed. All the file pages buffered must be in a replaceable state when
530 this function is called: not latched and not modified. */
533 buf_pool_invalidate(void);
534 /*=====================*/
536 /*========================================================================
537 --------------------------- LOWER LEVEL ROUTINES -------------------------
538 =========================================================================*/
540 /************************************************************************
541 Maps the page of block to a frame, if not mapped yet. Unmaps some page
542 from the end of the awe_LRU_free_mapped. */
545 buf_awe_map_page_to_frame(
546 /*======================*/
547 buf_block_t
* block
, /* in: block whose page should be
549 ibool add_to_mapped_list
);/* in: TRUE if we in the case
550 we need to map the page should also
552 awe_LRU_free_mapped list */
553 #ifdef UNIV_SYNC_DEBUG
554 /*************************************************************************
555 Adds latch level info for the rw-lock protecting the buffer frame. This
556 should be called in the debug version after a successful latching of a
557 page if we know the latching order level of the acquired latch. */
560 buf_page_dbg_add_level(
561 /*===================*/
562 buf_frame_t
* frame
, /* in: buffer page where we have acquired
564 ulint level
); /* in: latching order level */
565 #endif /* UNIV_SYNC_DEBUG */
566 /*************************************************************************
567 Gets a pointer to the memory frame of a block. */
572 /* out: pointer to the frame */
573 buf_block_t
* block
); /* in: pointer to the control block */
574 /*************************************************************************
575 Gets the space id of a block. */
581 buf_block_t
* block
); /* in: pointer to the control block */
582 /*************************************************************************
583 Gets the page number of a block. */
586 buf_block_get_page_no(
587 /*==================*/
588 /* out: page number */
589 buf_block_t
* block
); /* in: pointer to the control block */
590 /***********************************************************************
591 Gets the block to whose frame the pointer is pointing to. */
596 /* out: pointer to block */
597 byte
* ptr
); /* in: pointer to a frame */
598 /************************************************************************
599 This function is used to get info if there is an io operation
600 going on on a buffer page. */
605 /* out: TRUE if io going on */
606 buf_block_t
* block
); /* in: pool block, must be bufferfixed */
607 /***********************************************************************
608 Accessor function for block array. */
611 buf_pool_get_nth_block(
612 /*===================*/
613 /* out: pointer to block */
614 buf_pool_t
* pool
, /* in: pool */
615 ulint i
); /* in: index of the block */
616 /************************************************************************
617 Function which inits a page for read to the buffer buf_pool. If the page is
618 (1) already in buf_pool, or
619 (2) if we specify to read only ibuf pages and the page is not an ibuf page, or
620 (3) if the space is deleted or being deleted,
621 then this function does nothing.
622 Sets the io_fix flag to BUF_IO_READ and sets a non-recursive exclusive lock
623 on the buffer frame. The io-handler must take care that the flag is cleared
624 and the lock released later. This is one of the functions which perform the
625 state transition NOT_USED => FILE_PAGE to a block (the other is
629 buf_page_init_for_read(
630 /*===================*/
631 /* out: pointer to the block or NULL */
632 ulint
* err
, /* out: DB_SUCCESS or DB_TABLESPACE_DELETED */
633 ulint mode
, /* in: BUF_READ_IBUF_PAGES_ONLY, ... */
634 ulint space
, /* in: space id */
635 ib_longlong tablespace_version
,/* in: prevents reading from a wrong
636 version of the tablespace in case we have done
638 ulint offset
);/* in: page number */
639 /************************************************************************
640 Completes an asynchronous read or write request of a file page to or from
644 buf_page_io_complete(
645 /*=================*/
646 buf_block_t
* block
); /* in: pointer to the block in question */
647 /************************************************************************
648 Calculates a folded value of a file page address to use in the page hash
652 buf_page_address_fold(
653 /*==================*/
654 /* out: the folded value */
655 ulint space
, /* in: space id */
656 ulint offset
);/* in: offset of the page within space */
657 /**********************************************************************
658 Returns the control block of a file page, NULL if not found. */
663 /* out: block, NULL if not found */
664 ulint space
, /* in: space id */
665 ulint offset
);/* in: offset of the page within space */
666 /***********************************************************************
667 Increments the pool clock by one and returns its new value. Remember that
668 in the 32 bit version the clock wraps around at 4 billion! */
671 buf_pool_clock_tic(void);
672 /*====================*/
673 /* out: new clock value */
674 /*************************************************************************
675 Gets the current length of the free list of buffer blocks. */
678 buf_get_free_list_len(void);
679 /*=======================*/
683 /* The buffer control block structure */
685 struct buf_block_struct
{
687 /* 1. General fields */
689 ulint magic_n
; /* magic number to check */
690 ulint state
; /* state of the control block:
691 BUF_BLOCK_NOT_USED, ...; changing
692 this is only allowed when a thread
693 has BOTH the buffer pool mutex AND
694 block->mutex locked */
695 byte
* frame
; /* pointer to buffer frame which
696 is of size UNIV_PAGE_SIZE, and
697 aligned to an address divisible by
698 UNIV_PAGE_SIZE; if AWE is used, this
699 will be NULL for the pages which are
700 currently not mapped into the virtual
701 address space window of the buffer
703 os_awe_t
* awe_info
; /* if AWE is used, then an array of
705 UNIV_PAGE_SIZE / OS_AWE_X86_PAGE_SIZE
706 (normally = 4) physical memory
707 pages; otherwise NULL */
708 ulint space
; /* space id of the page */
709 ulint offset
; /* page number within the space */
710 ulint lock_hash_val
; /* hashed value of the page address
711 in the record lock hash table */
712 mutex_t mutex
; /* mutex protecting this block:
713 state (also protected by the buffer
714 pool mutex), io_fix, buf_fix_count,
715 and accessed; we introduce this new
716 mutex in InnoDB-5.1 to relieve
717 contention on the buffer pool mutex */
718 rw_lock_t lock
; /* read-write lock of the buffer
720 buf_block_t
* hash
; /* node used in chaining to the page
722 ibool check_index_page_at_flush
;
723 /* TRUE if we know that this is
724 an index page, and want the database
725 to check its consistency before flush;
726 note that there may be pages in the
727 buffer pool which are index pages,
728 but this flag is not set because
729 we do not keep track of all pages */
730 /* 2. Page flushing fields */
732 UT_LIST_NODE_T(buf_block_t
) flush_list
;
733 /* node of the modified, not yet
734 flushed blocks list */
735 dulint newest_modification
;
736 /* log sequence number of the youngest
737 modification to this block, zero if
739 dulint oldest_modification
;
740 /* log sequence number of the START of
741 the log entry written of the oldest
742 modification to this block which has
743 not yet been flushed on disk; zero if
744 all modifications are on disk */
745 ulint flush_type
; /* if this block is currently being
746 flushed to disk, this tells the
747 flush_type: BUF_FLUSH_LRU or
750 /* 3. LRU replacement algorithm fields */
752 UT_LIST_NODE_T(buf_block_t
) free
;
753 /* node of the free block list */
754 ibool in_free_list
; /* TRUE if in the free list; used in
756 UT_LIST_NODE_T(buf_block_t
) LRU
;
757 /* node of the LRU list */
758 UT_LIST_NODE_T(buf_block_t
) awe_LRU_free_mapped
;
759 /* in the AWE version node in the
760 list of free and LRU blocks which are
762 ibool in_LRU_list
; /* TRUE of the page is in the LRU list;
764 ulint LRU_position
; /* value which monotonically
765 decreases (or may stay constant if
766 the block is in the old blocks) toward
767 the end of the LRU list, if the pool
768 ulint_clock has not wrapped around:
769 NOTE that this value can only be used
770 in heuristic algorithms, because of
771 the possibility of a wrap-around! */
772 ulint freed_page_clock
;/* the value of freed_page_clock
773 of the buffer pool when this block was
774 the last time put to the head of the
775 LRU list; a thread is allowed to
776 read this for heuristic purposes
777 without holding any mutex or latch */
778 ibool old
; /* TRUE if the block is in the old
779 blocks in the LRU list */
780 ibool accessed
; /* TRUE if the page has been accessed
781 while in the buffer pool: read-ahead
782 may read in pages which have not been
783 accessed yet; this is protected by
784 block->mutex; a thread is allowed to
785 read this for heuristic purposes
786 without holding any mutex or latch */
787 ulint buf_fix_count
; /* count of how manyfold this block
788 is currently bufferfixed; this is
789 protected by block->mutex */
790 ulint io_fix
; /* if a read is pending to the frame,
791 io_fix is BUF_IO_READ, in the case
792 of a write BUF_IO_WRITE, otherwise 0;
793 this is protected by block->mutex */
794 /* 4. Optimistic search field */
796 dulint modify_clock
; /* this clock is incremented every
797 time a pointer to a record on the
798 page may become obsolete; this is
799 used in the optimistic cursor
800 positioning: if the modify clock has
801 not changed, we know that the pointer
802 is still valid; this field may be
803 changed if the thread (1) owns the
804 pool mutex and the page is not
805 bufferfixed, or (2) the thread has an
806 x-latch on the block */
808 /* 5. Hash search fields: NOTE that the first 4 fields are NOT
809 protected by any semaphore! */
811 ulint n_hash_helps
; /* counter which controls building
812 of a new hash index for the page */
813 ulint n_fields
; /* recommended prefix length for hash
814 search: number of full fields */
815 ulint n_bytes
; /* recommended prefix: number of bytes
816 in an incomplete field */
817 ibool left_side
; /* TRUE or FALSE, depending on
818 whether the leftmost record of several
819 records with the same prefix should be
820 indexed in the hash index */
822 /* These 6 fields may only be modified when we have
823 an x-latch on btr_search_latch AND
824 a) we are holding an s-latch or x-latch on block->lock or
825 b) we know that block->buf_fix_count == 0.
827 An exception to this is when we init or create a page
828 in the buffer pool in buf0buf.c. */
830 ibool is_hashed
; /* TRUE if hash index has already been
831 built on this page; note that it does
832 not guarantee that the index is
833 complete, though: there may have been
834 hash collisions, record deletions,
836 ulint n_pointers
; /* used in debugging: the number of
837 pointers in the adaptive hash index
838 pointing to this frame */
839 ulint curr_n_fields
; /* prefix length for hash indexing:
840 number of full fields */
841 ulint curr_n_bytes
; /* number of bytes in hash indexing */
842 ibool curr_left_side
; /* TRUE or FALSE in hash indexing */
843 dict_index_t
* index
; /* Index for which the adaptive
844 hash index has been created. */
845 /* 6. Debug fields */
846 #ifdef UNIV_SYNC_DEBUG
847 rw_lock_t debug_latch
; /* in the debug version, each thread
848 which bufferfixes the block acquires
849 an s-latch here; so we can use the
850 debug utilities in sync0rw */
852 ibool file_page_was_freed
;
853 /* this is set to TRUE when fsp
854 frees a page in buffer pool */
857 #define BUF_BLOCK_MAGIC_N 41526563
859 /* The buffer pool structure. NOTE! The definition appears here only for
860 other modules of this directory (buf) to see it. Do not use from outside! */
862 struct buf_pool_struct
{
864 /* 1. General fields */
866 mutex_t mutex
; /* mutex protecting the buffer pool
867 struct and control blocks, except the
868 read-write lock in them */
869 byte
* frame_mem
; /* pointer to the memory area which
870 was allocated for the frames; in AWE
871 this is the virtual address space
872 window where we map pages stored
873 in physical memory */
874 byte
* frame_zero
; /* pointer to the first buffer frame:
875 this may differ from frame_mem, because
876 this is aligned by the frame size */
877 byte
* high_end
; /* pointer to the end of the buffer
879 ulint n_frames
; /* number of frames */
880 buf_block_t
* blocks
; /* array of buffer control blocks */
881 buf_block_t
** blocks_of_frames
;/* inverse mapping which can be used
882 to retrieve the buffer control block
883 of a frame; this is an array which
884 lists the blocks of frames in the
886 frame_zero + UNIV_PAGE_SIZE, ...
887 a control block is always assigned
888 for each frame, even if the frame does
889 not contain any data; note that in AWE
890 there are more control blocks than
892 os_awe_t
* awe_info
; /* if AWE is used, AWE info for the
893 physical 4 kB memory pages associated
894 with buffer frames */
895 ulint max_size
; /* number of control blocks ==
896 maximum pool size in pages */
897 ulint curr_size
; /* current pool size in pages;
898 currently always the same as
900 hash_table_t
* page_hash
; /* hash table of the file pages */
902 ulint n_pend_reads
; /* number of pending read operations */
904 time_t last_printout_time
; /* when buf_print was last time
906 ulint n_pages_read
; /* number read operations */
907 ulint n_pages_written
;/* number write operations */
908 ulint n_pages_created
;/* number of pages created in the pool
910 ulint n_page_gets
; /* number of page gets performed;
911 also successful searches through
912 the adaptive hash index are
913 counted as page gets; this field
914 is NOT protected by the buffer
916 ulint n_pages_awe_remapped
; /* if AWE is enabled, the
917 number of remaps of blocks to
919 ulint n_page_gets_old
;/* n_page_gets when buf_print was
920 last time called: used to calculate
922 ulint n_pages_read_old
;/* n_pages_read when buf_print was
924 ulint n_pages_written_old
;/* number write operations */
925 ulint n_pages_created_old
;/* number of pages created in
926 the pool with no read */
927 ulint n_pages_awe_remapped_old
;
928 /* 2. Page flushing algorithm fields */
930 UT_LIST_BASE_NODE_T(buf_block_t
) flush_list
;
931 /* base node of the modified block
933 ibool init_flush
[BUF_FLUSH_LIST
+ 1];
934 /* this is TRUE when a flush of the
935 given type is being initialized */
936 ulint n_flush
[BUF_FLUSH_LIST
+ 1];
937 /* this is the number of pending
938 writes in the given flush type */
939 os_event_t no_flush
[BUF_FLUSH_LIST
+ 1];
940 /* this is in the set state when there
941 is no flush batch of the given type
943 ulint ulint_clock
; /* a sequence number used to count
944 time. NOTE! This counter wraps
945 around at 4 billion (if ulint ==
947 ulint freed_page_clock
;/* a sequence number used to count the
948 number of buffer blocks removed from
949 the end of the LRU list; NOTE that
950 this counter may wrap around at 4
951 billion! A thread is allowed to
952 read this for heuristic purposes
953 without holding any mutex or latch */
954 ulint LRU_flush_ended
;/* when an LRU flush ends for a page,
955 this is incremented by one; this is
956 set to zero when a buffer block is
959 /* 3. LRU replacement algorithm fields */
961 UT_LIST_BASE_NODE_T(buf_block_t
) free
;
962 /* base node of the free block list;
963 in the case of AWE, at the start are
964 always free blocks for which the
965 physical memory is mapped to a frame */
966 UT_LIST_BASE_NODE_T(buf_block_t
) LRU
;
967 /* base node of the LRU list */
968 buf_block_t
* LRU_old
; /* pointer to the about 3/8 oldest
969 blocks in the LRU list; NULL if LRU
970 length less than BUF_LRU_OLD_MIN_LEN */
971 ulint LRU_old_len
; /* length of the LRU list from
972 the block to which LRU_old points
973 onward, including that block;
974 see buf0lru.c for the restrictions
975 on this value; not defined if
977 UT_LIST_BASE_NODE_T(buf_block_t
) awe_LRU_free_mapped
;
978 /* list of those blocks which are
979 in the LRU list or the free list, and
980 where the page is mapped to a frame;
981 thus, frames allocated, e.g., to the
982 locki table, are not in this list */
985 /* States of a control block */
986 #define BUF_BLOCK_NOT_USED 211 /* is in the free list */
987 #define BUF_BLOCK_READY_FOR_USE 212 /* when buf_get_free_block returns
988 a block, it is in this state */
989 #define BUF_BLOCK_FILE_PAGE 213 /* contains a buffered file page */
990 #define BUF_BLOCK_MEMORY 214 /* contains some main memory object */
991 #define BUF_BLOCK_REMOVE_HASH 215 /* hash index should be removed
992 before putting to the free list */
994 /* Io_fix states of a control block; these must be != 0 */
995 #define BUF_IO_READ 561
996 #define BUF_IO_WRITE 562
998 /************************************************************************
999 Let us list the consistency conditions for different control block states.
1001 NOT_USED: is in free list, not in LRU list, not in flush list, nor
1003 READY_FOR_USE: is not in free list, LRU list, or flush list, nor page
1005 MEMORY: is not in free list, LRU list, or flush list, nor page
1007 FILE_PAGE: space and offset are defined, is in page hash table
1008 if io_fix == BUF_IO_WRITE,
1009 pool: no_flush[block->flush_type] is in reset state,
1010 pool: n_flush[block->flush_type] > 0
1012 (1) if buf_fix_count == 0, then
1013 is in LRU list, not in free list
1015 if and only if oldest_modification > 0
1017 if and only if io_fix == BUF_IO_READ
1019 if and only if io_fix == BUF_IO_WRITE
1021 (2) if buf_fix_count > 0, then
1022 is not in LRU list, not in free list
1024 if and only if oldest_modification > 0
1025 if io_fix == BUF_IO_READ,
1027 if io_fix == BUF_IO_WRITE,
1032 NOT_USED => READY_FOR_USE
1033 READY_FOR_USE => MEMORY
1034 READY_FOR_USE => FILE_PAGE
1036 FILE_PAGE => NOT_USED NOTE: This transition is allowed if and only if
1037 (1) buf_fix_count == 0,
1038 (2) oldest_modification == 0, and
1043 #include "buf0buf.ic"