postgresql 的buffer 的clock sweep 算法

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;

 

/* Nothing on the freelist, so run the "clock sweep" algorithm */
trycounter = NBuffers;
for (;;)
{
   buf = &BufferDescriptors[StrategyControl->nextVictimBuffer];

   if (++StrategyControl->nextVictimBuffer >= NBuffers)
   {
    StrategyControl->nextVictimBuffer = 0;
    StrategyControl->completePasses++;
   }

   /*
   * If the buffer is pinned or has a nonzero usage_count, we cannot use
   * it; decrement the usage_count (unless pinned) and keep scanning.
   */
   LockBufHdr(buf);
   if (buf->refcount == 0)
   {
    if (buf->usage_count > 0)
    {
     buf->usage_count--;
     trycounter = NBuffers;
    }
    else
    {
     /* Found a usable buffer */
     if (strategy != NULL)
      AddBufferToRing(strategy, buf);
     return buf;
    }
   }
   else if (--trycounter == 0)
   {
    /*
    * We've scanned all the buffers without making any state changes,
    * so all the buffers are pinned (or were when we looked at them).
    * We could hope that someone will free one eventually, but it's
    * probably better to fail than to risk getting stuck in an
    * infinite loop.
    */
    UnlockBufHdr(buf);
    elog(ERROR, "no unpinned buffers available");
   }
   UnlockBufHdr(buf);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值