Memslab can be divided into three layers from the structure,
1 The topmost layer is some cache queue,
2 Each cache queue also contains three slab queues,
3 each slab manages a page
We first introduce the overall structure of memslab from these three layers.
1 Cache Queue
The top-level cache-related queue has
- Ikmem_array: Pointing to a different size cache
- Ikmem_lookup: Arrays of caches in order from large to small
- Ikmem_size_lookup1: Size in 4 intervals (range from 4 to 1024), each pointing to the first cache greater than its size
- IKMEM_SIZE_LOOKUP2: Size at 1k (range from 1k to 256k), each pointing to the first cache greater than its size
- Ikmem_large_ptr: Storing pages larger than 128k
1.1 Ikmem_array and Ikmem_lookup arrays
Memslab is used to store the cache structure is the Ikmem_array array and ikmem_lookup array, the two arrays are placed in a size of sizelimit size of memory, size_limit with Ikmem_ The size of the array varies,
In Ikmem_init, create 2^16, 2^15 ... 2^3 a total of 14 cache,ikmem_array and ikmem_lookup all point to these caches, except that the caches in the ikmem_lookup are arranged in order from large to small
1.2 Ikmem_size_lookup1 and ikmem_size_lookup2 arrays
Ikmem_size_looup1: Size in 4 intervals (range from 4 to 1024), each pointing to the first cache greater than its size
IKMEM_SIZE_LOOKUP2: Size at 1k (range from 1k to 256k), each pointing to the first cache greater than its size, which is larger than the size of 2^16 points to null
2 Cache
The more important structure in the cache is the imemlru_t array, where a entry is placed in the slab object that was just returned by the application, and the objects in the slab are returned and not returned directly to slab, but placed first in the array
And the list of three slab: slabs_partial, Slabs_full, Slabs_free, slabs_partial objects in slabs are partially allocated, Slabs_full objects are all allocated, Slabs_ The objects in free are not assigned at all
3 Slab
Each slab manages a page, according to obj_size divides the page into Obj1, obj2 ..., when unassigned, each object starts at the address of the next object, and Bufctl points to the head of the list to be allocated.
Allocation algorithm, is a single-linked list to manage the page space, each allocation of the head pointer Bufctl point to the page, the Bufctl point to the next object, the object is applied release returned, the object is added to the list header, similar principle
The main functions are Ikmem_init, Ikmem_malloc and Ikmem_free, and we'll start with malloc and free and take a look at the process:
1 Ikmem_malloc:
When allocating, if you need to allocate a size less than 1024, first look in Ikmem_size_lookup1, otherwise find the cache in Ikmem_size_lookup2,
If there is no cache of the same size in Ikmem_size_lookup1 and ikmem_size_lookup2, the first cache that is larger than size is found in the Ikmem_lookup
When the required size is greater than the size of the previously prepared 2^16 page, malloc a page and joins the Ikmem_large_ptr queue
If the desired cache is found in ikmem_size_lookup1 or in ikmem_size_lookup2, call the Imemcache_alloc function and assign an object from the cache
1.1 Ikmem_malloc->imemcache_alloc:
The program will first look at the array in the cache there are no spare objects, the cache of the array is just released from the application of the page, the newly released page will not immediately put back to its own slab, and will be placed in the array, and other just released pages form a linked list, The benefit is to avoid assigning pages every time you go to slab, and also to adjust the queue that the slab belongs to.
If the cached object in the array is empty, the imemcache_fill_batch is called, a Batchcount object is allocated from the slab queue to the array, and the object is returned
The size of the objects allocated from the cache is added to the total allocated size Ikmem_inuse
2 Ikmem_free:
First, if the object being allocated is an object on a large page, the object is removed from the ikmem_large_ptr queue and the object is free
Otherwise, call Imemcache_free to release the object and, after releasing it, subtract the released size from the Ikmem_inuse.
2.2 Ikmem_free, Imemcache_free:
The released page will be placed in the array first,
If the number of objects in the array exceeds the size of the Array->limit, the program calls Imemcache_list_free, and the object is put back into slab, and the program adjusts the slab queue where the slab is located, such as the original Slab_ Slab in free, because the object is returned, it will be adjusted to slab_partial
If the free slab in the cache exceeds a certain number (Cache->free_limit), the cache calls Imemcache_drain_list to release half of the idle objects
Memslab Source Reading Summary