[Unix/Linux Programming] Buffered I/O vs Unbuffered I/O
2011-08-18 22:49
666 查看
SEE HERE: Stackoverflow: buffered I/O vs unbuffered IO
============================================================================================================
http://xiaomeng.yo2.cn/articles/tag/direct-io
实验需要对Flash Disk做无系统缓冲的I/O操作,顺便了解了一下Linux下的I/O.
Linux上的块设备的操作可以分为两类:
第一类是使用C标准库中的fopen/fread/fwrite 系列的函数,我们可以称其为 buffered I/O。
具体的I/O path如下
Application<->Library Buffer<->Operation System Cache<->File System/Volume Manager<->Device
library buffer是标准库提供的用户空间的buffer,可以通过setvbuf改变其大小。
第二类是使用Linux的系统调用的open/read/write 系列的函数,我们可以称其为 non-buffered I/O。
I/O Path
Application<-> Operation System Cache <->File System/Volume Manager<->Device
此外,我们可以通过设置open的O_DIRECT标志来实现Direct I/O(或者叫Raw I/O),即绕过OS Cache,直接读取Device ( that's
what we want^o^ ), 等于将OS cache换成自己管理的cache。不过,Linus在邮件列表中建议不这么做,而是使用posix_fadvice, madvice。[2]中表明Direct
I/O比buffered I/O的性能高很多。
在使用O_DIRECT的注意buffer的address必须是block alignment的(i.e. 初始地址必须是boundary), 可以用posix_memalign()函数分配内存以得到这样的buffer。至于为什么要这样,与实现的mmap有关,参见[5].
参考:
Linux: Accessing Files With O_DIRECT http://kerneltrap.org/node/7563
Andrea Arcangeli , O_DIRECT Whitepaperhttp://www.ukuug.org/events/linux2001/papers/html/AArcangeli-o_direct.html
A Trip Down the Data Path: I/O and Performancehttp://articles.directorym.net/_A_Trip_Down_the_Data_Path_IO_and_Performance-a894569.html
Operating Systems System Calls and I/Ohttp://articles.directorym.net/Operating_Systems_System_Calls_and_IO-a894576.html
Linux Device Drivers, 2nd Edition, Chapter 13 mmap and DMAhttp://www.xml.com/ldd/chapter/book/ch13.html
http://topic.csdn.net/u/20080806/10/cdb1faa1-0146-4e96-8b12-26ba60acdbb5.html
http://lists.alioth.debian.org/pipermail/parted-devel/2007-July/thread.html#1855
Read系统调用剖析, http://www.ibm.com/developerworks/cn/linux/l-cn-
另外推荐一篇不错的文章:http://www.ibm.com/developerworks/cn/linux/l-async/
===================================================================
http://zishen.blogspot.com/2011/03/buffered-io-unbuffered-io.html
簡單來說,
Buffered I/O 讀寫的資料都會經過 Cache Manager,會暫存一份在 Memory 當中,然後會根據作業系統決定寫回硬碟的時機(如果用 Write Through,還是可以自行控制)。此模式適合常讀寫檔案,因為可以快速的從 Memory 讀寫。
(想想 L1, L2 cache 之於 RAM 的關係。)
Unbuffered I/O 讀寫的資料不會經過 Cache Manager,而是直接對硬碟讀寫。因此也不會因為大量讀寫資料造成 Cache Trashing。
以下是我的推測,不代表真實測試結果。XD
Installer 的檔案複製可能就適合 Unbuffered I/O,因為 Buffered I/O 可能會造成大量的 Cache Trashing。
當有大量的 Cache Trashing 發生時,可能也會影響到 Unbuffered I/O 的效能。因為 Cache Manager 會對硬碟做大量讀寫,這個行為可能會和 Unbuffered I/O 產生 Resource Contention。
參考資料:
"Slow Large File Copy Issues" by Ask the Performance Team
Asynchronous Disk I/O Appears as Synchronous on Windows NT, Windows 2000, and Windows XP
====================================================================================================================================
Quote:
Try Google for more information:
Google: "BUffered I/O"
__________________
If this answer helps you, please click the thanks button.
-- Paul at http://arachnoid.com
============================================================================================================
Buffered I/O and non-buffered I/O
http://xiaomeng.yo2.cn/articles/tag/direct-io实验需要对Flash Disk做无系统缓冲的I/O操作,顺便了解了一下Linux下的I/O.
Linux上的块设备的操作可以分为两类:
第一类是使用C标准库中的fopen/fread/fwrite 系列的函数,我们可以称其为 buffered I/O。
具体的I/O path如下
Application<->Library Buffer<->Operation System Cache<->File System/Volume Manager<->Device
library buffer是标准库提供的用户空间的buffer,可以通过setvbuf改变其大小。
第二类是使用Linux的系统调用的open/read/write 系列的函数,我们可以称其为 non-buffered I/O。
I/O Path
Application<-> Operation System Cache <->File System/Volume Manager<->Device
此外,我们可以通过设置open的O_DIRECT标志来实现Direct I/O(或者叫Raw I/O),即绕过OS Cache,直接读取Device ( that's
what we want^o^ ), 等于将OS cache换成自己管理的cache。不过,Linus在邮件列表中建议不这么做,而是使用posix_fadvice, madvice。[2]中表明Direct
I/O比buffered I/O的性能高很多。
在使用O_DIRECT的注意buffer的address必须是block alignment的(i.e. 初始地址必须是boundary), 可以用posix_memalign()函数分配内存以得到这样的buffer。至于为什么要这样,与实现的mmap有关,参见[5].
参考:
Linux: Accessing Files With O_DIRECT http://kerneltrap.org/node/7563
Andrea Arcangeli , O_DIRECT Whitepaperhttp://www.ukuug.org/events/linux2001/papers/html/AArcangeli-o_direct.html
A Trip Down the Data Path: I/O and Performancehttp://articles.directorym.net/_A_Trip_Down_the_Data_Path_IO_and_Performance-a894569.html
Operating Systems System Calls and I/Ohttp://articles.directorym.net/Operating_Systems_System_Calls_and_IO-a894576.html
Linux Device Drivers, 2nd Edition, Chapter 13 mmap and DMAhttp://www.xml.com/ldd/chapter/book/ch13.html
http://topic.csdn.net/u/20080806/10/cdb1faa1-0146-4e96-8b12-26ba60acdbb5.html
http://lists.alioth.debian.org/pipermail/parted-devel/2007-July/thread.html#1855
Read系统调用剖析, http://www.ibm.com/developerworks/cn/linux/l-cn-
另外推荐一篇不错的文章:http://www.ibm.com/developerworks/cn/linux/l-async/
===================================================================
http://zishen.blogspot.com/2011/03/buffered-io-unbuffered-io.html
簡單來說,
Buffered I/O 讀寫的資料都會經過 Cache Manager,會暫存一份在 Memory 當中,然後會根據作業系統決定寫回硬碟的時機(如果用 Write Through,還是可以自行控制)。此模式適合常讀寫檔案,因為可以快速的從 Memory 讀寫。
(想想 L1, L2 cache 之於 RAM 的關係。)
Unbuffered I/O 讀寫的資料不會經過 Cache Manager,而是直接對硬碟讀寫。因此也不會因為大量讀寫資料造成 Cache Trashing。
以下是我的推測,不代表真實測試結果。XD
Installer 的檔案複製可能就適合 Unbuffered I/O,因為 Buffered I/O 可能會造成大量的 Cache Trashing。
當有大量的 Cache Trashing 發生時,可能也會影響到 Unbuffered I/O 的效能。因為 Cache Manager 會對硬碟做大量讀寫,這個行為可能會和 Unbuffered I/O 產生 Resource Contention。
參考資料:
"Slow Large File Copy Issues" by Ask the Performance Team
Asynchronous Disk I/O Appears as Synchronous on Windows NT, Windows 2000, and Windows XP
====================================================================================================================================
Quote:
Try Google for more information:
Google: "BUffered I/O"
__________________
If this answer helps you, please click the thanks button.
-- Paul at http://arachnoid.com
相关文章推荐
- 《Shell Programming in Unix, Linux and OS X, 4th Edition 》 笔记之Can I Quote You On That?
- 《Shell Programming in Unix, Linux and OS X, 4th Edition 》 笔记之Passing Arguments
- Understanding Unix/Linux Programming-用户程序play_again4.c
- Understanding Unix/Linux Programming 笔记:chapter 12:连接和协议:编写Web服务器
- Understanding Unix/Linux Programming-事件驱动编程:编写一个视频游戏
- UNIX环境高级编程——标准I/O库函数和Unbuffered I/O函数
- Understanding Unix/Linux Programming note:chapter 5:连接控制:学习stty
- Understanding Unix/Linux Programming-cp指令练习
- Understanding Unix/Linux Programming-时钟编程:Alarms
- [linux and unix shell programming] and [the unix environment programming]
- Understanding Unix/Linux Programming-who指令练习
- Understanding Unix/Linux Programming note:chapter 1:more函数的流程图
- 本周技术关注:Advanced Linux and Unix Programming
- UNIX环境高级编程——标准I/O库函数和Unbuffered I/O函数
- Understanding Unix/Linux Programming-ls指令练习一
- Understanding Unix/Linux Programming 笔记:chapter 9:可编程的shell、shell变量和环境; chapter 10:I/O重定向和管道
- Understanding Unix/Linux Programming-ls指令练习二
- Absloute path vs relative path in Linux/Unix(Linux/Unix中的绝对路径vs相对路径)
- Understanding Unix/Linux Programming note:chapter 6:为用户编程:终端控制和信号
- Understanding Unix/Linux Programming-pwd指令练习