• 对PostgreSQL中bufmgr.c 中 num_to_scan 的初步理解


    开始

    把BgBufferSync 的代码内容简略化,得到:

    bool                    
    BgBufferSync(void)                    
    {                    
        ……                
                        
        /* Used to compute how far we scan ahead */                
        long        strategy_delta;        
        int            bufs_to_lap;    
        ……                
        /* Variables for the scanning loop proper */                
        int            num_to_scan;    
        int            num_written;    
        int            reusable_buffers;    
        ……                
        if (saved_info_valid)                
        {                
            ……            
                        
            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;        
                ……        
            }            
            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);        
                ……        
            }            
            else            
            {            
                ……        
                bufs_to_lap = NBuffers;        
            }            
                        
        }                
        else                
        {                
            ……            
            bufs_to_lap = NBuffers;            
        }                
        ……                
                        
        /*                
         * Now write out dirty reusable buffers, working forward from the                
         * next_to_clean point, until we have lapped the strategy scan, or cleaned                
         * enough buffers to match our estimate of the next cycle's allocation                
         * requirements, or hit the bgwriter_lru_maxpages limit.                
         */                
                        
        ……                
        num_to_scan = bufs_to_lap;                
        ……                
        /* Execute the LRU scan */                
        while (num_to_scan > 0 && reusable_buffers < upcoming_alloc_est)                
        {                
                        
            //added by gaojian            
            fprintf(stderr,"num_to_scan is: %d \n",num_to_scan);            
                        
            int    buffer_state = SyncOneBuffer(next_to_clean, true);        
                        
            if (++next_to_clean >= NBuffers)            
            {            
                next_to_clean = 0;        
                        
                elog(INFO,"------------------next_passes++.\n");        
                next_passes++;        
            }            
            num_to_scan--;            
            ……            
        }                
                        
        ……                
        /* Return true if OK to hibernate */                
        return (bufs_to_lap == 0 && recent_alloc == 0);                
    }                    

    一开始的时候,bufs_to_lap =4096, 就是 4096*8K=32MB ,相当于 Shared_buffers 的数量。

    然后如果对代码进行跟踪,可以发现 bufs_to_lap 一开始就是 4096,然后逐次减1。

    [作者:技术者高健@博客园  mail: luckyjackgao@gmail.com ]

    结束

  • 相关阅读:
    php面向对象之构造函数和析构函数
    C#语言基础原理及优缺点
    零零散散学算法之具体解释几种最短路径
    Java解惑七:很多其它类之谜
    《Linux设备驱动开发具体解释(第3版)》进展同步更新
    setsockopt的作用
    全排列算法及实现
    【ASP.NET】怎样使用类创建公共函数,在不同ASP.NET页面间反复调用
    Git经常使用命令以及使用方法
    Ansi,UTF8,Unicode,ASCII编码的差别
  • 原文地址:https://www.cnblogs.com/gaojian/p/2750961.html
Copyright © 2020-2023  润新知