Linux中的冷热页机制简述
什么是冷热页?
在LinuxKernel的物理内存管理的BuddySystem中,引入了冷热页的概念。冷页表示该空闲页已经不再高速缓存中了(一般是指L2Cache),热页表示该空闲页仍然在高速缓存中。冷热页是针对于每CPU的,每个zone中,都会针对于所有的CPU初始化一个冷热页的per-cpu-pageset.
为什么要有冷热页?
作用有3点:
BuddyAllocator在分配order为0的空闲页的时候,如果分配一个热页,那么由于该页已经存在于L2Cache中了。CPU写访问的时候,不需要先把内存中的内容读到Cache中,然后再写。如果分配一个冷页,说明该页不在L2Cache中。一般情况下,尽可能用热页,是容易理解的。什么时候用冷页呢?Whileallocatingaphysicalpageframe,thereisabitspecifyingwhetherwewouldlikeahotoracoldpage(thatis,apagelikelytobeintheCPUcache,orapagenotlikelytobethere).IfthepagewillbeusedbytheCPU,ahotpagewillbefaster.IfthepagewillbeusedfordeviceDMAtheCPUcachewouldbeinvalidatedanyway,andacoldpagedoesnotwastepreciouscachecontents.
简单翻译一下:当内核分配一个物理页框时,有一些规范来约束我们是分配热页还是冷页。当页框是CPU使用的,则分配热页。当页框是DMA设备使用的,则分配冷页。因为DMA设备不会用到CPU高速缓存,所以没必要使用热页。
BuddySystem在给某个进程分配某个zone中空闲页的时候,首先需要用自旋锁锁住该zone,然后分配页。这样,如果多个CPU上的进程同时进行分配页,便会竞争。引入了per-cpu-set后,当多个CPU上的进程同时分配页的时候,竞争便不会发生,提高了效率。另外当释放单个页面时,空闲页面首先放回到per-cpu-pageset中,以减少zone中自旋锁的使用。当页面缓存中的页面数量超过阀值时,再将页面放回到伙伴系统中。
使用每CPU冷热页还有一个好处是,能保证某个页一直黏在1个CPU上,这有助于提高Cache的命中率。
冷热页的数据结构
structper_cpu_pages{ intcount;//numberofpagesinthelist inthigh;//highwatermark,emptyingneeded intbatch;//chunksizeforbuddyadd/remove //Listsofpages,onepermigratetypestoredonthepcp-lists 每个CPU在每个zone上都有MIGRATE_PCPTYPES个冷热页链表(根据迁移类型划分) structlist_headlists[MIGRATE_PCPTYPES]; };
在Linux中,对于UMA的架构,冷热页是在一条链表上进行管理。热页在前,冷页在后。CPU每释放一个order为0的页,如果per-cpu-pageset中的页数少于其指定的阈值,便会将释放的页插入到冷热页链表的开始处。这样,之前插入的热页便会随着其后热页源源不断的插入向后移动,其页由热变冷的几率便大大增加。
怎样分配冷热页
在分配order为0页的时候(冷热页机制只处理单页分配的情况),先找到合适的zone,然后根据需要的migratetype类型定位冷热页链表(每个zone,对于每个cpu,有3条冷热页链表,对应于:MIGRATE_UNMOVABLE、MIGRATE_RECLAIMABLE、MIGRATE_MOVABLE)。若需要热页,则从链表头取下一页(此页最“热”);若需要冷页,则从链表尾取下一页(此页最“冷”)。
分配函数(关键部分已添加注释):
/* *Really,prep_compound_page()shouldbecalledfrom__rmqueue_bulk().But *wecheatbycallingitfromhere,intheorder>0path.Savesabranch *ortwo. */ staticinline structpage*buffered_rmqueue(structzone*preferred_zone, structzone*zone,intorder,gfp_tgfp_flags, intmigratetype) { unsignedlongflags; structpage*page; //分配标志是__GFP_COLD才分配冷页 intcold=!!(gfp_flags&__GFP_COLD); again: if(likely(order==0)){ structper_cpu_pages*pcp; structlist_head*list; local_irq_save(flags); pcp=&this_cpu_ptr(zone->pageset)->pcp; list=&pcp->lists[migratetype]; if(list_empty(list)){ //如果缺少页,则从BuddySystem中分配。 pcp->count+=rmqueue_bulk(zone,0, pcp->batch,list, migratetype,cold); if(unlikely(list_empty(list))) gotofailed; } if(cold) //分配冷页时,从链表尾部分配,list为链表头,list->prev表示链表尾 page=list_entry(list->prev,structpage,lru); else //分配热页时,从链表头分配 page=list_entry(list->next,structpage,lru); //分配完一个页框后从冷热页链表中删去该页 list_del(&page->lru); pcp->count--; }else{//如果order!=0(页框数>1),则不从冷热页链表中分配 if(unlikely(gfp_flags&__GFP_NOFAIL)){ /* *__GFP_NOFAILisnottobeusedinnewcode. * *All__GFP_NOFAILcallersshouldbefixedsothatthey *properlydetectandhandleallocationfailures. * *Wemostdefinitelydon'twantcallersattemptingto *allocategreaterthanorder-1pageunitswith *__GFP_NOFAIL. */ WARN_ON_ONCE(order>1); } spin_lock_irqsave(&zone->lock,flags); page=__rmqueue(zone,order,migratetype); spin_unlock(&zone->lock); if(!page) gotofailed; __mod_zone_page_state(zone,NR_FREE_PAGES,-(1<<order)); } __count_zone_vm_events(PGALLOC,zone,1<<order); zone_statistics(preferred_zone,zone,gfp_flags); local_irq_restore(flags); VM_BUG_ON(bad_range(zone,page)); if(prep_new_page(page,order,gfp_flags)) gotoagain; returnpage; failed: local_irq_restore(flags); returnNULL; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。