Chinaunix首页 | 论坛 | 博客
  • 博客访问: 318058
  • 博文数量: 78
  • 博客积分: 1322
  • 博客等级: 中尉
  • 技术积分: 680
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-14 13:24
文章分类
文章存档

2012年(20)

2011年(55)

2010年(3)

分类: LINUX

2011-10-10 23:19:53

 

  1. /*
  2.  * Calculate the number of objects and left-over bytes for a given buffer size.
  3.  */
  4. /*内核提供了计算一个Slab缓存的大小的函数,Slab缓存的内存布局分成以下两种:
     *1. Slab缓存管理区在Slab内,即同对象存放在一起。
     *2. Slab缓存管理区存放在单独的一片区域,即同对象分别存放。
     *通过宏CFLGS_OFF_SLAB来区分。

     *参数介绍:
     * gfporder: Slab缓存大小为2^gfporder个页面
     * buffer_size: 每个对象的大小
     * align:对齐
     * flags:CFLGS_OFF_SLAB表示Slab管理区在缓存外,否则表示在缓存内。
     * left_over:输出参数,在一个Slab中被浪费的大小。
     * num: 输出参数,一个Slab中,对象的数量。

  5.  */

  6. static void cache_estimate(unsigned long gfporder, size_t buffer_size,
  7.       size_t align, int flags, size_t *left_over,
  8.       unsigned int *num)
  9. {
  10.  int nr_objs;
  11.  size_t mgmt_size;
  12.  size_t slab_size = PAGE_SIZE << gfporder;
  13.  /*
  14.   * The slab management structure can be either off the slab or
  15.   * on it. For the latter case, the memory allocated for a
  16.   * slab is used for:
  17.   *
  18.   * - The struct slab
  19.   * - One kmem_bufctl_t for each object
  20.   * - Padding to respect alignment of @align
  21.   * - @buffer_size bytes for each object
  22.   *
  23.   * If the slab management structure is off the slab, then the
  24.   * alignment will already be calculated into the size. Because
  25.   * the slabs are all pages aligned, the objects will be at the
  26.   * correct alignment when allocated.
  27.   */
  28.  /*如果slab描述符在外部,那么对象数为slab所占内存页长度除以对象大小*/
  29.  if (flags & CFLGS_OFF_SLAB) {
  30.   mgmt_size = 0;
  31.   nr_objs = slab_size / buffer_size
  32.  /*如果在一个Slab中对象数量,超过一定数量,将对象数重置为最大值*/
  33.  if (nr_objs > SLAB_LIMIT)
  34.    nr_objs = SLAB_LIMIT;    
  35.  } else {
  36.   /*
  37.    * Ignore padding for the initial guess. The padding
  38.    * is at most @align-1 bytes, and @buffer_size is at
  39.    * least @align. In the worst case, this result will
  40.    * be one greater than the number of objects that fit
  41.    * into the memory allocation when taking the padding
  42.    * into account.
  43.    */
  44.   /*如果slab描述符在内部,那么对象数为slab所占内存页减去
  45.     slab描述符的大小,然后除以对象大小加上每个对象的偏移量*/
  46.   nr_objs = (slab_size - sizeof(struct slab)) /
  47.      (buffer_size + sizeof(kmem_bufctl_t));  
  48.   /*
  49.    * This calculated number will be either the right
  50.    * amount, or one greater than what we want.
  51.    */
  52.   /*调用slab_mgmt_size()调整slab中第一个对象的地址对其align,
  53.     然后如果调整后的长度超过slab所占内存页的大小,减少slab中
  54.     对象的个数*/
  55.   if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
  56.          > slab_size)
  57.    nr_objs--;
  58.   if (nr_objs > SLAB_LIMIT)
  59.    nr_objs = SLAB_LIMIT;
  60.   /*再次调整slab中第一个对象的地址对其align*/
  61.   mgmt_size = slab_mgmt_size(nr_objs, align);
  62.  }
  63.  *num = nr_objs;
  64.  *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
  65. }
阅读(1504) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~