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

Linux内存管理之slab机制(初始化)

[日期:2012-01-11] 来源:Linux社区  作者:bullbat [字体:大 中 小]

辅助数据结构与变量

Linux内核中将所有的通用cache以不同的大小存放在数组中,以方便查找。其中malloc_sizes[]数组为cache_sizes类型的数组,存放各个cache的大小;cache_names[]数组为cache_names结构类型数组,存放各个cache大小的名称;malloc_sizes[]数组和cache_names[]数组下标对应,也就是说cache_names[i]名称的cache对应的大小为malloc_sizes[i]。

  1. /* Size description struct for general caches. */  
  2. struct cache_sizes {  
  3.     size_t          cs_size;  
  4.     struct kmem_cache   *cs_cachep;  
  5. #ifdef CONFIG_ZONE_DMA   
  6.     struct kmem_cache   *cs_dmacachep;  
  7. #endif   
  8. };  
  9. /* 
  10.  * These are the default caches for kmalloc. Custom caches can have other sizes. 
  11.  */  
  12. struct cache_sizes malloc_sizes[] = {  
  13. #define CACHE(x) { .cs_size = (x) },   
  14. #include <linux/kmalloc_sizes.h>   
  15.     CACHE(ULONG_MAX)  
  16. #undef CACHE   
  17. };  
 
  1. /* Must match cache_sizes above. Out of line to keep cache footprint low. */  
  2. struct cache_names {  
  3.     char *name;  
  4.     char *name_dma;  
  5. };  
  6.   
  7. static struct cache_names __initdata cache_names[] = {  
  8. #define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },   
  9. #include <linux/kmalloc_sizes.h>   
  10.     {NULL,}  
  11. #undef CACHE   
  12. };  

二、内核启动末期初始化

1,根据对象大小计算local cache中对象数目上限;

2,借助数据结构ccupdate_struct操作cpu本地cache。为每个在线cpu分配cpu本地cache;

3,用新分配的cpu本地cache替换原有的cache;

4,更新slab三链以及cpu本地共享cache。

第二阶段代码分析

