Linux内核AIO功能

12
我正在测试内核异步io函数(非posix aio)并试图弄清楚它的工作原理。下面的代码是一个完整的程序,其中我只是重复地写入一个数组到使用O_DIRECT打开的文件中。在回调函数“work_done()”中,我得到一个错误:“写入错过的字节期望1024,实际获得0”(请参见fprintf语句)。对于那些不熟悉内核aio的人,下面的代码执行以下操作:
1.初始化一些结构体 2.准备aio(io_prep_pwrite) 3.提交io请求(io_submit) 4.检查事件完成情况(io_getevents) 5.调用回调函数以查看是否一切正常。
我在第5步出现了一个错误。如果我不使用O_DIRECT打开文件,则一切正常,但这击败了使用异步写入的目的。有人能告诉我我做错了什么吗?这是使用内核aio的正确用法,例如我的回调使用是否正确? O_DIRECT的使用是否有任何限制?
我使用“gcc -Wall test.c -laio”进行编译。谢谢预先。
/* 
 * File:   myaiocp.c
 * Author: kmehta
 *
 * Created on July 11, 2011, 12:50 PM
 *
 *
 * Testing kernel aio. 
 * Program creates a 2D matrix and writes it multiple times to create a file of desired size. 
 * Writes are performed using kernel aio functions (io_prep_pwrite, io_submit, etc.)
 */
#define _GNU_SOURCE
#define _XOPEN_SOURCE 600

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <pthread.h>
#include <fcntl.h>
#include <string.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <omp.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <libaio.h>

char ** buf;
long seg_size;
int seg_rows;
double total_size;
char * filename;
static int wait_count = 0;

void io_task();
void cleanup();
void allocate_2D_matrix(int[]);
int file_open(char *);
void wr_done(io_context_t ctx, struct iocb* iocb, long res, long res2);

int main(int argc, char **argv) {
    total_size  = 1048576;      //1MB
    seg_size    = 1024;         //1kB
    seg_rows    = 1024;
    filename    = "aio.out";

    int dims[] = {seg_rows, seg_size};
    allocate_2D_matrix(dims);   //Creates 2D matrix

    io_task();
    cleanup();

    return 0;
}

/*
 * Create a 2D matrix
 */
void allocate_2D_matrix(int dims[2]) {
    int i;
    char *data;

    //create the matrix
    data = (char *) calloc(1, dims[0] * dims[1] * sizeof (char));
    if (data == NULL) {
        printf("\nCould not allocate memory for matrix.\n");
        exit(1);
    }

    buf = (char **) malloc(dims[0] * sizeof (char *));
    if (buf == NULL) {
        printf("\nCould not allocate memory for matrix.\n");
        exit(1);
    }

    for (i = 0; i < dims[0]; i++) {
        buf[i] = &(data[i * dims[1]]);
    }
}

static void io_error(const char *func, int rc)
{
    if (rc == -ENOSYS)
        fprintf(stderr, "AIO not in this kernel\n");
    else if (rc < 0)
        fprintf(stderr, "%s: %s\n", func, strerror(-rc));
    else
        fprintf(stderr, "%s: error %d\n", func, rc);

    exit(1);
}

/*
 * Callback function
 */
static void work_done(io_context_t ctx, struct iocb *iocb, long res, long res2)
{

    if (res2 != 0) {
        io_error("aio write", res2);
      }

      if (res != iocb->u.c.nbytes) {
            fprintf(stderr, "write missed bytes expect %lu got %ld\n",
                  iocb->u.c.nbytes, res2);
            exit(1);
      }
      wait_count --;
      printf("%d ", wait_count);
}

/*
 * Wait routine. Get events and call the callback function work_done()
 */
int io_wait_run(io_context_t ctx, long iter)
{
      struct io_event events[iter];
      struct io_event *ep;
      int ret, n;

      /*
       * get up to aio_maxio events at a time.
       */
      ret = n = io_getevents(ctx, iter, iter, events, NULL);
      printf("got %d events\n", n);
      /*
       * Call the callback functions for each event.
       */
      for (ep = events ; n-- > 0 ; ep++) {
            io_callback_t cb = (io_callback_t)ep->data ; struct iocb *iocb = ep->obj ; cb(ctx, iocb, ep->res, ep->res2);
      }
      return ret;
}

