Linux slab 分配器和缓存性能

标签 linux memory memory-management linux-kernel slab

来自指南了解 linux 内核第 3 版,第 8.2.10 章,Slab 着色-

We know from Chapter 2 that the same hardware cache line maps many different blocks of RAM. In this chapter, we have also seen that objects of the same size end up being stored at the same offset within a cache. Objects that have the same offset within different slabs will, with a relatively high probability, end up mapped in the same cache line. The cache hardware might therefore waste memory cycles transferring two objects from the same cache line back and forth to different RAM locations, while other cache lines go underutilized. The slab allocator tries to reduce this unpleasant cache behavior by a policy called slab coloring : different arbitrary values called colors are assigned to the slabs.

enter image description here

(1) 我无法理解 slab 着色试图解决的问题。当正常进程访问数据时,如果它不在缓存中并且遇到缓存未命中,则将数据与进程尝试访问的数据周围地址的数据一起提取到缓存中以提高性能。怎么会发生相同的特定缓存行不断交换的情况?一个进程在两个不同内存区域的内存区域中不断访问相同偏移量的两个不同数据地址的可能性非常低。即使它确实发生了,缓存策略通常会根据某些议程(例如 LRU、随机等)选择要交换的行。不存在根据被访问地址的最低有效位匹配来选择逐出行的策略.

(2) 我无法理解 slab 着色如何解决缓存问题,它从 slab 的末尾到开头获取空闲字节,并导致不同的 slab 具有不同的偏移量-交换问题?

[SOLVED] 经过小型调查后,我相信我找到了问题的答案。答案已发布。

最佳答案

经过多次研究和思考,我得到的解释似乎更合理,而不仅仅是具体地址示例。 首先,你必须学习缓存、标签、集合、线路分配等基础知识。

从linux内核代码可以确定colour_off的单位是cache_line_size。 colour_off是基本偏移单元,colour是struct kmem_cache中colour_off的个数。

int  __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
   cachep->align = ralign;
   cachep->colour_off = cache_line_size();  // colour_off's unit is cache_line_size
    /* Offset must be a multiple of the alignment. */
   if (cachep->colour_off < cachep->align)
      cachep->colour_off = cachep->align;
   .....
   err = setup_cpu_cache(cachep, gfp);

https://elixir.bootlin.com/linux/v4.6/source/mm/slab.c#L2056

所以我们可以分两种情况来分析。 首先是缓存 > slab。 enter image description here 你看到 slab 1 slab2 slab3 ... 不可能发生碰撞,主要是因为缓存足够大,除了 slab1 和 slab5 可能发生碰撞。所以着色机制在提高性能的情况下并不是那么清晰。但是对于 slab1 和 slab5 我们只是忽略了解释它的原因,我相信你在阅读以下内容后会解决它。

第二个是slab > 缓存。 enter image description here 空行表示 color_off 或缓存行。很明显,slab1 和 slab2 不可能在 tick 和 slab2 slab3 标记的线上发生碰撞。 我们确保着色机制优化两个相邻 slab 之间的两条线,更不用说 slab1 vs slab3 优化更多的线,2+2 = 4 线,你可以数一下。

总而言之,着色机制通过尽可能使用原本无用的内存来优化缓存性能(具体只是优化开头和结尾的一些 colour_off 行,而不是其他仍然可能发生冲突的行)。

关于Linux slab 分配器和缓存性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46731933/

相关文章:

c++ - 需要优化 - 算法实现占用太多内存

java - 如何在使用 HashMap<String,List<SomeObject>> 时最小化垃圾回收

c - 了解矩阵 c 的分配

linux - sed 删除 # 和 # 之间的字符并添加 :'s in bash script?

python - 在进程运行时读取进程的输出

linux - 如何测试命令anacron?

go - Golang中的指针地址

c - 优化 C 中的 vector/矩阵运算?

c++ - std::deque 内存使用 - Visual C++,以及与其他人的比较

linux - 看不到上传的图片