Linux memory management algorithms outlined the slab

slab allocation mechanism.

slab cache

Foundation slab allocator used in Linux is an algorithm Jeff Bonwick for the SunOS operating system was first introduced. Jeff distributor is around the object cache of. In the kernel will be limited to an object set (e.g., file descriptors and other common structures allocated amount of memory). Jeff ordinary objects found in the kernel initializes the time required to exceed the time required to be allocated and released . Therefore, he concluded that should not be released back into the memory of a global pool of memory , but the memory will remain for the specific purpose and initialized state . For example, if the memory is assigned to a mutex, you only need to do once mutex initialization function (mutex_init) can be mutually exclusive lock for the first time when allocating memory. No need to perform subsequent memory allocations this initialization function, because after release from the last and call the destructor, it is already in the desired state.

Linux slab allocator used this idea and other ideas to build in a space and time have efficient memory allocator.

Figure 1 shows a high-level organizational structure slab structure. At the top is cache_chain, which is a slab cache list of links. This is useful for best-fit algorithm can be used to find the most appropriate allocation of the required size of the cache (traversing the list). Each element is a reference kmem_cache cache_chain structure (referred to as a Cache ). It defines a given size to be managed object pool.

aaa

   

The main structure of the dispenser of FIG. 1. slab

 

Each cache contains a slabs  list, this is a contiguous memory blocks (usually page). There are three slab:

slabs_full 

Fully allocated slab

slabs_partial 

Part of the allocation of the slab

slabs_empty 

Empty slab, or no object is assigned

Note that the list slabs_empty slab is recovered (Reaping) the main alternative object . It is through this process, slab used memory is returned to the operating system available to other users.

slab list Each slab is a contiguous block of memory (one or more consecutive pages), which is divided into one object. These objects are allocated and released from a particular cache basic elements . Note slab  is a slab minimum allocation unit to operate the dispenser , so if you need to be extended slab, which is the minimum extension. Typically, each slab is allocated to a plurality of objects.

Since the object is allocated and released from the slab, so a single slab can be between slab list moves . For example, when a slab of all the objects have been used up when it slabs_partial from the list is moved to slabs_full  list . When a slab with a fully allocated and the object is released, is moved to the list from slabs_full slabs_partial list. Once all the objects are released, they move from slabs_partial list to slabs_empty list.

The motivation behind the slab

Compared with conventional memory management mode, slab cache allocator provides a number of advantages. First of all, the core generally rely on small objects allocated, they will be numerous distribution within the system life cycle . through the slab cache allocator of similar size object caching to provide this functionality, thus avoiding the common problem of fragmentation. The slab allocator also supports the initialization of common objects , thus avoiding for the same purpose to repeat an object is initialized . Finally, the slab dispenser may also support hardware cache alignment and coloring , which allows different cache objects occupy the same cache line , thereby improving cache utilization and better performance.

 

  Linux introduced in Slab main purpose is to reduce the number of partners algorithm calls .

  In fact, the kernel often repeated use of a memory area. For example, as long as the kernel creates a new process, it is necessary for the process-related data structures (task_struct, open the file object, etc.) to allocate memory area. When the end of the process, recovery of these memory areas. Because the process of creating and revocation very frequently, therefore, an earlier version of Linux to spend a lot of time in the repeated distribution or recovery of these memory areas. Linux2.2 from the beginning, those frequently used pages stored in the cache and re-use .

  They may be classified according to frequency of use of the memory area. For the intended frequently used memory area, you can create a set of specific size dedicated buffer for processing , in order to avoid the debris. For use less memory area, can create a set of universal buffer (e.g. Linux2.0 used two power-of) processed , even if chipping processing mode, but also has little effect on overall system performance .

  Hardware cache use, but also to minimize provide call partners algorithms another reason, because of the buddy algorithm of each call will be "dirty" hardware cache , therefore, increasing the average number of memory access .

  Slab allocation model to group objects into the buffer (Cache despite the use of the word in English, but actually refers to the memory area , rather than a hardware cache). Since shooting to organize and manage hardware cache buffers are closely related, therefore, Slab buffer consists of individual objects not directly, but by a series of "chunks ( Slab constitution)" , and each chunk in the It contains several different types of objects that have been assigned or, or idle , Fig. 6.12. In general, the object two, one large object is a small object. The so-called small objects , refers to several next page can contain the kind of object. For example, a plurality of inode structure approximately 300 bytes, and therefore, a page can contain more than 8 inode structure, therefore, it is the inode structure of small objects. Linux kernel to less than 512 bytes objects called small objects .

  In fact, the buffer is a main memory area, this area is divided into a plurality of sheet blocks, each block is a Slab , each Slab of one or more pages, and each Slab in the object is stored .

  Because achieve Slab allocation model is more complex, we are not going to be a detailed analysis, only to give a description of the main content.

Slab data structure

Slab allocation pattern has two main data structures, it is a description of the buffer structure of a kmem_cache_t, is a description of Slab structure kmem_slab_t, following a brief discussion of the two structures given:

(1)Slab

Slab Slab management model is the most basic structure. It consists of a set of consecutive physical page composition, the object is placed in the order of pages. Its data structure is defined in mm / slab.c in.

 

 

 

 

 

Slab schematic structure  


    Here the list for a front and rear Slab Slab a linked form a doubly linked list, colouroff Slab for the size of the colored region , the pointer points to s_mem start point of the object area , the inuse Slab is assigned as the number of objects . Finally, the value of free indicates the idle first object in the object chain, kmem_bufctl_t in fact, is an integer.

  For small objects, put Slab description on the structure slab_t Slab; for large objects, put Slab liberated structure, centralized storage. Slab on the coloring region and then give specific description:

    Each Slab header has a tiny area is not in use, called " colored area (coloring area)". The size of the colored region so Slab the start address of each object are by cache "cache line ( Cache Line )" to align the size (a cache line size of 16 bytes 80386, Pentium is 32 words section). Slab is because a page or multiple pages (up to 32), whereby at each Slab is from a page boundaries start , it naturally aligned cache buffer line. However, Slab size of the object is uncertain, a colored zone object is to Slab starting address of the first object is pushed back into the buffer line aligned position . Since there are a plurality of buffer Slab, therefore, it should be the size of each buffer in each of the colored regions Slab try to arrange a different size, so may that different Slab in the object in the same relative position, so that in the cache they start address offset each other , so that you can improve the access efficiency of the cache .

    Each Slab on after the last object has a small waste area is not used, which is a colored region the size of the compensation , which depend on the size of the colored regions, and the relative size of each object Slab thereto. However, the colored region area sum for the same object each Slab is constant .

    Size of each object is substantially the size of the required data structures. Only when the size of the data structure is not aligned with the cache buffer line, increasing a number of bytes to align it . Therefore, a Slab start addresses of all the objects are necessarily to cache buffer line alignment .

Guess you like

Origin blog.csdn.net/qq_38971487/article/details/90574044