我对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; }
那么,到底运行的时候,会发生什么呢?
我给代码加上调试语句,看看 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 |
[作者:技术者高健@博客园 mail: luckyjackgao@gmail.com ]
结束