mysql 查看函数fsync_sync/fsync/fdatasync的简单比较

mysql 查看函数fsync_sync/fsync/fdatasync的简单比较此文主要转载自官网上有关于MySQL的flushmethod的设置参数说明,但可能很多人不太明白。下文就详细说明此问题。首先官网的说明如下:http://dev.mysql.com/doc/refman/5.6/en/innodb-parameters.html#sysvar_innodb_flush_methodinnodb_flush_methodCommand-LineFormat–i…

大家好,又见面了,我是你们的朋友全栈君。

此文主要转载自

官网上有关于MySQL的flush method的设置参数说明,但可能很多人不太明白。下文就详细说明此问题。

首先官网的说明如下:

http://dev.mysql.com/doc/refman/5.6/en/innodb-parameters.html#sysvar_innodb_flush_method

innodb_flush_methodCommand-Line Format–innodb_flush_method=name

Option-File Formatinnodb_flush_method

System Variable Name

Variable ScopeGlobal

Dynamic VariableNo

Permitted Values (<= 5.6.6)

Type (Linux)string

Defaultfdatasync

Valid ValuesO_DSYNC

O_DIRECT

Permitted Values (<= 5.6.6)

Type (HP-UX)string

Defaultfdatasync

Valid ValuesO_DSYNC

O_DIRECT

Permitted Values (<= 5.6.6)

Type (Solaris)string

Defaultfdatasync

Valid ValuesO_DSYNC

O_DIRECT

Permitted Values (>= 5.6.7)

Type (Linux)string

Defaultfdatasync

Valid Valuesfdatasync

O_DSYNC

O_DIRECT

O_DIRECT_NO_FSYNC

Permitted Values (>= 5.6.7)

Type (Solaris)string

Defaultfdatasync

Valid Valuesfdatasync

O_DSYNC

O_DIRECT

O_DIRECT_NO_FSYNC

Permitted Values (>= 5.6.7)

Type (HP-UX)string

Defaultfdatasync

Valid Valuesfdatasync

O_DSYNC

O_DIRECT

O_DIRECT_NO_FSYNC

Controls the system calls used to          flush data to the          InnoDB data

files and log

files, which can influence I/O throughput. This

variable is relevant only for Unix and Linux systems. On

Windows systems, the flush method is always          async_unbuffered and cannot be changed.

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),          InnoDB uses 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.

An alternative setting is          O_DIRECT_NO_FSYNC: it uses the          O_DIRECT flag during flushing I/O, but

skips the fsync() system call afterwards.

This setting is suitable for some types of filesystems but not

others. For example, it is not suitable for XFS. If you are

not sure whether the filesystem you use requires an          fsync(), for example to preserve all file

metadata, use O_DIRECT instead.

Depending on hardware configuration, setting          innodb_flush_method to          O_DIRECT or          O_DIRECT_NO_FSYNC can have either a

positive or negative effect on performance. Benchmark your

particular configuration to decide which setting to use, or

whether to keep the default. Examine the          Innodb_data_fsyncs status

variable to see the overall number of          fsync() calls done with each setting. The

mix of read and write operations in your workload can also

affect which setting performs better for you. For example, on

a system with a hardware RAID controller and battery-backed

write cache, O_DIRECT can help to avoid

double buffering between the InnoDB buffer

pool and the operating system’s filesystem cache. On some

systems where InnoDB data and log files are

located on a SAN, the default value or          O_DSYNC might be faster for a read-heavy

workload with mostly SELECT statements.

Always test this parameter with the same type of hardware and

workload that reflects your production environment. For

general I/O tuning advice, see          Section 8.5.7, “Optimizing InnoDB Disk I/O”.

Formerly, a value of fdatasync also

specified the default behavior. This value was removed, due to

confusion that a value of fdatasync caused          fsync() system calls rather than          fdatasync() for flushing. To obtain the

default value now, do not set any value for          innodb_flush_method at

startup.

里面提到了fsync()和fdatasync()系统调用,下文给予了详细解释。

之前在研究MySQL的一个参数innodb_flush_method时,就涉及到了fsync/fdatasync这些系统调用[system

call](什么是系统调用?它与库函数的区别在哪?参见这里)。接下来就简单的分析一下sync/fsync/fdatasync的区别。

sync():int sync( void )这就是它的原型,A call to this function will not

return as long as there is data which has not been written to the

device,sync()同步写,没有写到物理设备就不会返回,但是现实中并不是这样的。在kernel的手册上有解释:BUGS部分(linux中用man查看命令的时候不是都有一个BUGS部分么,就是指的那个)According

to the standard specification (e.g., POSIX.1-2001), sync() schedules the writes, but may return before the

actual writing is done.  However, since version 1.3.20 Linux does

actually wait.  (This still does not guarantee

data integrity: modern disks have large

caches.)也就是sync()负责将这些写物理设备的请求放入写队列,但是不一定写真正被完成了。

fsync(int fd):The fsync function can be used to make sure all data

associated with the open file fildes is written to the device associated with

the

descriptor。fsync()负责将一个文件描述符(什么是文件描述符,它是unix、类unix系统打开文件的一种方式,应该相当于打开文件的一个句柄一样)打开的文件写到物理设备,而且是真正的同步写,没有写完成就不会返回,而且写的时候讲文件本身的一些元数据都会更新到物理设备上去,比如atime,mtime等等。

fdatasync(int fd):When a call to the fdatasync function returns, it

is ensured that all of the file data is written to the

device。它只保证开打文件的数据全部被写到物理设备上,但是一些元数据并不是一定的,这也是它与fsync的区别。

