Skip to contentSkip to conversation

ring: empty and count optimizations

PATCH New Morten Brørup 4 participants 16 messages May 13, 2020 – Jul 1, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ring: empty and count optimizations

Testing if the ring is empty is as simple as comparing the producer and\nconsumer pointers.\nIn theory, this optimization reduces the number of potential cache misses\nfrom 3 to 2 by not having to read r->mask in rte_ring_count().\n\nIt is not possible to enqueue more elements than the capacity of a ring,\nso the capacity comparison is a safeguard for observer threads only.\nInstead of completely removing the comparison, I have reorganized it to\nresemble the other trigrahps in the ring library and added a likely().\n\nThe modification of these two functions were discussed in the RFC here:\nhttps://mails.dpdk.org/archives/dev/2020-April/165752.html

---
 lib/librte_ring/rte_ring.h | 36 +++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 17 deletions(-)
lib/librte_ring/rte_ring.h +19 -17
index 86faede81..74a8fcdc8 100644
@@ -55,7 +55,7 @@ extern "C" {
  *   - The memory size needed for the ring on success.
  *   - -EINVAL if count is not a power of 2.
  */
-ssize_t rte_ring_get_memsize(unsigned count);
+ssize_t rte_ring_get_memsize(unsigned int count);
 
 /**
  * Initialize a ring structure.
@@ -109,8 +109,8 @@ ssize_t rte_ring_get_memsize(unsigned count);
  * @return
  *   0 on success, or a negative value on error.
  */
-int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
-	unsigned flags);
+int rte_ring_init(struct rte_ring *r, const char *name, unsigned int count,
+	unsigned int flags);
 
 /**
  * Create a new ring named *name* in memory.
@@ -169,8 +169,8 @@ int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
  *    - EEXIST - a memzone with the same name already exists
  *    - ENOMEM - no appropriate memory area found in which to create memzone
  */
-struct rte_ring *rte_ring_create(const char *name, unsigned count,
-				 int socket_id, unsigned flags);
+struct rte_ring *rte_ring_create(const char *name, unsigned int count,
+				 int socket_id, unsigned int flags);
 
 /**
  * De-allocate all memory used by the ring.
@@ -199,7 +199,7 @@ void rte_ring_dump(FILE *f, const struct rte_ring *r);
 	uint32_t idx = prod_head & (r)->mask; \
 	obj_type *ring = (obj_type *)ring_start; \
 	if (likely(idx + n < size)) { \
-		for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
+		for (i = 0; i < (n & ((~(unsigned int)0x3))); i+=4, idx+=4) { \
 			ring[idx] = obj_table[i]; \
 			ring[idx+1] = obj_table[i+1]; \
 			ring[idx+2] = obj_table[i+2]; \
@@ -230,7 +230,7 @@ void rte_ring_dump(FILE *f, const struct rte_ring *r);
 	const uint32_t size = (r)->size; \
 	obj_type *ring = (obj_type *)ring_start; \
 	if (likely(idx + n < size)) { \
-		for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
+		for (i = 0; i < (n & (~(unsigned int)0x3)); i+=4, idx+=4) {\
 			obj_table[i] = ring[idx]; \
 			obj_table[i+1] = ring[idx+1]; \
 			obj_table[i+2] = ring[idx+2]; \
@@ -683,13 +683,13 @@ rte_ring_reset(struct rte_ring *r);
  * @return
  *   The number of entries in the ring.
  */
-static inline unsigned
+static inline unsigned int
 rte_ring_count(const struct rte_ring *r)
 {
 	uint32_t prod_tail = r->prod.tail;
 	uint32_t cons_tail = r->cons.tail;
 	uint32_t count = (prod_tail - cons_tail) & r->mask;
-	return (count > r->capacity) ? r->capacity : count;
+	return likely(count <= r->capacity) ? count : r->capacity;
 }
 
 /**
@@ -700,7 +700,7 @@ rte_ring_count(const struct rte_ring *r)
  * @return
  *   The number of free entries in the ring.
  */
-static inline unsigned
+static inline unsigned int
 rte_ring_free_count(const struct rte_ring *r)
 {
 	return r->capacity - rte_ring_count(r);
@@ -733,7 +733,9 @@ rte_ring_full(const struct rte_ring *r)
 static inline int
 rte_ring_empty(const struct rte_ring *r)
 {
-	return rte_ring_count(r) == 0;
+	uint32_t prod_tail = r->prod.tail;
+	uint32_t cons_tail = r->cons.tail;
+	return cons_tail == prod_tail;
 }
 
 /**
@@ -860,7 +862,7 @@ struct rte_ring *rte_ring_lookup(const char *name);
  * @return
  *   - n: Actual number of objects enqueued.
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
 			 unsigned int n, unsigned int *free_space)
 {
@@ -883,7 +885,7 @@ rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
  * @return
  *   - n: Actual number of objects enqueued.
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
 			 unsigned int n, unsigned int *free_space)
 {
@@ -910,7 +912,7 @@ rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
  * @return
  *   - n: Actual number of objects enqueued.
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
 		      unsigned int n, unsigned int *free_space)
 {
@@ -954,7 +956,7 @@ rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
  * @return
  *   - n: Actual number of objects dequeued, 0 if ring is empty
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table,
 		unsigned int n, unsigned int *available)
 {
@@ -979,7 +981,7 @@ rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table,
  * @return
  *   - n: Actual number of objects dequeued, 0 if ring is empty
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table,
 		unsigned int n, unsigned int *available)
 {
@@ -1006,7 +1008,7 @@ rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table,
  * @return
  *   - Number of objects dequeued
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table,
 		unsigned int n, unsigned int *available)
 {
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/1 ring: empty and count optimizations

Testing if the ring is empty is as simple as comparing the producer and consumer pointers.
In theory, this optimization reduces the number of potential cache misses from 3 to 2 by not having to read r->mask in rte_ring_count().

It is not possible to enqueue more elements than the capacity of a ring, so the capacity comparison is a safeguard for observer threads only. Instead of completely removing the comparison, I have reorganized it to resemble the other trigrahps in the ring library and added a likely().

The modification of these two functions were discussed in the RFC here:
https://mails.dpdk.org/archives/dev/2020-April/165752.html

---
 lib/librte_ring/rte_ring.h | 36 +++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 17 deletions(-)
lib/librte_ring/rte_ring.h +19 -17
index 86faede81..74a8fcdc8 100644
@@ -55,7 +55,7 @@ extern "C" {
  *   - The memory size needed for the ring on success.
  *   - -EINVAL if count is not a power of 2.
  */
-ssize_t rte_ring_get_memsize(unsigned count);
+ssize_t rte_ring_get_memsize(unsigned int count);
 
 /**
  * Initialize a ring structure.
@@ -109,8 +109,8 @@ ssize_t rte_ring_get_memsize(unsigned count);
  * @return
  *   0 on success, or a negative value on error.
  */
-int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
-	unsigned flags);
+int rte_ring_init(struct rte_ring *r, const char *name, unsigned int count,
+	unsigned int flags);
 
 /**
  * Create a new ring named *name* in memory.
@@ -169,8 +169,8 @@ int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
  *    - EEXIST - a memzone with the same name already exists
  *    - ENOMEM - no appropriate memory area found in which to create memzone
  */
