mysql innodb flush method_对innodb_flush_method的一点解释
官方文檔描述如下:
By default, InnoDB uses the fsync()system call to flush both the data and log files. If
innodb_flush_method option is set to O_DSYNC, InnoDB uses O_SYNC to open and flush the
log files, and fsync()to flush the data files. If O_DIRECT is specified (available on some GNU/
Linux versions, FreeBSD, and Solaris), InnoDBuses O_DIRECT(or directio()on Solaris) to
open the data files, and uses fsync()to flush both the data and log files. Note that InnoDB uses
fsync() instead of fdatasync(), and it does not use O_DSYNC by default because there have
been problems with it on many varieties of Unix.
innodb_flush_method
常規(guī)3個(gè)值
1、fdatasync
2、O_DSYNC
3、O_DIRECT
正常的訪問方式
用戶態(tài)緩存---》內(nèi)核態(tài)緩存---》磁盤
按照MYSQL的描述
1、fdatasync
InnoDB uses the fsync() system call to flush both the data and log files.
Note that InnoDB uses fsync() instead of fdatasync()
雖然MYSQL可以使用fdatasync為參數(shù)但是實(shí)際上是調(diào)用的系統(tǒng)的 fsync()函數(shù),
我們可以看看LINUX下FSYNC()函數(shù)的描述
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) so that all changed information can be retrieved ?even
after ?the ?system crashed or was rebooted. ?This includes writing through or flushing a disk cache if present. ?The call blocks
until the device reports that the transfer has completed. ?It also flushes metadata information associated with ?the ?file ?(see
stat(2)).
簡(jiǎn)單的說這個(gè)參數(shù)用于同步所有線程修改過的文件,而進(jìn)程中的PCB中記錄了打開文件,他是通過文件描述符進(jìn)行匹配的
在LINUX內(nèi)核中/usr/src/linux-headers-3.19.0-25-generic/include/linux/sched.h
有如下的PCB結(jié)構(gòu)體
struct task_struct { }
其中有一個(gè)files_struct的結(jié)構(gòu)體,而文件描述符就是這樣一個(gè)結(jié)構(gòu)體的指針
那么只要MYSQL線程進(jìn)行了刷新動(dòng)作,那么他的這些文件的數(shù)據(jù)一定會(huì)同步到磁盤
2、O_DSYNC
InnoDB uses O_SYNC to open and flush the log files, and fsync()to flush the data files
當(dāng)設(shè)置為這個(gè)選項(xiàng)的時(shí)候,當(dāng)MYSQL線程打開LOGFILE的時(shí)候使用的O_SYNC的方式,而對(duì)數(shù)據(jù)文件還是使用的fsync()
我們知道對(duì)一個(gè)文件進(jìn)行讀,打開是使用LINUX的
open()函數(shù),而其中也就有這樣一個(gè)選項(xiàng)
O_SYNC The ?file ?is ?opened for synchronous I/O. ?Any write(2)s on the resulting file descriptor will block the calling process
until the data has been physically written to the underlying hardware.
他描述的是如果這樣打開一個(gè)文件那么在數(shù)據(jù)從內(nèi)核態(tài)緩存寫到了物理磁盤前,任何試圖修改文件描述符的進(jìn)程都會(huì)被堵塞。如此保證了日志文件
最大的安全性
3、O_DIRECT
If O_DIRECT is specified (available on some GNU/Linux versions, FreeBSD, and Solaris), InnoDB
uses O_DIRECT(or directio()on Solaris) to open the data files, and uses
fsync()to flush both the data and log files.
使用這個(gè)選項(xiàng)MYSQL使用O_DIRECT方式打開數(shù)據(jù)文件,而fsync()會(huì)最終同步所有的數(shù)據(jù)和日志文件。
我們同樣看看open()函數(shù)中關(guān)于O_DIRECT描述
O_DIRECT (Since Linux 2.4.10)
Try to minimize cache effects of the I/O to and from this file. ?In general this will degrade performance, but it is use‐
ful ?in special situations, such as when applications do their own caching. ?File I/O is done directly to/from user-space
buffers. ?The O_DIRECT flag on its own makes an effort to transfer data synchronously, but does not give ?the ?guarantees
of ?the ?O_SYNC flag that data and necessary metadata are transferred. ?To guarantee synchronous I/O, O_SYNC must be used
in addition to O_DIRECT.
使用這個(gè)選項(xiàng)一般來說會(huì)降低性能,但是在特定的情況下比如應(yīng)用程序有自己的緩存機(jī)制。I/O直接來自用戶態(tài)的緩存,O_DIRECT標(biāo)識(shí)對(duì)大量的
數(shù)據(jù)寫有利,因?yàn)樗@開了內(nèi)核態(tài)緩存,但是他并同步METADATA(這里占時(shí)理解為INODE緩存,也就是文件的基本信息比如打開時(shí)間修改時(shí)間等)
所以要完成同步必須同時(shí)調(diào)用O_SYNC。
如此我們也了解為什么為什么使用O_DIRECT還會(huì)調(diào)用FSYNC()我們知道FSYNC()是全同步的,LINUX上的傳統(tǒng)的FDATASYNC()是不同步METADATA的
如INODE緩存,進(jìn)程描述緩存。但是他能夠?qū)Υ罅康臄?shù)據(jù)繞開緩存,提高性能,需要最后同步的只是DATAFILE的METADAT。
MYSQL有自己的緩沖,這種可以使用O_DIRECT比較好
水平有限,如有錯(cuò)誤請(qǐng)指出
來自 “ ITPUB博客 ” ,鏈接:http://blog.itpub.net/7728585/viewspace-1980262/,如需轉(zhuǎn)載,請(qǐng)注明出處,否則將追究法律責(zé)任。
總結(jié)
以上是生活随笔為你收集整理的mysql innodb flush method_对innodb_flush_method的一点解释的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 匿名块_MySQL存储过程定
- 下一篇: mysql生成app接口_Java实现a