4.1.4.2 Buf的元信息结构
Buf的元信息数据块的结构信息如下,描述了每一个缓存块的使用情况:
typedef struct sbufdesc
{
BufferTag tag; /* ID of page contained in buffer */ // 起着连接内存和外存地址映射的作用,通过这个,可以决定缓存块的脏信息写到外存的什么位置;如果是新读入的块,则根据新读入的块的地址给本变量赋值
BufFlags flags; /* see bit definitions above */
uint16 usage_count; /* usage counter for clock sweep code */
unsigned refcount; /* # of backends holding pins on buffer */
int wait_backend_pid; /* backend PID of pin-count waiter */
slock_t buf_hdr_lock; /* protects the above fields */
int buf_id; /* buffer's index number (from 0) */
int freeNext; /* link in freelist chain */
LWLockId io_in_progress_lock; /* to wait for I/O to complete */
LWLockId content_lock; /* to lock access to buffer contents */
} BufferDesc;
结构中的其他成员,也很重要,如果想掌握全部用法,请核对代码,一一细看其调用方式。
第二个成员,标识了buf的状态,如下:
#define BM_DIRTY (1 << 0) /* data needs writing */
#define BM_VALID (1 << 1) /* data is valid */
#define BM_TAG_VALID (1 << 2) /* tag is assigned */
#define BM_IO_IN_PROGRESS (1 << 3) /* read or write in progress */
#define BM_IO_ERROR (1 << 4) /* previous I/O failed */
#define BM_JUST_DIRTIED (1 << 5) /* dirtied since write started */
#define BM_PIN_COUNT_WAITER (1 << 6) /* have waiter for sole pin */
#define BM_CHECKPOINT_NEEDED (1 << 7) /* must write for checkpoint */
不同状态,buf有需要不同的处理。
需要注意的是,buf层起着连接物理IO和上层数据访问的桥梁作用,所以,对上对下的重要信息,都在这个结构中。
如成员“io_in_progress_lock”,与物理IO相关,被调用关系如下:
sbufdesc.io_in_progress_lock
UnpinBuffer
WaitIO
InvalidateBuffer
StartBufferIO
WaitIO
StartBufferIO // 与缓存相关的一个重要函数,从物理存储往缓存块中加载数据
TerminateBufferIO
AbortBufferIO
InitBufferPool
sbufdesc
再有,与锁(content_lock、buf_hdr_lock等)相关的,和并发有关,需要仔细查看,本文讲解缓冲区,不再多述并发相关内容。