-struct rte_ring *rte_ring_create(const char *name, unsigned count,
-				 int socket_id, unsigned flags);
+struct rte_ring *rte_ring_create(const char *name, unsigned int count,
+				 int socket_id, unsigned int flags);
 
 /**
  * De-allocate all memory used by the ring.
@@ -199,7 +199,7 @@ void rte_ring_dump(FILE *f, const struct rte_ring *r);
 	uint32_t idx = prod_head & (r)->mask; \
 	obj_type *ring = (obj_type *)ring_start; \
 	if (likely(idx + n < size)) { \
-		for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
+		for (i = 0; i < (n & ((~(unsigned int)0x3))); i+=4, idx+=4) { \
 			ring[idx] = obj_table[i]; \
 			ring[idx+1] = obj_table[i+1]; \
 			ring[idx+2] = obj_table[i+2]; \
@@ -230,7 +230,7 @@ void rte_ring_dump(FILE *f, const struct rte_ring *r);
 	const uint32_t size = (r)->size; \
 	obj_type *ring = (obj_type *)ring_start; \
 	if (likely(idx + n < size)) { \
-		for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
+		for (i = 0; i < (n & (~(unsigned int)0x3)); i+=4, idx+=4) {\
 			obj_table[i] = ring[idx]; \
 			obj_table[i+1] = ring[idx+1]; \
 			obj_table[i+2] = ring[idx+2]; \
@@ -683,13 +683,13 @@ rte_ring_reset(struct rte_ring *r);
  * @return
  *   The number of entries in the ring.
  */
-static inline unsigned
+static inline unsigned int
 rte_ring_count(const struct rte_ring *r)
 {
 	uint32_t prod_tail = r->prod.tail;
 	uint32_t cons_tail = r->cons.tail;
 	uint32_t count = (prod_tail - cons_tail) & r->mask;
-	return (count > r->capacity) ? r->capacity : count;
+	return likely(count <= r->capacity) ? count : r->capacity;
 }
 
 /**
@@ -700,7 +700,7 @@ rte_ring_count(const struct rte_ring *r)
  * @return
  *   The number of free entries in the ring.
  */
-static inline unsigned
+static inline unsigned int
 rte_ring_free_count(const struct rte_ring *r)
 {
 	return r->capacity - rte_ring_count(r);
@@ -733,7 +733,9 @@ rte_ring_full(const struct rte_ring *r)
 static inline int
 rte_ring_empty(const struct rte_ring *r)
 {
-	return rte_ring_count(r) == 0;
+	uint32_t prod_tail = r->prod.tail;
+	uint32_t cons_tail = r->cons.tail;
+	return cons_tail == prod_tail;
 }
 
 /**
@@ -860,7 +862,7 @@ struct rte_ring *rte_ring_lookup(const char *name);
  * @return
  *   - n: Actual number of objects enqueued.
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
 			 unsigned int n, unsigned int *free_space)
 {
@@ -883,7 +885,7 @@ rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
  * @return
  *   - n: Actual number of objects enqueued.
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
 			 unsigned int n, unsigned int *free_space)
 {
@@ -910,7 +912,7 @@ rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
  * @return
  *   - n: Actual number of objects enqueued.
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
 		      unsigned int n, unsigned int *free_space)
 {
@@ -954,7 +956,7 @@ rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
  * @return
  *   - n: Actual number of objects dequeued, 0 if ring is empty
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table,
 		unsigned int n, unsigned int *available)
 {
@@ -979,7 +981,7 @@ rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table,
  * @return
  *   - n: Actual number of objects dequeued, 0 if ring is empty
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table,
 		unsigned int n, unsigned int *available)
 {
@@ -1006,7 +1008,7 @@ rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table,
  * @return
  *   - Number of objects dequeued
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table,
 		unsigned int n, unsigned int *available)
 {
Signature · 1 line
2.17.1
#2 lore raw

PATCH 1/1 ring: empty and count optimizations

In reply to Morten Brørup

Testing if the ring is empty is as simple as comparing the producer and consumer pointers.
In theory, this optimization reduces the number of potential cache misses from 3 to 2 by not having to read r->mask in rte_ring_count().

It is not possible to enqueue more elements than the capacity of a ring, so the capacity comparison is a safeguard for observer threads only. Instead of completely removing the comparison, I have reorganized it to resemble the other trigrahps in the ring library and added a likely().

The modification of these two functions were discussed in the RFC here:
https://mails.dpdk.org/archives/dev/2020-April/165752.html

Also fixed some existing code not passing checkpatch.

---
 lib/librte_ring/rte_ring.h | 36 +++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 17 deletions(-)
lib/librte_ring/rte_ring.h +19 -17
index 86faede81..36438d9cd 100644
@@ -55,7 +55,7 @@ extern "C" {
  *   - The memory size needed for the ring on success.
  *   - -EINVAL if count is not a power of 2.
  */
-ssize_t rte_ring_get_memsize(unsigned count);
+ssize_t rte_ring_get_memsize(unsigned int count);
 
 /**
  * Initialize a ring structure.
@@ -109,8 +109,8 @@ ssize_t rte_ring_get_memsize(unsigned count);
  * @return
  *   0 on success, or a negative value on error.
  */
-int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
-	unsigned flags);
+int rte_ring_init(struct rte_ring *r, const char *name, unsigned int count,
+	unsigned int flags);
 
 /**
  * Create a new ring named *name* in memory.
@@ -169,8 +169,8 @@ int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
  *    - EEXIST - a memzone with the same name already exists
  *    - ENOMEM - no appropriate memory area found in which to create memzone
  */
-struct rte_ring *rte_ring_create(const char *name, unsigned count,
-				 int socket_id, unsigned flags);
+struct rte_ring *rte_ring_create(const char *name, unsigned int count,
+				 int socket_id, unsigned int flags);
 
 /**
  * De-allocate all memory used by the ring.
@@ -199,7 +199,7 @@ void rte_ring_dump(FILE *f, const struct rte_ring *r);
 	uint32_t idx = prod_head & (r)->mask; \
 	obj_type *ring = (obj_type *)ring_start; \
 	if (likely(idx + n < size)) { \
-		for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
+		for (i = 0; i < (n & ~0x3); i += 4, idx += 4) { \
 			ring[idx] = obj_table[i]; \
 			ring[idx+1] = obj_table[i+1]; \
 			ring[idx+2] = obj_table[i+2]; \
@@ -230,7 +230,7 @@ void rte_ring_dump(FILE *f, const struct rte_ring *r);
 	const uint32_t size = (r)->size; \
 	obj_type *ring = (obj_type *)ring_start; \
 	if (likely(idx + n < size)) { \
-		for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
+		for (i = 0; i < (n & ~0x3); i += 4, idx += 4) {\
 			obj_table[i] = ring[idx]; \
 			obj_table[i+1] = ring[idx+1]; \
 			obj_table[i+2] = ring[idx+2]; \
@@ -683,13 +683,13 @@ rte_ring_reset(struct rte_ring *r);
  * @return
  *   The number of entries in the ring.
  */
