linux内存管理之slab系统

本文详细介绍了 Linux 内核中的 SLAB 分配器,包括其数据结构 kmem_cache 的组成及其各个字段的作用,同时提供了创建 slab 的示例代码,并解释了与内存管理相关的配置参数。

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

这里写图片描述


/*
 * struct kmem_cache
 *
 * manages a cache.
 */

struct kmem_cache {
/* 1) per-cpu data, touched during every alloc/free */
	struct array_cache *array[NR_CPUS];
/* 2) Cache tunables. Protected by cache_chain_mutex */
	unsigned int batchcount;
	unsigned int limit;
	unsigned int shared;

	unsigned int buffer_size;
	u32 reciprocal_buffer_size;
/* 3) touched by every alloc & free from the backend */

	unsigned int flags;		/* constant flags */
	unsigned int num;		/* # of objs per slab */

/* 4) cache_grow/shrink */
	/* order of pgs per slab (2^n) */
	unsigned int gfporder;

	/* force GFP flags, e.g. GFP_DMA */
	gfp_t gfpflags;

	size_t colour;			/* cache colouring range */
	unsigned int colour_off;	/* colour offset */
	struct kmem_cache *slabp_cache;
	unsigned int slab_size;
	unsigned int dflags;		/* dynamic flags */

	/* constructor func */
	void (*ctor) (void *, struct kmem_cache *, unsigned long);

/* 5) cache creation/removal */
	const char *name;
	struct list_head next; //指向下一个cache描述符,linux将所有的cache连成一个链表,头指针是cache_chain

/* 6) statistics */
#if STATS
	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;
#endif
#if DEBUG
	/*
	 * If debugging is enabled, then the allocator can add additional
	 * fields and/or padding to every object. buffer_size contains the total
	 * object size including these internal fields, the following two
	 * variables contain the offset to the user object and its size.
	 */
	int obj_offset;
	int obj_size;
#endif
	/*
	 * We put nodelists[] at the end of kmem_cache, because we want to size
	 * this array to nr_node_ids slots instead of MAX_NUMNODES
	 * (see kmem_cache_init())
	 * We still use [MAX_NUMNODES] and not [1] or [0] because cache_cache
	 * is statically defined, so we reserve the max number of nodes.
	 */
	struct kmem_list3 *nodelists[MAX_NUMNODES];//3个队列,分别代表满,半瓶咣当和空空
	/*
	 * Do not add fields after nodelists[]
	 */
};

static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
	=>kmem_freepages(cachep, addr);
	=>if (OFF_SLAB(cachep))
			kmem_cache_free(cachep->slabp_cache, slabp);
				=>__cache_free(cachep, objp);

图片或者部分描述选自:疯狂内核之内存管理 slab分配器
http://blog.youkuaiyun.com/yunsongice/article/details/5272715

fork_init
	=>task_struct_cachep =
		kmem_cache_create("task_struct", sizeof(struct task_struct), ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL); //创建常用的进程描述符的slab

SLAB_RECLAIM_ACCOUNT代表可回收。但是只有到了 /proc/sys/vm/min_free_kbytes的门限才会触发

Linux内存描述之内存区域zone–Linux内存管理(三)
http://write.blog.youkuaiyun.com/mdeditor#!postId=77905468

linux内存源码分析 - SLAB分配器概述
https://www.cnblogs.com/tolimit/p/4566189.html

如何诊断SLUB问题
http://linuxperf.com/?p=184

Linux slab 分配器剖析
https://www.ibm.com/developerworks/cn/linux/l-linux-slab-allocator/

内存问题排查手段及相关文件介绍
http://www.cnblogs.com/lidabo/p/5567768.html

/proc/meminfo之谜
http://blog.sina.com.cn/s/blog_3e8d09120102wkzh.html

详解slab机制(1) slab是什么
https://blog.youkuaiyun.com/u010246947/article/details/10133101

透过proc看内存
http://jasperzhang.blog.sohu.com/162339118.html

linux内存管理信息buddyinfo和slabinfo和zoneinfo
https://blog.youkuaiyun.com/shenhuxi_yu/article/details/72355068

linux内存相关proc文件解析
https://blog.youkuaiyun.com/u014089131/article/details/53175627

内存故障排查
https://blog.youkuaiyun.com/liupengying123/article/details/39483161

Linux 2.6 中的页面回收与反向映射
https://www.ibm.com/developerworks/cn/linux/l-cn-pagerecycle/

Linux服务器Cache占用过多内存导致系统内存不足问题的排查解决
http://www.cnblogs.com/panfeng412/p/drop-caches-under-linux-system.html

Linux服务器Cache占用过多内存导致系统内存不足问题的排查解决(续)
http://www.cnblogs.com/panfeng412/p/drop-caches-under-linux-system-2.html

宋牧春: Linux内核slab内存的越界检查——SLUB_DEBUG
https://blog.youkuaiyun.com/jus3ve/article/details/79285745

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值