Start_kernel()->kmem_cache_init_late()

 
  1. /*Slab系统初始化分两个部分,先初始化一些基本的,待系统初始化工作进行的差不多时,再配置一些特殊功能。*/  
  2. void __init kmem_cache_init_late(void)  
  3. {  
  4.     struct kmem_cache *cachep;  
  5.     /* 初始化阶段local cache的大小是固定的,要根据对象大小重新计算 */  
  6.     /* 6) resize the head arrays to their final sizes */  
  7.     mutex_lock(&cache_chain_mutex);  
  8.     list_for_each_entry(cachep, &cache_chain, next)  
  9.         if (enable_cpucache(cachep, GFP_NOWAIT))  
  10.             BUG();  
  11.     mutex_unlock(&cache_chain_mutex);  
  12.   
  13.     /* Done! */  
  14.     /* 大功告成,general cache终于全部建立起来了 */  
  15.     g_cpucache_up = FULL;  
  16.   
  17.     /* Annotate slab for lockdep -- annotate the malloc caches */  
  18.     init_lock_keys();  
  19.   
  20.     /* 
  21.      * Register a cpu startup notifier callback that initializes 
  22.      * cpu_cache_get for all new cpus 
  23.      */  
  24.      /* 注册cpu up回调函数,cpu up时配置local cache */  
  25.     register_cpu_notifier(&cpucache_notifier);  
  26.   
  27.     /* 
  28.      * The reap timers are started later, with a module init call: That part 
  29.      * of the kernel is not yet operational. 
  30.      */  
  31. }  
 
  1. /* Called with cache_chain_mutex held always */  
  2. /*local cache 初始化*/  
  3. static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)  
  4. {  
  5.     int err;  
  6.     int limit, shared;  
  7.   
  8.     /* 
  9.      * The head array serves three purposes: 
  10.      * - create a LIFO ordering, i.e. return objects that are cache-warm 
  11.      * - reduce the number of spinlock operations. 
  12.      * - reduce the number of linked list operations on the slab and 
  13.      *   bufctl chains: array operations are cheaper. 
  14.      * The numbers are guessed, we should auto-tune as described by 
  15.      * Bonwick. 
  16.      */ /* 根据对象大小计算local cache中对象数目上限 */  
  17.     if (cachep->buffer_size > 131072)  
  18.         limit = 1;  
  19.     else if (cachep->buffer_size > PAGE_SIZE)  
  20.         limit = 8;  
  21.     else if (cachep->buffer_size > 1024)  
  22.         limit = 24;  
  23.     else if (cachep->buffer_size > 256)  
  24.         limit = 54;  
  25.     else  
  26.         limit = 120;  
  27.   
  28.     /* 
  29.      * CPU bound tasks (e.g. network routing) can exhibit cpu bound 
  30.      * allocation behaviour: Most allocs on one cpu, most free operations 
  31.      * on another cpu. For these cases, an efficient object passing between 
  32.      * cpus is necessary. This is provided by a shared array. The array 
  33.      * replaces Bonwick's magazine layer. 
  34.      * On uniprocessor, it's functionally equivalent (but less efficient) 
  35.      * to a larger limit. Thus disabled by default. 
  36.      */  
  37.     shared = 0;  
  38.     /* 多核系统,设置shared local cache中对象数目 */  
  39.     if (cachep->buffer_size <= PAGE_SIZE && num_possible_cpus() > 1)  
  40.         shared = 8;  
  41.   
  42. #if DEBUG   
  43.     /* 
  44.      * With debugging enabled, large batchcount lead to excessively long 
  45.      * periods with disabled local interrupts. Limit the batchcount 
  46.      */  
  47.     if (limit > 32)  
  48.         limit = 32;  
  49. #endif   
  50.     /* 配置local cache */  
  51.     err = do_tune_cpucache(cachep, limit, (limit + 1) / 2, shared, gfp);  
  52.     if (err)  
  53.         printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",  
  54.                cachep->name, -err);  
  55.     return err;  
  56. }  
 
  1. /* Always called with the cache_chain_mutex held */  
  2. /*配置local cache、shared local cache和slab三链*/  
  3. static int do_tune_cpucache(struct kmem_cache *cachep, int limit,  
  4.                 int batchcount, int shared, gfp_t gfp)  
  5. {  
  6.     struct ccupdate_struct *new;  
  7.     int i;  
  8.   
  9.     new = kzalloc(sizeof(*new), gfp);  
  10.     if (!new)  
  11.         return -ENOMEM;  
  12.     /* 为每个cpu分配新的struct array_cache对象 */  
  13.     for_each_online_cpu(i) {  
  14.         new->new[i] = alloc_arraycache(cpu_to_node(i), limit,  
  15.                         batchcount, gfp);  
  16.         if (!new->new[i]) {  
  17.             for (i--; i >= 0; i--)  
  18.                 kfree(new->new[i]);  
  19.             kfree(new);  
  20.             return -ENOMEM;  
  21.         }  
  22.     }  
  23.     new->cachep = cachep;  
  24.     /* 用新的struct array_cache对象替换旧的struct array_cache对象 
  25.     ,在支持cpu热插拔的系统上,离线cpu可能没有释放local cache 
  26.     ,使用的仍是旧local cache,参见__kmem_cache_destroy函数 
  27.     。虽然cpu up时要重新配置local cache,也无济于事。考虑下面的情景 
  28.     :共有Cpu A和Cpu B,Cpu B down后,destroy Cache X,由于此时Cpu B是down状态 
  29.     ,所以Cache X中Cpu B的local cache未释放,过一段时间Cpu B又up了 
  30.     ,更新cache_chain 链中所有cache的local cache,但此时Cache X对象已经释放回 
  31.     cache_cache中了,其Cpu B local cache并未被更新。又过了一段时间 
  32.     ,系统需要创建新的cache,将Cache X对象分配出去,其Cpu B仍然是旧的 
  33.     local cache,需要进行更新。 
  34.     */  
  35.     on_each_cpu(do_ccupdate_local, (void *)new, 1);  
  36.   
  37.     check_irq_on();  
  38.     cachep->batchcount = batchcount;  
  39.     cachep->limit = limit;  
  40.     cachep->shared = shared;  
  41.     /* 释放旧的local cache */  
  42.     for_each_online_cpu(i) {  
  43.         struct array_cache *ccold = new->new[i];  
  44.         if (!ccold)  
  45.             continue;  
  46.         spin_lock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);  
  47.         /* 释放旧local cache中的对象 */  
  48.         free_block(cachep, ccold->entry, ccold->avail, cpu_to_node(i));  
  49.         spin_unlock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);  
  50.         /* 释放旧的struct array_cache对象 */  
  51.         kfree(ccold);  
  52.     }  
  53.     kfree(new);  
  54.     /* 初始化shared local cache 和slab三链 */  
  55.     return alloc_kmemlist(cachep, gfp);  
  56. }  
linux
相关资讯       Linux内存 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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