当前位置:网站首页>腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
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 的劣势
边栏推荐
- Landingsite eband B1 smoke test case
- Research Report on minimally invasive medical robot industry - market status analysis and development prospect prediction
- Five message formats of OSPF
- pyqt圖片解碼 編碼後加載圖片
- The web version of xshell supports FTP connection and SFTP connection [detailed tutorial] continued from the previous article
- Research Report on market supply and demand and strategy of China's plastic trunking industry
- [zero foundation I] Navicat download link
- Research Report on micro gripper industry - market status analysis and development prospect prediction
- 读博士吧,研究奶牛的那种!鲁汶大学 Livestock Technology 组博士招生,牛奶质量监测...
- Error in PIP installation WHL file: error: is not a supported wheel on this platform
猜你喜欢
《Just because》阅读感受
Analysis of neural network
It is said that this year gold three silver four has become gold one silver two..
System (hierarchical) clustering method and SPSS implementation
How to test the process of restoring backup files?
GEE:(二)对影像进行重采样
[shutter] shutter layout component (wrap component | expanded component)
Riding the wind of "cloud native" and stepping on the wave of "digitalization", new programmer 003 starts pre-sale
MySQL learning record (6)
MySQL learning record (3)
随机推荐
一周生活
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
SQL必需掌握的100个重要知识点:使用游标
MySQL learning record (8)
Analysis of neural network
Research Report on market supply and demand and strategy of China's Plastic Geogrid industry
TinyMCE visual editor adds Baidu map plug-in
Free open source web version of xshell [congratulations on a happy new year]
MySQL learning record (7)
Une semaine de vie
Research Report on micro gripper industry - market status analysis and development prospect prediction
MySQL inserts Chinese data and reports an error. Set the default collation
Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file
Official announcement! The golden decade of new programmers and developers was officially released
情感计算与理解研究发展概述
How to test the process of restoring backup files?
Daily book -- analyze the pain points of software automation from simple to deep
China plastic bottle market trend report, technological innovation and market forecast
kubernetes资源对象介绍及常用命令(四)
Centos7 installation and configuration of redis database