![为什么我的 min_free_kbytes 大于记录的计算结果(并且大于记录的最大值)?](https://linux22.com/image/155635/%E4%B8%BA%E4%BB%80%E4%B9%88%E6%88%91%E7%9A%84%20min_free_kbytes%20%E5%A4%A7%E4%BA%8E%E8%AE%B0%E5%BD%95%E7%9A%84%E8%AE%A1%E7%AE%97%E7%BB%93%E6%9E%9C%EF%BC%88%E5%B9%B6%E4%B8%94%E5%A4%A7%E4%BA%8E%E8%AE%B0%E5%BD%95%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC%EF%BC%89%EF%BC%9F.png)
我的机器有大约 8GB 的 RAM。为什么min_free_kbytes
设置为67584?内核代码注释说我应该看到min_free_kbytes
设置为 11584 左右。它还说它设置的最大值是 65536。
$ cat /proc/sys/vm/min_free_kbytes
67584
$ free -h
total used free shared buff/cache available
Mem: 7.7Gi 3.2Gi 615Mi 510Mi 3.9Gi 3.7Gi
Swap: 2.0Gi 707Mi 1.3Gi
$ grep -r min_free_kbytes /etc/sysctl* # No manual configuration
$
$ uname -r # My kernel version
5.0.17-200.fc29.x86_64
https://elixir.bootlin.com/linux/v5.0.17/source/mm/page_alloc.c#L7567
/*
* Initialise min_free_kbytes.
*
* For small machines we want it small (128k min). For large machines
* we want it large (64MB max). But it is not linear, because network
* bandwidth does not increase linearly with machine size. We use
*
* min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
* min_free_kbytes = sqrt(lowmem_kbytes * 16)
*
* which yields
*
* 16MB: 512k
* 32MB: 724k
* 64MB: 1024k
* 128MB: 1448k
* 256MB: 2048k
* 512MB: 2896k
* 1024MB: 4096k
* 2048MB: 5792k
* 4096MB: 8192k
* 8192MB: 11584k
* 16384MB: 16384k
*/
答案1
在使用大页面的系统上,建议 min_free_kbytes 更高,并使用 进行调整
hugeadm --set-recommended-min_free_kbytes
。随着透明大页支持的引入,这个推荐值也被应用[...]在具有 4G 内存的 X86-64 上,min_free_kbytes 变为 67584。
https://www.spinics.net/lists/linux-mm/msg14044.html
/* Ensure 2 pageblocks are free to assist fragmentation avoidance */
recommended_min = pageblock_nr_pages * nr_zones * 2;
/*
* Make sure that on average at least two pageblocks are almost free
* of another type, one for a migratetype to fall back to and a
* second to avoid subsequent fallbacks of other types There are 3
* MIGRATE_TYPES we care about.
*/
recommended_min += pageblock_nr_pages * nr_zones *
MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
linux-5.0.17/mm//mm/khugpaged.c:1862
“页面块”是一个潜在的大页面:x86-64 上为 2MiB。此计算表明 min_free 为 2MiB * 11 * nr_zones。 “使用 4G 内存”至少有三个区域:“正常”、“DMA32”和“DMA”(DMA16)。
2 * 11 * 3 = 66 MiB。
66 MiB = 66 * 1024 = 67584 KiB。
即使在 4G 系统上也会出现单独的“正常”区域和 DMA32 区域的原因是:“在 4G 系统上实现的微小 pci32 区域会重新定位 4g 上的一些小内存,为 pci32 mmio 腾出空间。”