t/aio-ring: print head/tail as unsigneds
[fio.git] / t / aio-ring.c
index 32eb949be68f479b2cd79a337f4d0c94bf8b2c1b..900f4640c6bba3d6c2cdb22f31ed4ba738581b03 100644 (file)
 #define IOCTX_FLAG_SQTHREAD    (1 << 3)        /* Use SQ thread */
 #define IOCTX_FLAG_SQWQ                (1 << 4)        /* Use SQ wq */
 
+#define IOEV_RES2_CACHEHIT     (1 << 0)
+
 #define barrier()      __asm__ __volatile__("": : :"memory")
 
 #define min(a, b)              ((a < b) ? (a) : (b))
 
+typedef uint64_t u64;
 typedef uint32_t u32;
 typedef uint16_t u16;
 
-struct aio_iocb_ring {
+struct aio_sq_ring {
        union {
                struct {
-                       u32 head, tail;
+                       u32 head;
+                       u32 tail;
                        u32 nr_events;
                        u16 sq_thread_cpu;
+                       u64 iocbs;
                };
-               struct iocb pad_iocb;
+               u32 pad[16];
        };
-       struct iocb iocbs[0];
+       u32 array[0];
 };
 
-struct aio_io_event_ring {
+struct aio_cq_ring {
        union {
                struct {
-                       u32 head, tail;
+                       u32 head;
+                       u32 tail;
                        u32 nr_events;
                };
-               struct io_event pad_event;
+               struct io_event pad;
        };
        struct io_event events[0];
 };
@@ -64,24 +70,28 @@ struct aio_io_event_ring {
 #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
 
+static unsigned sq_ring_mask = DEPTH - 1;
+static unsigned cq_ring_mask = (2 * DEPTH) - 1;
+
 struct submitter {
        pthread_t thread;
        unsigned long max_blocks;
        io_context_t ioc;
        struct drand48_data rand;
-       struct aio_iocb_ring *sq_ring;
-       struct aio_io_event_ring *cq_ring;
+       struct aio_sq_ring *sq_ring;
+       struct iocb *iocbs;
+       struct aio_cq_ring *cq_ring;
        int inflight;
        unsigned long reaps;
        unsigned long done;
        unsigned long calls;
+       unsigned long cachehit, cachemiss;
        volatile int finish;
        char filename[128];
 };
@@ -96,10 +106,10 @@ static int sq_thread = 0;  /* use kernel submission thread */
 static int sq_thread_cpu = 0;  /* pin above thread to this CPU */
 
 static int io_setup2(unsigned int nr_events, unsigned int flags,
-                    struct iocb *iocbs, struct aio_iocb_ring *sq_ring,
-                    struct aio_io_event_ring *cq_ring, io_context_t *ctx_idp)
+                    struct aio_sq_ring *sq_ring, struct aio_cq_ring *cq_ring,
+                    io_context_t *ctx_idp)
 {
-       return syscall(335, nr_events, flags, iocbs, sq_ring, cq_ring, ctx_idp);
+       return syscall(335, nr_events, flags, sq_ring, cq_ring, ctx_idp);
 }
 
 static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
@@ -132,22 +142,19 @@ 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_iocb_ring *ring = s->sq_ring;
-       struct iocb *iocb;
-       u32 tail, next_tail, prepped = 0;
+       struct aio_sq_ring *ring = s->sq_ring;
+       u32 index, tail, next_tail, prepped = 0;
 
        next_tail = tail = ring->tail;
        do {
                next_tail++;
-               if (next_tail == ring->nr_events)
-                       next_tail = 0;
-
                barrier();
                if (next_tail == ring->head)
                        break;
 
-               iocb = &s->sq_ring->iocbs[tail];
-               init_io(s, fd, iocb);
+               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);
@@ -185,7 +192,7 @@ static int get_file_size(int fd, unsigned long *blocks)
 
 static int reap_events(struct submitter *s)
 {
-       struct aio_io_event_ring *ring = s->cq_ring;
+       struct aio_cq_ring *ring = s->cq_ring;
        struct io_event *ev;
        u32 head, reaped = 0;
 
@@ -194,19 +201,20 @@ static int reap_events(struct submitter *s)
                barrier();
                if (head == ring->tail)
                        break;
-               ev = &ring->events[head];
+               ev = &ring->events[head & cq_ring_mask];
                if (ev->res != BS) {
-                       int index = (int) (uintptr_t) ev->obj;
-                       struct iocb *iocb = &s->sq_ring->iocbs[index];
+                       struct iocb *iocb = ev->obj;
 
                        printf("io: unexpected ret=%ld\n", ev->res);
                        printf("offset=%lu, size=%lu\n", (unsigned long) iocb->u.c.offset, (unsigned long) iocb->u.c.nbytes);
                        return -1;
                }
+               if (ev->res2 & IOEV_RES2_CACHEHIT)
+                       s->cachehit++;
+               else
+                       s->cachemiss++;
                reaped++;
                head++;
-               if (head == ring->nr_events)
-                       head = 0;
        } while (1);
 
        s->inflight -= reaped;
@@ -330,7 +338,7 @@ static void arm_sig_int(void)
 int main(int argc, char *argv[])
 {
        struct submitter *s = &submitters[0];
-       unsigned long done, calls, reap;
+       unsigned long done, calls, reap, cache_hit, cache_miss;
        int flags = 0, err;
        int j;
        size_t size;
@@ -351,24 +359,31 @@ int main(int argc, char *argv[])
 
        arm_sig_int();
 
-       size = sizeof(struct aio_iocb_ring) + RING_SIZE * sizeof(struct iocb);
+       size = sizeof(struct iocb) * DEPTH;
+       if (posix_memalign(&p, 4096, size))
+               return 1;
+       memset(p, 0, size);
+       s->iocbs = p;
+
+       size = sizeof(struct aio_sq_ring) + DEPTH * sizeof(u32);
        if (posix_memalign(&p, 4096, size))
                return 1;
        s->sq_ring = p;
-       s->sq_ring->nr_events = RING_SIZE;
        memset(p, 0, 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_io_event_ring) +
-                       2 * RING_SIZE * sizeof(struct io_event);
+       size = sizeof(struct aio_cq_ring) +
+                       2 * DEPTH * sizeof(struct io_event);
        if (posix_memalign(&p, 4096, size))
                return 1;
        s->cq_ring = p;
-       s->cq_ring->nr_events = 2 * RING_SIZE;
        memset(p, 0, size);
+       s->cq_ring->nr_events = 2 * DEPTH;
 
-       for (j = 0; j < RING_SIZE; j++) {
-               struct iocb *iocb = &s->sq_ring->iocbs[j];
+       for (j = 0; j < DEPTH; j++) {
+               struct iocb *iocb = &s->iocbs[j];
 
                if (posix_memalign(&iocb->u.c.buf, BS, BS)) {
                        printf("failed alloc\n");
@@ -389,7 +404,7 @@ int main(int argc, char *argv[])
                s->sq_ring->sq_thread_cpu = sq_thread_cpu;
        }
 
-       err = io_setup2(RING_SIZE, flags, s->sq_ring->iocbs, 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;
@@ -400,27 +415,42 @@ int main(int argc, char *argv[])
 
        pthread_create(&s->thread, NULL, submitter_fn, s);
 
-       reap = calls = done = 0;
+       cache_hit = cache_miss = reap = calls = done = 0;
        do {
                unsigned long this_done = 0;
                unsigned long this_reap = 0;
                unsigned long this_call = 0;
+               unsigned long this_cache_hit = 0;
+               unsigned long this_cache_miss = 0;
                unsigned long rpc = 0, ipc = 0;
+               double hit = 0.0;
 
                sleep(1);
                this_done += s->done;
                this_call += s->calls;
                this_reap += s->reaps;
+               this_cache_hit += s->cachehit;
+               this_cache_miss += s->cachemiss;
+               if (this_cache_hit && this_cache_miss) {
+                       unsigned long hits, total;
+
+                       hits = this_cache_hit - cache_hit;
+                       total = hits + this_cache_miss - cache_miss;
+                       hit = (double) hits / (double) total;
+                       hit *= 100.0;
+               }
                if (this_call - calls) {
                        rpc = (this_done - done) / (this_call - calls);
                        ipc = (this_reap - reap) / (this_call - calls);
                }
-               printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%d tail=%d), %lu, %lu\n",
+               printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
                                this_done - done, rpc, ipc, s->inflight,
-                               s->cq_ring->head, s->cq_ring->tail, s->reaps, s->done);
+                               s->cq_ring->head, s->cq_ring->tail, hit);
                done = this_done;
                calls = this_call;
                reap = this_reap;
+               cache_hit = s->cachehit;
+               cache_miss = s->cachemiss;
        } while (!finish);
 
        pthread_join(s->thread, &ret);