Linux中的冷热页机制简述

2019-10-13 21:40:53王振洲

怎样分配冷热页 

在分配order为0页的时候(冷热页机制只处理单页分配的情况),先找到合适的zone,然后根据需要的migratetype类型定位冷热页链表(每个zone,对于每个cpu,有3条冷热页链表,对应于:MIGRATE_UNMOVABLE、MIGRATE_RECLAIMABLE、MIGRATE_MOVABLE)。若需要热页,则从链表头取下一页(此页最“热”);若需要冷页,则从链表尾取下一页(此页最“冷”)。 

分配函数(关键部分已添加注释):

 /*
 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
 * we cheat by calling it from here, in the order > 0 path. Saves a branch
 * or two.
 */
static inline
struct page *buffered_rmqueue(struct zone *preferred_zone,
   struct zone *zone, int order, gfp_t gfp_flags,
   int migratetype)
{
 unsigned long flags;
 struct page *page;
 //分配标志是__GFP_COLD才分配冷页
 int cold = !!(gfp_flags & __GFP_COLD);
again:
 if (likely(order == 0)) {
  struct per_cpu_pages *pcp;
  struct list_head *list;
  local_irq_save(flags);
  pcp = &this_cpu_ptr(zone->pageset)->pcp;
  list = &pcp->lists[migratetype];
  if (list_empty(list)) {
   //如果缺少页,则从Buddy System中分配。
   pcp->count += rmqueue_bulk(zone, 0,
     pcp->batch, list,
     migratetype, cold);
   if (unlikely(list_empty(list)))
    goto failed;
  }
  if (cold)
  //分配冷页时,从链表尾部分配,list为链表头,list->prev表示链表尾
   page = list_entry(list->prev, struct page, lru);
  else
  //分配热页时,从链表头分配
   page = list_entry(list->next, struct page, lru);
  //分配完一个页框后从冷热页链表中删去该页
  list_del(&page->lru);
  pcp->count--;
 } else {//如果order!=0(页框数>1),则不从冷热页链表中分配
  if (unlikely(gfp_flags & __GFP_NOFAIL)) {
   /*
    * __GFP_NOFAIL is not to be used in new code.
    *
    * All __GFP_NOFAIL callers should be fixed so that they
    * properly detect and handle allocation failures.
    *
    * We most definitely don't want callers attempting to
    * allocate greater than order-1 page units with
    * __GFP_NOFAIL.
    */
   WARN_ON_ONCE(order > 1);
  }
  spin_lock_irqsave(&zone->lock, flags);
  page = __rmqueue(zone, order, migratetype);
  spin_unlock(&zone->lock);
  if (!page)
   goto failed;
  __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))
  goto again;
 return page;
failed:
 local_irq_restore(flags);
 return NULL;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易采站长站。