【原创】(八)Linux内存管理 - zoned page frame allocator - 3
背景
Read the fucking source code!--By 鲁迅A picture is worth a thousand words.--By 高尔基
说明:
- Kernel版本:4.14
- ARM64处理器,Contex-A53,双核
- 使用工具:Source Insight 3.5, Visio
1. 概述
本文将分析watermark。
简单来说,在使用zoned page frame allocator分配页面时,会将可用的free pages与zone的watermark进行比较,以便确定是否分配内存。
同时watermark也用来决定kswapd内核线程的睡眠与唤醒,以便对内存进行检索和压缩处理。
回忆一下之前提到过的struct zone结构体:
struct zone {
/* Read-mostly fields */
/* zone watermarks, access with *_wmark_pages(zone) macros */
unsigned long watermark[NR_WMARK];
unsigned long nr_reserved_highatomic;
....
}
enum zone_watermarks {
WMARK_MIN,
WMARK_LOW,
WMARK_HIGH,
NR_WMARK
};
#define min_wmark_pages(z) (z->watermark[WMARK_MIN])
#define low_wmark_pages(z) (z->watermark[WMARK_LOW])
#define high_wmark_pages(z) (z->watermark[WMARK_HIGH])
可以看出,总共有三种水印,并且只能通过特定的宏来访问。
WMARK_MIN
内存不足的最低点,如果计算出的可用页面低于该值,则无法进行页面计数;WMARK_LOW
默认情况下,该值为WMARK_MIN的125%,此时kswapd将被唤醒,可以通过修改watermark_scale_factor来改变比例值;WMARK_HIGH
默认情况下,该值为WMARK_MAX的150%,此时kswapd将睡眠,可以通过修改watermark_scale_factor来改变比例值;
图来了:

下边将对细节进一步分析。
1. watermark初始化
先看一下初始化的相关调用函数:

nr_free_buffer_pages:统计ZONE_DMA和ZONE_NORMAL中可用页面,managed_pages - high_pages;setup_per_zone_wmarks:根据min_free_kbytes来计算水印值,来一张图会比较清晰易懂:

refresh_zone_stat_thresholds:
先来回顾一下struct pglist_data和struct zone:
typedef struct pglist_data {
...
struct per_cpu_nodestat __percpu *per_cpu_nodestats;
...
} pg_data_t;
struct per_cpu_nodestat {
s8 stat_threshold;
s8 vm_node_stat_diff[NR_VM_NODE_STAT_ITEMS];
};
struct zone {
...
struct per_cpu_pageset __percpu *pageset;
...
}
struct per_cpu_pageset {
struct per_cpu_pages pcp;
#ifdef CONFIG_NUMA
s8 expire;
u16 vm_numa_stat_diff[NR_VM_NUMA_STAT_ITEMS];
#endif
#ifdef CONFIG_SMP
s8 stat_threshold;
s8 vm_stat_diff[NR_VM_ZONE_STAT_ITEMS];
#endif
};
从数据结构中可以看出,针对Node和Zone,都有一个Per-CPU的结构来存储信息,而refresh_zone_stat_thresholds就跟这两个结构相关,用于更新这两个结构中的stat_threshold字段,具体的计算方式就不表了,此外还计算了percpu_drift_mark,这个在水印判断的时候需要用到该值。阈值的作用就是用来进行判断,从而触发某个行为,比如内存压缩处理等。
setup_per_zone_lowmem_reserve:
设置每个zone的lowmem_reserve大小,代码中的实现逻辑如下图所示。

calculate_totalreserve_pages:
计算各个zone的保留页面,以及系统的总的保留页面,其中会将high watermark看成保留页面。如图:

2. watermark判断
老规矩,先看看函数调用关系图:

__zone_watermark_ok:
watermark判断的关键函数,从图中的调用关系可以看出,最终的处理都是通过它来完成判断的。还是用图片来说明整体逻辑吧:

