【原创】(八)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 ...
随机推荐
- CCF 模拟试题——出现次数最多的数 官方答案解析及自己写的正确答案
前几天知道的CCF计算机职业资格认证考试,觉得好像比软考含金量高一些,就去了解了一下,做了模拟试题中的 “出现次数最多的数” 这道题,我的算法和官方答案算法不同,个人觉得觉得官方的好一点,没那么繁琐, ...
- 记一次神奇的sql查询经历,group by慢查询优化
一.问题背景 现网出现慢查询,在500万数量级的情况下,单表查询速度在30多秒,需要对sql进行优化,sql如下: 我在测试环境构造了500万条数据,模拟了这个慢查询. 简单来说,就是查询一定条件下, ...
- IDEA微服务项目的application.yml没有绿色叶子的解决办法
1.今天在写微服务项目的时候成功入坑,那么问题是啥呢?接下来和我一起走入bug的世界吧,让我们看看究竟是怎么回事. *问题描述 1.application.yml是灰色的小格子 2.实在难看 *需要解 ...
- PTA A1011&A1012
A1011 World Cup Betting (20 分) 题目内容 With the 2010 FIFA World Cup running, football fans the world ov ...
- 关于svn更新失败,clearup异常解决
直接上主题: 1. 下载sqlite3工具(https://files.cnblogs.com/files/eric-fang/sqlite-tools-win32-x86-3210000.zip), ...
- spark运行信息及报错问题解决集锦
错误1: ERROR client.RemoteDriver: Failed to start SparkContext: java.lang.IllegalArgumentException: Ex ...
- Android 禁止Edittext弹出系统软键盘 的几种方法
第一种方法:在XML文件下添加: android:focusable="true" android:focusableInTouchMode="true" 第二 ...
- SharePoint 2013 Sandbox Solution
昨天在写SharePoint EventReceiver的时候遇到一个问题,创建了一个local farm SharePoint solution,添加了一个ItemAdded(SPItemEvent ...
- idea Error: java: OutOfMemoryError: insufficient memory处理
在更新项目代码或者运行项目时报错 OutOfMemoryError: insufficient memory,解决方式如下: 方式1: 点击file,选择Invalidate Caches 进行清理一 ...
- 难题解决:Mycat数据库中间件+Mybatis批量插入数据并返回行记录的所有主键ID
一.mybatis的版本必须为3.3.1及其以上 项目所依赖的mybatis的版本必须为3.3.1及其以上,低版本的不行,保证hap项目的依赖的mybatis的jar的版本必需为需要的版本: 二.在 ...