手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux Slab分配器(四)--分配对象

[日期:2012-06-17] 来源:Linux社区  作者:vanbreaker [字体: ]

从一个缓存中分配对象总是遵循下面的原则:

1.本地高速缓存中是否有空闲对象,如果有的话则从其中获取对象,这时分配的对象是最“热”的;

2.如果本地高速缓存中没有对象,则从kmem_list3中的slab链表中寻找空闲对象并填充到本地高速缓存再分配;

3.如果所有的slab中都没有空闲对象了,那么就要创建新的slab,再分配 。

函数kmem_cache_alloc用于从特定的缓存获取对象,kmalloc用于从普通缓存中获取对象,它们的执行流程如下图所示

实质性的工作是从____cache_alloc()开始的,因此从这个函数作为入口来分析

  1. static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)  
  2. {  
  3.     void *objp;  
  4.     struct array_cache *ac;  
  5.   
  6.     check_irq_off();  
  7.   
  8.     /*获取缓存的本地高速缓存的描述符array_cache*/  
  9.     ac = cpu_cache_get(cachep);  
  10.   
  11.     /*如果本地高速缓存中还有空闲对象可以分配则从本地高速缓存中分配*/  
  12.     if (likely(ac->avail)) {  
  13.         STATS_INC_ALLOCHIT(cachep);  
  14.         ac->touched = 1;  
  15.         /*先将avail的值减1,这样avail对应的空闲对象是最热的,即最近释放出来的, 
  16.           更有可能驻留在CPU高速缓存中*/  
  17.         objp = ac->entry[--ac->avail];  
  18.     } else {/*否则需要填充本地高速缓存*/  
  19.         STATS_INC_ALLOCMISS(cachep);  
  20.         objp = cache_alloc_refill(cachep, flags);  
  21.     }  
  22.     /* 
  23.      * To avoid a false negative, if an object that is in one of the 
  24.      * per-CPU caches is leaked, we need to make sure kmemleak doesn't 
  25.      * treat the array pointers as a reference to the object. 
  26.      */  
  27.     kmemleak_erase(&ac->entry[ac->avail]);  
  28.     return objp;  
  29. }  
 
  1. static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)  
  2. {  
  3.     int batchcount;  
  4.     struct kmem_list3 *l3;  
  5.     struct array_cache *ac;  
  6.     int node;  
  7.   
  8. retry:  
  9.     check_irq_off();  
  10.     node = numa_node_id();  
  11.     ac = cpu_cache_get(cachep);  
  12.     batchcount = ac->batchcount;  /*获取批量转移的数目*/  
  13.     if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {  
  14.         /* 
  15.          * If there was little recent activity on this cache, then 
  16.          * perform only a partial refill.  Otherwise we could generate 
  17.          * refill bouncing. 
  18.          */  
  19.         batchcount = BATCHREFILL_LIMIT;  
  20.     }  
  21.     /*获取kmem_list3*/  
  22.     l3 = cachep->nodelists[node];  
  23.   
  24.     BUG_ON(ac->avail > 0 || !l3);  
  25.     spin_lock(&l3->list_lock);  
  26.   
  27.     /* See if we can refill from the shared array */  
  28.     /*如果有共享本地高速缓存,则从共享本地高速缓存填充*/  
  29.     if (l3->shared && transfer_objects(ac, l3->shared, batchcount))  
  30.         goto alloc_done;  
  31.   
  32.     while (batchcount > 0) {  
  33.         struct list_head *entry;  
  34.         struct slab *slabp;  
  35.         /* Get slab alloc is to come from. */  
  36.         /*扫描slab链表,先从partial链表开始,如果整个partial链表都无法找到batchcount个空闲对象, 
  37.         再扫描free链表*/  
  38.         entry = l3->slabs_partial.next;  
  39.   
  40.         /*entry回到表头说明partial链表已经扫描完毕,开始扫描free链表*/  
  41.         if (entry == &l3->slabs_partial) {  
  42.             l3->free_touched = 1;  
  43.             entry = l3->slabs_free.next;  
  44.             if (entry == &l3->slabs_free)  
  45.                 goto must_grow;  
  46.         }  
  47.   
  48.         /*由链表项得到slab描述符*/  
  49.         slabp = list_entry(entry, struct slab, list);  
  50.         check_slabp(cachep, slabp);  
  51.         check_spinlock_acquired(cachep);  
  52.   
  53.         /* 
  54.          * The slab was either on partial or free list so 
  55.          * there must be at least one object available for 
  56.          * allocation. 
  57.          */  
  58.         BUG_ON(slabp->inuse >= cachep->num);  
  59.   
  60.         /*如果slabp中还存在空闲对象并且还需要继续填充对象到本地高速缓存*/  
  61.         while (slabp->inuse < cachep->num && batchcount--) {  
  62.             STATS_INC_ALLOCED(cachep);  
  63.             STATS_INC_ACTIVE(cachep);  
  64.             STATS_SET_HIGH(cachep);  
  65.   
  66.             /*填充的本质就是用ac后面的void*数组元素指向一个空闲对象*/  
  67.             ac->entry[ac->avail++] = slab_get_obj(cachep, slabp,  
  68.                                 node);  
  69.         }  
  70.         check_slabp(cachep, slabp);  
  71.   
  72.         /* move slabp to correct slabp list: */  
  73.         /*由于从slab中分配出去了对象,因此有可能需要将slab移到其他链表中去*/  
  74.         list_del(&slabp->list);  
  75.         /*free等于BUFCTL_END表示空闲对象已耗尽,将slab插入full链表*/  
  76.         if (slabp->free == BUFCTL_END)  
  77.             list_add(&slabp->list, &l3->slabs_full);  
  78.         else/*否则肯定是插入partial链表*/  
  79.             list_add(&slabp->list, &l3->slabs_partial);  
  80.     }  
  81.   
  82. must_grow:  
  83.     l3->free_objects -= ac->avail;/*刷新kmem_list3中的空闲对象*/  
  84. alloc_done:  
  85.     spin_unlock(&l3->list_lock);  
  86.   
  87.     /*avail为0表示kmem_list3中的slab全部处于full状态或者没有slab,则要为缓存分配slab*/  
  88.     if (unlikely(!ac->avail)) {  
  89.         int x;  
  90.         x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);  
  91.   
  92.         /* cache_grow can reenable interrupts, then ac could change. */  
  93.         ac = cpu_cache_get(cachep);  
  94.         if (!x && ac->avail == 0)    /* no objects in sight? abort */  
  95.             return NULL;  
  96.   
  97.         if (!ac->avail)      /* objects refilled by interrupt? */  
  98.             goto retry;  
  99.     }  
  100.     ac->touched = 1;  
  101.     /*返回最后一个末端的对象*/  
  102.     return ac->entry[--ac->avail];  
  103. }  

对于所有slab都空闲对象的情况,需要调用cache_grow()来增加cache的容量,这个函数在后面分析slab的分配时再做介绍。

相关阅读:

Linux Slab分配器(一)--概述 http://www.linuxidc.com/Linux/2012-06/62965.htm
Linux Slab分配器(二)--初始化 http://www.linuxidc.com/Linux/2012-06/62966.htm
Linux Slab分配器(三)--创建缓存 http://www.linuxidc.com/Linux/2012-06/63109.htm
Linux Slab分配器(五)--释放对象 http://www.linuxidc.com/Linux/2012-06/63167.htm

linux
相关资讯       Linux内存管理 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网���内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款