此文主要转载自

http://blog.csdn.net/zbszhangbosen/article/details/7956558

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

首先官网的说明如下:

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

innodb_flush_method

Command-Line Format --innodb_flush_method=name
Option-File Format innodb_flush_method
System Variable Name innodb_flush_method
Variable Scope Global
Dynamic Variable No
  Permitted Values (<= 5.6.6)
Type (Linux) string
Default fdatasync
Valid Values O_DSYNC
O_DIRECT
  Permitted Values (<= 5.6.6)
Type (HP-UX) string
Default fdatasync
Valid Values O_DSYNC
O_DIRECT
  Permitted Values (<= 5.6.6)
Type (Solaris) string
Default fdatasync
Valid Values O_DSYNC
O_DIRECT
  Permitted Values (>= 5.6.7)
Type (Linux) string
Default fdatasync
Valid Values fdatasync
O_DSYNC
O_DIRECT
O_DIRECT_NO_FSYNC
  Permitted Values (>= 5.6.7)
Type (Solaris) string
Default fdatasync
Valid Values fdatasync
O_DSYNC
O_DIRECT
O_DIRECT_NO_FSYNC
  Permitted Values (>= 5.6.7)
Type (HP-UX) string
Default fdatasync
Valid Values fdatasync
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_DSYNCInnoDB          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没错。

sync/fsync/fdatasync的简单比较的更多相关文章

  1. sync fsync fdatasync

    传统的UNIX实现在内核中设有缓冲区高速缓存或页面高速 缓存,大多数磁盘I/O都通过缓冲进行.当将数据写入文件时,内核通常先将该数据复制到其中一个缓冲区中,如果该缓冲区尚未写满,则并不将其排入输出队 ...

  2. sync fsync fdatasync ---systemtap跟踪

    aa.stp: probe kernel .function ( "sys_sync" ) { printf ( "probfunc:%s fun:%s\n", ...

  3. 【APUE】第3章 文件I/O (3) 文件共享、原子操作、函数dup/dup2、函数sync/fsync/fdatasync、函数fcntl、函数ioct1、目录/dev/fd 使用说明

    1.文件共享 UNIX系统支持在不同的进程间共享打开文件.为了说明这种共享,以下介绍内核用于所有I/O的数据结构. 内核使用3种数据结构表示打开文件,它们之间的关系决定了在文件共享方面一个进程对另一个 ...

  4. mysql strace fsync,fdatasync

    mysql> show create table y; +-------+------------------------------------------------------------ ...

  5. mysql在高内存、IO利用率上的几个优化点 (sync+fsync) 猎豹移动技术博客

    http://dev.cmcm.com/archives/107 Posted on 2014年10月16日 by liuding | 7条评论 以下优化都是基于CentOS系统下的一些优化整理,有不 ...

  6. Linux/UNIX编程如何保证文件落盘

    本文转载自Linux/UNIX编程如何保证文件落盘 导语 我们编写程序write数据到文件中时,其实数据不会立马写入磁盘,而是会经过层层缓存.每层缓存都有自己的刷新时机,每层缓存都刷新后才会写入磁盘. ...

  7. UBIFS文件系统介绍

    1.  引言 UBIFS,Unsorted Block Image File System,无排序区块图像文件系统.它是用于固态硬盘存储设备上,并与LogFS相互竞争,作为JFFS2的后继文件系统之一 ...

  8. 关于文件的INode与Java中的文件操作接口

    本文由作者周梁伟授权网易云社区发布. 近日做的项目中涉及到多进程共同读写多个文件的问题,文件名和最后修改时间都是可能会被频繁修改的,因而识别文件的唯一性会产生相当的麻烦,于是专门再学习了一下文件系统对 ...

  9. [03]APUE:文件 I/O

    [a] open #include <fcntl.h> int open(const char *path, int oflag, ... ,mode_t mode) 成功返回文件描术符, ...

随机推荐

  1. Seata分布式事务框架Sample

    前言 阿里官方给出了seata-sample地址,官方自己也对Sample提供了很多类型,可以查看学习. 我这里选择演示SpringBoot+MyBatis. 该聚合工程共包括5个module: sb ...

  2. 操作系统-Linux命令

    一.目录结构 #因为根目录与开机有关,开机过程中仅有根目录会被挂载, 因此根目录下与开机过程有关的目录(以下5个),不能与根目录放到不同的分区去. /etc:配置文件 /dev:所需要的装置文件 /l ...

  3. springboot——重定向解决刷新浏览器造成表单重复提交的问题(超详细)

    原因:造成表单重复提交的原因是当我们刷新浏览器的时候,浏览器会发送上一次提交的请求.由于上一次提交的请求方式为post,刷新浏览器就会重新发送这个post请求,造成表单重复提交. 解决办法: 将请求当 ...

  4. 内核、dns、网卡配置

    升级内核(安装新版软件包) rpm -ivh kernel-3.10.0-123.1.2.el7.x86_64.rpm 二.配置永久IP地址,子网掩码,网关地址   /etc/sysconfig/ne ...

  5. Reactor3 中文文档(用户手册)

    文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...

  6. MySQL 面试必备:又一神器“锁”,不会的在面试都挂了

    1 什么是锁 1.1 锁的概述 在生活中锁的例子多的不能再多了,从古老的简单的门锁,到密码锁,再到现在的指纹解锁,人脸识别锁,这都是锁的鲜明的例子,所以,我们理解锁应该是非常简单的. 再到MySQL中 ...

  7. WEB 三维引擎在高精地图数据生产的探索和实践

    1. 前言 高精地图(High Definition Map)作为自动驾驶安全性不可或缺的一部分,能有效强化自动驾驶的感知能力和决策能力,提升自动驾驶的等级.对于自动驾驶来说,高精地图主要是给机器用的 ...

  8. theUnforgiven——项目冲刺

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/computer-science-class1-2018/ 小组号和队名 8组theUnforgiven ...

  9. .NET Core/.NET5/.NET6 开源项目汇总10:实用工具

    系列目录     [已更新最新开发文章,点击查看详细] 开源项目是众多组织与个人分享的组件或项目,作者付出的心血我们是无法体会的,所以首先大家要心存感激.尊重.请严格遵守每个项目的开源协议后再使用.尊 ...

  10. Kubernetes自动横向伸缩集群节点以及介绍PDB资源

    在kubernetes中,有HPA在需要的时候创建更多的pod实例.但万一所有的节点都满了,放不下更多pod了,怎么办?显然这个问题并不局限于Autoscaler创建新pod实例的场景.即便是手动创建 ...