大佬教程收集整理的这篇文章主要介绍了c – Linux AIO:缩放比例不佳,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
>使用O_DIRECT.
>将IO缓冲区对齐到一个512字节的边界.
>将缓冲区大小设置为页面大小的倍数.
为了观察内核在io_submit中花费的时间,我运行了一个测试,其中我使用dd和/ dev / urandom创建了1 Gb测试文件,并重复删除了系统缓存(sync; echo 1> / proc / sys / vm / drop_caches),并读取越来越大的文件部分.在每次迭代时,我打印了io_submit花费的时间和等待读取请求完成的时间.我在运行Arch Linux的x86-64系统上运行了以下实验,内核版本为3.11.该机器有一个SSD和一个Core i7 cpu.第一个图表绘制了等待io_submit完成所花费的时间的页数.第二个图表显示等待读取请求完成的时间.时间以秒为单位.
为了比较,我创建了一个类似的测试,使用pread的同步IO.结果如下:
看来,异步IO可以像预期那样工作,要求大约20,000页的大小.之后,io_submit块.这些意见导致以下问题:
>为什么io_submit的执行时间不是常数?
什么是导致这种不良的缩放行为?
>我需要将ext4文件系统上的所有读请求分成多个请求,每个请求的大小小于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,b); } static int io_getevents(aio_context_t c,long min,long max,io_event* e,timespec* t) { return syscall(__NR_io_getevents,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,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,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; } }
我的理解是,内核AIO的主要用户主要关心AIO在原始块设备(即没有文件系统)上是真正的异步的.本质上是数据库供应商.
Here’s从linux-aio邮件列表中的相关帖子. (head的线程)
一个可能有用的建议:
以上是大佬教程为你收集整理的c – Linux AIO:缩放比例不佳全部内容,希望文章能够帮你解决c – Linux AIO:缩放比例不佳所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。