The relationship between the meaning of Linux slab and Kmalloc __linux

Source: Internet
Author: User

Slab literally meant to be divided into thick plates/slates, similar to the meaning of the Buddy algorithm (page can be in accordance with the power of 2 to form an element, can also be separated, just like a small partner, buddy algorithm because of "when the time to disperse" this performance and get the name); Slab meaning is to put a chunk of space, Break down into small chunks of space. Slab The specific feature is that it contains different objects, like bookshelves, where the same type of object is placed in the same type of slab (bookshelf), because a slab may not fit all the objects, so some object types can have multiple slab. Slab is not the beginning of the algorithm, it appears to optimize the buddy algorithm, buddy algorithm to page (4096 bytes) as a unit, management, even if very small data modification, will use 4096 bytes, spending, and data this piece, that piece, bad management.

So, the buddy system is like a big bookshelf, slab like a bookshelf. The bookshelf is used to manage a specialized field of books, such as C language/c++/java/c#, while the lattice is used to manage a small class, C language placed in a slab,c++ into a slab ... and the composition of slab "object" is specific books, such as "c Expert programming", "C language trap" and so is C language this slab object.

Because the memory initialization time is much longer than the allocation and release time, Slab adds a similar object pool feature, slab caches objects that have been "freed" for the next reuse and no more initialization. object is described using the cache structure Kmem_cache. Created by Kmem_cache_create. Although called a cache, it is not a cache on hardware, it is only an area on main memory, but it is highly related to the cache on the hardware.


Note that the kernel allocates memory function Kmalloc uses __do_kmalloc, which first finds a kmem_cache according to size, and then allocates memory objects from Kmem_cache. This means that the kmalloc is built into a general-purpose memory allocator by the size of the cache. With the Cat/proc/slabinfo command, you can view the Kmem_cache, and Kmalloc will use the Kmem_cache_init (pre-initialization cache) to initialize the cache, This cache is initialized by size (see red font), not initialized by type. Visible, the Kmem_cache is divided by type and by size. If you call the function Kmalloc (50,gfp_kernel), the memory overhead is 64 bytes, and the same 33-64 is the same overhead: 64 bytes, knowing this is beneficial for optimizing memory during kernel development.



Task_struct 356 380 5872 5 8:tunables 0 0 0:slabdata 76 76 0
ANON_VMA 6185 6400 128 1:tunables 0 0 0:slabdata 50 50 0
Shared_policy_node 1:tunables 0 0 0:slabdata 4 4 0
Numa_policy 680 680 170 1:tunables 0 0 0:slabdata 4 4 0
Idr_layer_cache 690 690 544 4:tunables 0 0 0:slabdata 23 23 0
kmalloc-8192 8192 4 8:tunables 0 0 0:slabdata 14 14 0
kmalloc-4096 139 144 4096 8 8:tunables 0 0 0:slabdata 18 18 0
kmalloc-2048 331 352 2048 8:tunables 0 0 0:slabdata 22 22 0
kmalloc-1024 1339 1376 1024 8:tunables 0 0 0:slabdata 43 43 0
kmalloc-512 5574 5632 4:tunables 0 0 0:slabdata 176 176 0
kmalloc-256 8141 8864 256 2:tunables 0 0 0:slabdata 277 277 0
kmalloc-128 844 896 128 1:tunables 0 0 0:slabdata 28 28 0
kmalloc-64 4820 5440 1:tunables 0 0 0:slabdata 85 85 0
kmalloc-32 4087 4480 128 1:tunables 0 0 0:slabdata 35 35 0
Kmalloc-16 11411 11776 256 1:tunables 0 0 0:slabdata 46 46 0
Kmalloc-8 6078 6144 8 1:tunables 0 0 0:slabdata 12 12 0
kmalloc-192 3961 4389 1:tunables 0 0 0:slabdata 209 209 0
kmalloc-96 735 756 1:tunables 0 0 0:slabdata 18 18 0
Kmem_cache_node 0 0 1:tunables 0 0 0:slabdata 0 0 0



Explain the reference in detail

Http://oss.org.cn/kernel-book/ch06/6.3.3.htm

The slab allocation pattern places objects into buffers (although the word cache is used in English, it actually refers to areas in memory, not hardware caches). Because the organization and management of the buffer are closely related to the hit rate of the hardware cache, thus, the slab buffer is not made directly by the individual objects, but by a series of "chunks" (slab), and each chunk contains several objects of the same type, which are either allocated, or idle, As shown in Figure 6.12. Generally speaking, objects are divided into two kinds, one is a large object, the other is a small object. Small objects are those that can accommodate several objects in a single page. For example, an INODE structure accounts for more than 300 bytes, so a page can hold more than 8 inode structures, so the inode structure is a small object. Objects less than 512 bytes are called small objects in the Linux kernel.


Figure 6.10 The composition of slab

In fact, the buffer is the main memory of a region, dividing the area into multiple blocks, each piece is a slab, each slab by one or more pages, each slab storage is the object.

Because the implementation of the slab allocation pattern is more complicated, we are not prepared to analyze it in detail, only to give a description of the main content.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.