ringtest: commonize implementation of poll_avail/poll_used
authorPaolo Bonzini <pbonzini@redhat.com>
Thu, 6 Oct 2016 09:39:11 +0000 (11:39 +0200)
committerMichael S. Tsirkin <mst@redhat.com>
Sun, 30 Oct 2016 22:21:43 +0000 (00:21 +0200)
Provide new primitives used_empty/avail_empty and
build poll_avail/poll_used on top of it.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
tools/virtio/ringtest/main.c
tools/virtio/ringtest/main.h
tools/virtio/ringtest/noring.c
tools/virtio/ringtest/ptr_ring.c
tools/virtio/ringtest/ring.c
tools/virtio/ringtest/virtio_ring_0_9.c

index bda7f0dad981d79f2d66b7b16dfbb5f9e428746b..b00ecd619969ac36afe45c0929dce48f0d675cd1 100644 (file)
@@ -96,6 +96,12 @@ void set_affinity(const char *arg)
        assert(!ret);
 }
 
+void poll_used(void)
+{
+       while (used_empty())
+               busy_wait();
+}
+
 static void __attribute__((__flatten__)) run_guest(void)
 {
        int completed_before;
@@ -149,6 +155,12 @@ static void __attribute__((__flatten__)) run_guest(void)
        }
 }
 
+void poll_avail(void)
+{
+       while (avail_empty())
+               busy_wait();
+}
+
 static void __attribute__((__flatten__)) run_host(void)
 {
        int completed_before;
index 16917acb0adef30beab588329e3e8e547aff04ef..34e63cc4c572bfcafe6fecb4784fc4ba5079bf8a 100644 (file)
@@ -56,15 +56,15 @@ void alloc_ring(void);
 int add_inbuf(unsigned, void *, void *);
 void *get_buf(unsigned *, void **);
 void disable_call();
+bool used_empty();
 bool enable_call();
 void kick_available();
-void poll_used();
 /* host side */
 void disable_kick();
+bool avail_empty();
 bool enable_kick();
 bool use_buf(unsigned *, void **);
 void call_used();
-void poll_avail();
 
 /* implemented by main */
 extern bool do_sleep;
index eda2f4824130e36f3970794f1a4b396809e6de44..b8d1c1daac7cc089734c6a6cf3453ae7277abe62 100644 (file)
@@ -24,8 +24,9 @@ void *get_buf(unsigned *lenp, void **bufp)
        return "Buffer";
 }
 
-void poll_used(void)
+bool used_empty()
 {
+       return false;
 }
 
 void disable_call()
@@ -54,8 +55,9 @@ bool enable_kick()
        assert(0);
 }
 
-void poll_avail(void)
+bool avail_empty()
 {
+       return false;
 }
 
 bool use_buf(unsigned *lenp, void **bufp)
index bd2ad1d3b7a9ef88e28e1ad982dd37638841fa04..635b07b4fdd3949c7883a2775575c0ff4d8ce228 100644 (file)
@@ -133,18 +133,9 @@ void *get_buf(unsigned *lenp, void **bufp)
        return datap;
 }
 
-void poll_used(void)
+bool used_empty()
 {
-       void *b;
-
-       do {
-               if (tailcnt == headcnt || __ptr_ring_full(&array)) {
-                       b = NULL;
-                       barrier();
-               } else {
-                       b = "Buffer\n";
-               }
-       } while (!b);
+       return (tailcnt == headcnt || __ptr_ring_full(&array));
 }
 
 void disable_call()
@@ -173,14 +164,9 @@ bool enable_kick()
        assert(0);
 }
 
-void poll_avail(void)
+bool avail_empty()
 {
-       void *b;
-
-       do {
-               barrier();
-               b = __ptr_ring_peek(&array);
-       } while (!b);
+       return !__ptr_ring_peek(&array);
 }
 
 bool use_buf(unsigned *lenp, void **bufp)
index c25c8d248b6b7dfc5a6e5bbce1bb9f6ba7561244..747c5dd47be8b075c7ca1558393e45c5b7a47e63 100644 (file)
@@ -163,12 +163,11 @@ void *get_buf(unsigned *lenp, void **bufp)
        return datap;
 }
 
-void poll_used(void)
+bool used_empty()
 {
        unsigned head = (ring_size - 1) & guest.last_used_idx;
 
-       while (ring[head].flags & DESC_HW)
-               busy_wait();
+       return (ring[head].flags & DESC_HW);
 }
 
 void disable_call()
