我对bufmgr.c 里的机理的粗浅理解

  1. 云栖社区>
  2. 博客>
  3. 正文

我对bufmgr.c 里的机理的粗浅理解

嗯哼9925 2017-12-20 13:47:00 浏览687
展开阅读全文

开始

bufmgr.c 中,对strategy_passes 与 next_passes 有判断处理的逻辑,

复制代码
        if ((int32) (next_passes - strategy_passes) > 0)
        {
            /* we're one pass ahead of the strategy point */
            bufs_to_lap = strategy_buf_id - next_to_clean;
#ifdef BGW_DEBUG
            elog(DEBUG2, "bgwriter ahead: bgw %u-%u strategy %u-%u delta=%ld lap=%d",
                 next_passes, next_to_clean,
                 strategy_passes, strategy_buf_id,
                 strategy_delta, bufs_to_lap);
#endif
        }
        else if (next_passes == strategy_passes &&
                 next_to_clean >= strategy_buf_id)
        {
            /* on same pass, but ahead or at least not behind */
            bufs_to_lap = NBuffers - (next_to_clean - strategy_buf_id);
#ifdef BGW_DEBUG
            elog(DEBUG2, "bgwriter ahead: bgw %u-%u strategy %u-%u delta=%ld lap=%d",
                 next_passes, next_to_clean,
                 strategy_passes, strategy_buf_id,
                 strategy_delta, bufs_to_lap);
#endif
        }
        else
        {
            /*
             * We're behind, so skip forward to the strategy point and start
             * cleaning from there.
             */
#ifdef BGW_DEBUG
            elog(DEBUG2, "bgwriter behind: bgw %u-%u strategy %u-%u delta=%ld",
                 next_passes, next_to_clean,
                 strategy_passes, strategy_buf_id,
                 strategy_delta);
#endif
            next_to_clean = strategy_buf_id;
            next_passes = strategy_passes;
            bufs_to_lap = NBuffers;
        }
复制代码

那么,到底运行的时候,会发生什么呢?

我给代码加上调试语句,看看 strategy_passes 和 next_passes 的值都是多少。

复制代码
    if (saved_info_valid)
    {
fprintf(stderr,"stragey_passes is %u ",stragegy_passes);
fprintf(stderr,"...next_passes is %u \n",next_passes);
int32 passes_delta
= strategy_passes - prev_strategy_passes; strategy_delta = strategy_buf_id - prev_strategy_buf_id; strategy_delta += (long) passes_delta *NBuffers; Assert(strategy_delta >= 0); if ((int32) (next_passes - strategy_passes) > 0) { ...... } else if (next_passes == strategy_passes && next_to_clean >= strategy_buf_id) { ...... } else { ...... } }
复制代码

还有

复制代码
    /* Execute the LRU scan */
    while (num_to_scan > 0 && reusable_buffers < upcoming_alloc_est)
    {
        int            buffer_state = SyncOneBuffer(next_to_clean, true);

        if (++next_to_clean >= NBuffers)
        {
            next_to_clean = 0;
fprintf(stderr,"----------next_passes++\n"); next_passes
++; } num_to_scan--; if (buffer_state & BUF_WRITTEN) { reusable_buffers++; if (++num_written >= bgwriter_lru_maxpages) { BgWriterStats.m_maxwritten_clean++; break; } } else if (buffer_state & BUF_REUSABLE) reusable_buffers++; }
复制代码

运行的结果

复制代码
strategy_passes is:0  ....next_passes is:0  
strategy_passes is:0  ....next_passes is:0  
strategy_passes is:0  ....next_passes is:0  
strategy_passes is:0  ....next_passes is:0  
strategy_passes is:0  ....next_passes is:0  
strategy_passes is:0  ....next_passes is:0  
strategy_passes is:0  ....next_passes is:0  
strategy_passes is:0  ....next_passes is:0  
strategy_passes is:0  ....next_passes is:0  
strategy_passes is:0  ....next_passes is:0  
strategy_passes is:0  ....next_passes is:0  
strategy_passes is:0  ....next_passes is:0  
strategy_passes is:0  ....next_passes is:0  
------------------next_passes++.
strategy_passes is:0  ....next_passes is:1  
strategy_passes is:0  ....next_passes is:1  
strategy_passes is:0  ....next_passes is:1  
strategy_passes is:0  ....next_passes is:1  
复制代码

刚运行的时候,哗哗出一堆 strategy_passes is:0  ....next_passes is:0

后来出来的行就慢一些,大概几秒一个。经过了 next_passes++ 的运算以后,next_passes 开始增长了。

通过监控可知,在 stragegy_passes = next_passes =0 的时间段里,进入 next_passes++ 之前,各个值如下:

 

next_passes next_to_clean strategy_passses strategy_buf_id strategy_delta bufs_to_lap
0 4080 0 0 0 16
0 4086 0 0 0 10
0 4092 0 0 0 4








本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2012/11/01/2749250.html,如需转载请自行联系原作者

网友评论

登录后评论
0/500
评论
嗯哼9925
+ 关注