如图所示,对于文件的读写操作如果小于 IO_CACHE 大小,就放到缓冲中,当 IO_CACHE 满了就进行一次 4KB 对齐的写入,如果一次读写超过 IO_CACHE 的大小,就把 4K 对齐的数据进行一次读写,剩余部分放到 IO_CACHE 中,等待下次读写一起合并。

IO_CACHE 有不同的类型,定义在 cache_type 中:

常用的 general log, slow log, err log, binlog 主要使用 READ_CACHE, WRITE_CACHE, SEQ_READ_APPEND 几种类型,本文主要介绍这几种。同时 IO_CACHE 也提供支持 AIO 的接口,支持多线程同时访问 IO_CACHE 等,目前来看来应用也不多,暂不涉及。

主要代码在 mysys/mf_iocache.c 中,

READ_CACHE 是读缓冲,WRITE_CACHE 是写缓冲,SEQ_READ_APPEND 同时支持读写,写线程不断 append 数据到文件尾,读线程去 read 数据。append 使用 IO_CACHE::write_buffer, read 使用 IO_CACHE::buffer。当读到 write_buffer 中的数据时,就从 write_buffer 中拿数据。SEQ_READ_APPEND 这种类型在 MySQL 复制模块使用,IO 线程负责 append 数据到 relay log,SQL 线程负责 read 出来应用(考虑下为什么在主库上的写入线程和 Dump 线程之间不是使用这种方法,而是简单的 read-write,因为主库上 order_commit 函数很可能成为性能的瓶颈,和 Dump 线程竞争 append_buffer_lock 似乎并不好),因为 SEQ_READ_APPEND 类型更具有代表性,就以这种类型为例介绍。

  1. {
  2. /* Offset in file corresponding to the first byte of uchar* buffer. */
  3. my_off_t pos_in_file;
  4. /*
  5. The offset of end of file for READ_CACHE and WRITE_CACHE.
  6. For SEQ_READ_APPEND it the maximum of the actual end of file and
  7. the position represented by read_end.
  8. */
  9. my_off_t end_of_file;
  10. /* Points to current read position in the buffer */
  11. uchar *read_pos;
  12. /* the non-inclusive boundary in the buffer for the currently valid read */
  13. uchar *read_end;
  14. uchar *buffer; /* The read buffer */
  15. /* Used in ASYNC_IO */
  16. uchar *request_pos;
  17. /* Only used in WRITE caches and in SEQ_READ_APPEND to buffer writes */
  18. uchar *write_buffer;
  19. /*
  20. Only used in SEQ_READ_APPEND, and points to the current read position
  21. in the write buffer. Note that reads in SEQ_READ_APPEND caches can
  22. happen from both read buffer (uchar* buffer) and write buffer
  23. (uchar* write_buffer).
  24. */
  25. uchar *append_read_pos;
  26. /* Points to current write position in the write buffer */
  27. uchar *write_pos;
  28. /* The non-inclusive boundary of the valid write area */
  29. uchar *write_end;
  30. /*
  31. Current_pos and current_end are convenience variables used by
  32. my_b_tell() and other routines that need to know the current offset
  33. current_pos points to &write_pos, and current_end to &write_end in a
  34. WRITE_CACHE, and &read_pos and &read_end respectively otherwise
  35. */
  36. uchar **current_pos, **current_end;
  37. /*
  38. The lock is for append buffer used in SEQ_READ_APPEND cache
  39. need mutex copying from append buffer to read buffer.
  40. */
  41. mysql_mutex_t append_buffer_lock;
  42. /*
  43. A caller will use my_b_read() macro to read from the cache
  44. if the data is already in cache, it will be simply copied with
  45. memcpy() and internal variables will be accordinging updated with
  46. no functions invoked. However, if the data is not fully in the cache,
  47. my_b_read() will call read_function to fetch the data. read_function
  48. must never be invoked directly.
  49. */
  50. int (*read_function)(struct st_io_cache *,uchar *,size_t);
  51. /*
  52. Same idea as in the case of read_function, except my_b_write() needs to
  53. be replaced with my_b_append() for a SEQ_READ_APPEND cache
  54. */
  55. int (*write_function)(struct st_io_cache *,const uchar *,size_t);
  56. /*
  57. Specifies the type of the cache.
  58. */
  59. enum cache_type type;
  60. /*
  61. Callbacks when the actual read I/O happens. These were added and
  62. are currently used for binary logging of LOAD DATA INFILE - when a
  63. block is read from the file, we create a block create/append event, and
  64. when IO_CACHE is closed, we create an end event. These functions could,
  65. of course be used for other things
  66. */
  67. IO_CACHE_CALLBACK pre_read;
  68. IO_CACHE_CALLBACK pre_close;
  69. /*
  70. Counts the number of times, when we were forced to use disk. We use it to
  71. variables.
  72. */
  73. ulong disk_writes;
  74. void* arg; /* for use by pre/post_read */
  75. char *file_name; /* if used with 'open_cached_file' */
  76. char *dir,*prefix;
  77. File file; /* file descriptor */
  78. /*
  79. seek_not_done is set by my_b_seek() to inform the upcoming read/write
  80. operation that a seek needs to be preformed prior to the actual I/O
  81. error is 0 if the cache operation was successful, -1 if there was a
  82. "hard" error, and the actual number of I/O-ed bytes if the read/write was
  83. partial.
  84. */
  85. int seek_not_done,error;
  86. /* buffer_length is memory size allocated for buffer or write_buffer */
  87. size_t buffer_length;
  88. /* read_length is the same as buffer_length except when we use async io */
  89. size_t read_length;
  90. myf myflags; /* Flags used to my_read/my_write */
  91. /*
  92. alloced_buffer is 1 if the buffer was allocated by init_io_cache() and
  93. 0 if it was supplied by the user.
  94. Currently READ_NET is the only one that will use a buffer allocated
  95. somewhere else
  96. */
  97. my_bool alloced_buffer;
  98. } IO_CACHE;

