t/aio-ring: update for continually rolling ring
authorJens Axboe <axboe@kernel.dk>
Fri, 21 Dec 2018 21:47:34 +0000 (14:47 -0700)
committerJens Axboe <axboe@kernel.dk>
Fri, 21 Dec 2018 21:47:34 +0000 (14:47 -0700)
Signed-off-by: Jens Axboe <axboe@kernel.dk>
t/aio-ring.c

index c813c4e7f1de1283c49cd0dc80c19b3bb9274bea..0d53b574bc3a5de897b990787cae1ebb841fb4c8 100644 (file)
@@ -70,13 +70,15 @@ struct aio_cq_ring {
 #define IORING_FLAG_GETEVENTS  (1 << 1)
 
 #define DEPTH                  32
 #define IORING_FLAG_GETEVENTS  (1 << 1)
 
 #define DEPTH                  32
-#define RING_SIZE              (DEPTH + 1)
 
 #define BATCH_SUBMIT           8
 #define BATCH_COMPLETE         8
 
 #define BS                     4096
 
 
 #define BATCH_SUBMIT           8
 #define BATCH_COMPLETE         8
 
 #define BS                     4096
 
+static unsigned sq_ring_mask = DEPTH - 1;
+static unsigned cq_ring_mask = (2 * DEPTH) - 1;
+
 struct submitter {
        pthread_t thread;
        unsigned long max_blocks;
 struct submitter {
        pthread_t thread;
        unsigned long max_blocks;
@@ -141,20 +143,18 @@ static void init_io(struct submitter *s, int fd, struct iocb *iocb)
 static int prep_more_ios(struct submitter *s, int fd, int max_ios)
 {
        struct aio_sq_ring *ring = s->sq_ring;
 static int prep_more_ios(struct submitter *s, int fd, int max_ios)
 {
        struct aio_sq_ring *ring = s->sq_ring;
-       u32 tail, next_tail, prepped = 0;
+       u32 index, tail, next_tail, prepped = 0;
 
        next_tail = tail = ring->tail;
        do {
                next_tail++;
 
        next_tail = tail = ring->tail;
        do {
                next_tail++;
-               if (next_tail == ring->nr_events)
-                       next_tail = 0;
-
                barrier();
                if (next_tail == ring->head)
                        break;
 
                barrier();
                if (next_tail == ring->head)
                        break;
 
-               init_io(s, fd, &s->iocbs[tail]);
-               s->sq_ring->array[tail] = tail;
+               index = tail & sq_ring_mask;
+               init_io(s, fd, &s->iocbs[index]);
+               s->sq_ring->array[index] = index;
                prepped++;
                tail = next_tail;
        } while (prepped < max_ios);
                prepped++;
                tail = next_tail;
        } while (prepped < max_ios);
@@ -201,7 +201,7 @@ static int reap_events(struct submitter *s)
                barrier();
                if (head == ring->tail)
                        break;
                barrier();
                if (head == ring->tail)
                        break;
-               ev = &ring->events[head];
+               ev = &ring->events[head & cq_ring_mask];
                if (ev->res != BS) {
                        struct iocb *iocb = ev->obj;
 
                if (ev->res != BS) {
                        struct iocb *iocb = ev->obj;
 
@@ -215,8 +215,6 @@ static int reap_events(struct submitter *s)
                        s->cachemiss++;
                reaped++;
                head++;
                        s->cachemiss++;
                reaped++;
                head++;
-               if (head == ring->nr_events)
-                       head = 0;
        } while (1);
 
        s->inflight -= reaped;
        } while (1);
 
        s->inflight -= reaped;
@@ -361,30 +359,30 @@ int main(int argc, char *argv[])
 
        arm_sig_int();
 
 
        arm_sig_int();
 
-       size = sizeof(struct iocb) * RING_SIZE;
+       size = sizeof(struct iocb) * DEPTH;
        if (posix_memalign(&p, 4096, size))
                return 1;
        memset(p, 0, size);
        s->iocbs = p;
 
        if (posix_memalign(&p, 4096, size))
                return 1;
        memset(p, 0, size);
        s->iocbs = p;
 
-       size = sizeof(struct aio_sq_ring) + RING_SIZE * sizeof(u32);
+       size = sizeof(struct aio_sq_ring) + DEPTH * sizeof(u32);
        if (posix_memalign(&p, 4096, size))
                return 1;
        s->sq_ring = p;
        memset(p, 0, size);
        if (posix_memalign(&p, 4096, size))
                return 1;
        s->sq_ring = p;
        memset(p, 0, size);
-       s->sq_ring->nr_events = RING_SIZE;
+       s->sq_ring->nr_events = DEPTH;
        s->sq_ring->iocbs = (u64) s->iocbs;
 
        /* CQ ring must be twice as big */
        size = sizeof(struct aio_cq_ring) +
        s->sq_ring->iocbs = (u64) s->iocbs;
 
        /* CQ ring must be twice as big */
        size = sizeof(struct aio_cq_ring) +
-                       2 * RING_SIZE * sizeof(struct io_event);
+                       2 * DEPTH * sizeof(struct io_event);
        if (posix_memalign(&p, 4096, size))
                return 1;
        s->cq_ring = p;
        memset(p, 0, size);
        if (posix_memalign(&p, 4096, size))
                return 1;
        s->cq_ring = p;
        memset(p, 0, size);
-       s->cq_ring->nr_events = 2 * RING_SIZE;
+       s->cq_ring->nr_events = 2 * DEPTH;
 
 
-       for (j = 0; j < RING_SIZE; j++) {
+       for (j = 0; j < DEPTH; j++) {
                struct iocb *iocb = &s->iocbs[j];
 
                if (posix_memalign(&iocb->u.c.buf, BS, BS)) {
                struct iocb *iocb = &s->iocbs[j];
 
                if (posix_memalign(&iocb->u.c.buf, BS, BS)) {
@@ -406,7 +404,7 @@ int main(int argc, char *argv[])
                s->sq_ring->sq_thread_cpu = sq_thread_cpu;
        }
 
                s->sq_ring->sq_thread_cpu = sq_thread_cpu;
        }
 
-       err = io_setup2(RING_SIZE, flags, s->sq_ring, s->cq_ring, &s->ioc);
+       err = io_setup2(DEPTH, flags, s->sq_ring, s->cq_ring, &s->ioc);
        if (err) {
                printf("ctx_init failed: %s, %d\n", strerror(errno), err);
                return 1;
        if (err) {
                printf("ctx_init failed: %s, %d\n", strerror(errno), err);
                return 1;