最近看到一个patch,当系统free内存碎片率较高时,通过计算当前系统的内存碎片率来启动lmk来杀进程,以达到释放内存的目的。内存碎片率的概念是相对某个size的连续free内存块而言的,其值从0~1000,值越大,说明内存碎片率越高。
内存碎片率的计算函数位于vmstat.c的函数中,具体如下:
int fragmentation_index(struct zone *zone, unsigned int order)
{
struct contig_page_info info;
fill_contig_page_info(zone, order, &info);//初始化当前zone下的contig_page_info结构体
return __fragmentation_index(order, &info);//计算当前zone相对于某个order下的内存碎片率
}
主要涉及到2个函数fill_contig_page_info()和__fragmentation_index():
static void fill_contig_page_info(struct zone *zone,
unsigned int suitable_order,
struct contig_page_info *info)
{
unsigned int order;
info->free_pages = 0;//当前zone下的free page个数
info->free_blocks_total = 0;//当前zone下free page block的个数,反映在buddyinfo里面就是每个order值的下的block个数相加
info->free_blocks_suitable = 0;//相对于suitable_order而言,当前zone