-static inline unsigned
+static inline unsigned int
 rte_ring_count(const struct rte_ring *r)
 {
 	uint32_t prod_tail = r->prod.tail;
 	uint32_t cons_tail = r->cons.tail;
 	uint32_t count = (prod_tail - cons_tail) & r->mask;
-	return (count > r->capacity) ? r->capacity : count;
+	return likely(count <= r->capacity) ? count : r->capacity;
 }
 
 /**
@@ -700,7 +700,7 @@ rte_ring_count(const struct rte_ring *r)
  * @return
  *   The number of free entries in the ring.
  */
-static inline unsigned
+static inline unsigned int
 rte_ring_free_count(const struct rte_ring *r)
 {
 	return r->capacity - rte_ring_count(r);
@@ -733,7 +733,9 @@ rte_ring_full(const struct rte_ring *r)
 static inline int
 rte_ring_empty(const struct rte_ring *r)
 {
-	return rte_ring_count(r) == 0;
+	uint32_t prod_tail = r->prod.tail;
+	uint32_t cons_tail = r->cons.tail;
+	return cons_tail == prod_tail;
 }
 
 /**
@@ -860,7 +862,7 @@ struct rte_ring *rte_ring_lookup(const char *name);
  * @return
  *   - n: Actual number of objects enqueued.
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
 			 unsigned int n, unsigned int *free_space)
 {
@@ -883,7 +885,7 @@ rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
  * @return
  *   - n: Actual number of objects enqueued.
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
 			 unsigned int n, unsigned int *free_space)
 {
@@ -910,7 +912,7 @@ rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
  * @return
  *   - n: Actual number of objects enqueued.
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
 		      unsigned int n, unsigned int *free_space)
 {
@@ -954,7 +956,7 @@ rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
  * @return
  *   - n: Actual number of objects dequeued, 0 if ring is empty
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table,
 		unsigned int n, unsigned int *available)
 {
@@ -979,7 +981,7 @@ rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table,
  * @return
  *   - n: Actual number of objects dequeued, 0 if ring is empty
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table,
 		unsigned int n, unsigned int *available)
 {
@@ -1006,7 +1008,7 @@ rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table,
  * @return
  *   - Number of objects dequeued
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table,
 		unsigned int n, unsigned int *available)
 {
Signature · 1 line
2.17.1
#3 lore raw

Hi Morten,

Quoted 25 lines

Testing if the ring is empty is as simple as comparing the producer and consumer pointers.
In theory, this optimization reduces the number of potential cache misses from 3 to 2 by not having to read r->mask in rte_ring_count().

It is not possible to enqueue more elements than the capacity of a ring, so the capacity comparison is a safeguard for observer threads only. Instead of completely removing the comparison, I have reorganized it to resemble the other trigrahps in the ring library and added a likely().

The modification of these two functions were discussed in the RFC here:
https://mails.dpdk.org/archives/dev/2020-April/165752.html

Also fixed some existing code not passing checkpatch.

---
 lib/librte_ring/rte_ring.h | 36 +++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 17 deletions(-)
lib/librte_ring/rte_ring.h +0 -0
index 86faede81..36438d9cd 100644

lib/librte_ring/rte_ring.h extern "C" { · line 58

Quoted 5 lines
lib/librte_ring/rte_ring.h +1 -1
@@ -55,7 +55,7 @@ extern "C" {
  *   - The memory size needed for the ring on success.
  *   - -EINVAL if count is not a power of 2.
  */
-ssize_t rte_ring_get_memsize(unsigned count);
+ssize_t rte_ring_get_memsize(unsigned int count);

All these changes to replace 'unsigned' with insigned int' - seems to be irrelevant to the patch subject, so can you put them to a separate patch in the series.

Quoted 45 lines
lib/librte_ring/rte_ring.h +6 -6
 /**
  * Initialize a ring structure.
@@ -109,8 +109,8 @@ ssize_t rte_ring_get_memsize(unsigned count);
  * @return
  *   0 on success, or a negative value on error.
  */
-int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
-	unsigned flags);
+int rte_ring_init(struct rte_ring *r, const char *name, unsigned int count,
+	unsigned int flags);
 
 /**
  * Create a new ring named *name* in memory.
@@ -169,8 +169,8 @@ int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
  *    - EEXIST - a memzone with the same name already exists
  *    - ENOMEM - no appropriate memory area found in which to create memzone
  */
-struct rte_ring *rte_ring_create(const char *name, unsigned count,
-				 int socket_id, unsigned flags);
+struct rte_ring *rte_ring_create(const char *name, unsigned int count,
+				 int socket_id, unsigned int flags);
 
 /**
  * De-allocate all memory used by the ring.
@@ -199,7 +199,7 @@ void rte_ring_dump(FILE *f, const struct rte_ring *r);
 	uint32_t idx = prod_head & (r)->mask; \
 	obj_type *ring = (obj_type *)ring_start; \
 	if (likely(idx + n < size)) { \
-		for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
+		for (i = 0; i < (n & ~0x3); i += 4, idx += 4) { \
 			ring[idx] = obj_table[i]; \
 			ring[idx+1] = obj_table[i+1]; \
 			ring[idx+2] = obj_table[i+2]; \
@@ -230,7 +230,7 @@ void rte_ring_dump(FILE *f, const struct rte_ring *r);
 	const uint32_t size = (r)->size; \
 	obj_type *ring = (obj_type *)ring_start; \
 	if (likely(idx + n < size)) { \
-		for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
+		for (i = 0; i < (n & ~0x3); i += 4, idx += 4) {\
 			obj_table[i] = ring[idx]; \
 			obj_table[i+1] = ring[idx+1]; \
 			obj_table[i+2] = ring[idx+2]; \
@@ -683,13 +683,13 @@ rte_ring_reset(struct rte_ring *r);
  * @return
  *   The number of entries in the ring.

lib/librte_ring/rte_ring.h rte_ring_reset(struct rte_ring *r); · line 692

Quoted 10 lines
lib/librte_ring/rte_ring.h +2 -2
  */
-static inline unsigned
+static inline unsigned int
 rte_ring_count(const struct rte_ring *r)
 {
 	uint32_t prod_tail = r->prod.tail;
 	uint32_t cons_tail = r->cons.tail;
 	uint32_t count = (prod_tail - cons_tail) & r->mask;
-	return (count > r->capacity) ? r->capacity : count;
+	return likely(count <= r->capacity) ? count : r->capacity;

Honestly, I don't see there is any point of that change:
I think it wouldn't change anything in terms of functionality or performance.

Quoted 79 lines
lib/librte_ring/rte_ring.h +10 -9
 }
 
 /**
@@ -700,7 +700,7 @@ rte_ring_count(const struct rte_ring *r)
  * @return
  *   The number of free entries in the ring.
  */
