aio_write 大文件

aio_write on Large Files

我试图从本质上模仿 sendfile(2) in an asynchronous fashion using aio_read(3) and aio_write(3) 的功能。

除了测试大型(> 150k)文件外,一切似乎都运行良好。

我有一个简单的 struct io_request 用于跟踪转帐:

struct io_request {
    int status;
    struct aiocb *aiocbp;
    int sfd;
};

首先,我构建 aio_read() 调用:

struct io_request * ioreq = malloc(sizeof(struct io_request));
ioreq->status = EINPROGRESS;
ioreq->sfd = sfd;

struct aiocb * aiocbreq = malloc(sizeof(struct aiocb));
memset(aiocbreq, 0, sizeof(struct aiocb));

ioreq->aiocbp = aiocbreq;

ioreq->aiocbp->aio_fildes = ffd;

if (ioreq->aiocbp->aio_fildes == -1) {
    perror("aio_fildes");
}

ioreq->aiocbp->aio_buf = malloc(st.st_size);
if (ioreq->aiocbp->aio_buf == NULL) {
    perror("aio_buf malloc");
}

ioreq->aiocbp->aio_nbytes = st.st_size;
ioreq->aiocbp->aio_reqprio = 0;
ioreq->aiocbp->aio_offset = 0;
ioreq->aiocbp->aio_sigevent.sigev_signo = IO_READ_SIGNAL;
ioreq->aiocbp->aio_sigevent.sigev_value.sival_ptr = ioreq;
if (aio_read(ioreq->aiocbp) == -1) {
    perror("aio_read");
}

稍后在 IO_READ_SIGNAL 处理程序中捕获:

static void
aio_read_handler(int sig, siginfo_t *si, void *ucontext)
{
    if (si->si_code == SI_ASYNCIO) {
        struct io_request *ioreq = si->si_value.sival_ptr;

        // Build the AIO write request
        struct aiocb aiocbreq;
        memset(&aiocbreq, 0, sizeof(struct aiocb));
        aiocbreq.aio_fildes = ioreq->sfd;
        aiocbreq.aio_buf = ioreq->aiocbp->aio_buf;
        aiocbreq.aio_nbytes = ioreq->aiocbp->aio_nbytes;
        aiocbreq.aio_sigevent.sigev_signo = IO_WRITE_SIGNAL;
        aiocbreq.aio_sigevent.sigev_value.sival_ptr = ioreq;

        if (aio_write((void *) &aiocbreq) == -1) {
            perror("aio_write");
        }
    }
}

我可以确认在处理程序内部,即使对于大文件,ioreq->aiocbp->aio_buf 的内容也是完整的。

稍后,aio_write()IO_WRITE_SIGNAL 处理程序中被捕获:

static void
aio_write_handler(int sig, siginfo_t *si, void *ucontext)
{
    if (si->si_code == SI_ASYNCIO) {
        struct io_request *ioreq = si->si_value.sival_ptr;

        ssize_t bytes_written = aio_return(ioreq->aiocbp);
        printf("Wrote %zu of %zu bytes\n", bytes_written, ioreq->aiocbp->aio_nbytes);
        //free(ioreq->aiocbp);
        //free(ioreq);

        if (aio_error(ioreq->aiocbp) != 0) {
            perror("aio_write_handler");
        }
    }
}

此时aio_write()应该已经完成​​了。我检查 return 值并采取相应行动。两次调用均报告已写入适当数量的字节,并且在写入期间未出现错误。

更大的应用是HTTP服务器。我推测出现此问题是因为远程客户端读取速度不足以跟上 aio_write()。当我有一个 sendfile() 实现时,我不得不多次调用 sendfile() 来完成文件传输。

几个直接的问题:

感谢您的帮助!

如果我没理解错的话,你在 aio_error 之前使用 aio_returnaio_return 手册页说,

This function should be called only once for any given request, after aio_error(3) returns something other than EINPROGRESS.