Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Linux AIO:伸缩性差_C++_Linux_Unix_Ext4_Aio - Fatal编程技术网

C++ Linux AIO:伸缩性差

C++ Linux AIO:伸缩性差,c++,linux,unix,ext4,aio,C++,Linux,Unix,Ext4,Aio,我正在编写一个使用Linux异步I/O系统调用的库,我想知道为什么io_submit函数在ext4文件系统上的伸缩性很差。如果可能,我可以做些什么来让io\u submit不阻止较大的io请求大小?我已经做了以下工作(如上所述): 使用O_DIRECT 将IO缓冲区与512字节边界对齐 将缓冲区大小设置为页面大小的倍数 为了观察内核在io\u submit上花费了多长时间,我运行了一个测试,其中我使用dd和/dev/uradom创建了一个1GB的测试文件,并反复删除系统缓存(sync;ech

我正在编写一个使用Linux异步I/O系统调用的库,我想知道为什么
io_submit
函数在ext4文件系统上的伸缩性很差。如果可能,我可以做些什么来让
io\u submit
不阻止较大的io请求大小?我已经做了以下工作(如上所述):

  • 使用
    O_DIRECT
  • 将IO缓冲区与512字节边界对齐
  • 将缓冲区大小设置为页面大小的倍数
为了观察内核在
io\u submit
上花费了多长时间,我运行了一个测试,其中我使用
dd
/dev/uradom
创建了一个1GB的测试文件,并反复删除系统缓存(
sync;echo 1>/proc/sys/vm/drop\u缓存
),读取文件中越来越大的部分。在每次迭代中,我都会打印
io_submit
所花费的时间以及等待读取请求完成所花费的时间。我在运行Arch Linux的x86-64系统上运行了以下实验,内核版本为3.11。该机器有一个SSD和一个核心i7 CPU。第一个图表根据等待
io\u submit
完成的时间绘制读取的页数。第二个图形显示等待读取请求完成所花费的时间。时间以秒为单位

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

异步IO的工作方式似乎与预期一致,请求大小大约为20000页。然后,
io\u submit
块。这些观察结果引出以下问题:

  • 为什么
    io\u submit
    的执行时间不是常量
  • 是什么导致这种不良的缩放行为
  • 我是否需要将ext4文件系统上的所有读取请求拆分为多个请求,每个请求的大小都小于20000页
  • 20000的“神奇”价值从何而来?如果我在另一个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;
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//对于`\uu NR ``系统调用定义。
#包括
#包括
静态整数
io_设置(未签名n,aio_上下文\u t*c)
{
返回系统调用(uuu NR_uio_usetup,n,c);
}
静态整数
io_销毁(aio_上下文c)
{
返回系统调用(_NR_io_destroy,c);
}
静态整数
io_提交(aio_上下文c、长n、iocb**b)
{
返回系统调用(提交,c,n,b);
}
静态整数
io_getevents(aio_context_t c、长最小值、长最大值、io_event*e、timespec*t)
{
返回系统调用(uuu NR_uio_ugetevents,c,min,max,e,t);
}
int main(int argc,字符**argv)
{
使用名称空间std::chrono;
const auto n=4096*size_t(std::atoi(argv[1]);
//初始化文件描述符。如果未使用O_DIRECT,则内核
//将在“io_submit”上阻止,直到作业完成,因为非直接
//据我所知,没有通过“aio”接口实现IO。
自动fd=::打开(“dat/test.dat”,O|RDONLY | O|u DIRECT | O|noautime);
如果(fd<0){
::perror(“打开文件时出错”);
返回退出失败;
}
char*p;
自动r=::posix_memalign((void**)和p,512,n);
如果(r!=0){

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

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

来自LinuxAIO邮件列表的相关帖子。(线程的)

一项可能有用的建议:

通过/sys/block/xxx/queue/nr_请求添加更多请求并解决问题 会好起来的


您首先缺少使用AIO的目的。引用的示例显示了一系列的[fill buffer]、[write]、[write]、[write]、…[read]、[read]、[read],…操作。实际上,您正在将数据塞入一个管道中。最终,当您达到存储的I/O带宽限制时,管道将填满。现在,您正在忙着等待,这表现在线性性能下降行为上

AIO写入的性能增益是,应用程序填充缓冲区,然后通知内核开始写入操作;当内核仍然拥有数据缓冲区及其内容时,控制立即返回到应用程序;在内核发出I/O完成信号之前,应用程序不得接触数据缓冲区,因为您不需要还不知道缓冲区的哪一部分(如果有的话)实际上已经进入了介质:在I/O完成之前修改缓冲区,您已经损坏了输出到介质的数据

相反,AIO读取的收益是当应用程序分配I/O缓冲区,然后通知内核开始填充缓冲区时。控制立即返回到应用程序,应用程序必须保留缓冲区,直到内核通过发布I/O完成事件表示缓冲区已完成

因此,您看到的行为就是快速将管道填充到存储器的示例。最终,数据生成的速度比存储器能够吸收数据的速度快,性能下降到线性,而管道在清空时会尽快重新填充:线性行为

示例程序确实使用AIO调用,但它仍然是一个线性停止等待程序

为什么io_submit的执行时间不是常量

因为您提交的I/O太大了,所以blo