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

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

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

辅助操作

1,slab三链初始化

  1. static void kmem_list3_init(struct kmem_list3 *parent)  
  2. {  
  3.     INIT_LIST_HEAD(&parent->slabs_full);  
  4.     INIT_LIST_HEAD(&parent->slabs_partial);  
  5.     INIT_LIST_HEAD(&parent->slabs_free);  
  6.     parent->shared = NULL;  
  7.     parent->alien = NULL;  
  8.     parent->colour_next = 0;  
  9.     spin_lock_init(&parent->list_lock);  
  10.     parent->free_objects = 0;  
  11.     parent->free_touched = 0;  
  12. }  

2,slab三链静态数据初始化

  1.  /*设置cache的slab三链指向静态分配的全局变量*/  
  2. static void __init set_up_list3s(struct kmem_cache *cachep, int index)  
  3. {  
  4.     int node;  
  5.     /* UMA只有一个节点 */  
  6.     for_each_online_node(node) {  
  7.         /* 全局变量initkmem_list3是初始化阶段使用的slab三链 */  
  8.         cachep->nodelists[node] = &initkmem_list3[index + node];  
  9.         /* 设置回收时间 */  
  10.         cachep->nodelists[node]->next_reap = jiffies +  
  11.             REAPTIMEOUT_LIST3 +  
  12.             ((unsigned long)cachep) % REAPTIMEOUT_LIST3;  
  13.     }  
  14. }  

3,计算每个slab中对象的数目

  1. /* 
  2.  * Calculate the number of objects and left-over bytes for a given buffer size. 
  3.  */  
  4.  /*计算每个slab中对象的数目。*/  
  5.  /* 
  6. 1)        gfporder:slab由2gfporder个页面组成。 
  7. 2)        buffer_size:对象的大小。 
  8. 3)        align:对象的对齐方式。 
  9. 4)        flags:内置式slab还是外置式slab。 
  10. 5)        left_over:slab中浪费空间的大小。 
  11. 6)        num:slab中的对象数目。 
  12. */  
  13. static void cache_estimate(unsigned long gfporder, size_t buffer_size,  
  14.                size_t align, int flags, size_t *left_over,  
  15.                unsigned int *num)  
  16. {  
  17.     int nr_objs;  
  18.     size_t mgmt_size;  
  19.     /* slab大小为1<<order个页面 */  
  20.     size_t slab_size = PAGE_SIZE << gfporder;  
  21.   
  22.     /* 
  23.      * The slab management structure can be either off the slab or 
  24.      * on it. For the latter case, the memory allocated for a 
  25.      * slab is used for: 
  26.      * 
  27.      * - The struct slab 
  28.      * - One kmem_bufctl_t for each object 
  29.      * - Padding to respect alignment of @align 
  30.      * - @buffer_size bytes for each object 
  31.      * 
  32.      * If the slab management structure is off the slab, then the 
  33.      * alignment will already be calculated into the size. Because 
  34.      * the slabs are all pages aligned, the objects will be at the 
  35.      * correct alignment when allocated. 
  36.      */  
  37.     if (flags & CFLGS_OFF_SLAB) {  
  38.         /* 外置式slab */  
  39.         mgmt_size = 0;  
  40.         /* slab页面不含slab管理对象,全部用来存储slab对象 */  
  41.         nr_objs = slab_size / buffer_size;  
  42.         /* 对象数不能超过上限 */  
  43.         if (nr_objs > SLAB_LIMIT)  
  44.             nr_objs = SLAB_LIMIT;  
  45.     } else {  
  46.         /* 
  47.          * Ignore padding for the initial guess. The padding 
  48.          * is at most @align-1 bytes, and @buffer_size is at 
  49.          * least @align. In the worst case, this result will 
  50.          * be one greater than the number of objects that fit 
  51.          * into the memory allocation when taking the padding 
  52.          * into account. 
  53.          *//* 内置式slab,slab管理对象与slab对象在一起 
  54.          ,此时slab页面中包含:一个struct slab对象,一个kmem_bufctl_t数组,slab对象。 
  55.          kmem_bufctl_t数组大小与slab对象数目相同 */  
  56.         nr_objs = (slab_size - sizeof(struct slab)) /  
  57.               (buffer_size + sizeof(kmem_bufctl_t));  
  58.   
  59.         /* 
  60.          * This calculated number will be either the right 
  61.          * amount, or one greater than what we want. 
  62.          *//* 计算cache line对齐后的大小,如果超出了slab总的大小,则对象数减一 */  
  63.         if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size  
  64.                > slab_size)  
  65.             nr_objs--;  
  66.   
  67.         if (nr_objs > SLAB_LIMIT)  
  68.             nr_objs = SLAB_LIMIT;  
  69.         /* 计算cache line对齐后slab管理对象的大小 */  
  70.         mgmt_size = slab_mgmt_size(nr_objs, align);  
  71.     }  
  72.     *num = nr_objs;/* 保存slab对象数目 */  
  73.     /* 计算浪费空间的大小 */  
  74.     *left_over = slab_size - nr_objs*buffer_size - mgmt_size;  
  75. }  
linux
相关资讯       Linux内存 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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