现在的位置: 首页 > 综合 > 正文

初读SLAB

2013年09月06日 ⁄ 综合 ⁄ 共 4379字 ⁄ 字号 评论关闭
 

 
快乐虾
http://blog.csdn.net/lights_joy/
lights@hb165.com
  
 
本文适用于
ADI bf561 DSP
uclinux-2008r1-rc8 (移植到vdsp5)
Visual DSP++ 5.0
 
欢迎转载,但请保留作者信息
 

1.1.1   基本思想

uclinux 所使用的 slab 分配器的基础是 Jeff Bonwick SunOS 操作系统首次引入的一种算法Jeff 的分配器是围绕对象缓存进行的。在内核中,会为有限的对象集(例如文件描述符和其他常见结构)分配大量内存。Jeff 发现对内核中普通对象进行初始化所需的时间超过了对其进行分配和释放所需的时间。因此他的结论是不应该将内存释放回一个全局的内存池,而是将内存保持为针对特定目而初始化的状态。例如,如果内存被分配给了一个互斥锁,那么只需在为互斥锁首次分配内存时执行一次互斥锁初始化函数(mutex_init)即可。后续的内存分配不需要执行这个初始化函数,因为从上次释放和调用析构之后,它已经处于所需的状态中了。

uclinux slab 分配器使用了这种思想和其他一些思想来构建一个在空间和时间上都具有高效性的内存分配器。

下图给出了 slab 结构的高层组织结构。在最高层是 cache_chain,这是一个 slab 缓存的链接列表。这对于 best-fit 算法非常有用,可以用来查找最适合所需要的分配大小的缓存(遍历列表)。cache_chain 的每个元素都是一个 kmem_cache 结构的引用(称为一个 cache)。它定义了一个要管理的给定大小的对象池。

cache


每个缓存都包含了一个 slabs 列表,这是一段连续的内存块(通常都是页面)。存在 3 slab

slabs_full完全分配的 slab

slabs_partial部分分配的 slab

slabs_empty slab,或者没有对象被分配

注意 slabs_empty 列表中的 slab 是进行回收(reaping)的主要备选对象。正是通过此过程,slab 所使用的内存被返回给操作系统供其他用户使用。

slab 列表中的每个 slab 都是一个连续的内存块(一个或多个连续页),它们被划分成一个个对象。这些对象是从特定缓存中进行分配和释放的基本元素。注意 slab slab 分配器进行操作的最小分配单位,因此如果需要对 slab 进行扩展,这也就是所扩展的最小值。通常来说,每个 slab 被分配为多个对象。

由于对象是从 slab 中进行分配和释放的,因此单个 slab 可以在 slab 列表之间进行移动。例如,当一个 slab 中的所有对象都被使用完时,就从 slabs_partial 列表中移动到 slabs_full 列表中。当一个 slab 完全被分配并且有对象被释放后,就从 slabs_full 列表中移动到 slabs_partial 列表中。当所有对象都被释放之后,就从 slabs_partial 列表移动到 slabs_empty 列表中。

与传统的内存管理模式相比, slab 缓存分配器提供了很多优点。首先,内核通常依赖于对小对象的分配,它们会在系统生命周期内进行无数次分配。slab 缓存分配器通过对类似大小的对象进行缓存而提供这种功能,从而避免了常见的碎片问题。slab 分配器还支持通用对象的初始化,从而避免了为同一目而对一个对象重复进行初始化。最后,slab 分配器还可以支持硬件缓存对齐和着色,这允许不同缓存中的对象占用相同的缓存行,从而提高缓存的利用率并获得更好的性能。

1.1.2   相关数据结构

1.1.2.1             array_cache

这个结构体的定义在mm/slab.c中:

/*

 * struct array_cache

 *

 * Purpose:

 * - LIFO ordering, to hand out cache-warm objects from _alloc

 * - reduce the number of linked list operations

 * - reduce spinlock operations

 *

 * The limit is stored in the per-cpu structure to reduce the data cache

 * footprint.

 *

 */

struct array_cache {

     unsigned int avail;

     unsigned int limit;

     unsigned int batchcount;

     unsigned int touched;

     spinlock_t lock;

     void *entry[0];    /*

               * Must have this definition in here for the proper

               * alignment of array_cache. Also simplifies accessing

               * the entries.

               * [0] is for gcc 2.95. It should really be [].

               */

};

l         entry

这个成员存放了要分配给每个对象的空间。

l         avail

表示在entry中还有多少个对象可供使用。

 

 

1.1.2.2             arraycache_init

这个结构体的定义在mm/slab.c

/*

 * bootstrap: The caches do not work without cpuarrays anymore, but the

 * cpuarrays are allocated from the generic caches...

 */

#define BOOT_CPUCACHE_ENTRIES    1

struct arraycache_init {

     struct array_cache cache;

     void *entries[BOOT_CPUCACHE_ENTRIES];

};

 

 

 

1.1.2.3             kmem_list3

这个结构体的定义在mm/slab.c中:

/*

 * The slab lists for all objects.

 */

struct kmem_list3 {

     struct list_head slabs_partial;  /* partial list first, better asm code */

     struct list_head slabs_full;

     struct list_head slabs_free;

     unsigned long free_objects;

     unsigned int free_limit;

     unsigned int colour_next;   /* Per-node cache coloring */

     spinlock_t list_lock;

     struct array_cache *shared; /* shared per node */

     struct array_cache **alien; /* on other nodes */

     unsigned long next_reap;    /* updated without locking */

     int free_touched;      /* updated without locking */

};

这个结构体存放了三个不同类型的SLAB链表,partialfullfree

 

1.1.2.4             kmem_cache

这个结构体的定义在mm/slab.c

/*

 * struct kmem_cache

 *

 * manages a cache.

 */

 

struct kmem_cache {

/* 1) per-cpu data, touched during every alloc/free */

     struct array_cache *array[NR_CPUS];

/* 2) Cache tunables. Protected by cache_chain_mutex */

     unsigned int batchcount;

     unsigned int limit;

     unsigned int shared;

 

     unsigned int buffer_size;

     u32 reciprocal_buffer_size;

/* 3) touched by every alloc & free from the backend */

 

     unsigned int flags;         /* constant flags */

     unsigned int num;      /* # of objs per slab */

 

/* 4) cache_grow/shrink */

     /* order of pgs per slab (2^n) */

     unsigned int gfporder;

 

     /* force GFP flags, e.g. GFP_DMA */

     gfp_t gfpflags;

 

     size_t colour;              /* cache colouring range */

     unsigned int colour_off;    /* colour offset */

     struct kmem_cache *slabp_cache;

     unsigned int slab_size;

     unsigned int dflags;        /* dynamic flags */

 

     /* constructor func */

     void (*ctor) (void *, struct kmem_cache *, unsigned long);

 

/* 5) cache creation/removal */

     const char *name;

     struct list_head next;

 

     /*

      * We put nodelists[] at the end of kmem_cache, because we want to size

      * this array to nr_node_ids slots instead of MAX_NUMNODES

      * (see kmem_cache_init())

      * We still use [MAX_NUMNODES] and not [1] or [0] because cache_cache

      * is statically defined, so we reserve the max number of nodes.

      */

     struct kmem_list3 *nodelists[MAX_NUMNODES];

     /*

      * Do not add fields after nodelists[]

抱歉!评论已关闭.