Linux Slab Memory Management Analysis

Source: Internet
Author: User

I recently read about the slab mechanism. The analysis on the internet is rich and comprehensive. I will not repeat it too much. I just made a simple summary myself.

 

1. Several important data structures

(1) kmem_cache data structure:

Struct kmem_cache {

Struct array_cache * array [nr_cpus];
CPU local high-speed cache

Unsigned int batchcount;

Unsigned int limit;

Unsigned int shared;

Unsigned int buffer_size;
Slab object size

U32 reciprocal_buffer_size; the reciprocal of buffer_size, used to calculate the index of the object in Slab

Unsigned int flags;
The slab management area can be distinguished by external or built-in

Unsigned int num; number of objects in an Slab

Unsigned int gfporder; Number of pages occupied by an Slab

Gfp_t gfpflags;

Size_t color; number of colored blocks required by the fragment area in an Slab

Unsigned int colour_off; the unit size of the Color Block in the cache

Struct kmem_cache * slabp_cache;
Slab is used to manage region objects. This is required when slab is external.

Unsigned int slab_size; size of the slab management object

Num * sizeof (kmem_bufctl_t) + sizeof (struct slab)

Unsigned int dflags;

Void (* ctor) (void * OBJ); object constructor

Const char * Name; Name of the object Cache

Struct list_head next; link to the global slab cache linked list

# Ifdef config_debug_slab
Statistical data

Unsigned long num_active;

Unsigned long num_allocations;

Unsigned long high_mark;

Unsigned long grown;

Unsigned long reaped;

Unsigned long errors;

Unsigned long max_freeable;

Unsigned long node_allocs;

Unsigned long node_frees;

Unsigned long node_overflow;

Atomic_t allochit;

Atomic_t allocmiss;

Atomic_t freehit;

Atomic_t freemiss;

Int obj_offset;

Int obj_size;

# Endif/* config_debug_slab */


Struct kmem_list3 * nodelists [max_numnodes]; slab three-Chain Structure

}

 

(2) slab three-chain data structure:

Struct kmem_list3 {

Struct list_head slabs_partial; non-empty non-full slab chain

Struct list_head slabs_full;
Full slab chain

Struct list_head slabs_free;
Empty slab chain

Unsigned long free_objects;
Number of idle objects in the slab linked list

Unsigned int free_limit;

Unsigned int colour_next;
Current slab coloring zone offset

Spinlock_t list_lock;

Struct array_cache * shared; shared object cache for each local cache

Struct array_cache ** alien;

Unsigned long next_reap;

Int free_touched; indicates that the empty slab linked list has been accessed.

};

 

(3) cache data structure of each CPU:

Struct array_cache {

Unsigned int avail;
Available object indexes in the current local cache

Unsigned int limit;
Number of objects in the current local cache

Unsigned int batchcount; number of batch fills. The local cache is filled by batch.

Unsigned int touched; Set 1 to indicate that the local cache has been used recently.

Spinlock_t lock;
Spin lock

Void * entry [];
Pointer Array

};

 

(4) slab data structure:

Struct slab {

Struct list_head list;

Unsigned long colouroff;
Coloring offset of this slab

Void * s_mem;
The starting address page address of the first object in slab + colouroff + management area size

Unsigned int inuse;
Counting objects in use

Kmem_bufctl_t free;
Index of the first idle object in this slab

Unsigned short nodeid;
Node ID

};

Slab is stored on the page address + colouroff

 

2. Some important operations

(1) initialization

Stage 1 start_kernel ()-> mm_init ()-> kmem_cache_init ()

Stage 2 start_kernel ()-> kmem_cache_init_late ()

(2) create a cache

Kmem_cache_create ()

Screenshots taken from numerous analyses on the Internet

Diagram of kmem_cache users:

 

(3) create Slab

Cache_grow ()

 

Alloc_slabmgmt ():

Slab_map_pages ():

 

Allocation object

First, check the local CPU cache, then the local shared CPU cache, and finally the three links. If no space exists in the first three instances, You need to allocate a new slab.

 

 

 

 

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.