Linux Slub分配器(三)--创建缓存

来源:互联网 发布:instantiates java 编辑:程序博客网 时间:2024/05/21 19:28

水平有限,描述不当之处还请之处,转载请注明出处http://blog.csdn.net/vanbreaker/article/details/7700338

      

      Slub分配器创建缓存的过程和Slab差不多,主要的区别在于Slub分配器并不是直接创建一个新的缓存,而是先试图在已有的缓存中找到一个各方面属性和待创建缓存差不多的缓存,如果能找到的话则不会去创建新缓存,而是复用这个已有的缓存,以提高缓存的利用率。      

[cpp] view plaincopy
  1. <span style="font-size:12px;">struct kmem_cache *kmem_cache_create(const char *name, size_t size,  
  2.         size_t align, unsigned long flags, void (*ctor)(void *))  
  3. {  
  4.     struct kmem_cache *s;  
  5.   
  6.     if (WARN_ON(!name))  
  7.         return NULL;  
  8.   
  9.     down_write(&slub_lock);  
  10.     /*先试图从已有的缓存中找到一个可以满足要求的进行复用,这样就不用创建新的缓存了*/  
  11.     s = find_mergeable(size, align, flags, name, ctor);  
  12.     if (s) {//找到了可以复用的缓存  
  13.         int cpu;  
  14.   
  15.         /*缓存的引用计数加1,表示缓存中多了一种对象*/  
  16.         s->refcount++;  
  17.         /* 
  18.          * Adjust the object sizes so that we clear 
  19.          * the complete object on kzalloc. 
  20.          */  
  21.          /*在新对象大小和原有对象大小中取较大者作为缓存的对象大小*/  
  22.         s->objsize = max(s->objsize, (int)size);  
  23.   
  24.         /* 
  25.          * And then we need to update the object size in the 
  26.          * per cpu structures 
  27.          */  
  28.         for_each_online_cpu(cpu)//更新每CPU结构中的对象大小值  
  29.             get_cpu_slab(s, cpu)->objsize = s->objsize;  
  30.   
  31.         s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));  
  32.         up_write(&slub_lock);  
  33.   
  34.         if (sysfs_slab_alias(s, name)) {  
  35.             down_write(&slub_lock);  
  36.             s->refcount--;  
  37.             up_write(&slub_lock);  
  38.             goto err;  
  39.         }  
  40.         return s;  
  41.     }  
  42.     /*没有找到可以复用的缓存,下面的步骤用来新创建一个缓存*/  
  43.       
  44.     /*分配kmem_size大小用来存储s*/  
  45.     s = kmalloc(kmem_size, GFP_KERNEL);  
  46.     if (s) {  
  47.         /*根据参数设置s中的各个变量完成初始化*/  
  48.         if (kmem_cache_open(s, GFP_KERNEL, name,  
  49.                 size, align, flags, ctor)) {  
  50.             list_add(&s->list, &slab_caches);//将缓存添加到slab_caches中  
  51.             up_write(&slub_lock);  
  52.             if (sysfs_slab_add(s)) {//将缓存添加到sysfs  
  53.                 down_write(&slub_lock);  
  54.                 list_del(&s->list);  
  55.                 up_write(&slub_lock);  
  56.                 kfree(s);  
  57.                 goto err;  
  58.             }  
  59.             return s;  
  60.         }  
  61.         kfree(s);  
  62.     }  
  63.     up_write(&slub_lock);  
  64.   
  65. err:  
  66.     if (flags & SLAB_PANIC)  
  67.         panic("Cannot create slabcache %s\n", name);  
  68.     else  
  69.         s = NULL;  
  70.     return s;  
  71. }</span>  


find_mergeable()用来寻找一个可以复用的缓存,它会考察已有缓存和带创建的缓存在各方面是否匹配