@@ -180,13 +179,11 @@ void disable_call()
 
 bool enable_call()
 {
-       unsigned head = (ring_size - 1) & guest.last_used_idx;
-
        event->call_index = guest.last_used_idx;
        /* Flush call index write */
        /* Barrier D (for pairing) */
        smp_mb();
-       return ring[head].flags & DESC_HW;
+       return used_empty();
 }
 
 void kick_available(void)
@@ -213,20 +210,17 @@ void disable_kick()
 
 bool enable_kick()
 {
-       unsigned head = (ring_size - 1) & host.used_idx;
-
        event->kick_index = host.used_idx;
        /* Barrier C (for pairing) */
        smp_mb();
-       return !(ring[head].flags & DESC_HW);
+       return avail_empty();
 }
 
-void poll_avail(void)
+bool avail_empty()
 {
        unsigned head = (ring_size - 1) & host.used_idx;
 
-       while (!(ring[head].flags & DESC_HW))
-               busy_wait();
+       return !(ring[head].flags & DESC_HW);
 }
 
 bool use_buf(unsigned *lenp, void **bufp)
index 761866212aacf1149d03ef1151ac7727c944db72..bbc3043b2fb169aa4764922a786e9cbfc34761ed 100644 (file)
@@ -194,24 +194,16 @@ void *get_buf(unsigned *lenp, void **bufp)
        return datap;
 }
 
-void poll_used(void)
+bool used_empty()
 {
+       unsigned short last_used_idx = guest.last_used_idx;
 #ifdef RING_POLL
-       unsigned head = (ring_size - 1) & guest.last_used_idx;
+       unsigned short head = last_used_idx & (ring_size - 1);
+       unsigned index = ring.used->ring[head].id;
 
-       for (;;) {
-               unsigned index = ring.used->ring[head].id;
-
-               if ((index ^ guest.last_used_idx ^ 0x8000) & ~(ring_size - 1))
-                       busy_wait();
-               else
-                       break;
-       }
+       return (index ^ last_used_idx ^ 0x8000) & ~(ring_size - 1);
 #else
-       unsigned head = guest.last_used_idx;
-
-       while (ring.used->idx == head)
-               busy_wait();
+       return ring.used->idx == last_used_idx;
 #endif
 }
 
@@ -224,22 +216,11 @@ void disable_call()
 
 bool enable_call()
 {
-       unsigned short last_used_idx;
-
-       vring_used_event(&ring) = (last_used_idx = guest.last_used_idx);
+       vring_used_event(&ring) = guest.last_used_idx;
        /* Flush call index write */
        /* Barrier D (for pairing) */
        smp_mb();
-#ifdef RING_POLL
-       {
-               unsigned short head = last_used_idx & (ring_size - 1);
-               unsigned index = ring.used->ring[head].id;
-
-               return (index ^ last_used_idx ^ 0x8000) & ~(ring_size - 1);
-       }
-#else
-       return ring.used->idx == last_used_idx;
-#endif
+       return used_empty();
 }
 
 void kick_available(void)
@@ -266,36 +247,21 @@ void disable_kick()
 
 bool enable_kick()
 {
-       unsigned head = host.used_idx;
-
-       vring_avail_event(&ring) = head;
+       vring_avail_event(&ring) = host.used_idx;
        /* Barrier C (for pairing) */
        smp_mb();
-#ifdef RING_POLL
-       {
-               unsigned index = ring.avail->ring[head & (ring_size - 1)];
-
-               return (index ^ head ^ 0x8000) & ~(ring_size - 1);
-       }
-#else
-       return head == ring.avail->idx;
-#endif
+       return avail_empty();
 }
 
-void poll_avail(void)
+bool avail_empty()
 {
        unsigned head = host.used_idx;
 #ifdef RING_POLL
-       for (;;) {
-               unsigned index = ring.avail->ring[head & (ring_size - 1)];
-               if ((index ^ head ^ 0x8000) & ~(ring_size - 1))
-                       busy_wait();
-               else
-                       break;
-       }
+       unsigned index = ring.avail->ring[head & (ring_size - 1)];
+
+       return ((index ^ head ^ 0x8000) & ~(ring_size - 1));
 #else
-       while (ring.avail->idx == head)
-               busy_wait();
+       return head == ring.avail->idx;
 #endif
 }