当前位置:网站首页>腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
2022-07-02 21:48:00 【InfoQ】


1. Page Cache
1.1 Page Cache 是什么?

1.2 如何查看系统的 Page Cache?
$ cat /proc/meminfo
...
Buffers: 1224 kB
Cached: 111472 kB
SwapCached: 36364 kB
Active: 6224232 kB
Inactive: 979432 kB
Active(anon): 6173036 kB
Inactive(anon): 927932 kB
Active(file): 51196 kB
Inactive(file): 51500 kB
...
Shmem: 10000 kB
...
SReclaimable: 43532 kB
...Buffers + Cached + SwapCached = Active(file) + Inactive(file) + Shmem + SwapCached
Page Cache = Buffers + Cached + SwapCached
1.3 page 与 Page Cache
- File-backed pages:文件备份页也就是 Page Cache 中的 page,对应于磁盘上的若干数据块;对于这些页最大的问题是脏页回盘;
- Anonymous pages:匿名页不对应磁盘上的任何磁盘数据块,它们是进程的运行是内存空间(例如方法栈、局部变量表等属性);
1.4 Swap 与缺页中断
- 高数值:较高频率的 swap,进程不活跃时主动将其转换出物理内存。
- 低数值:较低频率的 swap,这可以确保交互式不因为内存空间频繁地交换到磁盘而提高响应延迟。

1.5 Page Cache 与 buffer cache
~ free -m
total used free shared buffers cached
Mem: 128956 96440 32515 0 5368 39900
-/+ buffers/cache: 51172 77784
Swap: 16002 0 16001


1.6 Page Cache 与预读
- 用户线程仅仅请求读取磁盘上文件 A 的 offset 为 0-3KB 范围内的数据,由于磁盘的基本读写单位为 block(4KB),于是操作系统至少会读 0-4KB 的内容,这恰好可以在一个 page 中装下。
- 但是操作系统出于局部性原理[3]会选择将磁盘块 offset [4KB,8KB)、[8KB,12KB) 以及 [12KB,16KB) 都加载到内存,于是额外在内存中申请了 3 个 page;

2. Page Cache 与文件持久化的一致性&可靠性
- Write Through(写穿):向用户层提供特定接口,应用程序可主动调用接口来保证文件一致性;
- Write back(写回):系统中存在定期任务(表现形式为内核线程),周期性地同步文件系统中文件脏数据块,这是默认的 Linux 一致性方案;
- 创建的针对回写任务的内核线程数由系统中持久存储设备决定,为每个存储设备创建单独的刷新线程;
- 关于多线程的架构问题,Linux 内核采取了 Lighthttp 的做法,即系统中存在一个管理线程和多个刷新线程(每个持久存储设备对应一个刷新线程)。管理线程监控设备上的脏页面情况,若设备一段时间内没有产生脏页面,就销毁设备上的刷新线程;若监测到设备上有脏页面需要回写且尚未为该设备创建刷新线程,那么创建刷新线程处理脏页面回写。而刷新线程的任务较为单调,只负责将设备中的脏页面回写至持久存储设备中。
- 刷新线程刷新设备上脏页面大致设计如下:每个设备保存脏文件链表,保存的是该设备上存储的脏文件的 inode 节点。所谓的回写文件脏页面即回写该 inode 链表上的某些文件的脏页面;系统中存在多个回写时机,第一是应用程序主动调用回写接口(fsync,fdatasync 以及 sync 等),第二管理线程周期性地唤醒设备上的回写线程进行回写,第三是某些应用程序/内核任务发现内存不足时要回收部分缓存页面而事先进行脏页面回写,设计一个统一的框架来管理这些回写任务非常有必要。
- Write Through 以牺牲系统 I/O 吞吐量作为代价,向上层应用确保一旦写入,数据就已经落盘,不会丢失;
- Write back 在系统发生宕机的情况下无法确保数据已经落盘,因此存在数据丢失的问题。不过,在程序挂了,例如被 kill -9,Page Cache 中的数据操作系统还是会确保落盘;
3. Page Cache 的优劣势
3.1 Page Cache 的优势
3.2 Page Cache 的劣势
边栏推荐
- [shutter] shutter layout component (opacity component | clipprect component | padding component)
- 《Just because》阅读感受
- Research Report on market supply and demand and strategy of China's Plastic Geogrid industry
- The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
- 攻防世界pwn题:Recho
- Interpretation of CVPR paper | generation of high fidelity fashion models with weak supervision
- Off chip ADC commissioning record
- Unity3D学习笔记4——创建Mesh高级接口
- Landingsite eband B1 smoke test case
- 基本IO接口技术——微机第七章笔记
猜你喜欢

Baidu sued a company called "Ciba screen"

分享一下如何制作专业的手绘电子地图

pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform

#include<>和#include“”的区别
![[shutter] shutter layout component (wrap component | expanded component)](/img/a7/824a990235fc5ce67841ebdcf001fb.jpg)
[shutter] shutter layout component (wrap component | expanded component)

TinyMCE visual editor adds Baidu map plug-in

MySQL learning record (5)

Blue Bridge Cup Winter vacation homework (DFS backtracking + pruning)

MySQL learning record (8)

关于测试用例
随机推荐
Capacity expansion mechanism of ArrayList
[Yu Yue education] reference materials of analog electronic technology of Nanjing Institute of information technology
MySQL learning record (8)
The difference between include < > and include ""
Baidu sued a company called "Ciba screen"
MySQL learning record (7)
Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file
Riding the wind of "cloud native" and stepping on the wave of "digitalization", new programmer 003 starts pre-sale
China plastic box market trend report, technological innovation and market forecast
[C language] [sword finger offer article] - replace spaces
Basic knowledge of tree and binary tree (detailed illustration)
攻防世界pwn题:Recho
[shutter] statefulwidget component (pageview component)
发现你看不到的物体!南开&武大&ETH提出用于伪装目标检测SINet,代码已开源!...
#include<>和#include“”的区别
How is LinkedList added?
PIP version update timeout - download using domestic image
pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
一周生活
pyqt圖片解碼 編碼後加載圖片