-static inline unsigned
+static inline unsigned int
 rte_ring_free_count(const struct rte_ring *r)
 {
 	return r->capacity - rte_ring_count(r);
@@ -733,7 +733,9 @@ rte_ring_full(const struct rte_ring *r)
 static inline int
 rte_ring_empty(const struct rte_ring *r)
 {
-	return rte_ring_count(r) == 0;
+	uint32_t prod_tail = r->prod.tail;
+	uint32_t cons_tail = r->cons.tail;
+	return cons_tail == prod_tail;
 }
 
 /**
@@ -860,7 +862,7 @@ struct rte_ring *rte_ring_lookup(const char *name);
  * @return
  *   - n: Actual number of objects enqueued.
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
 			 unsigned int n, unsigned int *free_space)
 {
@@ -883,7 +885,7 @@ rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
  * @return
  *   - n: Actual number of objects enqueued.
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
 			 unsigned int n, unsigned int *free_space)
 {
@@ -910,7 +912,7 @@ rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
  * @return
  *   - n: Actual number of objects enqueued.
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
 		      unsigned int n, unsigned int *free_space)
 {
@@ -954,7 +956,7 @@ rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
  * @return
  *   - n: Actual number of objects dequeued, 0 if ring is empty
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table,
 		unsigned int n, unsigned int *available)
 {
@@ -979,7 +981,7 @@ rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table,
  * @return
  *   - n: Actual number of objects dequeued, 0 if ring is empty
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table,
 		unsigned int n, unsigned int *available)
 {
@@ -1006,7 +1008,7 @@ rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table,
  * @return
  *   - Number of objects dequeued
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table,
 		unsigned int n, unsigned int *available)
 {
--

2.17.1

#4 lore raw
Quoted 33 lines

From: dev [mailto:[email protected]] On Behalf Of Ananyev, Konstantin
Sent: Thursday, May 14, 2020 2:24 PM

Hi Morten,

Quoted 1 line

Testing if the ring is empty is as simple as comparing the producer

and

Quoted 2 lines

consumer pointers.
In theory, this optimization reduces the number of potential cache

misses

Quoted 3 lines

from 3 to 2 by not having to read r->mask in rte_ring_count().

It is not possible to enqueue more elements than the capacity of a

ring,

Quoted 2 lines

so the capacity comparison is a safeguard for observer threads only. Instead of completely removing the comparison, I have reorganized it

to

Quoted 1 line

resemble the other trigrahps in the ring library and added a

likely().

Quoted 2 lines

The modification of these two functions were discussed in the RFC

here:

Quoted 9 lines

https://mails.dpdk.org/archives/dev/2020-April/165752.html

Also fixed some existing code not passing checkpatch.

---
 lib/librte_ring/rte_ring.h | 36 +++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 17 deletions(-)
Quoted 14 lines

lib/librte_ring/rte_ring.h extern "C" { · line 58

Quoted 10 lines
lib/librte_ring/rte_ring.h +1 -1
index 86faede81..36438d9cd 100644
@@ -55,7 +55,7 @@ extern "C" {
  *   - The memory size needed for the ring on success.
  *   - -EINVAL if count is not a power of 2.
  */
-ssize_t rte_ring_get_memsize(unsigned count);
+ssize_t rte_ring_get_memsize(unsigned int count);

All these changes to replace 'unsigned' with insigned int' - seems to be irrelevant to the patch subject, so can you put them to a separate patch in the series.

Will do.

Perhaps you could find an intern in Intel to fix all these quirks in ancient DPDK code that checkpatch complains about in one big patch, so we don't run into this over and over again, when submitting patches. :-)

Quoted 59 lines

lib/librte_ring/rte_ring.h ssize_t rte_ring_get_memsize(unsigned count); · line 112

Quoted 8 lines
lib/librte_ring/rte_ring.h +0 -1
 /**
  * Initialize a ring structure.
@@ -109,8 +109,8 @@ ssize_t rte_ring_get_memsize(unsigned count);
  * @return
  *   0 on success, or a negative value on error.
  */
-int rte_ring_init(struct rte_ring *r, const char *name, unsigned

count,

lib/librte_ring/rte_ring.h ssize_t rte_ring_get_memsize(unsigned count); · line 112

Quoted 2 lines
lib/librte_ring/rte_ring.h +1 -1
-	unsigned flags);
+int rte_ring_init(struct rte_ring *r, const char *name, unsigned int

count,

lib/librte_ring/rte_ring.h int rte_ring_init(struct rte_ring *r, const char

Quoted 5 lines
lib/librte_ring/rte_ring.h +1 -0
+	unsigned int flags);
 
 /**
  * Create a new ring named *name* in memory.
@@ -169,8 +169,8 @@ int rte_ring_init(struct rte_ring *r, const char

*name, unsigned count,

lib/librte_ring/rte_ring.h int rte_ring_init(struct rte_ring *r, const char · line 170

Quoted 2 lines
lib/librte_ring/rte_ring.h +0 -0
  *    - EEXIST - a memzone with the same name already exists
  *    - ENOMEM - no appropriate memory area found in which to create

memzone

lib/librte_ring/rte_ring.h int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,

Quoted 4 lines
lib/librte_ring/rte_ring.h +1 -2
  */
-struct rte_ring *rte_ring_create(const char *name, unsigned count,
-				 int socket_id, unsigned flags);
+struct rte_ring *rte_ring_create(const char *name, unsigned int

count,

lib/librte_ring/rte_ring.h void rte_ring_dump(FILE *f, const struct rte_ring

Quoted 5 lines
lib/librte_ring/rte_ring.h +1 -0
+				 int socket_id, unsigned int flags);
 
 /**
  * De-allocate all memory used by the ring.
@@ -199,7 +199,7 @@ void rte_ring_dump(FILE *f, const struct rte_ring

*r);

lib/librte_ring/rte_ring.h void rte_ring_dump(FILE *f, const struct rte_ring

Quoted 9 lines
lib/librte_ring/rte_ring.h +1 -1
 	uint32_t idx = prod_head & (r)->mask; \
 	obj_type *ring = (obj_type *)ring_start; \
 	if (likely(idx + n < size)) { \
-		for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
+		for (i = 0; i < (n & ~0x3); i += 4, idx += 4) { \
 			ring[idx] = obj_table[i]; \
 			ring[idx+1] = obj_table[i+1]; \
 			ring[idx+2] = obj_table[i+2]; \
@@ -230,7 +230,7 @@ void rte_ring_dump(FILE *f, const struct rte_ring

*r);

Quoted 17 lines
lib/librte_ring/rte_ring.h +2 -2
 	const uint32_t size = (r)->size; \
 	obj_type *ring = (obj_type *)ring_start; \
 	if (likely(idx + n < size)) { \
-		for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
+		for (i = 0; i < (n & ~0x3); i += 4, idx += 4) {\
 			obj_table[i] = ring[idx]; \
 			obj_table[i+1] = ring[idx+1]; \
 			obj_table[i+2] = ring[idx+2]; \
@@ -683,13 +683,13 @@ rte_ring_reset(struct rte_ring *r);
  * @return
  *   The number of entries in the ring.
  */
-static inline unsigned
+static inline unsigned int
 rte_ring_count(const struct rte_ring *r)
 {
 	uint32_t prod_tail = r->prod.tail;
Quoted 8 lines

lib/librte_ring/rte_ring.h rte_ring_reset(struct rte_ring *r); · line 692

Quoted 4 lines
lib/librte_ring/rte_ring.h +1 -1
 	uint32_t cons_tail = r->cons.tail;
 	uint32_t count = (prod_tail - cons_tail) & r->mask;
-	return (count > r->capacity) ? r->capacity : count;
+	return likely(count <= r->capacity) ? count : r->capacity;

Honestly, I don't see there is any point of that change:
I think it wouldn't change anything in terms of functionality or performance.

Chapter 3.4.1 "Branch Prediction Optimization" in the Intel 64 and IA-32 Architectures Optimization Reference Manual recommends this kind of optimization as Assembly/Compiler Coding Rule 3, which is why I rearranged the trigraph. Essentially, there is a limit to the number of BTB (Branch Target Buffer) entries, so they should be conserved if possible.

In addition to that, I have added the likely() because I consider it nearly impossible that the count will exceed the capacity.

However, it's not the first time I see this kind of response to a suggested branch optimization on the DPDK mailing list. Everyone seem to think that branch prediction is infinite and always works. It may seem as if infinite on trivial applications, but BTB entries may be a scarce resource on complex applications. I assume Intel's recommendations are not just for the fun of it.

Konstantin, please note that I'm letting out my frustration about the general misconception about branch prediction here. You are doing a great job, so I feel bad about responding like this to you.

Quoted 88 lines

lib/librte_ring/rte_ring.h struct rte_ring *rte_ring_lookup(const char

Quoted 25 lines
lib/librte_ring/rte_ring.h +4 -2
 }
 
 /**
@@ -700,7 +700,7 @@ rte_ring_count(const struct rte_ring *r)
  * @return
  *   The number of free entries in the ring.
  */
-static inline unsigned
+static inline unsigned int
 rte_ring_free_count(const struct rte_ring *r)
 {
 	return r->capacity - rte_ring_count(r);
@@ -733,7 +733,9 @@ rte_ring_full(const struct rte_ring *r)
 static inline int
 rte_ring_empty(const struct rte_ring *r)
 {
-	return rte_ring_count(r) == 0;
+	uint32_t prod_tail = r->prod.tail;
+	uint32_t cons_tail = r->cons.tail;
+	return cons_tail == prod_tail;
 }
 
 /**
@@ -860,7 +862,7 @@ struct rte_ring *rte_ring_lookup(const char

*name);

lib/librte_ring/rte_ring.h struct rte_ring *rte_ring_lookup(const char *name);

Quoted 6 lines
lib/librte_ring/rte_ring.h +1 -1
  * @return
  *   - n: Actual number of objects enqueued.
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const

*obj_table,

lib/librte_ring/rte_ring.h rte_ring_mp_enqueue_burst(struct rte_ring *r,

Quoted 3 lines
lib/librte_ring/rte_ring.h +0 -0
 			 unsigned int n, unsigned int *free_space)
 {
@@ -883,7 +885,7 @@ rte_ring_mp_enqueue_burst(struct rte_ring *r,

void * const *obj_table,

lib/librte_ring/rte_ring.h struct rte_ring *rte_ring_lookup(const char *name);

Quoted 6 lines
lib/librte_ring/rte_ring.h +1 -1
  * @return
  *   - n: Actual number of objects enqueued.
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const

*obj_table,

lib/librte_ring/rte_ring.h rte_ring_sp_enqueue_burst(struct rte_ring *r,

Quoted 3 lines
lib/librte_ring/rte_ring.h +0 -0
 			 unsigned int n, unsigned int *free_space)
 {
@@ -910,7 +912,7 @@ rte_ring_sp_enqueue_burst(struct rte_ring *r,

void * const *obj_table,

lib/librte_ring/rte_ring.h rte_ring_enqueue_burst(struct rte_ring *r, void *

Quoted 9 lines
lib/librte_ring/rte_ring.h +1 -1
  * @return
  *   - n: Actual number of objects enqueued.
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
 		      unsigned int n, unsigned int *free_space)
 {
@@ -954,7 +956,7 @@ rte_ring_enqueue_burst(struct rte_ring *r, void *

const *obj_table,

lib/librte_ring/rte_ring.h rte_ring_mc_dequeue_burst(struct rte_ring *r,

Quoted 9 lines
lib/librte_ring/rte_ring.h +1 -1
  * @return
  *   - n: Actual number of objects dequeued, 0 if ring is empty
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table,
 		unsigned int n, unsigned int *available)
 {
@@ -979,7 +981,7 @@ rte_ring_mc_dequeue_burst(struct rte_ring *r,

void **obj_table,

lib/librte_ring/rte_ring.h rte_ring_sc_dequeue_burst(struct rte_ring *r,

Quoted 9 lines
lib/librte_ring/rte_ring.h +1 -1
  * @return
  *   - n: Actual number of objects dequeued, 0 if ring is empty
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table,
 		unsigned int n, unsigned int *available)
 {
@@ -1006,7 +1008,7 @@ rte_ring_sc_dequeue_burst(struct rte_ring *r,

void **obj_table,

Quoted 10 lines
lib/librte_ring/rte_ring.h +1 -2
  * @return
  *   - Number of objects dequeued
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table,
 		unsigned int n, unsigned int *available)
 {
--

2.17.1

#5 lore raw
Quoted 21 lines
Quoted 16 lines

lib/librte_ring/rte_ring.h rte_ring_reset(struct rte_ring *r); · line 692

Quoted 12 lines
lib/librte_ring/rte_ring.h +2 -2
-static inline unsigned
+static inline unsigned int
 rte_ring_count(const struct rte_ring *r)
 {
 	uint32_t prod_tail = r->prod.tail;
 	uint32_t cons_tail = r->cons.tail;
 	uint32_t count = (prod_tail - cons_tail) & r->mask;
-	return (count > r->capacity) ? r->capacity : count;
+	return likely(count <= r->capacity) ? count : r->capacity;

Honestly, I don't see there is any point of that change:
I think it wouldn't change anything in terms of functionality or performance.

Chapter 3.4.1 "Branch Prediction Optimization" in the Intel 64 and IA-32 Architectures Optimization Reference Manual recommends this kind of optimization as Assembly/Compiler Coding Rule 3, which is why I rearranged the trigraph. Essentially, there is a limit to the number of BTB (Branch Target Buffer) entries, so they should be conserved if possible.

Quoted 5 lines

In addition to that, I have added the likely() because I consider it nearly impossible that the count will exceed the capacity.

However, it's not the first time I see this kind of response to a suggested branch optimization on the DPDK mailing list. Everyone seem to think that branch prediction is infinite and always works. It may seem as if infinite on trivial applications, but BTB entries may be a scarce resource on complex applications. I assume Intel's recommendations are not just for the fun of it.

I think it is better to leave such level of micro-optimizations to the compiler. BTW, in that particular case, compiler most likely will generate a code without any branches at all (at least for IA).
Let say on my box with gcc 7.3:

$ cat trc1.c
#include <stdint.h>
#include <rte_config.h>
#include <rte_ring.h>

uint32_t
fffx1(const struct rte_ring *r)

{
        uint32_t prod_tail = r->prod.tail;
        uint32_t cons_tail = r->cons.tail;
        uint32_t count = (prod_tail - cons_tail) & r->mask;
        return (count > r->capacity) ? r->capacity : count;
}

uint32_t
fffx2(const struct rte_ring *r)

{
        uint32_t prod_tail = r->prod.tail;
        uint32_t cons_tail = r->cons.tail;
        uint32_t count = (prod_tail - cons_tail) & r->mask;
        return likely(count <= r->capacity) ? count : r->capacity;
}
$ gcc -m64 -O3 -march=native -I${RTE_SDK}/x86_64-native-linuxapp-gcc/include -c trc1.c
$ objdump -d trc1.o

0000000000000000 <fffx1>:

   0:   8b 87 84 00 00 00       mov    0x84(%rdi),%eax
   6:   8b 97 04 01 00 00       mov    0x104(%rdi),%edx
   c:   29 d0                   sub    %edx,%eax
   e:   8b 57 38                mov    0x38(%rdi),%edx
  11:   23 47 34                and    0x34(%rdi),%eax
  14:   39 d0                   cmp    %edx,%eax
  16:   0f 47 c2                cmova  %edx,%eax
  19:   c3                      retq
  1a:   66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)

0000000000000020 <fffx2>:

  20:   8b 87 84 00 00 00       mov    0x84(%rdi),%eax
  26:   8b 97 04 01 00 00       mov    0x104(%rdi),%edx
  2c:   29 d0                   sub    %edx,%eax
  2e:   8b 57 38                mov    0x38(%rdi),%edx
  31:   23 47 34                and    0x34(%rdi),%eax
  34:   39 d0                   cmp    %edx,%eax
  36:   0f 47 c2                cmova  %edx,%eax
  39:   c3                      retq

As you can see, there is no difference.

Quoted 3 lines

Konstantin, please note that I'm letting out my frustration about the general misconception about branch prediction here. You are doing a great job, so I feel bad about responding like this to you.

No worries, in fact I am glad to know that DPDK contributors read IA optimization manual that thoughtfully 😊

Konstantin

#6 lore raw
Quoted 92 lines

From: dev [mailto:[email protected]] On Behalf Of Ananyev, Konstantin
Sent: Thursday, May 14, 2020 6:47 PM

Quoted 15 lines
Quoted 14 lines

lib/librte_ring/rte_ring.h rte_ring_reset(struct rte_ring *r); · line 692

Quoted 10 lines
lib/librte_ring/rte_ring.h +2 -2
-static inline unsigned
+static inline unsigned int
 rte_ring_count(const struct rte_ring *r)
 {
 	uint32_t prod_tail = r->prod.tail;
 	uint32_t cons_tail = r->cons.tail;
 	uint32_t count = (prod_tail - cons_tail) & r->mask;
-	return (count > r->capacity) ? r->capacity : count;
+	return likely(count <= r->capacity) ? count : r->capacity;

Honestly, I don't see there is any point of that change:
I think it wouldn't change anything in terms of functionality or performance.

Quoted 1 line

Chapter 3.4.1 "Branch Prediction Optimization" in the Intel 64 and

IA-32 Architectures Optimization Reference Manual recommends this

Quoted 1 line

kind of optimization as Assembly/Compiler Coding Rule 3, which is why

I rearranged the trigraph. Essentially, there is a limit to the number

Quoted 1 line

of BTB (Branch Target Buffer) entries, so they should be conserved if

possible.

Quoted 2 lines

In addition to that, I have added the likely() because I consider it

nearly impossible that the count will exceed the capacity.

Quoted 2 lines

However, it's not the first time I see this kind of response to a

suggested branch optimization on the DPDK mailing list. Everyone seem to

Quoted 1 line

think that branch prediction is infinite and always works. It may

seem as if infinite on trivial applications, but BTB entries may be a scarce

Quoted 1 line

resource on complex applications. I assume Intel's recommendations

are not just for the fun of it.

I think it is better to leave such level of micro-optimizations to the compiler.
BTW, in that particular case, compiler most likely will generate a code without any branches at all (at least for IA).
Let say on my box with gcc 7.3:

$ cat trc1.c
#include <stdint.h>
#include <rte_config.h>
#include <rte_ring.h>

uint32_t
fffx1(const struct rte_ring *r)

{
        uint32_t prod_tail = r->prod.tail;
        uint32_t cons_tail = r->cons.tail;
        uint32_t count = (prod_tail - cons_tail) & r->mask;
        return (count > r->capacity) ? r->capacity : count;
}

uint32_t
fffx2(const struct rte_ring *r)

{
        uint32_t prod_tail = r->prod.tail;
        uint32_t cons_tail = r->cons.tail;
        uint32_t count = (prod_tail - cons_tail) & r->mask;
        return likely(count <= r->capacity) ? count : r->capacity;
}
$ gcc -m64 -O3 -march=native -I${RTE_SDK}/x86_64-native-linuxapp-
gcc/include -c trc1.c
$ objdump -d trc1.o

0000000000000000 <fffx1>:

   0:   8b 87 84 00 00 00       mov    0x84(%rdi),%eax
   6:   8b 97 04 01 00 00       mov    0x104(%rdi),%edx
   c:   29 d0                   sub    %edx,%eax
   e:   8b 57 38                mov    0x38(%rdi),%edx
  11:   23 47 34                and    0x34(%rdi),%eax
  14:   39 d0                   cmp    %edx,%eax
  16:   0f 47 c2                cmova  %edx,%eax
  19:   c3                      retq
  1a:   66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)

0000000000000020 <fffx2>:

  20:   8b 87 84 00 00 00       mov    0x84(%rdi),%eax
  26:   8b 97 04 01 00 00       mov    0x104(%rdi),%edx
  2c:   29 d0                   sub    %edx,%eax
  2e:   8b 57 38                mov    0x38(%rdi),%edx
  31:   23 47 34                and    0x34(%rdi),%eax
  34:   39 d0                   cmp    %edx,%eax
  36:   0f 47 c2                cmova  %edx,%eax
  39:   c3                      retq
Quoted 2 lines

As you can see, there is no difference.

Thank you for the detailed feedback.

Reality trumps theory, so I will leave the count function as is. :-)

Quoted 9 lines
Quoted 2 lines

Konstantin, please note that I'm letting out my frustration about the

general misconception about branch prediction here. You are doing a

Quoted 1 line

great job, so I feel bad about responding like this to you.

No worries, in fact I am glad to know that DPDK contributors read IA optimization manual that thoughtfully 😊

Konstantin

#7 lore raw

cover ring: empty optimization

In reply to Morten Brørup

Testing if the ring is empty is as simple as comparing the producer and consumer pointers.

Checkpatch complains about existing coding style violations, so the first part of the patch fixes those, and contains no functional changes.

Morten Brørup (2):
ring: coding style cleanup
ring: empty optimization

 lib/librte_ring/rte_ring.c      |  4 +--
 lib/librte_ring/rte_ring.h      | 46 +++++++++++++++++----------------
 lib/librte_ring/rte_ring_elem.h | 10 +++----
 lib/librte_ring/rte_ring_hts.h  |  8 +++---
 lib/librte_ring/rte_ring_rts.h  |  8 +++---
 5 files changed, 39 insertions(+), 37 deletions(-)
Signature · 1 line
2.17.1
#8 lore raw

PATCH 1/2 ring: coding style cleanup

Fix coding style violations that checkpatch will complain about.

Add missing "int" after "unsigned".
Add missing spaces around "+=" and "+". Remove superfluous type cast of numerical constant.

---
 lib/librte_ring/rte_ring.c      |  4 ++--
 lib/librte_ring/rte_ring.h      | 42 ++++++++++++++++-----------------
 lib/librte_ring/rte_ring_elem.h | 10 ++++----
 lib/librte_ring/rte_ring_hts.h  |  8 +++----
 lib/librte_ring/rte_ring_rts.h  |  8 +++----
 5 files changed, 36 insertions(+), 36 deletions(-)
lib/librte_ring/rte_ring.c +2 -2
index ebe5ccf0d..6e0ac5097 100644
@@ -174,8 +174,8 @@ get_sync_type(uint32_t flags, enum rte_ring_sync_type *prod_st,
 }
 
 int
-rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
-	unsigned flags)
+rte_ring_init(struct rte_ring *r, const char *name, unsigned int count,
+	unsigned int flags)
 {
 	int ret;
 
lib/librte_ring/rte_ring.h +21 -21

122 lines not on this page · raw message

lib/librte_ring/rte_ring_elem.h +5 -5
index a5a4c46f9..6b4643ef2 100644
@@ -889,7 +889,7 @@ rte_ring_dequeue_elem(struct rte_ring *r, void *obj_p, unsigned int esize)
  * @return
  *   - n: Actual number of objects enqueued.
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mp_enqueue_burst_elem(struct rte_ring *r, const void *obj_table,
 		unsigned int esize, unsigned int n, unsigned int *free_space)
 {
@@ -918,7 +918,7 @@ rte_ring_mp_enqueue_burst_elem(struct rte_ring *r, const void *obj_table,
  * @return
  *   - n: Actual number of objects enqueued.
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_sp_enqueue_burst_elem(struct rte_ring *r, const void *obj_table,
 		unsigned int esize, unsigned int n, unsigned int *free_space)
 {
@@ -949,7 +949,7 @@ rte_ring_sp_enqueue_burst_elem(struct rte_ring *r, const void *obj_table,
  * @return
  *   - n: Actual number of objects enqueued.
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_enqueue_burst_elem(struct rte_ring *r, const void *obj_table,
 		unsigned int esize, unsigned int n, unsigned int *free_space)
 {
@@ -1001,7 +1001,7 @@ rte_ring_enqueue_burst_elem(struct rte_ring *r, const void *obj_table,
  * @return
  *   - n: Actual number of objects dequeued, 0 if ring is empty
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mc_dequeue_burst_elem(struct rte_ring *r, void *obj_table,
 		unsigned int esize, unsigned int n, unsigned int *available)
 {
@@ -1030,7 +1030,7 @@ rte_ring_mc_dequeue_burst_elem(struct rte_ring *r, void *obj_table,
  * @return
  *   - n: Actual number of objects dequeued, 0 if ring is empty
  */
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_sc_dequeue_burst_elem(struct rte_ring *r, void *obj_table,
 		unsigned int esize, unsigned int n, unsigned int *available)
 {
lib/librte_ring/rte_ring_hts.h +4 -4
index c7701defc..359b15771 100644
@@ -189,7 +189,7 @@ rte_ring_mc_hts_dequeue_bulk_elem(struct rte_ring *r, void *obj_table,
  *   - n: Actual number of objects enqueued.
  */
 __rte_experimental
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mp_hts_enqueue_burst_elem(struct rte_ring *r, const void *obj_table,
 	unsigned int esize, unsigned int n, unsigned int *free_space)
 {
@@ -219,7 +219,7 @@ rte_ring_mp_hts_enqueue_burst_elem(struct rte_ring *r, const void *obj_table,
  *   - n: Actual number of objects dequeued, 0 if ring is empty
  */
 __rte_experimental
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mc_hts_dequeue_burst_elem(struct rte_ring *r, void *obj_table,
 	unsigned int esize, unsigned int n, unsigned int *available)
 {
@@ -291,7 +291,7 @@ rte_ring_mc_hts_dequeue_bulk(struct rte_ring *r, void **obj_table,
  *   - n: Actual number of objects enqueued.
  */
 __rte_experimental
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mp_hts_enqueue_burst(struct rte_ring *r, void * const *obj_table,
 			 unsigned int n, unsigned int *free_space)
 {
@@ -317,7 +317,7 @@ rte_ring_mp_hts_enqueue_burst(struct rte_ring *r, void * const *obj_table,
  *   - n: Actual number of objects dequeued, 0 if ring is empty
  */
 __rte_experimental
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mc_hts_dequeue_burst(struct rte_ring *r, void **obj_table,
 		unsigned int n, unsigned int *available)
 {
lib/librte_ring/rte_ring_rts.h +4 -4
index d40e9994f..afc12abe2 100644
@@ -216,7 +216,7 @@ rte_ring_mc_rts_dequeue_bulk_elem(struct rte_ring *r, void *obj_table,
  *   - n: Actual number of objects enqueued.
  */
 __rte_experimental
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mp_rts_enqueue_burst_elem(struct rte_ring *r, const void *obj_table,
 	unsigned int esize, unsigned int n, unsigned int *free_space)
 {
@@ -246,7 +246,7 @@ rte_ring_mp_rts_enqueue_burst_elem(struct rte_ring *r, const void *obj_table,
  *   - n: Actual number of objects dequeued, 0 if ring is empty
  */
 __rte_experimental
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mc_rts_dequeue_burst_elem(struct rte_ring *r, void *obj_table,
 	unsigned int esize, unsigned int n, unsigned int *available)
 {
@@ -318,7 +318,7 @@ rte_ring_mc_rts_dequeue_bulk(struct rte_ring *r, void **obj_table,
  *   - n: Actual number of objects enqueued.
  */
 __rte_experimental
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mp_rts_enqueue_burst(struct rte_ring *r, void * const *obj_table,
 			 unsigned int n, unsigned int *free_space)
 {
@@ -344,7 +344,7 @@ rte_ring_mp_rts_enqueue_burst(struct rte_ring *r, void * const *obj_table,
  *   - n: Actual number of objects dequeued, 0 if ring is empty
  */
 __rte_experimental
-static __rte_always_inline unsigned
+static __rte_always_inline unsigned int
 rte_ring_mc_rts_dequeue_burst(struct rte_ring *r, void **obj_table,
 		unsigned int n, unsigned int *available)
 {
Signature · 1 line
2.17.1
Acked-by on v1 1/2
#9 lore raw

Re: [dpdk-dev] [PATCH 1/2] ring: coding style cleanup

Quoted 10 lines

Fix coding style violations that checkpatch will complain about.

Add missing "int" after "unsigned".
Add missing spaces around "+=" and "+". Remove superfluous type cast of numerical constant.

---

Quoted 1 line

2.17.1

#10 lore raw

PATCH 2/2 ring: empty optimization

In reply to Morten Brørup

Testing if the ring is empty is as simple as comparing the producer and consumer pointers.

In theory, this optimization reduces the number of potential cache misses from 3 to 2 by not having to read r->mask in rte_ring_count().

The modification of this function were also discussed in the RFC here:
https://mails.dpdk.org/archives/dev/2020-April/165752.html

---
 lib/librte_ring/rte_ring.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/librte_ring/rte_ring.h +3 -1
index 9078e7c24..f67141482 100644
@@ -733,7 +733,9 @@ rte_ring_full(const struct rte_ring *r)
 static inline int
 rte_ring_empty(const struct rte_ring *r)
 {
-	return rte_ring_count(r) == 0;
+	uint32_t prod_tail = r->prod.tail;
+	uint32_t cons_tail = r->cons.tail;
+	return cons_tail == prod_tail;
 }
 
 /**
Signature · 1 line
2.17.1
#11 lore raw

Re: [dpdk-dev] [PATCH 2/2] ring: empty optimization

lib/librte_ring/rte_ring.h rte_ring_full(const struct rte_ring *r) · line 739

Quoted 13 lines · On Tue, 19 May 2020 15:27:25 +0000 Morten Brørup <[email protected]>…
lib/librte_ring/rte_ring.h +3 -1
index 9078e7c24..f67141482 100644
@@ -733,7 +733,9 @@ rte_ring_full(const struct rte_ring *r)
 static inline int
 rte_ring_empty(const struct rte_ring *r)
 {
-	return rte_ring_count(r) == 0;
+	uint32_t prod_tail = r->prod.tail;
+	uint32_t cons_tail = r->cons.tail;
+	return cons_tail == prod_tail;
 }

Blank line after declarations?

Are the temporary variable even needed?

#12 lore raw

Re: [dpdk-dev] [PATCH 2/2] ring: empty optimization

Quoted 22 lines

From: dev [mailto:[email protected]] On Behalf Of Stephen Hemminger
Sent: Tuesday, May 19, 2020 5:52 PM

lib/librte_ring/rte_ring.h rte_ring_full(const struct rte_ring *r) · line 739

Quoted 13 lines · On Tue, 19 May 2020 15:27:25 +0000 Morten Brørup <[email protected]>…
lib/librte_ring/rte_ring.h +3 -1
index 9078e7c24..f67141482 100644
@@ -733,7 +733,9 @@ rte_ring_full(const struct rte_ring *r)
 static inline int
 rte_ring_empty(const struct rte_ring *r)
 {
-	return rte_ring_count(r) == 0;
+	uint32_t prod_tail = r->prod.tail;
+	uint32_t cons_tail = r->cons.tail;
+	return cons_tail == prod_tail;
 }

Blank line after declarations?

Quoted 1 line

Are the temporary variable even needed?

Personally, I agree with you, but I was trying to match the existing coding style of the closely related rte_ring_count() function - only to avoid this kind of feedback.

Damn if you do, damn if you don't. :-)

#13 lore raw

Re: [dpdk-dev] [PATCH 2/2] ring: empty optimization

Quoted 7 lines · Morten Brørup
Quoted 3 lines

Blank line after declarations?

Are the temporary variable even needed?

Personally, I agree with you, but I was trying to match the existing coding style of the closely related rte_ring_count() function - only to avoid this kind of feedback.

Damn if you do, damn if you don't. :-)

Yes, looking at the code, it seems fair taking this patch as is.

Signature · 1 line
David Marchand
Acked-by on v1 2/2
#14 lore raw

Re: [dpdk-dev] [PATCH 2/2] ring: empty optimization

In reply to Morten Brørup
Quoted 21 lines

Testing if the ring is empty is as simple as comparing the producer and consumer pointers.

In theory, this optimization reduces the number of potential cache misses from 3 to 2 by not having to read r->mask in rte_ring_count().

The modification of this function were also discussed in the RFC here:
https://mails.dpdk.org/archives/dev/2020-April/165752.html

---
 lib/librte_ring/rte_ring.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/librte_ring/rte_ring.h +0 -0
index 9078e7c24..f67141482 100644
@@ -733,7 +733,9 @@ rte_ring_full(const struct rte_ring *r)
 static inline int

lib/librte_ring/rte_ring.h rte_ring_full(const struct rte_ring *r)

Quoted 10 lines
lib/librte_ring/rte_ring.h +3 -2
 rte_ring_empty(const struct rte_ring *r)
 {
-	return rte_ring_count(r) == 0;
+	uint32_t prod_tail = r->prod.tail;
+	uint32_t cons_tail = r->cons.tail;
+	return cons_tail == prod_tail;
 }
 
 /**
--
Quoted 1 line

2.17.1

#15 lore raw

Re: [dpdk-dev] [PATCH 0/2] ring: empty optimization

In reply to Morten Brørup
Quoted 18 lines · Morten Brørup

Testing if the ring is empty is as simple as comparing the producer and consumer pointers.

Checkpatch complains about existing coding style violations, so the first part of the patch fixes those, and contains no functional changes.

Morten Brørup (2):
ring: coding style cleanup
ring: empty optimization

 lib/librte_ring/rte_ring.c      |  4 +--
 lib/librte_ring/rte_ring.h      | 46 +++++++++++++++++----------------
 lib/librte_ring/rte_ring_elem.h | 10 +++----
 lib/librte_ring/rte_ring_hts.h  |  8 +++---
 lib/librte_ring/rte_ring_rts.h  |  8 +++---
 5 files changed, 39 insertions(+), 37 deletions(-)
Quoted 3 lines

--
2.17.1

Series applied, thanks.

Signature · 1 line
David Marchand