Open log | Flush log | Open datafile | Flush data | |
Fdatasync | fsync() | fsync() | ||
O_DSYNC | O_SYNC | fsync() | ||
O_DIRECT | fsync() | O_DIRECT |
Fsync() |
在学习innodb_flush_method的方法时,看到下面的文章:
http://www.orczhou.com/index.php/2009/08/innodb_flush_method-file-io/
文章对此参数介绍的比较详细。(摘录其中一段。。。。)
参数Innodb_flush_method(Linux)可以设定为:Fdatasync、O_DSYNC、O_DIRECT。我们看看这个三个参数是如何影响程序MySQL对日志和数据文件的操作:
Open log | Flush log | Open datafile | Flush data | |
Fdatasync | fsync() | fsync() | ||
O_DSYNC | O_SYNC | fsync() | ||
O_DIRECT | fsync() | O_DIRECT |
Fsync() |
POSIX provides for three different variants of synchronised I/O, corresponding to the flags O_SYNC , O_DSYNC and O_RSYNC . Currently (2.1.130) these are all synonymous under Linux.
#include
int fflush(FILE *stream);
Description
The function fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function. The open status of the stream is unaffected.
Notes
Note that fflush() only flushes the user space buffers provided by the C library. To ensure that the data is physically stored on disk the kernel buffers must be flushed too.
此时就需要用到fsync 或者sync函数。
#include
void sync(void);
Description
sync() first commits inodes to buffers, and then buffers to disk.
#include
int fsync(int fd);
int fdatasync(int fd);
Description
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)).
fsync 将文件相关的所有更改都发送到disk device。 这个调用是阻塞的,直到disk通知此函数传输完成。此函数也会将该文件的文件信息flush到disk。
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.
fdatasync与fsync的区别在于fdatasync不会flush文件的metadata信息。这个是为了减少对磁盘的操作。。。
Notes
If the underlying hard disk has write caching enabled , then the data may not really be on permanent storage when fsync() / fdatasync() return.
When an ext2 file system is mounted with the sync option, directory entries are also implicitly synced by fsync().
On kernels before 2.4, fsync() on big files can be inefficient. An alternative might be to use the O_SYNC flag to open.