linux异步io缺陷,c – 具有libaio性能问题的Linux异步IO

我正在尝试使用

Linux libaio来优化服务器应用程序中的IO性能.我相信我已经完成了所有必要的工作(使用O_DIRECT,将缓冲区与内存页面对齐…).我期待立即调用io_submit返回,但是一个简单的测试显示它实际需要大约80微秒才能在我的核心i7笔记本电脑上返回.我期待太多或我的测试程序有什么问题吗? (用g编译–cd = c 0x -laio)

#include

#include

#include

#include

#include

#include

#include

#include

// Open the file for write, return the file descriptor

int open_write(char const* file)

{

int fd = open(file, O_DIRECT|O_CREAT|O_WRONLY, S_IRWXU|S_IRWXG|S_IROTH);

if (fd < 0) {

perror("open_write");

exit(1);

}

}

// Make a buffer of _size_ byte, fill with 'a', return the buffer, it should be aligned to memory page

void* make_write_buffer(size_t size)

{

void* buf = 0;

int ret = posix_memalign((void**)&buf, sysconf(_SC_PAGESIZE), size);

if (ret < 0 || buf == 0) {

perror("make_write_buffer");

exit(1);

}

memset(buf, 'a', size);

return buf;

}

int main (int argc, char *argv[])

{

static const size_t SIZE = 16 * 1024;

// Prepare file and buffer to write

int write_fd = open_write("test.dat");

void* buf = make_write_buffer(SIZE);

// Prepare aio

io_context_t ctx;

memset(&ctx, 0, sizeof(ctx));

const int maxEvents = 32;

io_setup(maxEvents, &ctx);

iocb *iocbpp = new iocb;

io_prep_pwrite(iocbpp, write_fd, buf, SIZE, 0);

using namespace std::chrono;

// Submit aio task

auto start = monotonic_clock::now();

int status = io_submit(ctx, 1, &iocbpp);

if (status < 0) {

errno = -status;

perror("io_submit");

exit(1);

}

auto dur = duration_cast(monotonic_clock::now() - start);

std::cout << "io_submit takes: " << dur.count() << " microseconds." << std::endl;

io_event events[10];

int n = io_getevents(ctx, 1, 10, events, NULL);

close(write_fd);

io_destroy(ctx);

delete iocbpp;

free(buf);

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值