Linux Kernel Memory Management (2)

本文深入探讨Linux内核的Slab分配器,包括三层架构:Cache、Slab和Object。作者详细介绍了通用缓存和特定缓存的创建与使用,以及Slab和对象的管理。此外,还提到了per-CPU数据结构struct array_cache如何减少锁竞争,并利用硬件缓存。文章还解析了Cache Descriptor的结构,并展示了slabinfo文件如何展示系统中的通用和特定缓存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Author: Harold Wang

http://blog.youkuaiyun.com/hero7935

1.Slab Allocator

Three Layer architecture:
--Cache
--Slab
--Object
image

1.1 Caches
General cache
--kmem_cache_init() and kmem_cache_sizes_init() to establish general cache when system initializing.
--kmalloc() function to allocate object
image
Specific cache
--caches that are frequently used.
--kmem_cache_create() function to create specific cache

Author: Harold Wang

http://blog.youkuaiyun.com/hero7935

/proc/slabinfo to illustrate the general cache and specific cache in the system.

Linux 2.6 involves per-CPU data structure(struct array_cache which is slab local cache) to reduce spin lock contention among processors and to make better use of the hardware caches, Most allocations and releases of slab objects affects the local cache only!

Cache Descriptor
struct kmem_cache
{
struct kmem_list3* nodelists[MAX_NUMNODES];
/*constructor func*/
/*de-constructor func*/
struct list_head next;
}

struct kmem_list3
{
struct list_head slabs_partial; /* list of slab descriptors with both free and non-free objects */
struct list_head slabs_full; /* list of slab descriptors with non-free objects */
struct list_head slabs_free; /* list of slab descriptors with free objects only */
unsigned long free_objects;/*number of objects available in kmem_list3*/
unsigned int free_limit;/*maxmimum of objects in kmem_list3*/
/*other functions*/
}

struct slab
{
struct list_head list;
unsigned long colouroff;/*offset(in bytes) of the first object in a Slab*/
void* s_mem;/*Address of first object in the slab*/
unsigned int inuse;
kmem_bufctl_t free;/*Index of next free object in the slab, or BUFCTL_END if there are no free objects left*/
}
image

All caches are orgnised by double-linked chain and pointed by cache_chain.

Author: Harold Wang

http://blog.youkuaiyun.com/hero7935


image

1.2 Slabs and objects


image

Slub allocation:

image

 

Noncontiguous memory area management(updating)
image

Process address space

Process-Related Memory Structures
image

Process Memory Layout

image 

Linux Process Page Table
image

Author: Harold Wang

http://blog.youkuaiyun.com/hero7935

updating…….

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值