Linux I/0 and Innodb flushing

How Linux does I/O


1. By default, the write() system call returns after all data has been copied from the user space file descriptor into the kernel space buffers.

There is no guarantee that data has actually reached the physical storage.

默認情況下,write()系統調用返回後所有數據的安全提供程序已從用戶空間文件描述符都複製到內核空間緩衝區。那裏是不能保證數據實際上已達到的物理存儲。


2. The fsync() call is our friend here. This will block and return only after the data and metadata

   (e.g. file size, last update time) is completely transferred to the actual physical storage.

Fsync()調用是經常用的這裏。這將阻止並將返回直到數據和元數據(例如文件大小,上次更新時間)完全轉移到實際的物理存儲。

fsync的功能是確保文件fd所有已修改的內容已經正確同步到硬盤上,該調用會阻塞等待直到設備報告IO完成。


3. There is also fdatasync() which only guarantees the data portion will be transferred, so it should be faster.

    有幾個選項,我們可以指定在文件打開時,修改的 write()的行爲︰

    fdatasync函數類似於fsync,但它隻影響文件的數據部分。而除數據外,fsync還會同步更新文件的屬性(metadata)。


a. O_SYNC

In this case, the write() system call will still write data to kernel space buffers, 

but it will block until the data is actually transferred from the kernel space buffers to the physical storage. There is no need to call fsync() after.


b. O_DIRECT

This completely bypasses any kernel space buffers, but requires that the writes are the same size as the underlying storage block size (usually 512 bytes or 4k).

By itself, it does not guarantee that the data is completely transferred to the device when the call returns.


c.O_SYNC + O_DIRECT

As stated above, we would need to use both options together guarantee true synchronous IO.



Relation with InnoDB flushing(Innodb引擎刷盤策略)


1. fsync: InnoDB uses the fsync() system call to flush both the data and log files. fsync is the default setting.(innodb_flush_method=NULL


2. O_DSYNC: InnoDB uses O_SYNC to open and flush the log files, and fsync() to flush the data files. InnoDB does not use O_DSYNC directly because there have been problems with it on many varieties of Unix.


3. nosync: This option is used for internal performance testing and is currently unsupported. Use at your own risk.


4. O_DIRECT: InnoDB uses O_DIRECT (or directio() on Solaris) to open the data files, and uses fsync() to flush both the data and log files


5. O_DIRECT_NO_FSYNC:  use O_DIRECT to open the data files(InnoDB uses O_DIRECT during flushing I/O) but don’t use fsync() system call to flush both the data and log files. This option isn’t suitable for XFS file system.



https://www.pythian.com/blog/innodb-flushing-linux-io/

https://www.percona.com/doc/percona-server/5.6/scalability/innodb_io.html#innodb_flush_method

http://www.gpfeng.com/?p=474


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章