这三个系统调用都简单的介绍完,那么为什么需要它们三个呢?最简单的说是从应用的需求来考虑的,sync是全局的,对整个系统都flush,fsync值针对单个文件,fdatasync当初设计是考虑到有特殊的时候一些基本的元数据比如atime,mtime这些不会对以后读取造成不一致性,因此少了这些元数据的同步可能会在性能上有提升(但fsync和fdatasync两者的性能差别有多大?这个不知道有谁测过没)。所以说三者是根据不同的需求而定的。

接下来谈谈flush dirty

page,也就是前面说的同步写(没写完的话阻塞后面,直到写完才返回)。为什么是刷脏页?脏页表示缓存中的页(一般也就是内存中)也物理设备上的页处于不一致,不一致是由于在内存中被修改。所以为了使内存中的修改持久化到物理磁盘上我们需要将其从内存中flush到物理磁盘上。根据我的理解,一般来说缓存分成这几种:1>应用程序自己带了缓存,比如InnoDB的buffer

pool;2>os层面上的缓存

;3>磁盘设备自己的缓存,比如raid卡一般都管理着自己的缓存;4>磁盘本身或许会有一点点缓存(这个不确定,自己猜想的,这个即使有估计也是极小的)。好了,那么大部分的时候我们说的flush

dirty

page都是指从应用程序的缓存->os的缓存->物理设备,如果物理设备没有缓存的话,此时也就相当于持久化成功,但是像磁盘做了raid,raid卡有缓存的话,实际上还没真正持久化成功,因为此时还只到了raid卡的缓存,没到物理设备,但是由于raid卡一般都带有备用电池,所以即使此时断电也不会造成数据丢失。

刚才说了很多时候应用自己也有缓存机制,那么你是否想过此时与os的缓存有重复呢?答案是:会的。刚才说了我是通过研究MySQL的一个参数innodb_flush_method注意这些的,innodb_flush_method表示flush策略,MySQL提供了fdatasync/O_DSYNC/O_DIRECT这三个选项,默认是fdatasync(详情可参看博文)我这里主要说明为什么会提供选项:O_DIRECT。这个选项告诉os,InnoDB在读写数据的时候都不经过os的缓存,因为刚才说过InnoDB会维护自己的缓存buffer

pool,如果还使用os的缓存那么两者就会有一定的重复。在前面参考的文章里面说O_DIRECT对大量随即读写有效率提升,顺序读写则会下降。所以根据自己的需求来定,不过如果你的MySQL用在是OLTP上,基本上选择O_DIRECT没错。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/138444.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • CAP原理详解「建议收藏」

    文章目录一、CAP原理介绍对CAP原理的一些常见的理解误区二、CAP原理简单证明三、CAP原理在各个系统的应用四、总结一、CAP原理介绍先简单介绍一下CAP原理是什么:C:Consistency即一致性,访问所有的节点得到的数据应该是一样的。注意,这里的一致性指的是强一致性,也就是数据更新完,访问任何节点看到的数据完全一致,要和弱一致性,最终一致性区分开来。A:Availability…

    2022年4月3日
  • java中文乱码_Java中文乱码问题的解决方案[通俗易懂]

    java中文乱码_Java中文乱码问题的解决方案[通俗易懂]只要掌握了中文乱码问题产生的原因,然后对症下药,就可以顺利地解决这些问题。下面我们对容易产生乱码问题的场景进行分析,并提出解决方案。1.以POST方法提交的表单数据中有中文字符由于Web容器默认的编码方式是ISO-8859-1,在Servlet/JSP程序中,通过请求对象的getParameter()方法得到的字符串是以ISO-8859-1转换而来,这是导致乱码产生的原因之一。为了避免容器以ISO…

    2022年7月8日
  • 优先队列「建议收藏」

    优先队列「建议收藏」优先队列比如现实生活中的排队,就符合这种先进先出的队列形式,但是像急诊医院排队,就不可能按照先到先治疗的规则,所以需要使用优先队列。实现优先队列其实都是基于下面这些实现的:可以看出来实现优先队列最

    2022年7月3日
  • java、spring线程池面试题

    java、spring线程池面试题一、线程池的好处?1.通过newThread来创建线程池会比较耗时,性能差,当我们在通过线程的时候,有可能会出现(创建线程+销毁线程)的时长>线程执行(业务逻辑)的时长;2.线程缺乏统一管理,可能会出现无限制的创建线程,线程之间相互竞争,争夺资源而导致系统崩溃;3.缺乏更多的管理功能,比如定时执行、定期执行、线程中断;相比较于newThread,创建线程的好处在于:1.重用已存在的线程,避免线程新建和消亡产生的开销。2.可以控制最大并发数,避免同时多个线程执行,争夺资源,导致系统崩溃;

    2022年5月6日
  • 织梦CMS系统中power by dedecms怎么去掉?power by dedecms什么意思?

    织梦CMS系统中power by dedecms怎么去掉?power by dedecms什么意思?织梦CMS近期的新版本至2013-6-7更新包以来,不管新版还是旧版更新补丁包,更新后网站页底都会出现powerbydedecms。powerbydedecms什么意思呢,那powerbydedecms怎么去掉呢,请大家看以下方法:一、powerbydedecms什么意思在我们上网的时候,会见到页面页底很多带powerbydedecms的网站,powerbyded…

    2022年7月13日
  • java发送邮件mail.jar[通俗易懂]

    java发送邮件mail.jar[通俗易懂]这里短信发送是基于对mail.jar封装的jar包开发的,但是基本参数是一样的,可以参考packagecom.dw;importcom.dareway.framework.exception.AppException;importcom.dareway.framework.mail.MailInfo;importcom.dareway.framework.mail.Mail…

    2022年5月15日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号