[cpp] view plaincopy
  1. <span style="font-size:12px;">static struct kmem_cache *find_mergeable(size_t size,  
  2.         size_t align, unsigned long flags, const char *name,  
  3.         void (*ctor)(void *))  
  4. {  
  5.     struct kmem_cache *s;  
  6.   
  7.     /*设置了slub_nomerge或待创建的缓存的标识位中标明了不进行复用,则不查找复用缓存*/  
  8.     if (slub_nomerge || (flags & SLUB_NEVER_MERGE))  
  9.         return NULL;  
  10.   
  11.     /*待创建缓存指定了构造函数,则不查找复用缓存*/  
  12.     if (ctor)  
  13.         return NULL;  
  14.   
  15.     /*确定待创建缓存的各个属性*/  
  16.     size = ALIGN(size, sizeof(void *));  
  17.     align = calculate_alignment(flags, align, size);  
  18.     size = ALIGN(size, align);  
  19.     flags = kmem_cache_flags(size, flags, name, NULL);  
  20.   
  21.     /*遍历slab_caches中的缓存*/  
  22.     list_for_each_entry(s, &slab_caches, list) {  
  23.             /*缓存不允许复用则放弃该缓存*/  
  24.         if (slab_unmergeable(s))  
  25.             continue;  
  26.   
  27.         /*缓存的对象占用内存小于要求的size,则放弃该缓存*/  
  28.         if (size > s->size)  
  29.             continue;  
  30.   
  31.         /*相关标识不一致,放弃该缓存*/  
  32.         if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))  
  33.                 continue;  
  34.         /* 
  35.          * Check if alignment is compatible. 
  36.          * Courtesy of Adrian Drzewiecki 
  37.          */  
  38.         /*缓存给对象分配的内存大小和对齐值不相符,则放弃该缓存*/  
  39.         if ((s->size & ~(align - 1)) != s->size)  
  40.             continue;  
  41.                 /*缓存给对象分配的内存大小比指定的size要多出1个字长以上,则放弃该缓存*/  
  42.         if (s->size - size >= sizeof(void *))  
  43.             continue;  
  44.   
  45.         return s;//以上条件都不满足则表示找到了符合条件的缓存,可以进行复用,返回之  
  46.     }  
  47.     return NULL;  
  48. }  
  49. </span>  


 

再来看一个kmem_cache_create()中比较关键的函数--kmem_cache_open()

[cpp] view plaincopy
  1. static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,  
  2.         const char *name, size_t size,  
  3.         size_t align, unsigned long flags,  
  4.         void (*ctor)(void *))  
  5. {  
  6.     memset(s, 0, kmem_size);  
  7.   
  8.     /*设置各个变量*/  
  9.     s->name = name;  
  10.     s->ctor = ctor;  
  11.     s->objsize = size;  
  12.     s->align = align;  
  13.     s->flags = kmem_cache_flags(size, flags, name, ctor);  
  14.   
  15.     /*计算和大小相关的各项数值*/  
  16.     if (!calculate_sizes(s, -1))  
  17.         goto error;  
  18.     if (disable_higher_order_debug) {  
  19.         /* 
  20.          * Disable debugging flags that store metadata if the min slab 
  21.          * order increased. 
  22.          */  
  23.         if (get_order(s->size) > get_order(s->objsize)) {  
  24.             s->flags &= ~DEBUG_METADATA_FLAGS;  
  25.             s->offset = 0;  
  26.             if (!calculate_sizes(s, -1))  
  27.                 goto error;  
  28.         }  
  29.     }  
  30.   
  31.     /* 
  32.      * The larger the object size is, the more pages we want on the partial 
  33.      * list to avoid pounding the page allocator excessively. 
  34.      */  
  35.      /*根据对象大小计算partial slab链表中slab的最小数目,size越大,slab数目越大, 
  36.        以免过度使用页框分配器*/  
  37.     set_min_partial(s, ilog2(s->size));  
  38.     s->refcount = 1;//对象种类数为1  
  39. #ifdef CONFIG_NUMA  
  40.     s->remote_node_defrag_ratio = 1000;  
  41. #endif  
  42.     /*初始化struct kmem_cache_node,对于NUMA架构要先分配kmem_cache_node*/  
  43.     if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))  
  44.         goto error;  
  45.   
  46.     /*初始化struct kmem_cache_cpu,对于SMP系统要先分配kmem_cache_cpu*/  
  47.     if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))  
  48.         return 1;  
  49.     free_kmem_cache_nodes(s);  
  50. error:  
  51.     if (flags & SLAB_PANIC)  
  52.         panic("Cannot create slab %s size=%lu realsize=%u "  
  53.             "order=%u offset=%u flags=%lx\n",  
  54.             s->name, (unsigned long)size, s->size, oo_order(s->oo),  
  55.             s->offset, flags);  
  56.     return 0;  
  57. }  


 

