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

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

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;
}

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

 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

+ 关注