Three: slab allocator

Table of contents

slab allocator

basic concept

slab allocates memory

main structure

kmem_cache

per cpu freelist


slab allocator

basic concept

For small-grained memory allocation

The buddy system uses 4kb pages as the minimum allocation unit, but for some cases, this is too large, which will cause serious memory waste and generate a lot of memory fragments.

For partner systems and slab allocators, they are like "wholesalers" and "retailers". "Wholesalers" refer to the mechanism for managing and allocating memory by pages; while "retailers" are the mechanisms for obtaining resources wholesale from "wholesalers" and managing and allocating memory in units of bytes.

There are currently three implementation algorithms, namely slab, slob, and slub. Moreover, according to their respective allocation algorithms, there will be a certain emphasis on applicability.

Continuous pages are obtained based on the partner allocator, as a cache of a data structure object, and then the continuous pages are internally cut into aligned objects, which are used when used. Such a continuous page is called a slab.

Each cache (kmem_cache) consists of multiple slabs, and each slab consists of one or more physically contiguous pages.

When the kernel requests memory for an object, the slab allocator can return the required memory that exactly represents the object.

slab allocates memory

When using kmem_cache_alloc() to apply for memory, first apply from the slab cache pool of the local cpu. If there is no available object, allocate it from the public per node partial. If there is no available object, you can only allocate a slab from the partner system and hang it into the per cpu freelist.

slab release memory

Too complicated, put it aside for now

main structure

kmem_cache

Use the kmem_cache_create() interface to create kmem_cache

A section of memory described by the structure is called a slab cache pool. A slab cache pool is like a carton of milk. There are many bottles of milk in a carton of milk, and each bottle of milk is an object. When you allocate memory, it's like taking a bottle from the milk crate. There will always be a day when it is finished. When the box is empty, you need to go to the supermarket and buy another box. The supermarket is equivalent to a partial linked list, and the supermarket stores many boxes of milk. If the supermarket is also sold out, it will naturally buy from the manufacturer and sell it to you. The manufacturer is equivalent to the partner system.

per cpu freelist

Each cpu will allocate a struct kmem_cacche_cpu structure. It can be called a local cache pool.

Guess you like

Origin blog.csdn.net/qq_52353238/article/details/130215008