linux - fs io 的用户级反弹缓冲区?为什么写文件缓冲区地址空间要对齐?

标签 linux io filesystems posix memory-alignment

我看到一段代码像这样执行磁盘io:

static bool is_aligned(unsigned char *buffer) {
  return ( ((unsigned long)buffer) & (DISK_PAGE_SIZE -1)) == 0;
}


void do_write_IO(int fd, unsignced char *buffer, unsigned long buffer_bytes) {
  ...

  if (is_aligned(buffer)) {
    write_to_file(fd, buffer, io_size);
  } else {
    // bounce buffer is an aligned memory space.
    // if buffer not aligned, copy to an aligned address
    // and do fs write. WHY ?
    // What are the benefits ? 
    memcpy(bounce_buffer, buffer, io_size);
    write_to_file(fd, bounce_buffer, io_size);
  }

  ...
}

// Just call posix write, write output to fd, with bytes_to_write size.
static void write_to_file(int fd, 
          unsigned char *output,
          unsigned long bytes_to_write) 
{
  __sync_fetch_and_add(&stat_bytes_written, bytes_to_write);
  while(bytes_to_write) {
    unsigned long bytes_written = write(fd, output, bytes_to_write);
    if(bytes_written == -1UL) {
      if(errno != EAGAIN) {
        BOOST_LOG_TRIVIAL(fatal) << 
          "Stream writeout unsuccessful:" << strerror(errno);
        exit(-1);
      }
    }
    else {
      output += bytes_written;
      bytes_to_write -= bytes_written;
    }
  }
}

我注意到它使用地址空间对齐的缓冲区写入文件。 那么,写入具有对齐缓冲区的文件有什么好处吗?

最佳答案

我认为“正常工作”是一种好处。为此,您需要满足调用的每个函数的先决条件。

如果 write 需要对齐缓冲区,则

write_to_file 需要对齐缓冲区,并且这是与设备相关的。可能您的fd是用O_DIRECT打开的。 open 手册页显示:

The O_DIRECT flag may impose alignment restrictions on the length and address of user-space buffers and the file offset of I/Os.

Under Linux 2.4, transfer sizes, and the alignment of the user buffer and the file offset must all be multiples of the logical block size of the file system. Under Linux 2.6, alignment to 512-byte boundaries suffices.

如果您不遵守此前提条件,write 手册页会记录错误:

EINVAL fd is attached to an object which is unsuitable for writing; or the file was opened with the O_DIRECT flag, and either the address specified in buf, the value specified in count, or the current file offset is not suitably aligned.

关于linux - fs io 的用户级反弹缓冲区?为什么写文件缓冲区地址空间要对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25459938/

相关文章:

filesystems - 这是一个关于OS文件存储管理和inode的问题

java - Maven:mvn --version java.lang.ClassNotFoundException

c++ - 大多数时候从 POSIX 线程在 C++ 中打开/proc/net/tcp 失败

python - (Python) 使用线程通过 getch 查找键输入

java - 写 "compressed"数组提高IO性能?

.net - .net 应用程序是否有任何类型的文件系统事务机制可用?

c++ - 我无法理解的段错误错误

c++ - 树莓派交叉编译——执行程序结束于 "Segmentation fault"

c - 使用 MPI I/O 提取 C 结构的一部分

Eclipse 集成开发环境;在当前窗口/实例中打开?