X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fsvm%2Fmessage_queue.c;h=b381173c70b31082d0177700d51e6f20d2231113;hb=37b445468e45b537621269fc1e375f26ca2100ee;hp=e97cab898e8ded3dfbdff3f5409ebc4642a05960;hpb=c470e22f12a68f06990f57f12f551fee50b6bb0d;p=vpp.git diff --git a/src/svm/message_queue.c b/src/svm/message_queue.c index e97cab898e8..b381173c70b 100644 --- a/src/svm/message_queue.c +++ b/src/svm/message_queue.c @@ -15,6 +15,8 @@ #include #include +#include +#include static inline svm_msg_q_ring_t * svm_msg_q_ring_inline (svm_msg_q_t * mq, u32 ring_index) @@ -71,7 +73,7 @@ svm_msg_q_alloc (svm_msg_q_cfg_t * cfg) vh = (vec_header_t *) ((u8 *) mq->q + q_sz); vh->len = cfg->n_rings; mq->rings = (svm_msg_q_ring_t *) (vh + 1); - rings_ptr = (u8 *) mq->rings + vec_sz; + rings_ptr = (u8 *) mq->rings + sizeof (svm_msg_q_ring_t) * cfg->n_rings; for (i = 0; i < cfg->n_rings; i++) { ring = &mq->rings[i]; @@ -107,7 +109,7 @@ svm_msg_q_alloc_msg_w_ring (svm_msg_q_t * mq, u32 ring_index) msg.ring_index = ring - mq->rings; msg.elt_index = ring->tail; ring->tail = (ring->tail + 1) % ring->nitems; - __sync_fetch_and_add (&ring->cursize, 1); + clib_atomic_fetch_add (&ring->cursize, 1); return msg; } @@ -119,22 +121,19 @@ svm_msg_q_lock_and_alloc_msg_w_ring (svm_msg_q_t * mq, u32 ring_index, { if (svm_msg_q_try_lock (mq)) return -1; - if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, ring_index))) + if (PREDICT_FALSE (svm_msg_q_is_full (mq) + || svm_msg_q_ring_is_full (mq, ring_index))) { svm_msg_q_unlock (mq); return -2; } *msg = svm_msg_q_alloc_msg_w_ring (mq, ring_index); - if (PREDICT_FALSE (svm_msg_q_msg_is_invalid (msg))) - { - svm_msg_q_unlock (mq); - return -2; - } } else { svm_msg_q_lock (mq); - while (svm_msg_q_ring_is_full (mq, ring_index)) + while (svm_msg_q_is_full (mq) + || svm_msg_q_ring_is_full (mq, ring_index)) svm_msg_q_wait (mq); *msg = svm_msg_q_alloc_msg_w_ring (mq, ring_index); } @@ -154,7 +153,7 @@ svm_msg_q_alloc_msg (svm_msg_q_t * mq, u32 nbytes) msg.ring_index = ring - mq->rings; msg.elt_index = ring->tail; ring->tail = (ring->tail + 1) % ring->nitems; - __sync_fetch_and_add (&ring->cursize, 1); + clib_atomic_fetch_add (&ring->cursize, 1); break; } return msg; @@ -171,9 +170,9 @@ void svm_msg_q_free_msg (svm_msg_q_t * mq, svm_msg_q_msg_t * msg) { svm_msg_q_ring_t *ring; + int need_signal; - if (vec_len (mq->rings) <= msg->ring_index) - return; + ASSERT (vec_len (mq->rings) > msg->ring_index); ring = &mq->rings[msg->ring_index]; if (msg->elt_index == ring->head) { @@ -181,10 +180,16 @@ svm_msg_q_free_msg (svm_msg_q_t * mq, svm_msg_q_msg_t * msg) } else { + clib_warning ("message out of order"); /* for now, expect messages to be processed in order */ ASSERT (0); } - __sync_fetch_and_sub (&ring->cursize, 1); + + need_signal = ring->cursize == ring->nitems; + clib_atomic_fetch_sub (&ring->cursize, 1); + + if (PREDICT_FALSE (need_signal)) + svm_queue_send_signal (mq->q, 0); } static int @@ -235,6 +240,51 @@ svm_msg_q_sub_w_lock (svm_msg_q_t * mq, svm_msg_q_msg_t * msg) svm_queue_sub_raw (mq->q, (u8 *) msg); } +void +svm_msg_q_set_consumer_eventfd (svm_msg_q_t * mq, int fd) +{ + mq->q->consumer_evtfd = fd; +} + +void +svm_msg_q_set_producer_eventfd (svm_msg_q_t * mq, int fd) +{ + mq->q->producer_evtfd = fd; +} + +int +svm_msg_q_alloc_consumer_eventfd (svm_msg_q_t * mq) +{ + int fd; + if ((fd = eventfd (0, EFD_NONBLOCK)) < 0) + return -1; + svm_msg_q_set_consumer_eventfd (mq, fd); + return 0; +} + +int +svm_msg_q_alloc_producer_eventfd (svm_msg_q_t * mq) +{ + int fd; + if ((fd = eventfd (0, EFD_NONBLOCK)) < 0) + return -1; + svm_msg_q_set_producer_eventfd (mq, fd); + return 0; +} + +u8 * +format_svm_msg_q (u8 * s, va_list * args) +{ + svm_msg_q_t *mq = va_arg (*args, svm_msg_q_t *); + s = format (s, " [Q:%d/%d]", mq->q->cursize, mq->q->maxsize); + for (u32 i = 0; i < vec_len (mq->rings); i++) + { + s = format (s, " [R%d:%d/%d]", i, mq->rings[i].cursize, + mq->rings[i].nitems); + } + return s; +} + /* * fd.io coding-style-patch-verification: ON *