初始化

初始化函数是 init_io_cache ,主要会做以下几件事:

  1. 和对应的文件描述符绑定,初始化 IO_CACHE 中各种变量。
  2. 分配 write_buffer 和 read_buffer 的空间。
  3. 初始化互斥变量 append_buffer_lock. (对于 SEQ_READ_APPEND 类型而言)
  4. init_functions 初始化对应的文件读写函数。

其中根据传入的参数 cache_size 分配缓冲空间,一般传入的空间都不算大,例如 Binlog 的 IO_CACHE 初始化传入的大小就是 IO_SIZE(4KB),因为文件系统本身是有 page cache 的,只有调用 fsync 操作才会保证数据落盘,所以 IO_CACHE 就没必要缓冲太多的数据,只做把数据对齐写入的活。但并不是传进来多大空间就分配多大空间,看下代码:

  1. min_cache=use_async_io ? IO_SIZE*4 : IO_SIZE*2;
  2. cachesize= ((cachesize + min_cache-1) & ~(min_cache-1));
  3. for (;;)
  4. {
  5. if (cachesize < min_cache)
  6. cachesize = min_cache;
  7. buffer_block= cachesize;
  8. if (type == SEQ_READ_APPEND)
  9. buffer_block *= 2;
  10. if ((info->buffer= (uchar*) my_malloc(buffer_block, flags)) != 0)
  11. {
  12. info->write_buffer=info->buffer;
  13. if (type == SEQ_READ_APPEND)
  14. info->write_buffer = info->buffer + cachesize;
  15. info->alloced_buffer=1;
  16. break; /* Enough memory found */
  17. }
  18. if (cachesize == min_cache)
  19. DBUG_RETURN(2); /* Can't alloc cache */
  20. /* Try with less memory */
  21. cachesize= (cachesize*3/4 & ~(min_cache-1));
  22. }

最小的分配空间在不使用 AIO 的情况下是 8K,这个后面会用到,SEQ_READ_APPEND 类型会分配两倍空间,因为有读缓冲和写缓冲。如果申请的空间无法满足就试图申请小一点的空间。

init_functions 是根据 IO_CACHE 的类型初始化 IO_CACHE::read_function 和 IO_CACHE::write_function,当缓冲大小没法满足文件 IO 请求的时候就会调用这两个函数去文件中交换数据。

SEQ_READ_APPEND 的写直接调用 my_b_append。

主要的接口在 include/my_sys.h 文件中,大多是宏定义形式。简单看几个常用的:

  1. #define my_b_read(info,Buffer,Count) \
  2. ((info)->read_pos + (Count) <= (info)->read_end ?\
  3. (memcpy(Buffer,(info)->read_pos,(size_t) (Count)), \
  4. ((info)->read_pos+=(Count)),0) :\
  5. (*(info)->read_function)((info),Buffer,Count))
  1. #define my_b_write(info,Buffer,Count) \
  2. ((info)->write_pos + (Count) <=(info)->write_end ?\
  3. (memcpy((info)->write_pos, (Buffer), (size_t)(Count)),\
  4. ((info)->write_pos+=(Count)),0) : \
  5. (*(info)->write_function)((info),(uchar *)(Buffer),(Count)))

从 Buffer 中向 IO_CACHE info 写 Count 个字节数据,逻辑类似,如果写入缓冲不够,就做一次文件 IO。

这里 request_pos 是指向 IO_CACHE::buffer 的,而 current_pos 在 setup_io_cache 中初始化为 read_pos 或者 write_pos, 这种设计就可以为不同的 cache type 提供统一的接口。

还有一些非宏定义的接口比如 my_b_seek 等在文件 mysys_iocache2.c 中,不一一介绍,总之文件系统常用的操作在 IO_CACHE 中基本都可以找到。

_my_b_seq_read

以 SEQ_READ_APPEND 类型为例,文件 IO 的函数是 _my_b_seq_read, 整个流程分为三个阶段:

  1. read from info->buffer
  2. read from file description
  3. try append buffer