[cpp] view plaincopy
  1. static int calculate_sizes(struct kmem_cache *s, int forced_order)  
  2. {  
  3.     unsigned long flags = s->flags;  
  4.     unsigned long size = s->objsize;  
  5.     unsigned long align = s->align;  
  6.     int order;  
  7.   
  8.     /* 
  9.      * Round up object size to the next word boundary. We can only 
  10.      * place the free pointer at word boundaries and this determines 
  11.      * the possible location of the free pointer. 
  12.      */  
  13.      //先将size按照字长进行对齐,以便访问空闲指针  
  14.     size = ALIGN(size, sizeof(void *));  
  15.   
  16. #ifdef CONFIG_SLUB_DEBUG  
  17.     /* 
  18.      * Determine if we can poison the object itself. If the user of 
  19.      * the slab may touch the object after free or before allocation 
  20.      * then we should never poison the object itself. 
  21.      */  
  22.     if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&  
  23.             !s->ctor)  
  24.         s->flags |= __OBJECT_POISON;  
  25.     else  
  26.         s->flags &= ~__OBJECT_POISON;  
  27.   
  28.   
  29.     /* 
  30.      * If we are Redzoning then check if there is some space between the 
  31.      * end of the object and the free pointer. If not then add an 
  32.      * additional word to have some bytes to store Redzone information. 
  33.      */  
  34.     if ((flags & SLAB_RED_ZONE) && size == s->objsize)  
  35.         size += sizeof(void *);  
  36. #endif  
  37.   
  38.     /* 
  39.      * With that we have determined the number of bytes in actual use 
  40.      * by the object. This is the potential offset to the free pointer. 
  41.      */  
  42.     s->inuse = size;//将inuse设置为size  
  43.   
  44.     if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||  
  45.         s->ctor)) {  
  46.         /* 
  47.          * Relocate free pointer after the object if it is not 
  48.          * permitted to overwrite the first word of the object on 
  49.          * kmem_cache_free. 
  50.          * 
  51.          * This is the case if we do RCU, have a constructor or 
  52.          * destructor or are poisoning the objects. 
  53.          */  
  54.         /*将offset设置为size,这样对象的后面就用来存放下一个空闲对象的指针*/  
  55.         s->offset = size;  
  56.         size += sizeof(void *);  
  57.     }  
  58.   
  59. #ifdef CONFIG_SLUB_DEBUG  
  60.     if (flags & SLAB_STORE_USER)  
  61.         /* 
  62.          * Need to store information about allocs and frees after 
  63.          * the object. 
  64.          */  
  65.         size += 2 * sizeof(struct track);  
  66.   
  67.     if (flags & SLAB_RED_ZONE)  
  68.         /* 
  69.          * Add some empty padding so that we can catch 
  70.          * overwrites from earlier objects rather than let 
  71.          * tracking information or the free pointer be 
  72.          * corrupted if a user writes before the start 
  73.          * of the object. 
  74.          */  
  75.         size += sizeof(void *);  
  76. #endif  
  77.   
  78.     /* 
  79.      * Determine the alignment based on various parameters that the 
  80.      * user specified and the dynamic determination of cache line size 
  81.      * on bootup. 
  82.      */  
  83.      /*计算对齐值*/  
  84.     align = calculate_alignment(flags, align, s->objsize);  
  85.     s->align = align;  
  86.   
  87.     /* 
  88.      * SLUB stores one object immediately after another beginning from 
  89.      * offset 0. In order to align the objects we have to simply size 
  90.      * each object to conform to the alignment. 
  91.      */  
  92.     /*根据计算出的对齐值重新进行对齐*/  
  93.     size = ALIGN(size, align);  
  94.     s->size = size;  
  95.   
  96.     /*如果指定了分配页面的阶数的话则将其作为缓存的页面分配阶数*/  
  97.     if (forced_order >= 0)  
  98.         order = forced_order;  
  99.     else/*否则的话要根据size进行计算*/  
  100.         order = calculate_order(size);  
  101.   
  102.     if (order < 0)  
  103.         return 0;  
  104.   
  105.     s->allocflags = 0;  
  106.     if (order)  
  107.         s->allocflags |= __GFP_COMP;  
  108.   
  109.     if (s->flags & SLAB_CACHE_DMA)  
  110.         s->allocflags |= SLUB_DMA;  
  111.   
  112.     if (s->flags & SLAB_RECLAIM_ACCOUNT)  
  113.         s->allocflags |= __GFP_RECLAIMABLE;  
  114.   
  115.     /* 
  116.      * Determine the number of objects per slab 
  117.      */  
  118.      /*用oo保存slab的页框阶数和对象数*/  
  119.     s->oo = oo_make(order, size);  
  120.     /*min保存了slab只有一个对象时对应的order和此时可以存放的对象数*/  
  121.     s->min = oo_make(get_order(size), size);  
  122.     if (oo_objects(s->oo) > oo_objects(s->max))  
  123.         s->max = s->oo;  
  124.   
  125.     return !!oo_objects(s->oo);  
  126.   
  127. }  


 

