From fad1f23c20e5bd0eb73cb8e124f063ac95cb307d Mon Sep 17 00:00:00 2001 From: Kevin Grittner Date: Sat, 24 Dec 2011 13:07:13 -0600 Subject: [PATCH] WIP on background hinting. --- src/backend/storage/buffer/bufmgr.c | 48 ++++++++++++++++++++++++++++- src/include/storage/bufmgr.h | 1 + 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index bc9c2da427..46daa3c471 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -49,6 +49,10 @@ #include "utils/resowner.h" #include "utils/timestamp.h" +/* Is the buffer in a suitable state for backgroun hinting? */ +#define BufferNeedsHinting(bufHdr) (((BM_DIRTY | BM_VALID | BM_TAG_VALID | BM_IO_IN_PROGRESS) \ + & ((bufHdr)->flags)) \ + == (BM_DIRTY | BM_VALID | BM_TAG_VALID)) /* Note: these two macros only work on shared buffers, not local ones! */ #define BufHdrGetBlock(bufHdr) ((Block) (BufferBlocks + ((Size) (bufHdr)->buf_id) * BLCKSZ)) @@ -1304,12 +1308,54 @@ BufferSync(int flags) } /* - * BgHintBuffers -- hint any dirty buffers that are ripe for it + * BgHintBuffers -- Hint any dirty buffers that are ripe for it. * * TODO: Fill this in. */ void BgHintBuffers(void) +{ + int buf_id; + + /* Make sure we can handle the pin inside HintOneBuffer */ + ResourceOwnerEnlargeBuffers(CurrentResourceOwner); + + for (buf_id = 0; buf_id < NBuffers; buf_id++) + { + volatile BufferDesc *bufHdr = &BufferDescriptors[buf_id]; + + /* TODO: Is anything special needed here regarding local buffers? */ + + if (!BufferNeedsHinting(bufHdr)) + continue; + + if (!PinBuffer(bufHdr)) + { + /* Buffer was not valid on pin; move on with minimum work. */ + UnpinBuffer(bufHdr, true); + continue; + } + + if (BufferNeedsHinting(bufHdr)) + { + LWLockAcquire(bufHdr->content_lock, LW_SHARED); + HintOneBuffer(BufferDescriptorGetBuffer(bufHdr)); + LWLockRelease(bufHdr->content_lock); + } + + UnpinBuffer(bufHdr, true); + } +} + +/* + * BgHintBuffers -- Set hint bits where possible for a pinned and locked buffer. + * + * Buffer must be pinned and content-locked by caller. + * + * TODO: Fill this in. + */ +void +HintOneBuffer(Buffer buffer) { } diff --git a/src/include/storage/bufmgr.h b/src/include/storage/bufmgr.h index ee52cbf4fd..fc3c0b177d 100644 --- a/src/include/storage/bufmgr.h +++ b/src/include/storage/bufmgr.h @@ -216,6 +216,7 @@ extern void BufmgrCommit(void); extern void BgBufferSync(void); extern void BgHintBuffers(void); +extern void HintOneBuffer(Buffer buffer); extern void AtProcExit_LocalBuffers(void); -- 2.39.5