void io_task() {
    long offset = 0;
    int bufIndex = 0;

    //Open file
    int fd = file_open(filename);

    //Initialize structures
    long i; 
    long iter = total_size / seg_size;  //No. of iterations to reach desired file size (total_size)
    io_context_t myctx;
    if(0 != io_queue_init(iter, &myctx))
    {
        perror("Could not initialize io queue");
        exit(EXIT_FAILURE);
    }
    struct iocb * ioq[iter];

    //loop through iter times to reach desired file size
    for (i = 0; i < iter; i++) {
        struct iocb *io = (struct iocb*) malloc(sizeof (struct iocb));
        io_prep_pwrite(io, fd, buf[bufIndex], seg_size, offset);
        io_set_callback(io, work_done);
        ioq[i] = io;

        offset += seg_size;
        bufIndex ++;
        if (bufIndex > seg_rows - 1)    //If entire matrix written, start again from index 0
            bufIndex = 0;
    }

    printf("done preparing. Now submitting..\n");
    if(iter != io_submit(myctx, iter, ioq))
    {
        perror("Failure on submit");
        exit(EXIT_FAILURE);
    }

    printf("now awaiting completion..\n");
    wait_count = iter;
    int res;

    while (wait_count) {
        res = io_wait_run(myctx, iter);
        if (res < 0)
            io_error("io_wait_run", res);
    }

    close(fd);
}

void cleanup() {
    free(buf[0]);
    free(buf);
}

int file_open(char *filename) {
    int fd;
    if (-1 == (fd = open(filename, O_DIRECT | O_CREAT | O_WRONLY | O_TRUNC, 0666))) {
        printf("\nError opening file. \n");
        exit(-1);
    }

    return fd;
}
1个回答

10
首先,使用libaio而不是POSIX aio很好。

使用O_DIRECT有任何限制吗?

我不确定这是否是真正的问题,但是O_DIRECT有一些要求(主要引用自TLPI):
  • 传输的数据缓冲区必须对齐到块大小的内存边界(使用posix_memalign
  • 数据传输开始时的文件或设备偏移量必须是块大小的倍数
  • 要传输的数据长度必须是块大小的倍数
乍一看,我可以看出你没有在allocate_2D_matrix中采取任何对齐内存的预防措施。

如果我不使用O_DIRECT打开文件,就可以正常工作,但这违背了异步写入的目的。

实际情况并非如此。异步I/O在没有O_DIRECT的情况下工作良好(例如,请考虑减少的系统调用数量)。

@cnicutar 很好,问题解决了。不过有几个问题:
  1. 为什么你建议使用libaio而不是posix aio?我对posix aio没有太多经验,所以不知道。
  2. 你为什么说异步I/O在没有O_DIRECT的情况下也能很好地工作?它如何减少系统调用次数?实际上,我认为O_DIRECT更有效率,因为它可以绕过内核缓存。
- Korizon
2
mmap 可能比 posix_memalign 更好地获取 4k 对齐的内存。后者肯定会浪费 4k 的内存用于少量字节的簿记,因为它是按页粒度分配的(假设您分配了足够的内存,以便 posix_memalign 通过 mmap 而不是 brk 来服务请求)。 - R.. GitHub STOP HELPING ICE
2
但根据罗伯特·洛夫的《Linux系统编程》一书,只有在使用O_DIRECT打开时,Linux才支持对常规文件进行aio操作。 - ovais.tariq
我刚开始研究Linux异步IO。我不熟悉回调功能,但这里的示例使用方式似乎很奇怪。通常,您提供一个回调函数,某个子系统将使用它来在完成您要求它执行的任务后调用您。这里的示例似乎在调用io_getevents()以获取已完成事件列表,然后为每个事件调用回调函数。那听起来不正确。在这种情况下,没有必要使用回调函数。 - user4238474
警告:[libaio/Linux Kernel AIO]在*io_submit()*时间可能会表现出阻塞行为,如果您不使用O_DIRECT,则可能会以同步*方式静默*执行! - Anon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接