Linux AIO:扩展性差

voi*_*ter 13 c++ unix linux aio ext4

我正在编写一个使用Linux异步I/O系统调用的库,并且想知道为什么该io_submit函数在ext4文件系统上表现出较差的扩展.如果可能,我该怎么做io_submit才能阻止大IO请求大小?我已经做了以下(如描述这里):

  • 使用O_DIRECT.
  • 将IO缓冲区与512字节边界对齐.
  • 将缓冲区大小设置为页面大小的倍数.

为了观察内核花了多长时间io_submit,我运行了一个测试,在其中我用dd和创建了1 Gb测试文件/dev/urandom,并重复删除系统缓存(sync; echo 1 > /proc/sys/vm/drop_caches)并读取文件越来越大的部分.在每次迭代中,我打印io_submit了等待读取请求完成所花费的时间和所花费的时间.我在运行Arch Linux的x86-64系统上运行了以下实验,内核版本为3.11.该机器有一个SSD和一个Core i7 CPU.第一个图表绘制了读取的页数与等待io_submit完成所花费的时间.第二个图显示等待读取请求完成所花费的时间.时间以秒为单位.

在此输入图像描述

在此输入图像描述

为了比较,我创建了一个类似的测试,通过使用同步IO pread.结果如下:

在此输入图像描述

似乎异步IO按预期工作,最大请求大小为20,000页.之后,io_submit阻止.这些观察结果导致以下问题:

  • 为什么执行时间io_submit不变?
  • 是什么导致这种不良的缩放行为?
  • 我是否需要将ext4文件系统上的所有读取请求拆分为多个请求,每个请求的大小小于20,000页?
  • 20,000的"神奇"价值来自哪里?如果我在另一个Linux系统上运行我的程序,如何确定要使用的最大IO请求大小而不会遇到不良的扩展行为?

用于测试异步IO的代码如下所示.如果您认为它们是相关的,我可以添加其他源列表,但我尝试仅发布我认为可能相关的详细信息.

#include <cstddef>
#include <cstdint>
#include <cstring>
#include <chrono>
#include <iostream>
#include <memory>
#include <fcntl.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
// For `__NR_*` system call definitions.
#include <sys/syscall.h>
#include <linux/aio_abi.h>

static int
io_setup(unsigned n, aio_context_t* c)
{
    return syscall(__NR_io_setup, n, c);
}

static int
io_destroy(aio_context_t c)
{
    return syscall(__NR_io_destroy, c);
}

static int
io_submit(aio_context_t c, long n, iocb** b)
{
    return syscall(__NR_io_submit, c, n, b);
}

static int
io_getevents(aio_context_t c, long min, long max, io_event* e, timespec* t)
{
    return syscall(__NR_io_getevents, c, min, max, e, t);
}

int main(int argc, char** argv)
{
    using namespace std::chrono;
    const auto n = 4096 * size_t(std::atoi(argv[1]));

    // Initialize the file descriptor. If O_DIRECT is not used, the kernel
    // will block on `io_submit` until the job finishes, because non-direct
    // IO via the `aio` interface is not implemented (to my knowledge).
    auto fd = ::open("dat/test.dat", O_RDONLY | O_DIRECT | O_NOATIME);
    if (fd < 0) {
        ::perror("Error opening file");
        return EXIT_FAILURE;
    }

    char* p;
    auto r = ::posix_memalign((void**)&p, 512, n);
    if (r != 0) {
        std::cerr << "posix_memalign failed." << std::endl;
        return EXIT_FAILURE;
    }
    auto del = [](char* p) { std::free(p); };
    std::unique_ptr<char[], decltype(del)> buf{p, del};

    // Initialize the IO context.
    aio_context_t c{0};
    r = io_setup(4, &c);
    if (r < 0) {
        ::perror("Error invoking io_setup");
        return EXIT_FAILURE;
    }

    // Setup I/O control block.
    iocb b;
    std::memset(&b, 0, sizeof(b));
    b.aio_fildes = fd;
    b.aio_lio_opcode = IOCB_CMD_PREAD;

    // Command-specific options for `pread`.
    b.aio_buf = (uint64_t)buf.get();
    b.aio_offset = 0;
    b.aio_nbytes = n;
    iocb* bs[1] = {&b};

    auto t1 = high_resolution_clock::now();
    auto r = io_submit(c, 1, bs);
    if (r != 1) {
        if (r == -1) {
            ::perror("Error invoking io_submit");
        }
        else {
            std::cerr << "Could not submit request." << std::endl;
        }
        return EXIT_FAILURE;
    }
    auto t2 = high_resolution_clock::now();
    auto count = duration_cast<duration<double>>(t2 - t1).count();
    // Print the wait time.
    std::cout << count << " ";

    io_event e[1];
    t1 = high_resolution_clock::now();
    r = io_getevents(c, 1, 1, e, NULL);
    t2 = high_resolution_clock::now();
    count = duration_cast<duration<double>>(t2 - t1).count();
    // Print the read time.
    std::cout << count << std::endl;

    r = io_destroy(c);
    if (r < 0) {
        ::perror("Error invoking io_destroy");
        return EXIT_FAILURE;
    }
}
Run Code Online (Sandbox Code Playgroud)

Arv*_*vid 5

我的理解是,Linux上很少有(如果有的话)文件系统完全支持AIO。一些文件系统操作仍然会阻塞,有时io_submit()会间接通过文件系统操作来调用此类阻塞调用。

我的理解是,内核AIO的主​​要用户主要关心的是AIO在原始块设备(即无文件系统)上真正异步。本质上是数据库供应商。

这是 linux-aio邮件列表中的相关文章。(线程的

可能有用的建议:

通过/ sys / block / xxx / queue / nr_requests添加更多请求,问题将变得更好。