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

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

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

更新本地cache

  1. /*更新每个cpu的struct array_cache对象*/  
  2. static void do_ccupdate_local(void *info)  
  3. {  
  4.     struct ccupdate_struct *new = info;  
  5.     struct array_cache *old;  
  6.   
  7.     check_irq_off();  
  8.     old = cpu_cache_get(new->cachep);  
  9.      /* 指向新的struct array_cache对象 */  
  10.     new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];  
  11.         /* 保存旧的struct array_cache对象 */  
  12.     new->new[smp_processor_id()] = old;  
  13. }  
 
  1. /*初始化shared local cache和slab三链,初始化完成后,slab三链中没有任何slab*/  
  2. static int alloc_kmemlist(struct kmem_cache *cachep, gfp_t gfp)  
  3. {  
  4.     int node;  
  5.     struct kmem_list3 *l3;  
  6.     struct array_cache *new_shared;  
  7.     struct array_cache **new_alien = NULL;  
  8.   
  9.     for_each_online_node(node) {  
  10.          /* NUMA相关 */  
  11.                 if (use_alien_caches) {  
  12.                         new_alien = alloc_alien_cache(node, cachep->limit, gfp);  
  13.                         if (!new_alien)  
  14.                                 goto fail;  
  15.                 }  
  16.   
  17.         new_shared = NULL;  
  18.         if (cachep->shared) {  
  19.             /* 分配shared local cache */  
  20.             new_shared = alloc_arraycache(node,  
  21.                 cachep->shared*cachep->batchcount,  
  22.                     0xbaadf00d, gfp);  
  23.             if (!new_shared) {  
  24.                 free_alien_cache(new_alien);  
  25.                 goto fail;  
  26.             }  
  27.         }  
  28.         /* 获得旧的slab三链 */  
  29.         l3 = cachep->nodelists[node];  
  30.         if (l3) {  
  31.             /* 就slab三链指针不为空,需要先释放旧的资源 */  
  32.             struct array_cache *shared = l3->shared;  
  33.   
  34.             spin_lock_irq(&l3->list_lock);  
  35.             /* 释放旧的shared local cache中的对象 */  
  36.             if (shared)  
  37.                 free_block(cachep, shared->entry,  
  38.                         shared->avail, node);  
  39.             /* 指向新的shared local cache */  
  40.             l3->shared = new_shared;  
  41.             if (!l3->alien) {  
  42.                 l3->alien = new_alien;  
  43.                 new_alien = NULL;  
  44.             }/* 计算cache中空闲对象的上限 */  
  45.             l3->free_limit = (1 + nr_cpus_node(node)) *  
  46.                     cachep->batchcount + cachep->num;  
  47.             spin_unlock_irq(&l3->list_lock);  
  48.             /* 释放旧shared local cache的struct array_cache对象 */  
  49.             kfree(shared);  
  50.             free_alien_cache(new_alien);  
  51.             continue;/*访问下一个节点*/  
  52.         }  
  53.          /* 如果没有旧的l3,分配新的slab三链 */  
  54.         l3 = kmalloc_node(sizeof(struct kmem_list3), gfp, node);  
  55.         if (!l3) {  
  56.             free_alien_cache(new_alien);  
  57.             kfree(new_shared);  
  58.             goto fail;  
  59.         }  
  60.          /* 初始化slab三链 */  
  61.         kmem_list3_init(l3);  
  62.         l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +  
  63.                 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;  
  64.         l3->shared = new_shared;  
  65.         l3->alien = new_alien;  
  66.         l3->free_limit = (1 + nr_cpus_node(node)) *  
  67.                     cachep->batchcount + cachep->num;  
  68.         cachep->nodelists[node] = l3;  
  69.     }  
  70.     return 0;  
  71.   
  72. fail:  
  73.     if (!cachep->next.next) {  
  74.         /* Cache is not active yet. Roll back what we did */  
  75.         node--;  
  76.         while (node >= 0) {  
  77.             if (cachep->nodelists[node]) {  
  78.                 l3 = cachep->nodelists[node];  
  79.   
  80.                 kfree(l3->shared);  
  81.                 free_alien_cache(l3->alien);  
  82.                 kfree(l3);  
  83.                 cachep->nodelists[node] = NULL;  
  84.             }  
  85.             node--;  
  86.         }  
  87.     }  
  88.     return -ENOMEM;  
  89. }  

看一个辅助函数

  1. /*分配struct array_cache对象。*/  
  2. static struct array_cache *alloc_arraycache(int node, int entries,  
  3.                         int batchcount, gfp_t gfp)  
  4. {  
  5.     /* struct array_cache后面紧接着的是entry数组,合在一起申请内存 */  
  6.     int memsize = sizeof(void *) * entries + sizeof(struct array_cache);  
  7.     struct array_cache *nc = NULL;  
  8.     /* 分配一个local cache对象,kmalloc从general cache中分配 */  
  9.     nc = kmalloc_node(memsize, gfp, node);  
  10.     /* 
  11.      * The array_cache structures contain pointers to free object. 
  12.      * However, when such objects are allocated or transfered to another 
  13.      * cache the pointers are not cleared and they could be counted as 
  14.      * valid references during a kmemleak scan. Therefore, kmemleak must 
  15.      * not scan such objects. 
  16.      */  
  17.     kmemleak_no_scan(nc);  
  18.      /* 初始化local cache */  
  19.     if (nc) {  
  20.         nc->avail = 0;  
  21.         nc->limit = entries;  
  22.         nc->batchcount = batchcount;  
  23.         nc->touched = 0;  
  24.         spin_lock_init(&nc->lock);  
  25.     }  
  26.     return nc;  
  27. }  

源代码中涉及了slab的分配、释放等操作在后面分析中陆续总结。slab相关数据结构、工作机制以及整体框架在分析完了slab的创建、释放工作后再做总结,这样可能会对slab机制有更好的了解。当然,从代码中看运行机制会更有说服了,也是一种习惯。linux
相关资讯       Linux内存 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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