[cpp] view plaincopy
  1. static inline int calculate_order(int size)  
  2. {  
  3.     int order;  
  4.     int min_objects;  
  5.     int fraction;  
  6.     int max_objects;  
  7.   
  8.     /* 
  9.      * Attempt to find best configuration for a slab. This 
  10.      * works by first attempting to generate a layout with 
  11.      * the best configuration and backing off gradually. 
  12.      * 
  13.      * First we reduce the acceptable waste in a slab. Then 
  14.      * we reduce the minimum objects required in a slab. 
  15.      */  
  16.     /*slub_min_objects全局变量指定了slab中的最小对象数*/  
  17.     min_objects = slub_min_objects;  
  18.   
  19.     /*没有指定slab中的最小对象数则根据系统的CPU数进行计算*/  
  20.     if (!min_objects)  
  21.         min_objects = 4 * (fls(nr_cpu_ids) + 1);  
  22.   
  23.     /*slub_max_order全局变量指定了一个slab可以占用的最大页框阶数,根据该值 
  24.       计算出slab中的最大对象数*/  
  25.     max_objects = (PAGE_SIZE << slub_max_order)/size;  
  26.     min_objects = min(min_objects, max_objects);  
  27.   
  28.     /*两个while循环嵌套用来计算slab的阶数,当无法找到满足条件的order时, 
  29.     内循环用来减小fraction的值以放宽对碎片的要求,外循环用来减小min_objects以放宽对slab的 
  30.     最小对象数的要求*/  
  31.     while (min_objects > 1) {  
  32.         fraction = 16;/*fraction用来衡量对碎片的要求标准,该值越大,则允许的碎片越少!*/  
  33.         while (fraction >= 4) {//fraction不能小于4  
  34.             /*计算slab的页框阶数*/  
  35.             order = slab_order(size, min_objects,  
  36.                         slub_max_order, fraction);  
  37.             /*阶数不大于slub_max_order,则符合要求*/  
  38.             if (order <= slub_max_order)  
  39.                 return order;  
  40.             /*否则降低fraction,重新计算*/  
  41.             fraction /= 2;  
  42.         }  
  43.         min_objects--;  
  44.     }  
  45.   
  46.     /* 
  47.      * We were unable to place multiple objects in a slab. Now 
  48.      * lets see if we can place a single object there. 
  49.      */  
  50.     /*经过前面的步骤无法找到满足要求的order,那么只能选择存放1个对象,并且忽略碎片*/  
  51.     order = slab_order(size, 1, slub_max_order, 1);  
  52.     if (order <= slub_max_order)  
  53.         return order;  
  54.   
  55.     /* 
  56.      * Doh this slab cannot be placed using slub_max_order. 
  57.      */  
  58.      /*还不行的话,则将slab的order上限提升至MAX_ORDER,对应伙伴系统的最高分配阶*/  
  59.     order = slab_order(size, 1, MAX_ORDER, 1);  
  60.     if (order < MAX_ORDER)  
  61.         return order;  
  62.     return -ENOSYS;  
  63. }  


 

[cpp] view plaincopy
  1. static inline int slab_order(int size, int min_objects,  
  2.                 int max_order, int fract_leftover)  
  3. {  
  4.     int order;  
  5.     int rem;  
  6.     int min_order = slub_min_order;  
  7.   
  8.     /*MAX_OBJS_PER_PAGE定义了一个页面存储的最大对象数(65535),如果根据min_order和size计算出来的 
  9.      对象数超过了该值,则对象数取MAX_OBJS_PER_PAGE,再计算阶数*/  
  10.     if ((PAGE_SIZE << min_order) / size > MAX_OBJS_PER_PAGE)  
  11.         return get_order(size * MAX_OBJS_PER_PAGE) - 1;  
  12.   
  13.     /*从最小阶数开始向最高阶数遍历*/  
  14.     for (order = max(min_order,  
  15.                 fls(min_objects * size - 1) - PAGE_SHIFT);  
  16.             order <= max_order; order++) {  
  17.   
  18.         unsigned long slab_size = PAGE_SIZE << order;//计算出该阶数的slab大小  
  19.   
  20.         /*slab大小小于根据最小对象数和size计算出来的slab大小,不符合要求*/  
  21.         if (slab_size < min_objects * size)  
  22.             continue;  
  23.       
  24.         rem = slab_size % size;//计算出碎片大小  
  25.   
  26.         /*满足此关系式表示碎片在要求范围内,则该order可以满足要求, 
  27.           fract_leftover越小,则对碎片的要求越宽松*/  
  28.         if (rem <= slab_size / fract_leftover)  
  29.             break;  
  30.   
  31.     }  
  32.   
  33.     return order;  
  34. }  


至此,关于新缓存各项属性的计算工作已完成,接下来只需在kmem_cache_open()中调用init_kmem_cache_nodes()和alloc_kmem_cache_cpus()来初始化struct kmem_cache_node和struct kmem_cache_cpu结构即可,对于UMA系统,这两种结构都是直接在struct kmem_cache中定义的,因此只需直接初始化即可,而对于NUMA系统,还需为这些对象在普通缓存中申请空间,过程较为繁琐,在此不做介绍了!

0 0
原创粉丝点击