Package: libaio-dev Version: 0.3.107-3 Severity: normal Hi,
attached is a example source that shows how to use libaio to read, write and how to use eventfd to poll for events. MfG Goswin -- System Information: Debian Release: squeeze/sid APT prefers unstable-i386 APT policy: (500, 'unstable-i386'), (500, 'unstable'), (200, 'experimental') Architecture: amd64 (x86_64) Kernel: Linux 2.6.29.4-frosties-1 Locale: LANG=C, LC_CTYPE=de_DE (charmap=ISO-8859-1) Shell: /bin/sh linked to /bin/bash Versions of packages libaio-dev depends on: ii libaio1 0.3.107-3 Linux kernel AIO access library - libaio-dev recommends no packages. libaio-dev suggests no packages. -- no debconf information
/* * eventfd-aio-test by Davide Libenzi (test app for eventfd hooked into KAIO) * Copyright (C) 2007 Davide Libenzi * Modified to use libaio by Goswin von Brederlow <goswin-...@web.de> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Davide Libenzi <davi...@xmailserver.org> * */ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/eventfd.h> #include <poll.h> #include <libaio.h> #define TESTFILE_SIZE (4096 * 5120) #define IORTX_SIZE (1024 * 4) #define NUM_EVENTS 128 static long waitasync(int afd, int timeo) { struct pollfd pfd; pfd.fd = afd; pfd.events = POLLIN; pfd.revents = 0; if (poll(&pfd, 1, timeo) < 0) { perror("poll"); return -1; } if ((pfd.revents & POLLIN) == 0) { fprintf(stderr, "no results completed\n"); return 0; } return 1; } static long test_read(io_context_t ctx, int fd, long range, int afd) { long i, n, r, j; u_int64_t eval; struct iocb **piocb; struct iocb *iocb; struct timespec tmo; static struct io_event events[NUM_EVENTS]; static char buf[IORTX_SIZE]; n = range / IORTX_SIZE; iocb = malloc(n * sizeof(struct iocb)); piocb = malloc(n * sizeof(struct iocb *)); if (!iocb || !piocb) { perror("iocb alloc"); return -1; } for (i = 0; i < n; i++) { piocb[i] = &iocb[i]; io_prep_pread(&iocb[i], fd, buf, sizeof(buf), (n - i - 1) * IORTX_SIZE); io_set_eventfd(&iocb[i], afd); io_set_callback(&iocb[i], (io_callback_t)(i + 1)); } fprintf(stdout, "submitting read request ...\n"); if (io_submit(ctx, n, piocb) <= 0) { perror("io_submit"); return -1; } for (i = 0; i < n;) { fprintf(stdout, "waiting ... "); waitasync(afd, -1); eval = 0; if (read(afd, &eval, sizeof(eval)) != sizeof(eval)) perror("read"); fprintf(stdout, "done! %llu\n", (unsigned long long) eval); while (eval > 0) { tmo.tv_sec = 0; tmo.tv_nsec = 0; r = io_getevents(ctx, 1, eval > NUM_EVENTS ? NUM_EVENTS: (long) eval, events, &tmo); if (r > 0) { for (j = 0; j < r; j++) { } i += r; eval -= r; fprintf(stdout, "test_write got %ld/%ld results so far\n", i, n); } } } free(iocb); free(piocb); return n; } static long test_write(io_context_t ctx, int fd, long range, int afd) { long i, n, r, j; u_int64_t eval; struct iocb **piocb; struct iocb *iocb; struct timespec tmo; static struct io_event events[NUM_EVENTS]; static char buf[IORTX_SIZE]; for (i = 0; i < IORTX_SIZE; i++) buf[i] = i & 0xff; n = range / IORTX_SIZE; iocb = malloc(n * sizeof(struct iocb)); piocb = malloc(n * sizeof(struct iocb *)); if (!iocb || !piocb) { perror("iocb alloc"); return -1; } for (i = 0; i < n; i++) { piocb[i] = &iocb[i]; io_prep_pwrite(&iocb[i], fd, buf, sizeof(buf), (n - i - 1) * IORTX_SIZE); io_set_eventfd(&iocb[i], afd); io_set_callback(&iocb[i], (io_callback_t)(i + 1)); } fprintf(stdout, "submitting write request ...\n"); if (io_submit(ctx, n, piocb) <= 0) { perror("io_submit"); return -1; } for (i = 0; i < n;) { fprintf(stdout, "waiting ... "); waitasync(afd, -1); eval = 0; if (read(afd, &eval, sizeof(eval)) != sizeof(eval)) perror("read"); fprintf(stdout, "done! %llu\n", (unsigned long long) eval); while (eval > 0) { tmo.tv_sec = 0; tmo.tv_nsec = 0; r = io_getevents(ctx, 1, eval > NUM_EVENTS ? NUM_EVENTS: (long) eval, events, &tmo); if (r > 0) { for (j = 0; j < r; j++) { } i += r; eval -= r; fprintf(stdout, "test_write got %ld/%ld results so far\n", i, n); } } } free(iocb); free(piocb); return n; } int main(int argc, char **argv) { int afd, fd; io_context_t ctx; char const *testfn = "/tmp/eventfd-aio-test.data"; (void)argc; (void)argv; fprintf(stdout, "creating an eventfd ...\n"); if ((afd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC)) == -1) { perror("eventfd"); return 2; } fprintf(stdout, "done! eventfd = %d\n", afd); if (io_queue_init(TESTFILE_SIZE / IORTX_SIZE + 256, &ctx)) { perror("io_queue_init"); return 3; } if ((fd = open(testfn, O_RDWR | O_CREAT, 0644)) == -1) { perror(testfn); return 4; } ftruncate(fd, TESTFILE_SIZE); test_write(ctx, fd, TESTFILE_SIZE, afd); test_read(ctx, fd, TESTFILE_SIZE, afd); io_queue_release(ctx); close(fd); close(afd); remove(testfn); return 0; }