Why Linux IO Is the Slowest Subsystem and How to Optimize It
This article explains why the Linux I/O subsystem is the slowest part of the system, describes page‑level disk access, major and minor page faults, the file buffer cache, page types, write‑back mechanisms, monitoring tools, and practical tips for reducing I/O latency.
IO子系统一般是linux系统中最慢的部分。一个原因是它距离CPU的距离,另一个原因是它的物理结构。访问磁盘的时间与访问内存的时间是7天与7分钟的区别。linux kernel要尽量减少磁盘IO。
1. Reading and Writing Data
linux内核以page为单位访问磁盘IO,一般为4K。
查看页大小:/usr/bin/time -v date
Page size (bytes): 40962. Major and Minor Page Faults
linux会将内存物理地址空间映射到虚拟内存,内核仅会映射需要的内存页。当应用启动时,内核依次搜索CPU cache和物理内存,查找是否有相应的内存页,如果不存在,则内核将会发起一次MPF(major page fault),将磁盘中的数据读出并缓存到内存中。
如果在buffer cache找到了对应的内存页,则会产生一个MnPF(minor page fault)。
/usr/bin/time -v helloworld
第一次执行会发现大部分是MPF
第二次执行会发现大部分是MnPF3. The File Buffer Cache
file buffer cache用来减少MPF,增加MnPF,它将会持续增长,直到可用内存比较少或是内核需要为其它应用来释放一些内存。free内存比较少,并不能说明系统内存紧张,只能说明linux系统充分使用内存来做cache.
cat /proc/meminfo
MemTotal: 24730888 kB # 总内存
MemFree: 2633168 kB # 空闲内存
Buffers: 2191776 kB # 写buffer(这里有问题吧,应该是block cache吧)
Cached: 15879728 kB # 读cache4. Types of Memory Pages
read pages: 只读的页,并且在磁盘中有对应文件,一般是静态文件,二进制文件,库。当内存短缺时,这些页可以直接丢弃,放到free list.
dirty pages: 在内存中被修改的页,需要使用pdflush/kswapd刷回磁盘。
anonymous pages: 属于某个进程的内存,但在磁盘中没有对应的文件,当内存短缺时,要写到swap5. Writing Data Pages Back to Disk
可以使用fsync()或是sync()立即写回,如果没有直接调用这些函数,pdflush会定期刷回磁盘。6. Tools for Monitoring I/O
top, vmstat, iostat, sar
10万转速的磁盘,一般的响应时间是8ms,可以达到120~150 IOPS.7. Sequential I/O vs Random I/O
## iotop可以显示所有应用的IO占用情况9. Summary
一旦CPU在等待IO,说明磁盘负载过重
计算磁盘可以承受的IOPS
顺序IO与随机IO
监控慢盘的等待时间和服务时间
监控swapSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