因为 SEQ_READ_APPEND 类型的读可能会读到 info->write_buffer 中还没来及写到文件系统里的数据,所以第三步就是去写缓冲中读。整个代码的精髓在于计算需要读多少数据才能保证对齐,看下代码:

  1. // 先把 IO_CACHE 里剩下的数据读到 Buffer 里
  2. if ((left_length=(size_t) (info->read_end-info->read_pos))
  3. {
  4. memcpy(Buffer, info->read_pos, left_length);
  5. Buffer+=left_length;
  6. Count-=left_length;
  7. }
  8. if (pos_in_file=info->pos_in_file +
  9. (size_t)(info->read_end - info->buffer)) > info->end_of_file)
  10. goto read_append_buffer;
  11. // diff_length 为了对齐读
  12. // 第二阶段,从文件里读数据
  13. // 一般 IO_CACHE 默认初始化是 2*IO_CACHE,8KB,这个意思是 Count 的大小已经不能放在一个 IO_CACHE
  14. // 的 Buffer 里
  15. if (Count >= (size_t)(IO_SIZE + (IO_SIZE - diff_length)
  16. {
  17. // 到这里面说明 Count 要读的数据超过了 IO_CACHE 中的 Buffer 大小,直接读到 Buffer
  18. // 那么读多少比较合适呢?
  19. // 取出高阶的 IO_CACHE,整数个。(Count & (size_t)~(IO_SIZAE-1))
  20. // 但是因为 pos_in_file 相对于 4K 对齐地址还有一定的偏移量,再减去这个偏移,保证整个读取是对齐的
  21. length=(Count & (size_t)~(IO_SIZE-1))-diff_Lenght;
  22. if (read_length=mysql_file_read(info->file, Buffer, length..){}
  23. // update after read
  24. Count -= read_lenght;
  25. Buffer += read_leagth;
  26. pos_in_file += read_length;
  27. if(read_length != length)
  28. goto read_append_buffer; // 没有读到想要的长度
  29. left_length += length;
  30. diff_length=0; // no diff length now
  31. }
  32. // IO_CACHE buffer 中还可以读多少数据。
  33. max_length= info->read_length-diff_length;
  34. // 可能会超出文件结尾,需要到 append buffer 读取
  35. if (max_length > (info->end_of_file - pos_in_file)
  36. max_length= (size_t)(info->end_of_file - pos_in_file)
  37. if (!max_length) // 已经到了文件尾
  38. {
  39. if (Count) // 如果还有东西要读
  40. goto read_append_buffer; append buffer
  41. }else // 还可以读一些东西
  42. {
  43. // 读到 info->buffer 里,max_length 要么读到真实文件尾,要么读到 read buffer的尽头
  44. length= mysql_file_read(info->file, info->bufffer, max_length);
  45. if (lenth < Count) 还有东西要读
  46. {
  47. goto read_append_buffer;
  48. }
  49. }
  50. return 0
  51. read_append_buffer:
  52. {
  53. // 先看 append buffer 剩余多少空间
  54. size_t len_in_buffer = (size_t)(info->write_pos - info->append_read_pos);
  55. // 取其精华
  56. copy_len= MY_MIN(Count, len_in_buffer);
  57. memcpy BUffer
  58. /* Fill read buffer with data from wirte buffer*/
  59. }

介绍写之前先介绍下这个函数,它的作用就是把 write_buffer 中数据写到文件系统里,在 IO_CACHE 的操作用这个函数才会真正发生不对齐的 IO,因为要写入的数据已经都在这了,最后的长度谁也没法保证对齐,IO_CACHE 并不会填充一些无意义的数据进去。

  1. if ((length=(size_t)(info->write_pos - info->write_buffer)))
  2. {// length 是写缓冲区中数据的长度
  3. pos_in_file=info->pos_in_file; // 保存一下,后面有用
  4. ...
  5. if (!append_cache)
  6. info->pos_in_file += length;
  7. // 这一步是 write_cache 的精华,获得总的 buffer 大小,然后减去当前文件 pos + length
  8. // 之和,剩余部分就是文件中还没有对齐的地方,下一个写入这么大的数据,就可以满足一次对齐写
  9. // 所以当 buffer 比较小的时候,为了满足这种对齐的要求,可使用的 buffer 就会比较小,
  10. // 从而触发更多的文件 IO 操作
  11. info->write_end= (info->write_buffer + info->buffer_length -
  12. (pos_in_file + length) & (IO_SIZE - 1)));
  13. if (mysql_file_wirte(info->file, info->write_buffer length, info->myflags |
  14. MY_NABP)
  15. info->error= -1;
  16. set_if_bigger(info->end_of_file, (pos_in_file+length));
  17. info->append_read_pos=info->write_pos=info->write_buffer;
  18. ....

my_b_append

经过分析,IO_CACHE 是建立的文件系统之上的,把一系列顺序的 IO 操作经过缓冲,转化成 4K 对齐的 IO 操作落到文件系统中,因为文件系统的页缓冲,IO_CACHE 并不大,没有缓冲很多的数据。接口丰富,可以作为一个单独的组件提高文件系统的 IO 性能。