上图中左边判断是否有足够的空闲页面,右边直接查询free_area[]是否可以最终进行分配。
zone_watermark_ok
:直接调用__zone_watermark_ok`,没有其他逻辑。zone_watermark_fast:
从名字可以看出,这个是进行快速判断,快速的体现主要是在order = 0的时候进行判断决策,满足条件时直接返回true,否则调用__zone_watermark_ok。
贴个代码吧,清晰明了:
static inline bool zone_watermark_fast(struct zone *z, unsigned int order,
unsigned long mark, int classzone_idx, unsigned int alloc_flags)
{
long free_pages = zone_page_state(z, NR_FREE_PAGES);
long cma_pages = 0;
#ifdef CONFIG_CMA
/* If allocation can't use CMA areas don't use free CMA pages */
if (!(alloc_flags & ALLOC_CMA))
cma_pages = zone_page_state(z, NR_FREE_CMA_PAGES);
#endif
/*
* Fast check for order-0 only. If this fails then the reserves
* need to be calculated. There is a corner case where the check
* passes but only the high-order atomic reserve are free. If
* the caller is !atomic then it'll uselessly search the free
* list. That corner case is then slower but it is harmless.
*/
if (!order && (free_pages - cma_pages) > mark + z->lowmem_reserve[classzone_idx])
return true;
return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
free_pages);
}
zone_watermark_ok_safe:
在zone_watermark_ok_safe函数中,主要增加了zone_page_state_snapshot的调用,用来计算free_pages,这个计算过程将比直接通过zone_page_state(z, NR_FREE_PAGES)更加精确。
bool zone_watermark_ok_safe(struct zone *z, unsigned int order,
unsigned long mark, int classzone_idx)
{
long free_pages = zone_page_state(z, NR_FREE_PAGES);
if (z->percpu_drift_mark && free_pages < z->percpu_drift_mark)
free_pages = zone_page_state_snapshot(z, NR_FREE_PAGES);
return __zone_watermark_ok(z, order, mark, classzone_idx, 0,
free_pages);
}
percpu_drift_mask在refresh_zone_stat_thresholds函数中设置的,这个在上文中已经讨论过了。
每个zone维护了三个字段用于页面的统计,如下:
struct zone {
...
struct per_cpu_pageset __percpu *pageset;
...
/*
* When free pages are below this point, additional steps are taken
* when reading the number of free pages to avoid per-cpu counter
* drift allowing watermarks to be breached
*/
unsigned long percpu_drift_mark;
...
/* Zone statistics */
atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
}
内核在内存管理中,读取空闲页面与watermark值进行比较,要读取正确的空闲页面值,必须同时读取vm_stat[]和__percpu *pageset计算器。如果每次都读取的话会降低效率,因此设定了percpu_drift_mark值,只有在低于这个值的时候,才触发更精确的计算来保持性能。
__percpu *pageset计数器的值更新时,当计数器值超过stat_threshold值,会更新到vm_stat[]中,如下图:

zone_watermark_ok_safe中调用了zone_page_state_snapshot,与zone_page_state的区别如下图所示:

watermark的分析到此为止,收工!

【原创】(八)Linux内存管理 - zoned page frame allocator - 3的更多相关文章
- 【原创】(六)Linux内存管理 - zoned page frame allocator - 1
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...
- 【原创】(七)Linux内存管理 - zoned page frame allocator - 2
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...
- 【原创】(九)Linux内存管理 - zoned page frame allocator - 4
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...
- 【原创】(十)Linux内存管理 - zoned page frame allocator - 5
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...
- Linux内存管理 (11)page引用计数
专题:Linux内存管理专题 关键词:struct page._count._mapcount.PG_locked/PG_referenced/PG_active/PG_dirty等. Linux的内 ...
- 【原创】(十四)Linux内存管理之page fault处理
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...
- Linux内存管理6---伙伴算法与slab
1.前言 本文所述关于内存管理的系列文章主要是对陈莉君老师所讲述的内存管理知识讲座的整理. 本讲座主要分三个主题展开对内存管理进行讲解:内存管理的硬件基础.虚拟地址空间的管理.物理地址空间的管理. 本 ...
- Linux内存描述之内存页面page–Linux内存管理(四)
服务器体系与共享存储器架构 日期 内核版本 架构 作者 GitHub CSDN 2016-06-14 Linux-4.7 X86 & arm gatieme LinuxDeviceDriver ...
- [转帖]Linux分页机制之分页机制的演变--Linux内存管理(七)
Linux分页机制之分页机制的演变--Linux内存管理(七) 2016年09月01日 20:01:31 JeanCheng 阅读数:4543 https://blog.csdn.net/gatiem ...
随机推荐
- Mybatis系列(一)入门
Mybatis系列(一)入门 mybatis简介 MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除 了几乎所有的 JDBC 代码和参数的手工设置以及结 ...
- Spring Boot(一):快速开始
Spring Boot(一):快速开始 本系列文章旨在使用最小依赖.最简单配置,帮助初学者快速掌握Spring Boot各组件使用,达到快速入门的目的.全部文章所使用示例代码均同步Github仓库和G ...
- 【第十七篇】easyui-datagrid 导出Excel (在客户端能弹出下载框)
//导出Excel function exportExcel(obj) { var SaleOrderNo = $("#SaleOrderNo").val().trim(); va ...
- .Net基础篇_学习笔记_第三天_Convert类型转换
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Java如何创建不存在的指定路径的文件?
实际应用中,要在指定位置创建一个文件,但文件及文件之前的目录都不存在,此时可用以下方法进行创建. 以下是主要代码: File f = new File("I:" + File.se ...
- Java面试-容器的遍历
当我们用增强for循环遍历非并发容器(HashMap.ArrayList等),如果修改其结构,会抛出异常ConcurrentModificationException,因此在阿里巴巴的Java规范中有 ...
- 章节十六、9-Listeners监听器
一.IInokedMethodListener 1.实现一个类来监听testcase的运行情况. package listenerspackage; import org.testng.IInvoke ...
- 03 (OC)* UITableView优化
一:cell注册和初始化 1:不注册cell 2:注册类 3:注册nib 4:storyboard 二:核心思想 1:UITableView的核心思想是:cell的重用机制.UITbleView只会创 ...
- JsonConvert 转DateTime类型为json 带T
在调用接口的时候 将Model转换成json Datetime类型多了个T 用的是Newtonsoft.Json.dll 版本v4.5.0.0 代码:paramsjson = JsonConvert ...
- AsyncLocal和Async原理解读
AsyncLocal 的实现很简单,将AsyncLocal实例和当前线程的值以键值对的形式保存在Thread.CurrentThread.ExecutionContext.m_localValues. ...