innodb_flush_log_at_trx_commit参数确定日志文件何时刷新,innodb_flush_method则决定日志及数据文件如何刷新。
在Linux下,innodb_flush_method可以取如下值:fdatasync, O_DSYNC, O_DIRECT
Fdatasync—默认设置,InnDB调用fsync()刷新数据和日志文件;实际上InnoDB使用fsync()替换fdatasync(),该名称很有歧义;
O_DSYNC—InnoDB使用O_SYNC打开并刷新日志文件,使用fsync()刷新数据文件;
O_DIRECT—只用于数据文件,不能用于日志文件;InnoDB使用O_DIRECT打开数据文件,fsync()刷新数据/日志文件;
其对照方式如下
|
Open log |
Flush log |
Open datafile |
Flush data |
|
Fdatasync |
|
fsync() |
|
fsync() |
|
O_DSYNC |
O_SYNC |
|
|
fsync() |
|
O_DIRECT |
|
Fsync() |
O_DIRECT |
Fsync() |
什么是fsync()
--将数据从buffer刷新至磁盘,包括data和metadata;相比之下fdatasync()只刷新data,节省了1次IO操作;
--但是InnoDB并不支持fdatasyn(),尽管innodb_flush_method的默认选项为fdatasync,它实际上调用的还是fsync()
--如果磁盘开启了缓存
以下是linux 2.6.32的解释
fsync() transfers ("flushes") all modified in-core data of (i.e., modified buffer cache pages for) the file referred to by the file descriptor fd to the disk device (or other permanent storage device) where that file resides. The call blocks until the device reports that the transfer has completed. It
also flushes metadata information associated with the file (see stat(2)).
Calling fsync() does not necessarily ensure that the entry in the directory containing the file has also reached disk. For that an explicit fsync() on a file descriptor for the directory is also needed.
fdatasync() is similar to fsync(), but does not flush modified metadata unless that metadata is needed in order to allow a subsequent data retrieval to be correctly handled. For example, changes to st_atime or st_mtime (respectively, time of last access and time of last modification; see stat(2)) do not not require flushing because they are not necessary for a subsequent data read to be handled correctly. On the other hand, a change to the file size (st_size, as made by say ftruncate(2)), would require a metadata flush.
The aim of fdatasync(2) is to reduce disk activity for applications that do not require all metadata to be synchronised with the disk.
Note:
If the underlying hard disk has write caching enabled, then the data may not really be on permanent storage when fsync() / fdatasync() return.
什么是O_DSYNC和O_DIRECT
O_DSYNC是mysql定义的(但linux 2.6.33引入了此选项),就是OS上的O_SYNC,linux的open()第2个参数flag的一个选项,即同步IO写;O_DSYNC与O_SYNC的关系就好像Fdatasync与fsync,D代表data;
O_SYNC:The file is opened for synchronous I/O. Any write()s on the resulting file descriptor will block the calling process until the data has been physically written to the underlying hardware
O_DIRECT跳过OS缓存,也是采用同步机制;2.6下必须block size对齐
O_DIRECT:Try to minimize cache effects of the I/O to and from this file. In general this will degrade performance, but it is useful in special situations, such as when applications do their own caching. File I/O is done directly to/from user space buffers. The I/O is synchronous, i.e., at the completion of a read(2) or write(2), data is guaranteed to have been transferred. Under Linux 2.4 transfer sizes, and the alignment of user buffer and file offset must all be multiples of the logical block size of the file system. Under Linux 2.6 alignment must fit the block size of the device.
Fsync()和O_SYNC的异同
O_SYNC是在open()函数中声明的,任何后续的write()都会自动将其缓存数据同步到磁盘;
Fsync()则需要系统调用,
与innoDB不信任fdatasync()相比,mysql服务器对其他文件刷新都调用此API
sync_binlog、sync_frm、sync_master_info、sync_relay_log、sync_relay_log_info
均采用fdatasync(),除sync_frm,其他参数默认值都为0,即交由OS负责处理,如果N>0,则每N次触发后调用fdatasync()刷新至磁盘
http://space.itpub.net/15480802/viewspace-755325
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/15480802/viewspace-757291/
,如需转载,请注明出处,否则将追究法律责任。