Unit 2 Buffer Pool Management Based on: Sections 9.4, 9.4.1, 9.4.2 of Ramakrishnan & Gehrke (text); Silberschatz, et. al. ( Operating System Concepts ); Other sources Original slides by Ed Knorr; Updates and changes by George Tsiknis
Learning Goals Explain the purpose of a DBMS buffer pool, and justify why a DBMS should manage its own buffer pool, rather than let the OS handle it. Provide an example of sequential flooding in a DBMS buffer pool. For a given reference string, compute the behavior of the page replacement algorithms: FIFO, LRU, MRU and Clock. Create a reference string that produces worst-case performance for a given page replacement algorithm. [Later] Explain how the page replacement policy and the access plan can have a big impact on the number of I/Os required by a given application. [Later] Predict which buffer pool page replacement strategy works best with a given workload. Unit 2 2
Some Definitions A DBMS buffer pool is part of RAM, and is managed independently of the operating system. We assume that a page is the smallest unit of transfer between disk and main memory. Logical memory has pages. Physical memory (RAM) has frames. The page/frame size is determined by the hardware. The Translation Lookaside Buffer (TLB) is a very fast L1 hardware cache. It is used to determine whether or not a particular page is currently in memory. Unit 2 3
Buffer Management in a DBMS Page Requests from Higher Levels BUFFER POOL disk page free frame MAIN MEMORY DISK DB Data must be in RAM for DBMS to operate on it A table of <frame#, pageid> pairs is maintained. choice of frame dictated by page replacement policy Unit 2 4
When a Page is Requested... If requested page is in buffer pool, the address of its frame is returned If requested page is not in buffer pool: Choose a frame for replacement If that frame is dirty, write it to disk Read requested page into chosen frame Pin the new page and return its address Pin = hold in memory & don t allow another page to replace it add 1 to the in-use count Note: If requests can be predicted, then several pages can be prefetched at a time! Unit 2 5
When a Page is Requested (cont.) When no longer needs a page, a requestor must unpin it, and indicate whether the page has been modified: Maintain a dirty bit for the page Page may be requested many times Maintain a pin count A page is a candidate for replacement iff: pin count = 0 Concurrency control and recovery mechanisms may entail additional I/O when a frame is chosen for replacement e.g., may need to write log records as well (later) Unit 2 6
Page Faults When a newly requested page is not in the buffer pool causes a page fault. This means we have to read the page into RAM from disk. Suppose the buffer pool is full and there is a page fault. We need to find a frame to store the new page. The page to be replaced is called the victim page. A victim page is chosen from the pages whose pin count is 0 using a page replacement algorithm (PRA). Unit 2 7
Some Page Replacement Algorithms FIFO Victim = oldest page Can use a queue showing the frames in the order they first used Least Recently Used (LRU) Victim = page that hasn t been referenced for the longest time Can use a queue or priority queue to implement it Good algorithm but it is expensive to implement it Most Recently Used (MRU) Victim = page that has been most recently used Clock A variant of LRU with less overhead. Each page is given a second chance. We ll cycle through the pages in the same order, as necessary, looking for a victim. The first page with 0 pin counter that is visited for a second time by the algorithm is selected as victim. Note: there are several variants. The version on next page is the simplest one. Unit 2 8 1
Clock Algorithm (One Version) Frames are numbered from 1 to N. Each frame has a reference bit (RB) and a pin count (PC) Initially RB is 0. It is set to 1 when PC is reduced to 0. Frames are checked in that order in circle starting from the current frame. Clock Algorithm if requested page is in a frame set this frame's RB =1, increment frame's PC current = the frame after it else repeat until a frame is found or no more frames to check: if current's PC is 0 and its RB is 1 set RB = 0 else if current's PC is 0 choose this frame for replacement set RB =1, set PC =1 current = next frame Unit 2 9... N-1 1 2 3 current
Clock Algorithm Example Note: Reference string = sequence of pages to be accessed p/r = page in frame / frame s reference bit Assume: initially current = 1; all RB s are 0; no frames are ever pinned ref. string frames frame 1 7/1 frame 2 0/1 frame 3 new current frame 7 0 1 2 0 3 0 4 2 3 page fault? yes yes Unit 2 10
Sequential Flooding Page replacement policy can have big impact on # of I/Os Also, depends on the data access pattern Sequential flooding: Nasty situation caused by LRU + repeated sequential scans: If # buffer frames < # pages in file each page request causes an I/O (page fault) See example on next page Unit 2 11
Sequential Flooding Example: Suppose we perform 2 file scans one after the other on a file with 4 pages using LRU with 3 available buffer frames: Assume that initially the LRU queue is [1,2,3] ref. str frames frame 1 1 2 3 4 1 2 3 4 1 frame 2 2 frame 3 queue [2,3,1] [3,1,2] PF? y y Unit 2 12
Other Algorithms (FYI Only) Extended Clock Algorithm The Clock Algorithm can be extended to include both a reference bit and a dirty bit (RB/DB). DB is set, if page needs to be written to disk This version is similar to previous, but assign higher preference to pages that are not dirty Not Frequently Used (NFU) A crude approximation of LRU Every page has a reference counter which is initially 0 At each interval, when a page is referenced, its counter is incremented The page with the lowest counter is the victim Working Set Model Each transaction keeps w (window) number of pages in the buffer and replaces the pages in a manner similar to LRU Unit 2 13 2,3,4
DBMS vs. OS File System OS does disk space & buffer management, so why not let the OS manage these tasks? Differences in OS support: portability issues Buffer management in DBMS requires ability to: Pin a page in the buffer pool, or force a page to disk (both are important for implementing CC & recovery) some disk subsystems do not provide these operations (not good) Adjust replacement policy, based on access patterns Prefetch pages based on access patterns in typical DB operations even if pages are not consecutive, disk can access these pages in an order that minimizes seek and latency Unit 2 14
Summary Buffer manager brings pages into RAM Page stays in RAM until released Dirty page is written to disk when frame is chosen as victim (which may be some time after the requestor writes, and releases, the page) Choice of frame to replace is based on replacement policy Tries to prefetch several pages at a time DBMS vs. OS file support DBMS needs features not found in many OSs, e.g., forcing a page to disk, controlling the order of page writes to disk, files spanning disks, ability to control prefetching and page replacement policy based on predictable access patterns, etc. Unit 2 15