辅助操作
1,slab三链初始化
- static void kmem_list3_init(struct kmem_list3 *parent)
- {
- INIT_LIST_HEAD(&parent->slabs_full);
- INIT_LIST_HEAD(&parent->slabs_partial);
- INIT_LIST_HEAD(&parent->slabs_free);
- parent->shared = NULL;
- parent->alien = NULL;
- parent->colour_next = 0;
- spin_lock_init(&parent->list_lock);
- parent->free_objects = 0;
- parent->free_touched = 0;
- }
2,slab三链静态数据初始化
- /*设置cache的slab三链指向静态分配的全局变量*/
- static void __init set_up_list3s(struct kmem_cache *cachep, int index)
- {
- int node;
- /* UMA只有一个节点 */
- for_each_online_node(node) {
- /* 全局变量initkmem_list3是初始化阶段使用的slab三链 */
- cachep->nodelists[node] = &initkmem_list3[index + node];
- /* 设置回收时间 */
- cachep->nodelists[node]->next_reap = jiffies +
- REAPTIMEOUT_LIST3 +
- ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
- }
- }
3,计算每个slab中对象的数目
- /*
- * Calculate the number of objects and left-over bytes for a given buffer size.
- */
- /*计算每个slab中对象的数目。*/
- /*
- 1) gfporder:slab由2gfporder个页面组成。
- 2) buffer_size:对象的大小。
- 3) align:对象的对齐方式。
- 4) flags:内置式slab还是外置式slab。
- 5) left_over:slab中浪费空间的大小。
- 6) num:slab中的对象数目。
- */
- static void cache_estimate(unsigned long gfporder, size_t buffer_size,
- size_t align, int flags, size_t *left_over,
- unsigned int *num)
- {
- int nr_objs;
- size_t mgmt_size;
- /* slab大小为1<<order个页面 */
- size_t slab_size = PAGE_SIZE << gfporder;
- /*
- * The slab management structure can be either off the slab or
- * on it. For the latter case, the memory allocated for a
- * slab is used for:
- *
- * - The struct slab
- * - One kmem_bufctl_t for each object
- * - Padding to respect alignment of @align
- * - @buffer_size bytes for each object
- *
- * If the slab management structure is off the slab, then the
- * alignment will already be calculated into the size. Because
- * the slabs are all pages aligned, the objects will be at the
- * correct alignment when allocated.
- */
- if (flags & CFLGS_OFF_SLAB) {
- /* 外置式slab */
- mgmt_size = 0;
- /* slab页面不含slab管理对象,全部用来存储slab对象 */
- nr_objs = slab_size / buffer_size;
- /* 对象数不能超过上限 */
- if (nr_objs > SLAB_LIMIT)
- nr_objs = SLAB_LIMIT;
- } else {
- /*
- * Ignore padding for the initial guess. The padding
- * is at most @align-1 bytes, and @buffer_size is at
- * least @align. In the worst case, this result will
- * be one greater than the number of objects that fit
- * into the memory allocation when taking the padding
- * into account.
- *//* 内置式slab,slab管理对象与slab对象在一起
- ,此时slab页面中包含:一个struct slab对象,一个kmem_bufctl_t数组,slab对象。
- kmem_bufctl_t数组大小与slab对象数目相同 */
- nr_objs = (slab_size - sizeof(struct slab)) /
- (buffer_size + sizeof(kmem_bufctl_t));
- /*
- * This calculated number will be either the right
- * amount, or one greater than what we want.
- *//* 计算cache line对齐后的大小,如果超出了slab总的大小,则对象数减一 */
- if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
- > slab_size)
- nr_objs--;
- if (nr_objs > SLAB_LIMIT)
- nr_objs = SLAB_LIMIT;
- /* 计算cache line对齐后slab管理对象的大小 */
- mgmt_size = slab_mgmt_size(nr_objs, align);
- }
- *num = nr_objs;/* 保存slab对象数目 */
- /* 计算浪费空间的大小 */
- *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
- }
