mempool: cleanup, fixes, improvements and optimizations
cover mempool: cleanup, fixes, improvements and optimizations
Consolidated mempool patches previously submitted individually into one series, and added a couple more.
Patch 1
Fixed a minor bug regarding the private data size alignment.
Patch 2 and 3
Removed the deprecated cache flush threshold field.
Supersedes: patch-167311
("[v3] mempool: remove cache flush threshold field")
Patch 4
Annotated private cache alloc/free functions as such, improved the ops registration and lookup functions, and improved the audit functions, and
updated some comments.
Patch 5
Swapped the location of the private data and local cache.
Supersedes: patch-169235
("[v2] mempool: no cache size limit")
Patch 6
Rightsized the memory allocated for cache, and
eliminated the cache size max limit.
Supersedes: patch-169235
("[v2] mempool: no cache size limit")
Morten Brørup (6):
mempool: fix private data area size alignment net/sxe2: do not use deprecated mempool cache field
mempool: remove deprecated cache flush threshold field
mempool: various cleanups
mempool: optimize access to private data
mempool: no cache size limit
app/test/test_mempool.c | 17 ++- doc/guides/rel_notes/release_26_11.rst | 16 +++ drivers/net/sxe2/sxe2_txrx_vec_avx512.c | 2 +- lib/mempool/mempool_trace.h | 1 - lib/mempool/rte_mempool.c | 151 +++++++++++++----------- lib/mempool/rte_mempool.h | 91 +++++++------- lib/mempool/rte_mempool_ops.c | 30 +++-- 7 files changed, 178 insertions(+), 130 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/6 mempool: fix private data area size alignment
Object size alignment should not affect alignment of mempool header size or private data size, which are both cache aligned.
Reverted the parts of the patch that incorrectly changed this.
--- lib/mempool/rte_mempool.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
lib/mempool/rte_mempool.c +1 -4
@@ -873,9 +873,7 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,* reserve a memory zone for this mempool: private data is * cache-aligned */ - private_data_size = (private_data_size + -RTE_MEMPOOL_ALIGN_MASK ) & (~RTE_MEMPOOL_ALIGN_MASK ); - + private_data_size =RTE_CACHE_LINE_ROUNDUP (private_data_size);/* try to allocate tailq entry */ te =rte_zmalloc ("MEMPOOL_TAILQ_ENTRY" ,sizeof (*te),0 );
@@ -886,7 +884,6 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, mempool_size =RTE_MEMPOOL_HEADER_SIZE (mp, cache_size); mempool_size += private_data_size; - mempool_size =RTE_ALIGN_CEIL (mempool_size,RTE_MEMPOOL_ALIGN ); ret =snprintf (mz_name,sizeof (mz_name),RTE_MEMPOOL_MZ_FORMAT , name);if (ret <0 || ret >= (int )sizeof (mz_name)) {
Signature · 1 line
2.43.0
PATCH 2/6 net/sxe2: do not use deprecated mempool cache field
In reply to Morten BrørupThe driver was updated to use the mempool cache ``size`` field instead of the deprecated ``flushthresh`` field; it contains the same value.
---
Note:
This driver patch may be omitted if another driver patch eliminates the use of the deprecated ``flushthresh`` field.
Supersedes: patch-167311
("[v3] mempool: remove cache flush threshold field")
--- drivers/net/sxe2/sxe2_txrx_vec_avx512.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/sxe2/sxe2_txrx_vec_avx512.c +1 -1
@@ -67,7 +67,7 @@ static __rte_always_inline int32_t sxe2_tx_bufs_free_vec_avx512(struct sxe2_tx_q } cache->len += rs_thresh; -if (cache->len >= cache->flushthresh) { +if (cache->len >= cache->size) { (void )rte_mempool_ops_enqueue_bulk (mp, &cache->objs[cache->size], cache->len - cache->size); cache->len = cache->size;
Signature · 1 line
2.43.0
PATCH 3/6 mempool: remove deprecated cache flush threshold field
In reply to Morten BrørupRemoved the mempool cache flush threshold field (``flushthresh``), which was obsolete and marked for removal.
Reduced the size of the mempool cache objects array; it was double the required size.
Updated release notes accordingly.
---
Supersedes: patch-167311
("[v3] mempool: remove cache flush threshold field")
--- doc/guides/rel_notes/release_26_11.rst | 6 ++++++ lib/mempool/mempool_trace.h | 1 - lib/mempool/rte_mempool.c | 1 - lib/mempool/rte_mempool.h | 16 +++++----------- 4 files changed, 11 insertions(+), 13 deletions(-)
doc/guides/rel_notes/release_26_11.rst +6 -0
@@ -79,6 +79,8 @@ Removed Items ``rte_rib6_is_equal`` * table: ``RTE_LPM_IPV6_ADDR_SIZE`` +* mempool: Removed the deprecated and obsolete ``flushthresh`` field from the ``rte_mempool_cache`` structure. + API Changes -----------
@@ -95,6 +97,10 @@ API Changes Also, make sure to start the actual text at the margin. ======================================================= +* mempool: Updated the ``rte_mempool_cache`` structure as follows: + - Removed the deprecated and obsolete ``flushthresh`` field. + - Removed the ``unused`` field. + - Reduced the size of the ``objs`` array from ``RTE_MEMPOOL_CACHE_MAX_SIZE`` * 2 to ``RTE_MEMPOOL_CACHE_MAX_SIZE``. ABI Changes -----------
lib/mempool/mempool_trace.h +0 -1
@@ -119,7 +119,6 @@ RTE_TRACE_POINT(rte_trace_point_emit_i32 (socket_id);rte_trace_point_emit_ptr (cache);rte_trace_point_emit_u32 (cache->len); -rte_trace_point_emit_u32 (cache->flushthresh); ) RTE_TRACE_POINT(
lib/mempool/rte_mempool.c +0 -1
@@ -753,7 +753,6 @@ static void mempool_cache_init(struct rte_mempool_cache *cache, uint32_t size) { cache->size = size; - cache->flushthresh = size;/* Obsolete; for API/ABI compatibility purposes only */ cache->len =0 ; }
lib/mempool/rte_mempool.h +5 -11
@@ -89,14 +89,14 @@ struct __rte_cache_aligned rte_mempool_debug_stats {*/ struct __rte_cache_aligned rte_mempool_cache {uint32_t size;/**< Size of the cache */ -uint32_t flushthresh;/**< Obsolete; for API/ABI compatibility purposes only */ uint32_t len;/**< Current cache count */ #ifdef RTE_LIBRTE_MEMPOOL_STATS - uint32_t unused; /* * Alternative location for the most frequently updated mempool statistics (per-lcore), * providing faster update access when using a mempool cache. + * Note: 16-byte aligned for optimal SIMD access, when updating pairs of counters. */ + alignas(16) struct { uint64_t put_bulk; /**< Number of puts. */ uint64_t put_objs; /**< Number of objects successfully put. */
@@ -104,15 +104,9 @@ struct __rte_cache_aligned rte_mempool_cache { uint64_t get_success_objs; /**< Objects successfully allocated. */ } stats;/**< Statistics */ #endif -/** -* Cache objects -* -* Note: -* Cache is allocated at double size for API/ABI compatibility purposes only. -* When reducing its size at an API/ABI breaking release, -* remember to add a cache guard after it. -*/ - alignas(RTE_CACHE_LINE_SIZE )void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE *2 ]; +/** Cache objects */ + alignas(RTE_CACHE_LINE_SIZE )void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE ]; +RTE_CACHE_GUARD ; };/**
Signature · 1 line
2.43.0
PATCH 4/6 mempool: various cleanups
In reply to Morten BrørupRemoved audit call from dump function, where it doesn't belong.
Added malloc/dealloc attribute to private cache alloc/free functions.
Improved checks in mempool ops registration function.
Improved cache audit checks.
Added detailed description to definition of mempool name length.
Updated some comments, and moved around some misplaced comments.
--- app/test/test_mempool.c | 12 +++++----- lib/mempool/rte_mempool.c | 20 ++++++++--------- lib/mempool/rte_mempool.h | 42 ++++++++++++++++++++++++----------- lib/mempool/rte_mempool_ops.c | 30 +++++++++++++++++++------ 4 files changed, 67 insertions(+), 37 deletions(-)
app/test/test_mempool.c +6 -6
@@ -191,7 +191,7 @@ static int test_mempool_creation_with_exceeded_cache_size(void) {struct rte_mempool *mp_cov; - mp_cov = rte_mempool_create("test_mempool_cache_too_big", + mp_cov = rte_mempool_create("test_cache_too_big", MEMPOOL_SIZE, MEMPOOL_ELT_SIZE, RTE_MEMPOOL_CACHE_MAX_SIZE + 32, 0,
@@ -211,7 +211,7 @@ static int test_mempool_creation_with_invalid_flags(void) {struct rte_mempool *mp_cov; - mp_cov = rte_mempool_create("test_mempool_invalid_flags", MEMPOOL_SIZE, + mp_cov = rte_mempool_create("test_invalid_flags", MEMPOOL_SIZE, MEMPOOL_ELT_SIZE, 0, 0, NULL, NULL, NULL, NULL,
@@ -333,7 +333,7 @@ test_mempool_sp_sc(void)/* create a mempool with single producer/consumer ring */ if (mp_spsc ==NULL ) { - mp_spsc = rte_mempool_create("test_mempool_sp_sc", MEMPOOL_SIZE, + mp_spsc = rte_mempool_create("test_sp_sc", MEMPOOL_SIZE, MEMPOOL_ELT_SIZE, 0, 0, my_mp_init, NULL, my_obj_init, NULL,
@@ -343,7 +343,7 @@ test_mempool_sp_sc(void)if (mp_spsc ==NULL )RET_ERR (); } -if (rte_mempool_lookup ("test_mempool_sp_sc" ) != mp_spsc) { +if (rte_mempool_lookup ("test_sp_sc" ) != mp_spsc) {printf ("Cannot lookup mempool from its name\n" ); ret =-1 ; goto err;
@@ -440,7 +440,7 @@ test_mempool_same_name_twice_creation(void) {struct rte_mempool *mp_tc, *mp_tc2; - mp_tc = rte_mempool_create("test_mempool_same_name", MEMPOOL_SIZE, + mp_tc = rte_mempool_create("test_same_name", MEMPOOL_SIZE, MEMPOOL_ELT_SIZE, 0, 0, NULL, NULL, NULL, NULL,
@@ -449,7 +449,7 @@ test_mempool_same_name_twice_creation(void)if (mp_tc ==NULL )RET_ERR (); - mp_tc2 = rte_mempool_create("test_mempool_same_name", MEMPOOL_SIZE, + mp_tc2 = rte_mempool_create("test_same_name", MEMPOOL_SIZE, MEMPOOL_ELT_SIZE, 0, 0, NULL, NULL, NULL, NULL,
lib/mempool/rte_mempool.c +9 -11
@@ -1193,6 +1193,7 @@ mempool_obj_audit(struct rte_mempool *mp, __rte_unused void *opaque,RTE_MEMPOOL_CHECK_COOKIES (mp, &obj,1 ,2 ); } +/* check cookies before and after objects */ static void mempool_audit_cookies (struct rte_mempool *mp) {
@@ -1209,11 +1210,10 @@ mempool_audit_cookies(struct rte_mempool *mp)#define mempool_audit_cookies (mp) do {} while(0)#endif -/* check cookies before and after objects */ +/* check cache size consistency */ static void mempool_audit_cache (const struct rte_mempool *mp) { -/* check cache size consistency */ unsigned lcore_id;if (mp->cache_size ==0 )
@@ -1222,10 +1222,13 @@ mempool_audit_cache(const struct rte_mempool *mp)for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) {const struct rte_mempool_cache *cache; cache = &mp->local_cache[lcore_id]; -if (cache->len >RTE_DIM (cache->objs)) { -RTE_MEMPOOL_LOG (CRIT ,"badness on cache[%u]" , - lcore_id); -rte_panic ("MEMPOOL: invalid cache len\n" ); +if (cache->size >RTE_DIM (cache->objs)) { +RTE_MEMPOOL_LOG (CRIT ,"badness on cache[%u] size" , lcore_id); +rte_panic ("MEMPOOL: invalid cache[%u] size\n" , lcore_id); + } +if (cache->len > cache->size) { +RTE_MEMPOOL_LOG (CRIT ,"badness on cache[%u] len" , lcore_id); +rte_panic ("MEMPOOL: invalid cache[%u] len\n" , lcore_id); } } }
@@ -1237,9 +1240,6 @@ rte_mempool_audit(struct rte_mempool *mp) {mempool_audit_cache (mp);mempool_audit_cookies (mp); - -/* For case where mempool DEBUG is not set, and cache size is 0 */ -RTE_SET_USED (mp); }/* dump the status of the mempool on the console */
@@ -1367,8 +1367,6 @@ rte_mempool_dump(FILE *f, struct rte_mempool *mp)#else fprintf (f," no statistics available\n" );#endif - -rte_mempool_audit (mp); }/* dump the status of all mempools on the console */
lib/mempool/rte_mempool.h +29 -13
72 lines not on this page · raw message
lib/mempool/rte_mempool_ops.c +23 -7
@@ -27,7 +27,7 @@ int rte_mempool_register_ops(const struct rte_mempool_ops *h) {struct rte_mempool_ops *ops; -int16_t ops_index; +unsigned int ops_index;rte_spinlock_lock (&rte_mempool_ops_table.sl);
@@ -47,12 +47,22 @@ rte_mempool_register_ops(const struct rte_mempool_ops *h)return -EINVAL ; } -if (strlen (h->name) >=sizeof (ops->name) -1 ) { +if (strnlen (h->name,sizeof (h->name)) >sizeof (ops->name) -1 ) {rte_spinlock_unlock (&rte_mempool_ops_table.sl); -RTE_MEMPOOL_LOG (DEBUG ,"%s(): mempool_ops <%s>: name too long" , - __func__, h->name); - rte_errno =EEXIST ; -return -EEXIST ; +char name[sizeof (h->name) +1 ]; +strlcpy (name, h->name,sizeof (name)); +RTE_MEMPOOL_LOG (ERR ,"%s(): mempool_ops <%s>: name too long" , + __func__, name); +return -ENAMETOOLONG ; + } + +for (ops_index =0 ; ops_index < rte_mempool_ops_table.num_ops; ops_index++) { +if (!strcmp (h->name, rte_mempool_ops_table.ops[ops_index].name)) { +rte_spinlock_unlock (&rte_mempool_ops_table.sl); +RTE_MEMPOOL_LOG (ERR ,"%s(): mempool_ops <%s>: name exists" , + __func__, h->name); +return -EEXIST ; + } } ops_index = rte_mempool_ops_table.num_ops++;
@@ -70,6 +80,8 @@ rte_mempool_register_ops(const struct rte_mempool_ops *h)rte_spinlock_unlock (&rte_mempool_ops_table.sl); +RTE_MEMPOOL_LOG (DEBUG ,"Registered mempool ops <%s> at index %u" , ops->name, ops_index); +return ops_index; }
@@ -185,8 +197,12 @@ rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name, } } -if (ops ==NULL ) +if (ops ==NULL ) { +RTE_MEMPOOL_LOG (DEBUG , +"Cannot set unknown mempool ops <%s>, of %u ops registered" , + name, i);return -EINVAL ; + } mp->ops_index = i; mp->pool_config = pool_config;
Signature · 1 line
2.43.0
RE: [PATCH 4/6] mempool: various cleanups
Quoted 16 lines
Removed audit call from dump function, where it doesn't belong.
Added malloc/dealloc attribute to private cache alloc/free functions.
Improved checks in mempool ops registration function.
Improved cache audit checks.
Added detailed description to definition of mempool name length.
Updated some comments, and moved around some misplaced comments.
Signed-off-by Morten Brørup <[email protected]>---
....
Quoted 19 lines
lib/mempool/rte_mempool_ops.c +1
-1@@ -27,7 +27,7 @@ int rte_mempool_register_ops(const struct rte_mempool_ops *h) {struct rte_mempool_ops *ops; -int16_t ops_index; +unsigned int ops_index;rte_spinlock_lock (&rte_mempool_ops_table.sl);@@ -47,12 +47,22 @@ rte_mempool_register_ops(const struct rte_mempool_ops *h)return -EINVAL ; }
lib/mempool/rte_mempool_ops.c rte_mempool_register_ops(const struct · line 55
Quoted 10 lines
lib/mempool/rte_mempool_ops.c +3
-5-if (strlen (h->name) >=sizeof (ops->name) -1 ) { +if (strnlen (h->name,sizeof (h->name)) >sizeof (ops->name) -1 ) {rte_spinlock_unlock (&rte_mempool_ops_table.sl); - RTE_MEMPOOL_LOG(DEBUG, "%s(): mempool_ops <%s>: name too long", - __func__, h->name); - rte_errno =EEXIST ; -return -EEXIST ; + char name[sizeof(h->name) + 1]; +strlcpy (name, h->name,sizeof (name));
Curious, why do you need to copy?
Why can't t->name be used directly withiin the LOG()?
Quoted 48 lines
lib/mempool/rte_mempool_ops.c +6
-0+RTE_MEMPOOL_LOG (ERR ,"%s(): mempool_ops <%s>: name too long", + __func__, name); +return -ENAMETOOLONG ; + } + + for (ops_index = 0; ops_index < rte_mempool_ops_table.num_ops;ops_index++) {
lib/mempool/rte_mempool_ops.c +1
-0+ if (!strcmp(h->name,rte_mempool_ops_table.ops[ops_index].name)) {
lib/mempool/rte_mempool_ops.c +2
-0+rte_spinlock_unlock (&rte_mempool_ops_table.sl); + RTE_MEMPOOL_LOG(ERR, "%s(): mempool_ops <%s>:name exists",
lib/mempool/rte_mempool_ops.c +5
-0+ __func__, h->name); +return -EEXIST ; + } } ops_index = rte_mempool_ops_table.num_ops++;@@ -70,6 +80,8 @@ rte_mempool_register_ops(const struct rte_mempool_ops *h)rte_spinlock_unlock (&rte_mempool_ops_table.sl); + RTE_MEMPOOL_LOG(DEBUG, "Registered mempool ops <%s> at index %u", ops->name, ops_index); + return ops_index;}
lib/mempool/rte_mempool_ops.c +5
-1@@ -185,8 +197,12 @@ rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name, } } -if (ops ==NULL ) +if (ops ==NULL ) { +RTE_MEMPOOL_LOG (DEBUG , + "Cannot set unknown mempool ops <%s>, of %u ops registered", + name, i);return -EINVAL ; + }lib/mempool/rte_mempool_ops.c +0
-1mp->ops_index = i; mp->pool_config = pool_config; --2.43.0
RE: [PATCH 4/6] mempool: various cleanups
Quoted 36 lines
Quoted 14 lines
Removed audit call from dump function, where it doesn't belong.
Added malloc/dealloc attribute to private cache alloc/free functions.
Improved checks in mempool ops registration function.
Improved cache audit checks.
Added detailed description to definition of mempool name length.
Updated some comments, and moved around some misplaced comments.
Signed-off-by Morten Brørup <[email protected]>---
....
Quoted 19 lines
lib/mempool/rte_mempool_ops.c +1
-1@@ -27,7 +27,7 @@ int rte_mempool_register_ops(const struct rte_mempool_ops *h) {struct rte_mempool_ops *ops; -int16_t ops_index; +unsigned int ops_index;rte_spinlock_lock (&rte_mempool_ops_table.sl);@@ -47,12 +47,22 @@ rte_mempool_register_ops(const struct rte_mempool_ops *h)return -EINVAL ; }
Quoted 13 lines
lib/mempool/rte_mempool_ops.c rte_mempool_register_ops(const struct rte_mempool_ops *h) · line 53
Quoted 10 lines
lib/mempool/rte_mempool_ops.c +3
-5-if (strlen (h->name) >=sizeof (ops->name) -1 ) { +if (strnlen (h->name,sizeof (h->name)) >sizeof (ops->name) -1 ) {rte_spinlock_unlock (&rte_mempool_ops_table.sl); - RTE_MEMPOOL_LOG(DEBUG, "%s(): mempool_ops <%s>: name too long", - __func__, h->name); - rte_errno =EEXIST ; -return -EEXIST ; + char name[sizeof(h->name) + 1]; +strlcpy (name, h->name,sizeof (name));Curious, why do you need to copy?
Why can'tt->namebe used directly withiin theLOG()?
If the name uses the entire h->name array, it is not NUL-terminated, so I copy it to add the NUL character. Note the local variable I copy it to has room for +1 character, and strlcpy() provides NUL termination.
Quoted 50 lines
lib/mempool/rte_mempool_ops.c rte_mempool_register_ops(const struct
Quoted 21 lines
lib/mempool/rte_mempool_ops.c +6
-0+RTE_MEMPOOL_LOG (ERR ,"%s(): mempool_ops <%s>: name too long", + __func__, name); +return -ENAMETOOLONG ; + } + + for (ops_index = 0; ops_index < rte_mempool_ops_table.num_ops;ops_index++) {
lib/mempool/rte_mempool_ops.c +1
-0+ if (!strcmp(h->name,rte_mempool_ops_table.ops[ops_index].name)) {
lib/mempool/rte_mempool_ops.c +2
-0+rte_spinlock_unlock (&rte_mempool_ops_table.sl); + RTE_MEMPOOL_LOG(ERR, "%s(): mempool_ops <%s>:name exists",
lib/mempool/rte_mempool_ops.c +3
-0+ __func__, h->name); +return -EEXIST ; + } } ops_index = rte_mempool_ops_table.num_ops++;@@ -70,6 +80,8 @@ rte_mempool_register_ops(const struct
rte_mempool_opsQuoted 28 lines
lib/mempool/rte_mempool_ops.c +2
-0*h)rte_spinlock_unlock (&rte_mempool_ops_table.sl); + RTE_MEMPOOL_LOG(DEBUG, "Registered mempool ops <%s> at index %u", ops->name, ops_index); + return ops_index;}
lib/mempool/rte_mempool_ops.c +5
-1@@ -185,8 +197,12 @@ rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name, } } -if (ops ==NULL ) +if (ops ==NULL ) { +RTE_MEMPOOL_LOG (DEBUG , + "Cannot set unknown mempool ops <%s>, of %u ops registered", + name, i);return -EINVAL ; + }lib/mempool/rte_mempool_ops.c +0
-1mp->ops_index = i; mp->pool_config = pool_config; --2.43.0
PATCH 5/6 mempool: optimize access to private data
In reply to Morten BrørupSwapped the location of the private data and the local cache, so the private data is located immediately after the mempool header, and the local cache after that.
This way, getting the address of the private data is as simple as adding a constant to the address of the mempool.
The local cache is accessed by dereferencing a pointer to it anyway, so the performance of accessing it is not affected by moving its location.
Note:
The mempool private data API describes the private data as following the mempool header; but it has been implemented differently for a long time without causing problems, so this is considered an optimization, not a bugfix.
---
Supersedes: patch-169235
("[v2] mempool: no cache size limit")
--- app/test/test_mempool.c | 3 +-- lib/mempool/rte_mempool.c | 17 +++++++++++++---- lib/mempool/rte_mempool.h | 20 ++++++-------------- 3 files changed, 20 insertions(+), 20 deletions(-)
app/test/test_mempool.c +1 -2
@@ -112,8 +112,7 @@ test_mempool_basic(struct rte_mempool *mp, int use_external_cache)GOTO_ERR (ret, out);printf ("get private data\n" ); -if (rte_mempool_get_priv (mp) != (char *)mp + -RTE_MEMPOOL_HEADER_SIZE (mp, mp->cache_size)) +if (rte_mempool_get_priv (mp) != (char *)mp +sizeof (struct rte_mempool ))GOTO_ERR (ret, out);#ifndef RTE_EXEC_ENV_FREEBSD /* rte_mem_virt2iova() not supported on bsd */
lib/mempool/rte_mempool.c +13 -4
@@ -873,6 +873,13 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,* cache-aligned */ private_data_size =RTE_CACHE_LINE_ROUNDUP (private_data_size); +/* +* If any private data, add padding, to guard against false sharing-like +* effects on systems with a next-N-lines hardware prefetcher, when +* accessing private data. +*/ +if (private_data_size !=0 ) + private_data_size +=RTE_CACHE_GUARD_LINES *RTE_CACHE_LINE_SIZE ;/* try to allocate tailq entry */ te =rte_zmalloc ("MEMPOOL_TAILQ_ENTRY" ,sizeof (*te),0 );
@@ -881,8 +888,10 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, goto exit_unlock; } - mempool_size =RTE_MEMPOOL_HEADER_SIZE (mp, cache_size); + mempool_size =sizeof (struct rte_mempool ); mempool_size += private_data_size; +if (cache_size !=0 ) + mempool_size +=RTE_MAX_LCORE *sizeof (struct rte_mempool_cache ); ret =snprintf (mz_name,sizeof (mz_name),RTE_MEMPOOL_MZ_FORMAT , name);if (ret <0 || ret >= (int )sizeof (mz_name)) {
@@ -896,7 +905,7 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,/* init the mempool structure */ mp = mz->addr; -memset (mp,0 ,RTE_MEMPOOL_HEADER_SIZE (mp, cache_size)); +memset (mp,0 , mempool_size); ret =strlcpy (mp->name, name,sizeof (mp->name));if (ret <0 || ret >= (int )sizeof (mp->name)) { rte_errno =ENAMETOOLONG ;
@@ -935,10 +944,10 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,/* * local_cache pointer is set even if cache_size is zero. -* The local_cache points to just past the elt_pa[] array. +* The local_cache points to just past the private data. */ mp->local_cache = (struct rte_mempool_cache *) -RTE_PTR_ADD (mp,RTE_MEMPOOL_HEADER_SIZE (mp,0 )); +RTE_PTR_ADD (mp,sizeof (struct rte_mempool ) + private_data_size);/* Init all default caches. */ if (cache_size !=0 ) {
lib/mempool/rte_mempool.h +6 -14
@@ -278,6 +278,11 @@ struct __rte_cache_aligned rte_mempool {*/ struct rte_mempool_debug_stats stats[RTE_MAX_LCORE +1 ];#endif + +/* +* Private data, if any, is located after the mempool header. +* Per-lcore local cache, if any, is located after the private data. +*/ };/** Spreading among memory channels not required. */
@@ -369,18 +374,6 @@ struct __rte_cache_aligned rte_mempool {#define RTE_MEMPOOL_CACHE_STAT_ADD (cache, name, n) do {} while (0)#endif -/** -* @internal Calculate the size of the mempool header. -* -* @param mp -* Pointer to the memory pool. -* @param cs -* Size of the per-lcore cache. -*/ -#define RTE_MEMPOOL_HEADER_SIZE (mp, cs) \ - (sizeof(*(mp)) + (((cs) == 0) ? 0 : \ - (sizeof(struct rte_mempool_cache) * RTE_MAX_LCORE))) -/* return the header of a mempool object (internal) */ static inline struct rte_mempool_objhdr *rte_mempool_get_header (void *obj)
@@ -1902,8 +1895,7 @@ void rte_mempool_audit(struct rte_mempool *mp);*/ static inline void *rte_mempool_get_priv (struct rte_mempool *mp) { -return (char *)mp + -RTE_MEMPOOL_HEADER_SIZE (mp, mp->cache_size); +return (void *)(mp +1 ); }/**
Signature · 1 line
2.43.0
RE: [PATCH 5/6] mempool: optimize access to private data
Quoted 9 lines
Swapped the location of the private data and the local cache, so the private data is located immediately after the mempool header, and the local cache after that.
This way, getting the address of the private data is as simple as adding a constant to the address of the mempool.The local cache is accessed by dereferencing a pointer to it anyway, so the performance of accessing it is not affected by moving its location.
In theory, if we keep current layout, we can access local cache by just pointer arithmetic, without actual pointer de-referencing.
I am not oppose to that change - it does make sense to me,
and I don't know would it make any real difference in terms of performance (my guess - it wouldn't).
Just another option to consider.
Quoted 22 lines
Note:
The mempool private data API describes the private data as following the mempool header; but it has been implemented differently for a long time without causing problems, so this is considered an optimization, not a bugfix.Signed-off-by Morten Brørup <[email protected]>---
Supersedes: patch-169235
("[v2] mempool: no cache size limit")--- app/test/test_mempool.c | 3 +-- lib/mempool/rte_mempool.c | 17 +++++++++++++---- lib/mempool/rte_mempool.h | 20 ++++++-------------- 3 files changed, 20 insertions(+), 20 deletions(-)app/test/test_mempool.c +0
-0
app/test/test_mempool.c test_mempool_basic(struct rte_mempool *mp, int · line 116
Quoted 7 lines
app/test/test_mempool.c +1
-2@@ -112,8 +112,7 @@ test_mempool_basic(struct rte_mempool *mp, int use_external_cache)GOTO_ERR (ret ,out );printf ("get private data\n" ); -if (rte_mempool_get_priv (mp) != (char *)mp + -RTE_MEMPOOL_HEADER_SIZE (mp, mp->cache_size)) + if (rte_mempool_get_priv(mp) != (char *)mp + sizeof(struct[]
I'd say - remove that completely.
From my perspective it is wrong to make assumptions on internal structure layout.
Quoted 112 lines
app/test/test_mempool.c +0
-0rte_mempool))GOTO_ERR (ret ,out );#ifndef
RTE_EXEC_ENV_FREEBSD/*rte_mem_virt2iova()not supported on bsd
*/lib/mempool/rte_mempool.c +7
-0@@ -873,6 +873,13 @@ rte_mempool_create_empty(const char *name,unsigned n, unsigned elt_size, * cache-aligned */ private_data_size =RTE_CACHE_LINE_ROUNDUP (private_data_size); +/* +* If any private data, add padding, to guard against false sharing-like +* effects on systems with a next-N-lines hardware prefetcher, when +* accessing private data. +*/ +if (private_data_size !=0 ) + private_data_size +=RTE_CACHE_GUARD_LINES *RTE_CACHE_LINE_SIZE ;lib/mempool/rte_mempool.c +3
-1/* try to allocate tailq entry */ te =rte_zmalloc ("MEMPOOL_TAILQ_ENTRY" ,sizeof (*te),0 );@@ -881,8 +888,10 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, goto exit_unlock; } - mempool_size =RTE_MEMPOOL_HEADER_SIZE (mp, cache_size); + mempool_size =sizeof (struct rte_mempool ); mempool_size += private_data_size; +if (cache_size !=0 ) + mempool_size +=RTE_MAX_LCORE *sizeof (struct rte_mempool_cache );ret = snprintf(mz_name,
sizeof(mz_name),RTE_MEMPOOL_MZ_FORMAT, name);lib/mempool/rte_mempool.c +1
-1if (ret <0 || ret >= (int )sizeof (mz_name)) {@@ -896,7 +905,7 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,/* init the mempool structure */ mp = mz->addr; -memset (mp,0 ,RTE_MEMPOOL_HEADER_SIZE (mp, cache_size)); +memset (mp,0 , mempool_size); ret =strlcpy (mp->name, name,sizeof (mp->name));if (ret <0 || ret >= (int )sizeof (mp->name)) {lib/mempool/rte_mempool.c +2
-2rte_errno =ENAMETOOLONG ;@@ -935,10 +944,10 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,/* * local_cache pointer is set even if cache_size is zero. -* The local_cache points to just past the elt_pa[] array. +* The local_cache points to just past the private data. */ mp->local_cache = (struct rte_mempool_cache *) -RTE_PTR_ADD (mp,RTE_MEMPOOL_HEADER_SIZE (mp,0 )); +RTE_PTR_ADD (mp,sizeof (struct rte_mempool ) + private_data_size);lib/mempool/rte_mempool.c +0
-0/* Init all default caches. */ if (cache_size !=0 ) {lib/mempool/rte_mempool.h +6
-15@@ -278,6 +278,11 @@ struct __rte_cache_aligned rte_mempool {*/ struct rte_mempool_debug_stats stats[RTE_MAX_LCORE +1 ];#endif + +/* +* Private data, if any, is located after the mempool header. +* Per-lcore local cache, if any, is located after the private data. +*/ };/** Spreading among memory channels not required. */ @@ -369,18 +374,6 @@ struct __rte_cache_aligned rte_mempool {#define RTE_MEMPOOL_CACHE_STAT_ADD (cache, name, n) do {} while (0)#endif -/** -* @internal Calculate the size of the mempool header. -* -* @param mp -* Pointer to the memory pool. -* @param cs -* Size of the per-lcore cache. -*/ -#define RTE_MEMPOOL_HEADER_SIZE (mp, cs) \ - (sizeof(*(mp)) + (((cs) == 0) ? 0 : \ - (sizeof(struct rte_mempool_cache) * RTE_MAX_LCORE))) -/* return the header of a mempool object (internal) */ static inline struct rte_mempool_objhdr *rte_mempool_get_header (void *obj)@@ -1902,8 +1895,7 @@ void rte_mempool_audit(struct rte_mempool *mp);*/ static inline void *rte_mempool_get_priv (struct rte_mempool *mp) { -return (char *)mp + -RTE_MEMPOOL_HEADER_SIZE (mp, mp->cache_size); +return (void *)(mp +1 ); }/** -- 2.43.0
RE: [PATCH 5/6] mempool: optimize access to private data
Quoted 10 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Friday, 18 September 2026 12.02Quoted 6 lines
Swapped the location of the private data and the local cache, so the private data is located immediately after the mempool header, and the local cache after that.
This way, getting the address of the private data is as simple as adding a constant to the address of the mempool.
Quoted 8 lines
Quoted 1 line
The local cache is accessed by dereferencing a pointer to it anyway,
so
Quoted 1 line
the performance of accessing it is not affected by moving its
location.
In theory, if we keep current layout, we can access local cache by just pointer
arithmetic, without actual pointer de-referencing.
Yes, I explored that option in a previous patch [1], but self-rejected it.
The downside to that option is that memory for local cache (RTE_MAX_LCORE * 512 pointers) is always allocated, also for mempools without cache.
I prefer this series with variable size cache; the cache is no longer limited to 512 objects, but can be any size.
And since we have to fetch the size multiplier anyway, fetching the pointer does not require additional memory load operations (they are in the same cache line).
Also, mbuf mempools use the mempool private data for information about the mbuf buffer size (fetched when resetting mbufs), and for mbufs with external buffer. So direct access to the mempool private data is beneficial for the mbuf library. (Although the actual performance difference is probably insignificant between the two ways of getting the address of the mempool private data.)
[1]: https://patchwork.dpdk.org/project/dpdk/patch/[email protected]/
Quoted 5 lines
I am not oppose to that change - it does make sense to me, and I don't know would it make any real difference in terms of performance
(my guess - it wouldn't).
Just another option to consider.
In theory pointer arithmetic should be faster (assuming fixed-size cache). But when I experimented with accessing the local cache by pointer arithmetic instead of pointer dereferencing, I didn't observe any performance difference. I guess the pointer is sufficiently hot in the cache, so the cost of dereferencing it ("cache = mp->local_cache_ptr") is similar to the cost of adding the constant offset to the mempool cache ("cache = &mp->local_cache_array"). So the performance benefit turned out to be insignificant, and I didn't feel bad about abandoning my "Access local cache without first accessing the mempool header struct" optimization.
Quoted 28 lines
Quoted 4 lines
Note:
The mempool private data API describes the private data as followingthe
Quoted 1 line
mempool header; but it has been implemented differently for a long
time
Quoted 21 lines
without causing problems, so this is considered an optimization, not a bugfix.
Signed-off-by Morten Brørup <[email protected]>---
Supersedes: patch-169235
("[v2] mempool: no cache size limit")--- app/test/test_mempool.c | 3 +-- lib/mempool/rte_mempool.c | 17 +++++++++++++---- lib/mempool/rte_mempool.h | 20 ++++++-------------- 3 files changed, 20 insertions(+), 20 deletions(-)app/test/test_mempool.c +0
-0@@ -112,8 +112,7 @@ test_mempool_basic(struct rte_mempool *mp, int use_external_cache)GOTO_ERR (ret ,out );
Quoted 8 lines
app/test/test_mempool.c test_mempool_basic(struct rte_mempool *mp, int use_external_cache)
Quoted 4 lines
app/test/test_mempool.c +1
-2printf ("get private data\n" ); -if (rte_mempool_get_priv (mp) != (char *)mp + -RTE_MEMPOOL_HEADER_SIZE (mp, mp->cache_size)) + if (rte_mempool_get_priv(mp) != (char *)mp + sizeof(struct[]I'd say - remove that completely.
From my perspective it is wrong to make assumptions on internal structure layout.
Agree.
The size is fixed now, so this test has become superfluous anyway. But it's harmless, so I'll leave it there (for now).
Quoted 117 lines
Quoted 5 lines
app/test/test_mempool.c +0
-0rte_mempool))GOTO_ERR (ret ,out );#ifndef
RTE_EXEC_ENV_FREEBSD/*rte_mem_virt2iova()not supported onbsd
lib/mempool/rte_mempool.c rte_mempool_create_empty(const char *name, · line 878
Quoted 12 lines
*/
lib/mempool/rte_mempool.c +2
-0@@ -873,6 +873,13 @@ rte_mempool_create_empty(const char *name,unsigned n, unsigned elt_size, * cache-aligned */ private_data_size =RTE_CACHE_LINE_ROUNDUP (private_data_size); +/* +* If any private data, add padding, to guard against false sharing-like
lib/mempool/rte_mempool.c rte_mempool_create_empty(const char *name, · line 879
Quoted 1 line
lib/mempool/rte_mempool.c +1
-0+* effects on systems with a next-N-lines hardware prefetcher, when
Quoted 53 lines
lib/mempool/rte_mempool.c +4
-0+* accessing private data. +*/ +if (private_data_size !=0 ) + private_data_size +=RTE_CACHE_GUARD_LINES *RTE_CACHE_LINE_SIZE ;lib/mempool/rte_mempool.c +3
-1/* try to allocate tailq entry */ te =rte_zmalloc ("MEMPOOL_TAILQ_ENTRY" ,sizeof (*te),0 );@@ -881,8 +888,10 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, goto exit_unlock; } - mempool_size =RTE_MEMPOOL_HEADER_SIZE (mp, cache_size); + mempool_size =sizeof (struct rte_mempool ); mempool_size += private_data_size; +if (cache_size !=0 ) + mempool_size +=RTE_MAX_LCORE *sizeof (struct rte_mempool_cache );ret = snprintf(mz_name,
sizeof(mz_name),RTE_MEMPOOL_MZ_FORMAT, name);lib/mempool/rte_mempool.c +1
-1if (ret <0 || ret >= (int )sizeof (mz_name)) {@@ -896,7 +905,7 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,/* init the mempool structure */ mp = mz->addr; -memset (mp,0 ,RTE_MEMPOOL_HEADER_SIZE (mp, cache_size)); +memset (mp,0 , mempool_size); ret =strlcpy (mp->name, name,sizeof (mp->name));if (ret <0 || ret >= (int )sizeof (mp->name)) {lib/mempool/rte_mempool.c +2
-2rte_errno =ENAMETOOLONG ;@@ -935,10 +944,10 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,/* * local_cache pointer is set even if cache_size is zero. -* The local_cache points to just past the elt_pa[] array. +* The local_cache points to just past the private data. */ mp->local_cache = (struct rte_mempool_cache *) -RTE_PTR_ADD (mp,RTE_MEMPOOL_HEADER_SIZE (mp,0 )); +RTE_PTR_ADD (mp,sizeof (struct rte_mempool ) + private_data_size);lib/mempool/rte_mempool.c +0
-0/* Init all default caches. */ if (cache_size !=0 ) {lib/mempool/rte_mempool.h +0
-0lib/mempool/rte_mempool.h struct __rte_cache_aligned rte_mempool { · line 284
Quoted 7 lines
lib/mempool/rte_mempool.h +4
-0@@ -278,6 +278,11 @@ struct __rte_cache_aligned rte_mempool {*/ struct rte_mempool_debug_stats stats[RTE_MAX_LCORE +1 ];#endif + +/* +* Private data, if any, is located after the mempool header. +* Per-lcore local cache, if any, is located after the private data.
Quoted 35 lines
lib/mempool/rte_mempool.h +2
-15+*/ };/** Spreading among memory channels not required. */ @@ -369,18 +374,6 @@ struct __rte_cache_aligned rte_mempool {#define RTE_MEMPOOL_CACHE_STAT_ADD (cache, name, n) do {} while (0)#endif -/** -* @internal Calculate the size of the mempool header. -* -* @param mp -* Pointer to the memory pool. -* @param cs -* Size of the per-lcore cache. -*/ -#define RTE_MEMPOOL_HEADER_SIZE (mp, cs) \ - (sizeof(*(mp)) + (((cs) == 0) ? 0 : \ - (sizeof(struct rte_mempool_cache) * RTE_MAX_LCORE))) -/* return the header of a mempool object (internal) */ static inline struct rte_mempool_objhdr *rte_mempool_get_header (void *obj)@@ -1902,8 +1895,7 @@ void rte_mempool_audit(struct rte_mempool *mp);*/ static inline void *rte_mempool_get_priv (struct rte_mempool *mp) { -return (char *)mp + -RTE_MEMPOOL_HEADER_SIZE (mp, mp->cache_size); +return (void *)(mp +1 ); }/** -- 2.43.0
PATCH 6/6 mempool: no cache size limit
In reply to Morten BrørupReplaced the fixed size object array in the per-lcore local cache with a variable size array, allocated at mempool creation. For faster indexing into the per-lcore array of caches, pre-calculate the size (in bytes) of the per-lcore local cache.
Using a variable size array makes the RTE_MEMPOOL_CACHE_MAX_SIZE build time configuration parameter superfluous,
but it was kept for compatibility purposes, as it is often used for the cache_size parameter when creating mempools.
---
Supersedes: patch-169235
("[v2] mempool: no cache size limit")
--- app/test/test_mempool.c | 2 +- doc/guides/rel_notes/release_26_11.rst | 12 ++- lib/mempool/rte_mempool.c | 118 ++++++++++++++----------- lib/mempool/rte_mempool.h | 17 ++-- 4 files changed, 86 insertions(+), 63 deletions(-)
app/test/test_mempool.c +1 -1
@@ -193,7 +193,7 @@ static int test_mempool_creation_with_exceeded_cache_size(void) mp_cov =rte_mempool_create ("test_cache_too_big" ,MEMPOOL_SIZE ,MEMPOOL_ELT_SIZE , -RTE_MEMPOOL_CACHE_MAX_SIZE +32 ,0 , +MEMPOOL_SIZE +32 ,0 ,NULL ,NULL , my_obj_init,NULL ,SOCKET_ID_ANY ,0 );
doc/guides/rel_notes/release_26_11.rst +11 -1
@@ -97,10 +97,20 @@ API Changes Also, make sure to start the actual text at the margin. ======================================================= +* mempool: When creating a mempool, the cache size can be freely specified + (although still not exceed the number of elements), + and is no longer limited by the ``RTE_MEMPOOL_CACHE_MAX_SIZE`` build time configuration parameter. + Although ``RTE_MEMPOOL_CACHE_MAX_SIZE`` has lost its original meaning, + it was kept for compatibility purposes, + as it is often used for the ``cache_size`` parameter when creating mempools. + +* mempool: Updated the ``rte_mempool`` structure as follows: + - Added the ``sizeof_cache_per_lcore`` field, for indexing into the per-lcore local cache. + * mempool: Updated the ``rte_mempool_cache`` structure as follows: - Removed the deprecated and obsolete ``flushthresh`` field. - Removed the ``unused`` field. - - Reduced the size of the ``objs`` array from ``RTE_MEMPOOL_CACHE_MAX_SIZE`` * 2 to ``RTE_MEMPOOL_CACHE_MAX_SIZE``. + - Changed the ``objs`` array from fixed size to variable size. ABI Changes -----------
lib/mempool/rte_mempool.c +65 -53
224 lines not on this page · raw message
lib/mempool/rte_mempool.h +9 -8
@@ -105,8 +105,7 @@ struct __rte_cache_aligned rte_mempool_cache { } stats; /**< Statistics */#endif /** Cache objects */ - alignas(RTE_CACHE_LINE_SIZE )void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE ]; -RTE_CACHE_GUARD ; + alignas(RTE_CACHE_LINE_SIZE )void *objs[]; };/**
@@ -265,6 +264,7 @@ struct __rte_cache_aligned rte_mempool {int32_t ops_index;struct rte_mempool_cache *local_cache;/**< Per-lcore local cache */ +uint32_t sizeof_cache_per_lcore;/**< Multiplier for indexing into the local cache. */ uint32_t populated_size;/**< Number of populated objects. */ struct rte_mempool_objhdr_list elt_list;/**< List of objects in pool */
@@ -1050,8 +1050,7 @@ rte_mempool_free(struct rte_mempool *mp);* @param cache_size * If cache_size is non-zero, the rte_mempool library will try to * limit the accesses to the common lockless pool, by maintaining a -* per-lcore object cache. This argument must be lower or equal to -* RTE_MEMPOOL_CACHE_MAX_SIZE and n. +* per-lcore object cache. This argument must be lower or equal to n. * The access to the per-lcore table is of course * faster than the multi-producer/consumer pool. The cache can be * disabled if the cache_size argument is set to 0; it can be useful to
@@ -1371,15 +1370,17 @@ rte_mempool_cache_create(uint32_t size, int socket_id) static __rte_always_inline struct rte_mempool_cache *rte_mempool_default_cache (struct rte_mempool *mp,unsigned lcore_id) { -if (unlikely (mp->cache_size ==0 )) +if (unlikely (mp->local_cache ==NULL ))return NULL ;if (unlikely (lcore_id ==LCORE_ID_ANY ))return NULL ; -rte_mempool_trace_default_cache (mp, lcore_id, - &mp->local_cache[lcore_id]); -return &mp->local_cache[lcore_id]; +struct rte_mempool_cache *cache = (struct rte_mempool_cache *)RTE_PTR_ADD (mp->local_cache, + lcore_id * (size_t )mp->sizeof_cache_per_lcore); +rte_mempool_trace_default_cache (mp, lcore_id, cache); +__rte_assume (cache !=NULL ); +return cache; }/**
Signature · 1 line
2.43.0
RE: [PATCH 6/6] mempool: no cache size limit
Recheck-request: iol-unit-arm64-testing, iol-mellanox-Performance
The arm64 test failure seems unrelated.
The mellanox perf test result seems unlikely.
RE: [PATCH 6/6] mempool: no cache size limit
In reply to Morten BrørupRecheck-request: iol-mellanox-Performance
The perf test result, -9.5 % for 64 byte frames, still seems unlikely.
RE: [PATCH 6/6] mempool: no cache size limit
In reply to Morten BrørupQuoted 348 lines
Replaced the fixed size object array in the per-lcore local cache with a variable size array, allocated at mempool creation. For faster indexing into the per-lcore array of caches, pre-calculate the size (in bytes) of the per-lcore local cache.
Using a variable size array makes the
RTE_MEMPOOL_CACHE_MAX_SIZEbuild time configuration parameter superfluous,
but it was kept for compatibility purposes, as it is often used for thecache_sizeparameter when creating mempools.Signed-off-by Morten Brørup <[email protected]>---
Supersedes: patch-169235
("[v2] mempool: no cache size limit")--- app/test/test_mempool.c | 2 +- doc/guides/rel_notes/release_26_11.rst | 12 ++- lib/mempool/rte_mempool.c | 118 ++++++++++++++----------- lib/mempool/rte_mempool.h | 17 ++-- 4 files changed, 86 insertions(+), 63 deletions(-)app/test/test_mempool.c +1
-1@@ -193,7 +193,7 @@ static inttest_mempool_creation_with_exceeded_cache_size (void ) mp_cov = rte_mempool_create("test_cache_too_big", MEMPOOL_SIZE, MEMPOOL_ELT_SIZE, - RTE_MEMPOOL_CACHE_MAX_SIZE + 32, 0, + MEMPOOL_SIZE + 32, 0, NULL, NULL, my_obj_init, NULL,app/test/test_mempool.c +0
-0SOCKET_ID_ANY, 0);doc/guides/rel_notes/release_26_11.rst +3
-0@@ -97,10 +97,20 @@ API Changes Also, make sure to start the actual text at the margin. ======================================================= +* mempool: When creating a mempool, the cache size can be freely specified + (although still not exceed the number of elements), + and is no longer limited by the ``RTE_MEMPOOL_CACHE_MAX_SIZE`` build timeconfiguration parameter.
doc/guides/rel_notes/release_26_11.rst +6
-0+ Although ``RTE_MEMPOOL_CACHE_MAX_SIZE`` has lost its original meaning, + it was kept for compatibility purposes, + as it is often used for the ``cache_size`` parameter when creating mempools. + +* mempool: Updated the ``rte_mempool`` structure as follows: + - Added the ``sizeof_cache_per_lcore`` field, for indexing into the per-lcorelocal cache.
doc/guides/rel_notes/release_26_11.rst +1
-1+ * mempool: Updated the ``rte_mempool_cache`` structure as follows: - Removed the deprecated and obsolete ``flushthresh`` field. - Removed the ``unused`` field. - - Reduced the size of the ``objs`` array from``RTE_MEMPOOL_CACHE_MAX_SIZE`` * 2 to
``RTE_MEMPOOL_CACHE_MAX_SIZE``.doc/guides/rel_notes/release_26_11.rst +1
-0+ - Changed the ``objs`` array from fixed size to variable size. ABI Changes -----------lib/mempool/rte_mempool.c +4
-3@@ -759,20 +759,21 @@ mempool_cache_init(struct rte_mempool_cache *cache, uint32_t size)/* * Create and initialize a cache for objects that are retrieved from and * returned to an underlying mempool. This structure is identical to the -* local_cache[lcore_id] pointed to by the mempool structure. +* local_cache entry pointed to by the mempool structure. */ RTE_EXPORT_SYMBOL(rte_mempool_cache_create)struct rte_mempool_cache *rte_mempool_cache_create (uint32_t size,int socket_id) {struct rte_mempool_cache *cache; +size_t sizeof_cache =sizeof (struct rte_mempool_cache ) + size *sizeof (void *); -if (size ==0 || size >RTE_MEMPOOL_CACHE_MAX_SIZE ) { +if (size ==0 || sizeof_cache >UINT32_MAX ) { rte_errno =EINVAL ;return NULL ; } - cache =rte_zmalloc_socket ("MEMPOOL_CACHE" ,sizeof (*cache), + cache =rte_zmalloc_socket ("MEMPOOL_CACHE" , sizeof_cache,RTE_CACHE_LINE_SIZE , socket_id);lib/mempool/rte_mempool.c +1
-2if (cache ==NULL ) {RTE_MEMPOOL_LOG (ERR ,"Cannot allocate mempool cache." );@@ -811,10 +812,9 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, struct rte_mempool *mp = NULL;struct rte_tailq_entry *te =NULL ;const struct rte_memzone *mz =NULL ; -size_t mempool_size; +size_t mempool_size, sizeof_cache_per_lcore;unsigned int mz_flags =RTE_MEMZONE_1GB |RTE_MEMZONE_SIZE_HINT_ONLY ;struct rte_mempool_objsz objsz; -unsigned lcore_id;int ret;lib/mempool/rte_mempool.c +2
-0/* compilation-time checks */ @@ -822,6 +822,8 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, RTE_CACHE_LINE_MASK) != 0);RTE_BUILD_BUG_ON ((sizeof (struct rte_mempool_cache ) &RTE_CACHE_LINE_MASK ) !=0 ); +RTE_BUILD_BUG_ON (offsetof(struct rte_mempool_cache , objs) != +sizeof (struct rte_mempool_cache ));#ifdef RTE_LIBRTE_MEMPOOL_STATS RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &lib/mempool/rte_mempool.c +12
-1RTE_CACHE_LINE_MASK) != 0);@@ -838,7 +840,18 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, }/* asked cache too big */ -if (cache_size >RTE_MEMPOOL_CACHE_MAX_SIZE || + sizeof_cache_per_lcore =0 ; +if (cache_size !=0 ) { + sizeof_cache_per_lcore =sizeof (struct rte_mempool_cache ); + sizeof_cache_per_lcore +=RTE_CACHE_LINE_ROUNDUP (cache_size *sizeof (void *)); +/* +* Add padding, to guard against false sharing-like effects +* on systems with a next-N-lines hardware prefetcher, when +* accessing objects at the end of the cache. +*/ + sizeof_cache_per_lcore +=RTE_CACHE_GUARD_LINES *RTE_CACHE_LINE_SIZE ; + } + if (sizeof_cache_per_lcore > UINT32_MAX ||lib/mempool/rte_mempool.c +1
-2cache_size > n) { rte_errno =EINVAL ;return NULL ;@@ -890,8 +903,7 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, mempool_size =sizeof (struct rte_mempool ); mempool_size += private_data_size; -if (cache_size !=0 ) - mempool_size +=RTE_MAX_LCORE *sizeof (struct rte_mempool_cache); + mempool_size +=RTE_MAX_LCORE * sizeof_cache_per_lcore;ret = snprintf(mz_name,
sizeof(mz_name),RTE_MEMPOOL_MZ_FORMAT, name);lib/mempool/rte_mempool.c +0
-1if (ret <0 || ret >= (int )sizeof (mz_name)) {@@ -918,7 +930,6 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, mp->elt_size = objsz.elt_size; mp->header_size = objsz.header_size; mp->trailer_size = objsz.trailer_size; -/* Size of default caches, zero means disabled. */ mp->cache_size = cache_size; mp->private_data_size = private_data_size;lib/mempool/rte_mempool.c +9
-11STAILQ_INIT (&mp->elt_list);@@ -942,18 +953,17 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, goto exit_unlock; } -/* -* local_cache pointer is set even if cache_size is zero. -* The local_cache points to just past the private data. -*/ - mp->local_cache = (struct rte_mempool_cache *) -RTE_PTR_ADD (mp,sizeof (struct rte_mempool ) + private_data_size); - -/* Init all default caches. */ + /* local_cache pointer is only set if per-lcore local cache is present */ if (cache_size != 0) { -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) -mempool_cache_init (&mp->local_cache[lcore_id], - cache_size); + mp->local_cache = (struct rte_mempool_cache *) + RTE_PTR_ADD(mp, sizeof(struct rte_mempool) + private_data_size); + mp->sizeof_cache_per_lcore = sizeof_cache_per_lcore; + + /* Init all default caches. */ + struct rte_mempool_cache *cache = mp->local_cache; + for (unsigned int lcore_id = 0; lcore_id < RTE_MAX_LCORE; + lcore_id++, cache = RTE_PTR_ADD(cache,
sizeof_cache_per_lcore))lib/mempool/rte_mempool.c +7
-5+mempool_cache_init (cache, cache_size); } te->data = mp;@@ -1016,16 +1026,17 @@ RTE_EXPORT_SYMBOL(rte_mempool_avail_count)unsigned int rte_mempool_avail_count (const struct rte_mempool *mp) { -unsigned count; -unsigned lcore_id; +unsigned int count; count =rte_mempool_ops_get_count (mp); -if (mp->cache_size ==0 ) +if (mp->local_cache ==NULL )return count; -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) - count += mp->local_cache[lcore_id].len; +const struct rte_mempool_cache *cache = mp->local_cache; +for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; + lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) + count += cache->len;/* lib/mempool/rte_mempool.c +5
-5* due to race condition (access to len is not locked), the @@ -1053,11 +1064,11 @@ rte_mempool_stats_reset(struct rte_mempool *mp)#ifdef RTE_LIBRTE_MEMPOOL_STATS memset (&mp->stats,0 ,sizeof (mp->stats)); -if (mp->cache_size !=0 ) { -for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) { -memset (&mp->local_cache[lcore_id].stats,0 , -sizeof (mp->local_cache[lcore_id].stats)); - } +if (mp->local_cache !=NULL ) { +struct rte_mempool_cache *cache = mp->local_cache; +for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; + lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) +memset (&cache->stats,0 ,sizeof (cache->stats));lib/mempool/rte_mempool.c +0
-0} RTE_MEMPOOL_LOG(DEBUG, "<%s>@%p: statistics reset", mp->name,mp);
lib/mempool/rte_mempool.c +6
-6@@ -1071,18 +1082,18 @@ rte_mempool_stats_reset(struct rte_mempool *mp)static unsigned rte_mempool_dump_cache (FILE *f,const struct rte_mempool *mp) { -unsigned lcore_id; -unsigned count =0 ; -unsigned cache_count; +unsigned int count =0 ;fprintf (f," internal cache infos (hide zero value items):\n" );fprintf (f," cache_size=%" PRIu32"\n" , mp->cache_size); -if (mp->cache_size ==0 ) +if (mp->local_cache ==NULL )return count; -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) { - cache_count = mp->local_cache[lcore_id].len; +const struct rte_mempool_cache *cache = mp->local_cache; +for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; + lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) { +unsigned int cache_count = cache->len;if (cache_count ==0 )lib/mempool/rte_mempool.c +5
-7continue ; fprintf(f, " cache_count[%u]=%"PRIu32"\n",@@ -1223,15 +1234,13 @@ mempool_audit_cookies(struct rte_mempool *mp)static void mempool_audit_cache (const struct rte_mempool *mp) { -unsigned lcore_id; - -if (mp->cache_size ==0 ) +if (mp->local_cache ==NULL )return ; -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) { -const struct rte_mempool_cache *cache; - cache = &mp->local_cache[lcore_id]; -if (cache->size >RTE_DIM (cache->objs)) { +const struct rte_mempool_cache *cache = mp->local_cache; +for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; + lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) { +if (cache->size > mp->size) {RTE_MEMPOOL_LOG (CRIT ,"badness on cache[%u] size" , lcore_id);rte_panic("MEMPOOL: invalid cache[%u] size\n",
lcore_id);lib/mempool/rte_mempool.c +1
-6}@@ -1324,13 +1333,15 @@ rte_mempool_dump(FILE *f, struct rte_mempool *mp) sum.get_success_blks += mp->stats[lcore_id].get_success_blks; sum.get_fail_blks += mp->stats[lcore_id].get_fail_blks; } -if (mp->cache_size !=0 ) { +if (mp->local_cache !=NULL ) {/* Add the statistics stored in the mempool caches. */ -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) { - sum.put_bulk += mp- >local_cache[lcore_id].stats.put_bulk; - sum.put_objs += mp- >local_cache[lcore_id].stats.put_objs; - sum.get_success_bulk += mp- >local_cache[lcore_id].stats.get_success_bulk; - sum.get_success_objs += mp-Quoted 1 line
local_cache[lcore_id].stats.get_success_objs;
lib/mempool/rte_mempool.c +3
-0+const struct rte_mempool_cache *cache = mp->local_cache; + for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; + lcore_id++, cache = RTE_PTR_ADD(cache, mp-Quoted 1 line
sizeof_cache_per_lcore)) {lib/mempool/rte_mempool.c +9
-4+ sum.put_bulk += cache->stats.put_bulk; + sum.put_objs += cache->stats.put_objs; + sum.get_success_bulk += cache->stats.get_success_bulk; + sum.get_success_objs += cache->stats.get_success_objs; } }fprintf (f," stats:\n" );@@ -1627,10 +1638,11 @@ mempool_info_cb(struct rte_mempool *mp, void *arg) mp->populated_size); cache_count =0 ; -if (mp->cache_size >0 ) { -int lcore_id; -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) - cache_count += mp->local_cache[lcore_id].len; +if (mp->local_cache !=NULL ) { +const struct rte_mempool_cache *cache = mp->local_cache; +for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; + lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) + cache_count += cache->len; }lib/mempool/rte_mempool.c +0
-0rte_tel_data_add_dict_uint (info->d,"total_cache_count" , cache_count); common_count =rte_mempool_ops_get_count (mp);lib/mempool/rte_mempool.h +0
-0
lib/mempool/rte_mempool.h struct __rte_cache_aligned rte_mempool_cache { · line 109
Quoted 7 lines
lib/mempool/rte_mempool.h +1
-2@@ -105,8 +105,7 @@ struct __rte_cache_aligned rte_mempool_cache { } stats; /**< Statistics */#endif /** Cache objects */ - alignas(RTE_CACHE_LINE_SIZE )void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE ]; -RTE_CACHE_GUARD ; + alignas(RTE_CACHE_LINE_SIZE )void *objs[];
LGTM to me in general.
Again, just as a thought:
Would it be plausible to have a pointer per lcore instead?
i.e.:alignas(RTE_CACHE_LINE_SIZE) void *objs[RTE_MAX_LCORE];
That way we can avoid multiply op in rte_mempool_default_cache(), also it will allow us to allocate lcore cache on demand (only for enabled lcores). As a downside - sizeof(rte_mempool_cache) will grow by
(RTE_MAX_LCORE - 1) * sizeof(uintptr_t), but that's probably not a big deal.
Quoted 48 lines
lib/mempool/rte_mempool.h +0
-0};/**
lib/mempool/rte_mempool.h +1
-0@@ -265,6 +264,7 @@ struct __rte_cache_aligned rte_mempool {int32_t ops_index;struct rte_mempool_cache *local_cache;/**< Per-lcore local cache */ +uint32_t sizeof_cache_per_lcore;/**< Multiplier for indexing into the local cache. */
lib/mempool/rte_mempool.h +7
-6uint32_t populated_size;/**< Number of populated objects. */ struct rte_mempool_objhdr_list elt_list;/**< List of objects in pool */ @@ -1050,8 +1050,7 @@ rte_mempool_free(struct rte_mempool *mp);* @param cache_size * If cache_size is non-zero, the rte_mempool library will try to * limit the accesses to the common lockless pool, by maintaining a -* per-lcore object cache. This argument must be lower or equal to -* RTE_MEMPOOL_CACHE_MAX_SIZE and n. +* per-lcore object cache. This argument must be lower or equal to n. * The access to the per-lcore table is of course * faster than the multi-producer/consumer pool. The cache can be * disabled if the cache_size argument is set to 0; it can be useful to @@ -1371,15 +1370,17 @@ rte_mempool_cache_create(uint32_t size, int socket_id) static __rte_always_inline struct rte_mempool_cache *rte_mempool_default_cache (struct rte_mempool *mp,unsigned lcore_id) { -if (unlikely (mp->cache_size ==0 )) +if (unlikely (mp->local_cache ==NULL ))return NULL ;if (unlikely (lcore_id ==LCORE_ID_ANY ))return NULL ; -rte_mempool_trace_default_cache (mp, lcore_id, - &mp->local_cache[lcore_id]); -return &mp->local_cache[lcore_id]; +struct rte_mempool_cache *cache = (struct rte_mempool_cache *)RTE_PTR_ADD (mp->local_cache, + lcore_id * (size_t )mp->sizeof_cache_per_lcore); +rte_mempool_trace_default_cache (mp, lcore_id, cache); +__rte_assume (cache !=NULL ); +return cache; }/**
--
2.43.0
RE: [PATCH 6/6] mempool: no cache size limit
Quoted 374 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Friday, 18 September 2026 14.44Quoted 6 lines
Replaced the fixed size object array in the per-lcore local cache with a variable size array, allocated at mempool creation. For faster indexing into the per-lcore array of caches, pre-calculate the size (in bytes) of the per-lcore local cache.
Using a variable size array makes the
RTE_MEMPOOL_CACHE_MAX_SIZEbuild
Quoted 4 lines
time configuration parameter superfluous,
but it was kept for compatibility purposes, as it is often used for thecache_sizeparameter when creating mempools.Quoted 8 lines
Signed-off-by Morten Brørup <[email protected]>---
Supersedes: patch-169235
("[v2] mempool: no cache size limit")--- app/test/test_mempool.c | 2 +- doc/guides/rel_notes/release_26_11.rst | 12 ++- lib/mempool/rte_mempool.c | 118 ++++++++++++++-----------
Quoted 23 lines
lib/mempool/rte_mempool.h | 17 ++-- 4 files changed, 86 insertions(+), 63 deletions(-)app/test/test_mempool.c +1
-1@@ -193,7 +193,7 @@ static inttest_mempool_creation_with_exceeded_cache_size (void ) mp_cov = rte_mempool_create("test_cache_too_big", MEMPOOL_SIZE, MEMPOOL_ELT_SIZE, - RTE_MEMPOOL_CACHE_MAX_SIZE + 32, 0, + MEMPOOL_SIZE + 32, 0, NULL, NULL, my_obj_init, NULL,app/test/test_mempool.c +0
-0SOCKET_ID_ANY, 0);doc/guides/rel_notes/release_26_11.rst +0
-0doc/guides/rel_notes/release_26_11.rst API Changes · line 100
Quoted 4 lines
doc/guides/rel_notes/release_26_11.rst +1
-0@@ -97,10 +97,20 @@ API Changes Also, make sure to start the actual text at the margin. ======================================================= +* mempool: When creating a mempool, the cache size can be freelyspecified
doc/guides/rel_notes/release_26_11.rst API Changes · line 102
Quoted 2 lines
doc/guides/rel_notes/release_26_11.rst +2
-0+ (although still not exceed the number of elements), + and is no longer limited by the ``RTE_MEMPOOL_CACHE_MAX_SIZE``build time
Quoted 2 lines
configuration parameter.
+ Although ``RTE_MEMPOOL_CACHE_MAX_SIZE`` has lost its originalmeaning,
doc/guides/rel_notes/release_26_11.rst
Quoted 2 lines
doc/guides/rel_notes/release_26_11.rst +2
-0+ it was kept for compatibility purposes, + as it is often used for the ``cache_size`` parameter when creatingmempools.
doc/guides/rel_notes/release_26_11.rst API Changes
Quoted 3 lines
doc/guides/rel_notes/release_26_11.rst +3
-0+ +* mempool: Updated the ``rte_mempool`` structure as follows: + - Added the ``sizeof_cache_per_lcore`` field, for indexing intothe per-lcore
Quoted 17 lines
local cache.
doc/guides/rel_notes/release_26_11.rst +1
-1+ * mempool: Updated the ``rte_mempool_cache`` structure as follows: - Removed the deprecated and obsolete ``flushthresh`` field. - Removed the ``unused`` field. - - Reduced the size of the ``objs`` array from``RTE_MEMPOOL_CACHE_MAX_SIZE`` * 2 to
``RTE_MEMPOOL_CACHE_MAX_SIZE``.doc/guides/rel_notes/release_26_11.rst +1
-0+ - Changed the ``objs`` array from fixed size to variable size. ABI Changes -----------lib/mempool/rte_mempool.c +0
-0lib/mempool/rte_mempool.c mempool_cache_init(struct rte_mempool_cache · line 761
Quoted 3 lines
lib/mempool/rte_mempool.c +0
-0@@ -759,20 +759,21 @@ mempool_cache_init(struct rte_mempool_cache *cache, uint32_t size)/* * Create and initialize a cache for objects that are retrieved from and
lib/mempool/rte_mempool.c mempool_cache_init(struct rte_mempool_cache · line 762
Quoted 1 line
lib/mempool/rte_mempool.c +0
-0* returned to an underlying mempool. This structure is identical to the
Quoted 96 lines
lib/mempool/rte_mempool.c +4
-3-* local_cache[lcore_id] pointed to by the mempool structure. +* local_cache entry pointed to by the mempool structure. */ RTE_EXPORT_SYMBOL (rte_mempool_cache_create) struct rte_mempool_cache *rte_mempool_cache_create (uint32_t size,int socket_id) {struct rte_mempool_cache *cache; +size_t sizeof_cache =sizeof (struct rte_mempool_cache ) + size *sizeof (void *); -if (size ==0 || size >RTE_MEMPOOL_CACHE_MAX_SIZE ) { +if (size ==0 || sizeof_cache >UINT32_MAX ) { rte_errno =EINVAL ;return NULL ; } - cache =rte_zmalloc_socket ("MEMPOOL_CACHE" ,sizeof (*cache), + cache =rte_zmalloc_socket ("MEMPOOL_CACHE" , sizeof_cache,RTE_CACHE_LINE_SIZE , socket_id);lib/mempool/rte_mempool.c +1
-2if (cache ==NULL ) {RTE_MEMPOOL_LOG (ERR ,"Cannot allocate mempool cache." );@@ -811,10 +812,9 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, struct rte_mempool *mp = NULL;struct rte_tailq_entry *te =NULL ;const struct rte_memzone *mz =NULL ; -size_t mempool_size; +size_t mempool_size, sizeof_cache_per_lcore;unsigned int mz_flags =RTE_MEMZONE_1GB |RTE_MEMZONE_SIZE_HINT_ONLY ;struct rte_mempool_objsz objsz; -unsigned lcore_id;int ret;lib/mempool/rte_mempool.c +2
-0/* compilation-time checks */ @@ -822,6 +822,8 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, RTE_CACHE_LINE_MASK) != 0);RTE_BUILD_BUG_ON ((sizeof (struct rte_mempool_cache ) &RTE_CACHE_LINE_MASK ) !=0 ); +RTE_BUILD_BUG_ON (offsetof(struct rte_mempool_cache , objs) != +sizeof (struct rte_mempool_cache ));#ifdef RTE_LIBRTE_MEMPOOL_STATS RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &lib/mempool/rte_mempool.c +12
-1RTE_CACHE_LINE_MASK) != 0);@@ -838,7 +840,18 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, }/* asked cache too big */ -if (cache_size >RTE_MEMPOOL_CACHE_MAX_SIZE || + sizeof_cache_per_lcore =0 ; +if (cache_size !=0 ) { + sizeof_cache_per_lcore =sizeof (struct rte_mempool_cache ); + sizeof_cache_per_lcore +=RTE_CACHE_LINE_ROUNDUP (cache_size *sizeof (void *)); +/* +* Add padding, to guard against false sharing-like effects +* on systems with a next-N-lines hardware prefetcher, when +* accessing objects at the end of the cache. +*/ + sizeof_cache_per_lcore +=RTE_CACHE_GUARD_LINES *RTE_CACHE_LINE_SIZE ; + } + if (sizeof_cache_per_lcore > UINT32_MAX ||lib/mempool/rte_mempool.c +1
-2cache_size > n) { rte_errno =EINVAL ;return NULL ;@@ -890,8 +903,7 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, mempool_size =sizeof (struct rte_mempool ); mempool_size += private_data_size; -if (cache_size !=0 ) - mempool_size +=RTE_MAX_LCORE *sizeof (struct rte_mempool_cache); + mempool_size +=RTE_MAX_LCORE * sizeof_cache_per_lcore;ret = snprintf(mz_name,
sizeof(mz_name),RTE_MEMPOOL_MZ_FORMAT, name);lib/mempool/rte_mempool.c +0
-1if (ret <0 || ret >= (int )sizeof (mz_name)) {@@ -918,7 +930,6 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, mp->elt_size = objsz.elt_size; mp->header_size = objsz.header_size; mp->trailer_size = objsz.trailer_size; -/* Size of default caches, zero means disabled. */ mp->cache_size = cache_size; mp->private_data_size = private_data_size;lib/mempool/rte_mempool.c +0
-0STAILQ_INIT (&mp->elt_list);@@ -942,18 +953,17 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, goto exit_unlock; }lib/mempool/rte_mempool.c rte_mempool_create_empty(const char *name, · line 958
Quoted 10 lines
lib/mempool/rte_mempool.c +1
-8-/* -* local_cache pointer is set even if cache_size is zero. -* The local_cache points to just past the private data. -*/ - mp->local_cache = (struct rte_mempool_cache *) -RTE_PTR_ADD (mp,sizeof (struct rte_mempool ) + private_data_size); - -/* Init all default caches. */ +/* local_cache pointer is only set if per-lcore local cache is present */
Quoted 161 lines
lib/mempool/rte_mempool.c +8
-3if (cache_size != 0) { -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) -mempool_cache_init (&mp->local_cache[lcore_id], - cache_size); + mp->local_cache = (struct rte_mempool_cache *) + RTE_PTR_ADD(mp, sizeof(struct rte_mempool) + private_data_size); + mp->sizeof_cache_per_lcore = sizeof_cache_per_lcore; + + /* Init all default caches. */ + struct rte_mempool_cache *cache = mp->local_cache; + for (unsigned int lcore_id = 0; lcore_id < RTE_MAX_LCORE; + lcore_id++, cache = RTE_PTR_ADD(cache,
sizeof_cache_per_lcore))lib/mempool/rte_mempool.c +7
-5+mempool_cache_init (cache, cache_size); } te->data = mp;@@ -1016,16 +1026,17 @@ RTE_EXPORT_SYMBOL(rte_mempool_avail_count)unsigned int rte_mempool_avail_count (const struct rte_mempool *mp) { -unsigned count; -unsigned lcore_id; +unsigned int count; count =rte_mempool_ops_get_count (mp); -if (mp->cache_size ==0 ) +if (mp->local_cache ==NULL )return count; -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) - count += mp->local_cache[lcore_id].len; +const struct rte_mempool_cache *cache = mp->local_cache; +for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; + lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) + count += cache->len;/* lib/mempool/rte_mempool.c +5
-5* due to race condition (access to len is not locked), the @@ -1053,11 +1064,11 @@ rte_mempool_stats_reset(struct rte_mempool *mp)#ifdef RTE_LIBRTE_MEMPOOL_STATS memset (&mp->stats,0 ,sizeof (mp->stats)); -if (mp->cache_size !=0 ) { -for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) { -memset (&mp->local_cache[lcore_id].stats,0 , -sizeof (mp->local_cache[lcore_id].stats)); - } +if (mp->local_cache !=NULL ) { +struct rte_mempool_cache *cache = mp->local_cache; +for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; + lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) +memset (&cache->stats,0 ,sizeof (cache->stats));lib/mempool/rte_mempool.c +0
-0} RTE_MEMPOOL_LOG(DEBUG, "<%s>@%p: statistics reset", mp->name,mp);
lib/mempool/rte_mempool.c +6
-6@@ -1071,18 +1082,18 @@ rte_mempool_stats_reset(struct rte_mempool *mp)static unsigned rte_mempool_dump_cache (FILE *f,const struct rte_mempool *mp) { -unsigned lcore_id; -unsigned count =0 ; -unsigned cache_count; +unsigned int count =0 ;fprintf (f," internal cache infos (hide zero value items):\n" );fprintf (f," cache_size=%" PRIu32"\n" , mp->cache_size); -if (mp->cache_size ==0 ) +if (mp->local_cache ==NULL )return count; -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) { - cache_count = mp->local_cache[lcore_id].len; +const struct rte_mempool_cache *cache = mp->local_cache; +for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; + lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) { +unsigned int cache_count = cache->len;if (cache_count ==0 )lib/mempool/rte_mempool.c +5
-7continue ; fprintf(f, " cache_count[%u]=%"PRIu32"\n",@@ -1223,15 +1234,13 @@ mempool_audit_cookies(struct rte_mempool *mp)static void mempool_audit_cache (const struct rte_mempool *mp) { -unsigned lcore_id; - -if (mp->cache_size ==0 ) +if (mp->local_cache ==NULL )return ; -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) { -const struct rte_mempool_cache *cache; - cache = &mp->local_cache[lcore_id]; -if (cache->size >RTE_DIM (cache->objs)) { +const struct rte_mempool_cache *cache = mp->local_cache; +for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; + lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) { +if (cache->size > mp->size) {RTE_MEMPOOL_LOG (CRIT ,"badness on cache[%u] size" , lcore_id);rte_panic("MEMPOOL: invalid cache[%u] size\n",
lcore_id);lib/mempool/rte_mempool.c +1
-5}@@ -1324,13 +1333,15 @@ rte_mempool_dump(FILE *f, struct rte_mempool *mp) sum.get_success_blks += mp- stats[lcore_id].get_success_blks; sum.get_fail_blks += mp->stats[lcore_id].get_fail_blks; } -if (mp->cache_size !=0 ) { +if (mp->local_cache !=NULL ) {/* Add the statistics stored in the mempool caches. */ -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) { - sum.put_bulk += mp- >local_cache[lcore_id].stats.put_bulk; - sum.put_objs += mp- >local_cache[lcore_id].stats.put_objs; - sum.get_success_bulk += mp-Quoted 1 line
local_cache[lcore_id].stats.get_success_bulk;
lib/mempool/rte_mempool.c +0
-1- sum.get_success_objs += mp-Quoted 1 line
local_cache[lcore_id].stats.get_success_objs;
lib/mempool/rte_mempool.c +3
-0+const struct rte_mempool_cache *cache = mp->local_cache; + for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; + lcore_id++, cache = RTE_PTR_ADD(cache, mp-Quoted 1 line
sizeof_cache_per_lcore)) {lib/mempool/rte_mempool.c +3
-0+ sum.put_bulk += cache->stats.put_bulk; + sum.put_objs += cache->stats.put_objs; + sum.get_success_bulk += cache-stats.get_success_bulk;
lib/mempool/rte_mempool.c +1
-0+ sum.get_success_objs += cache-stats.get_success_objs;
lib/mempool/rte_mempool.c +5
-4} }fprintf (f," stats:\n" );@@ -1627,10 +1638,11 @@ mempool_info_cb(struct rte_mempool *mp, void *arg) mp->populated_size); cache_count =0 ; -if (mp->cache_size >0 ) { -int lcore_id; -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) - cache_count += mp->local_cache[lcore_id].len; +if (mp->local_cache !=NULL ) { +const struct rte_mempool_cache *cache = mp->local_cache; +for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; + lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) + cache_count += cache->len; }Quoted 1 line
rte_tel_data_add_dict_uint(info->d, "
total_cache_count",
cache_count);lib/mempool/rte_mempool.h struct __rte_cache_aligned rte_mempool_cache { · line 109
Quoted 13 lines
lib/mempool/rte_mempool.c +0
-0common_count =rte_mempool_ops_get_count (mp);lib/mempool/rte_mempool.h +1
-2@@ -105,8 +105,7 @@ struct __rte_cache_aligned rte_mempool_cache { } stats; /**< Statistics */#endif /** Cache objects */ - alignas(RTE_CACHE_LINE_SIZE )void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE ]; -RTE_CACHE_GUARD ; + alignas(RTE_CACHE_LINE_SIZE )void *objs[];LGTM to me in general.
Again, just as a thought:
Would it be plausible to have a pointer per lcore instead?
Quoted 8 lines
i.e.:
alignas(RTE_CACHE_LINE_SIZE)void *objs[RTE_MAX_LCORE]; That way we can avoid multiply op inrte_mempool_default_cache(), also it will allow us to allocate lcore cache on demand (only for enabled lcores).
As a downside -sizeof(rte_mempool_cache)will grow by (RTE_MAX_LCORE- 1) *sizeof(uintptr_t), but that's probably not a big deal.
The multiply op is not expensive.
This patch has:
struct rte_mempool_cache *cache =
(struct rte_mempool_cache *) RTE_PTR_ADD( mp->local_cache, lcore_id * (size_t)mp->sizeof_cache_per_lcore);
Which compiles to something like:
ptr = LOAD(mp + offsetof(cache_ptr));
off = LOAD(mp + offsetof(sizeof_cache_per_lcore)); off *= lcore_id;
ptr += off;
Yes, having an array of pointers to the caches in the mempool would be a good alternative:
struct __rte_cache_aligned rte_mempool {
- struct rte_mempool_cache *local_cache; /**< Per-lcore local cache */
[...]
struct rte_mempool_debug_stats stats[RTE_MAX_LCORE + 1];
#endif
+ /** Per-lcore local cache */
+ struct rte_mempool_cache *local_cache[RTE_MAX_LCORE];
};
Lookup for that would become simple:
struct rte_mempool_cache *cache =
mp->local_cache[lcore_id];
Which compiles to something like:
off = lcore_id * sizeof(void*); // Shift operation off += offsetof(cache_ptr_array);
ptr = LOAD(mp + off);
Yes, I suppose that would be faster.
I'll take a stab at it.
Caches for all lcores must be allocated at mempool creation. Or we would need special handling to allocate/free the relevant per-lcore cache for all registered mempools whenever a new lcore is registered for use at runtime, e.g. by rte_thread_register()/unregister().
Quoted 56 lines
lib/mempool/rte_mempool.h struct __rte_cache_aligned rte_mempool { · line 266
Quoted 8 lines
lib/mempool/rte_mempool.h +0
-0};/**
lib/mempool/rte_mempool.h +0
-0@@ -265,6 +264,7 @@ struct __rte_cache_aligned rte_mempool {int32_t ops_index;struct rte_mempool_cache *local_cache;/**< Per-lcore local cache */
lib/mempool/rte_mempool.h struct __rte_cache_aligned rte_mempool { · line 267
Quoted 1 line
lib/mempool/rte_mempool.h +1
-0+uint32_t sizeof_cache_per_lcore;/**< Multiplier for indexing into the
Quoted 3 lines
local cache. */
uint32_tpopulated_size; /**< Number of populatedobjects. */
Quoted 1 line
struct
rte_mempool_objhdr_listelt_list; /**< List of objects inpool */
lib/mempool/rte_mempool.h rte_mempool_free(struct rte_mempool *mp); · line 1052
Quoted 4 lines
lib/mempool/rte_mempool.h +0
-0@@ -1050,8 +1050,7 @@ rte_mempool_free(struct rte_mempool *mp);* @param cache_size * If cache_size is non-zero, the rte_mempool library will try to * limit the accesses to the common lockless pool, by maintaining a
lib/mempool/rte_mempool.h rte_mempool_free(struct rte_mempool *mp);
Quoted 3 lines
lib/mempool/rte_mempool.h +1
-2-* per-lcore object cache. This argument must be lower or equal to -* RTE_MEMPOOL_CACHE_MAX_SIZE and n. +* per-lcore object cache. This argument must be lower or equal to n.
lib/mempool/rte_mempool.h rte_mempool_free(struct rte_mempool *mp);
Quoted 3 lines
lib/mempool/rte_mempool.h +0
-0* The access to the per-lcore table is of course * faster than the multi-producer/consumer pool. The cache can be * disabled if the cache_size argument is set to 0; it can be useful to
Quoted 26 lines
lib/mempool/rte_mempool.h +6
-4@@ -1371,15 +1370,17 @@ rte_mempool_cache_create(uint32_t size, int socket_id) static __rte_always_inline struct rte_mempool_cache *rte_mempool_default_cache (struct rte_mempool *mp,unsigned lcore_id) { -if (unlikely (mp->cache_size ==0 )) +if (unlikely (mp->local_cache ==NULL ))return NULL ;if (unlikely (lcore_id ==LCORE_ID_ANY ))return NULL ; -rte_mempool_trace_default_cache (mp, lcore_id, - &mp->local_cache[lcore_id]); -return &mp->local_cache[lcore_id]; +struct rte_mempool_cache *cache = (struct rte_mempool_cache *)RTE_PTR_ADD (mp->local_cache, + lcore_id * (size_t )mp->sizeof_cache_per_lcore); +rte_mempool_trace_default_cache (mp, lcore_id, cache); +__rte_assume (cache !=NULL ); +return cache; }/**
--
2.43.0
RE: [PATCH 6/6] mempool: no cache size limit
Quoted 413 lines
Quoted 210 lines
Quoted 8 lines
Replaced the fixed size object array in the per-lcore local cache with a variable size array, allocated at mempool creation. For faster indexing into the per-lcore array of caches, pre-calculate the size (in bytes) of the per-lcore local cache.
Using a variable size array makes the
RTE_MEMPOOL_CACHE_MAX_SIZEbuild
Quoted 4 lines
time configuration parameter superfluous,
but it was kept for compatibility purposes, as it is often used for thecache_sizeparameter when creating mempools.Quoted 8 lines
Signed-off-by Morten Brørup <[email protected]>---
Supersedes: patch-169235
("[v2] mempool: no cache size limit")--- app/test/test_mempool.c | 2 +- doc/guides/rel_notes/release_26_11.rst | 12 ++- lib/mempool/rte_mempool.c | 118 ++++++++++++++-----------
Quoted 23 lines
lib/mempool/rte_mempool.h | 17 ++-- 4 files changed, 86 insertions(+), 63 deletions(-)app/test/test_mempool.c +1
-1@@ -193,7 +193,7 @@ static inttest_mempool_creation_with_exceeded_cache_size (void ) mp_cov = rte_mempool_create("test_cache_too_big", MEMPOOL_SIZE, MEMPOOL_ELT_SIZE, - RTE_MEMPOOL_CACHE_MAX_SIZE + 32, 0, + MEMPOOL_SIZE + 32, 0, NULL, NULL, my_obj_init, NULL,app/test/test_mempool.c +0
-0SOCKET_ID_ANY, 0);doc/guides/rel_notes/release_26_11.rst +0
-0doc/guides/rel_notes/release_26_11.rst API Changes · line 100
Quoted 4 lines
doc/guides/rel_notes/release_26_11.rst +1
-0@@ -97,10 +97,20 @@ API Changes Also, make sure to start the actual text at the margin. ======================================================= +* mempool: When creating a mempool, the cache size can be freelyspecified
doc/guides/rel_notes/release_26_11.rst API Changes · line 102
Quoted 2 lines
doc/guides/rel_notes/release_26_11.rst +2
-0+ (although still not exceed the number of elements), + and is no longer limited by the ``RTE_MEMPOOL_CACHE_MAX_SIZE``build time
Quoted 2 lines
configuration parameter.
+ Although ``RTE_MEMPOOL_CACHE_MAX_SIZE`` has lost its originalmeaning,
doc/guides/rel_notes/release_26_11.rst
Quoted 2 lines
doc/guides/rel_notes/release_26_11.rst +2
-0+ it was kept for compatibility purposes, + as it is often used for the ``cache_size`` parameter when creatingmempools.
doc/guides/rel_notes/release_26_11.rst API Changes
Quoted 3 lines
doc/guides/rel_notes/release_26_11.rst +3
-0+ +* mempool: Updated the ``rte_mempool`` structure as follows: + - Added the ``sizeof_cache_per_lcore`` field, for indexing intothe per-lcore
Quoted 17 lines
local cache.
doc/guides/rel_notes/release_26_11.rst +1
-1+ * mempool: Updated the ``rte_mempool_cache`` structure as follows: - Removed the deprecated and obsolete ``flushthresh`` field. - Removed the ``unused`` field. - - Reduced the size of the ``objs`` array from``RTE_MEMPOOL_CACHE_MAX_SIZE`` * 2 to
``RTE_MEMPOOL_CACHE_MAX_SIZE``.doc/guides/rel_notes/release_26_11.rst +1
-0+ - Changed the ``objs`` array from fixed size to variable size. ABI Changes -----------lib/mempool/rte_mempool.c +0
-0lib/mempool/rte_mempool.c mempool_cache_init(struct rte_mempool_cache · line 761
Quoted 3 lines
lib/mempool/rte_mempool.c +0
-0@@ -759,20 +759,21 @@ mempool_cache_init(struct rte_mempool_cache *cache, uint32_t size)/* * Create and initialize a cache for objects that are retrieved from and
lib/mempool/rte_mempool.c mempool_cache_init(struct rte_mempool_cache · line 762
Quoted 1 line
lib/mempool/rte_mempool.c +0
-0* returned to an underlying mempool. This structure is identical to the
Quoted 96 lines
lib/mempool/rte_mempool.c +4
-3-* local_cache[lcore_id] pointed to by the mempool structure. +* local_cache entry pointed to by the mempool structure. */ RTE_EXPORT_SYMBOL (rte_mempool_cache_create) struct rte_mempool_cache *rte_mempool_cache_create (uint32_t size,int socket_id) {struct rte_mempool_cache *cache; +size_t sizeof_cache =sizeof (struct rte_mempool_cache ) + size *sizeof (void *); -if (size ==0 || size >RTE_MEMPOOL_CACHE_MAX_SIZE ) { +if (size ==0 || sizeof_cache >UINT32_MAX ) { rte_errno =EINVAL ;return NULL ; } - cache =rte_zmalloc_socket ("MEMPOOL_CACHE" ,sizeof (*cache), + cache =rte_zmalloc_socket ("MEMPOOL_CACHE" , sizeof_cache,RTE_CACHE_LINE_SIZE , socket_id);lib/mempool/rte_mempool.c +1
-2if (cache ==NULL ) {RTE_MEMPOOL_LOG (ERR ,"Cannot allocate mempool cache." );@@ -811,10 +812,9 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, struct rte_mempool *mp = NULL;struct rte_tailq_entry *te =NULL ;const struct rte_memzone *mz =NULL ; -size_t mempool_size; +size_t mempool_size, sizeof_cache_per_lcore;unsigned int mz_flags =RTE_MEMZONE_1GB |RTE_MEMZONE_SIZE_HINT_ONLY ;struct rte_mempool_objsz objsz; -unsigned lcore_id;int ret;lib/mempool/rte_mempool.c +2
-0/* compilation-time checks */ @@ -822,6 +822,8 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, RTE_CACHE_LINE_MASK) != 0);RTE_BUILD_BUG_ON ((sizeof (struct rte_mempool_cache ) &RTE_CACHE_LINE_MASK ) !=0 ); +RTE_BUILD_BUG_ON (offsetof(struct rte_mempool_cache , objs) != +sizeof (struct rte_mempool_cache ));#ifdef RTE_LIBRTE_MEMPOOL_STATS RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &lib/mempool/rte_mempool.c +12
-1RTE_CACHE_LINE_MASK) != 0);@@ -838,7 +840,18 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, }/* asked cache too big */ -if (cache_size >RTE_MEMPOOL_CACHE_MAX_SIZE || + sizeof_cache_per_lcore =0 ; +if (cache_size !=0 ) { + sizeof_cache_per_lcore =sizeof (struct rte_mempool_cache ); + sizeof_cache_per_lcore +=RTE_CACHE_LINE_ROUNDUP (cache_size *sizeof (void *)); +/* +* Add padding, to guard against false sharing-like effects +* on systems with a next-N-lines hardware prefetcher, when +* accessing objects at the end of the cache. +*/ + sizeof_cache_per_lcore +=RTE_CACHE_GUARD_LINES *RTE_CACHE_LINE_SIZE ; + } + if (sizeof_cache_per_lcore > UINT32_MAX ||lib/mempool/rte_mempool.c +1
-2cache_size > n) { rte_errno =EINVAL ;return NULL ;@@ -890,8 +903,7 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, mempool_size =sizeof (struct rte_mempool ); mempool_size += private_data_size; -if (cache_size !=0 ) - mempool_size +=RTE_MAX_LCORE *sizeof (struct rte_mempool_cache); + mempool_size +=RTE_MAX_LCORE * sizeof_cache_per_lcore;ret = snprintf(mz_name,
sizeof(mz_name),RTE_MEMPOOL_MZ_FORMAT, name);lib/mempool/rte_mempool.c +0
-1if (ret <0 || ret >= (int )sizeof (mz_name)) {@@ -918,7 +930,6 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, mp->elt_size = objsz.elt_size; mp->header_size = objsz.header_size; mp->trailer_size = objsz.trailer_size; -/* Size of default caches, zero means disabled. */ mp->cache_size = cache_size; mp->private_data_size = private_data_size;lib/mempool/rte_mempool.c +0
-0STAILQ_INIT (&mp->elt_list);@@ -942,18 +953,17 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, goto exit_unlock; }lib/mempool/rte_mempool.c rte_mempool_create_empty(const char *name, · line 958
Quoted 10 lines
lib/mempool/rte_mempool.c +1
-8-/* -* local_cache pointer is set even if cache_size is zero. -* The local_cache points to just past the private data. -*/ - mp->local_cache = (struct rte_mempool_cache *) -RTE_PTR_ADD (mp,sizeof (struct rte_mempool ) + private_data_size); - -/* Init all default caches. */ +/* local_cache pointer is only set if per-lcore local cache is present */
Quoted 17 lines
lib/mempool/rte_mempool.c +8
-3if (cache_size != 0) { -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) -mempool_cache_init (&mp->local_cache[lcore_id], - cache_size); + mp->local_cache = (struct rte_mempool_cache *) + RTE_PTR_ADD(mp, sizeof(struct rte_mempool) + private_data_size); + mp->sizeof_cache_per_lcore = sizeof_cache_per_lcore; + + /* Init all default caches. */ + struct rte_mempool_cache *cache = mp->local_cache; + for (unsigned int lcore_id = 0; lcore_id < RTE_MAX_LCORE; + lcore_id++, cache = RTE_PTR_ADD(cache,
sizeof_cache_per_lcore))lib/mempool/rte_mempool.c +1
-0+mempool_cache_init (cache, cache_size); }Quoted 2 lines
lib/mempool/rte_mempool.c
Quoted 2 lines
lib/mempool/rte_mempool.c +0
-0te->data = mp;@@ -1016,16 +1026,17 @@
RTE_EXPORT_SYMBOL(rte_mempool_avail_count)Quoted 66 lines
Quoted 66 lines
lib/mempool/rte_mempool.c +6
-5unsigned int rte_mempool_avail_count (const struct rte_mempool *mp) { -unsigned count; -unsigned lcore_id; +unsigned int count; count =rte_mempool_ops_get_count (mp); -if (mp->cache_size ==0 ) +if (mp->local_cache ==NULL )return count; -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) - count += mp->local_cache[lcore_id].len; +const struct rte_mempool_cache *cache = mp->local_cache; +for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; + lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) + count += cache->len;/* lib/mempool/rte_mempool.c +5
-5* due to race condition (access to len is not locked), the @@ -1053,11 +1064,11 @@ rte_mempool_stats_reset(struct rte_mempool *mp)#ifdef RTE_LIBRTE_MEMPOOL_STATS memset (&mp->stats,0 ,sizeof (mp->stats)); -if (mp->cache_size !=0 ) { -for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) { -memset (&mp->local_cache[lcore_id].stats,0 , -sizeof (mp->local_cache[lcore_id].stats)); - } +if (mp->local_cache !=NULL ) { +struct rte_mempool_cache *cache = mp->local_cache; +for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; + lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) +memset (&cache->stats,0 ,sizeof (cache->stats));lib/mempool/rte_mempool.c +0
-0} RTE_MEMPOOL_LOG(DEBUG, "<%s>@%p: statistics reset", mp->name,mp);
lib/mempool/rte_mempool.c +5
-6@@ -1071,18 +1082,18 @@ rte_mempool_stats_reset(struct rte_mempool *mp) static unsigned rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp) { -unsigned lcore_id; -unsigned count =0 ; -unsigned cache_count; + unsigned int count = 0; fprintf(f, " internal cache infos (hide zero value items):\n"); fprintf(f, " cache_size=%"PRIu32"\n", mp->cache_size); -if (mp->cache_size ==0 ) + if (mp->local_cache == NULL) return count; -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) { - cache_count = mp->local_cache[lcore_id].len; + const struct rte_mempool_cache *cache = mp->local_cache; + for (unsigned int lcore_id = 0; lcore_id < RTE_MAX_LCORE; + lcore_id++, cache = RTE_PTR_ADD(cache, mp-Quoted 6 lines
lib/mempool/rte_mempool.c mempool_audit_cookies(struct rte_mempool
Quoted 6 lines
lib/mempool/rte_mempool.c +1
-0>sizeof_cache_per_lcore)) { +unsigned int cache_count = cache->len;if (cache_count ==0 )lib/mempool/rte_mempool.c +0
-0continue ; fprintf(f, " cache_count[%u]=%"PRIu32"\n",@@ -1223,15 +1234,13 @@ mempool_audit_cookies(struct rte_mempool*mp)
Quoted 16 lines
Quoted 16 lines
lib/mempool/rte_mempool.c +4
-7static void mempool_audit_cache(const struct rte_mempool *mp) { -unsigned lcore_id; - -if (mp->cache_size ==0 ) + if (mp->local_cache == NULL) return; -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) { -const struct rte_mempool_cache *cache; - cache = &mp->local_cache[lcore_id]; -if (cache->size >RTE_DIM (cache->objs)) { + const struct rte_mempool_cache *cache = mp->local_cache; + for (unsigned int lcore_id = 0; lcore_id < RTE_MAX_LCORE; + lcore_id++, cache = RTE_PTR_ADD(cache, mp-Quoted 8 lines
lib/mempool/rte_mempool.c rte_mempool_dump(FILE *f, struct
Quoted 8 lines
lib/mempool/rte_mempool.c +1
-0>sizeof_cache_per_lcore)) { +if (cache->size > mp->size) {RTE_MEMPOOL_LOG (CRIT ,"badness on cache[%u] size" , lcore_id);rte_panic("MEMPOOL: invalid cache[%u] size\n",
lcore_id);lib/mempool/rte_mempool.c +0
-0}@@ -1324,13 +1333,15 @@ rte_mempool_dump(FILE *f, struct
rte_mempoolQuoted 21 lines
Quoted 21 lines
lib/mempool/rte_mempool.c +1
-5*mp) sum.get_success_blks += mp- stats[lcore_id].get_success_blks; sum.get_fail_blks += mp->stats[lcore_id].get_fail_blks; } -if (mp->cache_size !=0 ) { +if (mp->local_cache !=NULL ) {/* Add the statistics stored in the mempool caches. */ -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) { - sum.put_bulk += mp- >local_cache[lcore_id].stats.put_bulk; - sum.put_objs += mp- >local_cache[lcore_id].stats.put_objs; - sum.get_success_bulk += mp-Quoted 1 line
local_cache[lcore_id].stats.get_success_bulk;
lib/mempool/rte_mempool.c +0
-1- sum.get_success_objs += mp-Quoted 1 line
local_cache[lcore_id].stats.get_success_objs;
lib/mempool/rte_mempool.c +3
-0+const struct rte_mempool_cache *cache = mp->local_cache; + for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; + lcore_id++, cache = RTE_PTR_ADD(cache, mp-Quoted 1 line
sizeof_cache_per_lcore)) {Quoted 10 lines
lib/mempool/rte_mempool.c mempool_info_cb(struct rte_mempool *mp,
Quoted 10 lines
lib/mempool/rte_mempool.c +3
-0+ sum.put_bulk += cache->stats.put_bulk; + sum.put_objs += cache->stats.put_objs; + sum.get_success_bulk += cache-stats.get_success_bulk;
lib/mempool/rte_mempool.c +1
-0+ sum.get_success_objs += cache-stats.get_success_objs;
lib/mempool/rte_mempool.c +0
-0} }fprintf (f," stats:\n" );@@ -1627,10 +1638,11 @@ mempool_info_cb(struct rte_mempool *mp,void
Quoted 34 lines
Quoted 15 lines
lib/mempool/rte_mempool.c +5
-4*arg) mp->populated_size); cache_count =0 ; -if (mp->cache_size >0 ) { -int lcore_id; -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) - cache_count += mp->local_cache[lcore_id].len; +if (mp->local_cache !=NULL ) { +const struct rte_mempool_cache *cache = mp->local_cache; +for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; + lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) + cache_count += cache->len; }Quoted 1 line
rte_tel_data_add_dict_uint(info->d, "
total_cache_count",
cache_count);lib/mempool/rte_mempool.h struct __rte_cache_aligned rte_mempool_cache { · line 109
Quoted 13 lines
lib/mempool/rte_mempool.c +0
-0common_count =rte_mempool_ops_get_count (mp);lib/mempool/rte_mempool.h +1
-2@@ -105,8 +105,7 @@ struct __rte_cache_aligned rte_mempool_cache { } stats; /**< Statistics */#endif /** Cache objects */ - alignas(RTE_CACHE_LINE_SIZE )void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE ]; -RTE_CACHE_GUARD ; + alignas(RTE_CACHE_LINE_SIZE )void *objs[];LGTM to me in general.
Again, just as a thought:
Would it be plausible to have a pointer per lcore instead?Quoted 8 lines
i.e.:
alignas(RTE_CACHE_LINE_SIZE)void *objs[RTE_MAX_LCORE]; That way we can avoid multiply op inrte_mempool_default_cache(), also it will allow us to allocate lcore cache on demand (only for enabled lcores).
As a downside -sizeof(rte_mempool_cache)will grow by (RTE_MAX_LCORE- 1) *sizeof(uintptr_t), but that's probably not a big deal.The multiply op is not expensive.
This patch has:
structrte_mempool_cache*cache =(struct rte_mempool_cache *) RTE_PTR_ADD( mp->local_cache, lcore_id * (size_t)mp->sizeof_cache_per_lcore);Which compiles to something like:
ptr =LOAD(mp + offsetof(cache_ptr));
off =LOAD(mp + offsetof(sizeof_cache_per_lcore)); off *=lcore_id;
ptr += off;Yes, having an array of pointers to the caches in the mempool would be a good alternative:
struct
__rte_cache_alignedrte_mempool{(quoted diff) +0
-1- struct rte_mempool_cache *local_cache; /**< Per-lcore local cache */struct rte_mempool_debug_stats stats[RTE_MAX_LCORE + 1];#endif
(quoted diff) +2
-0+ /** Per-lcore local cache */ + struct rte_mempool_cache *local_cache[RTE_MAX_LCORE];
Quoted 1 line
};
Yes, you right, off-course it has to be that way.
Quoted 11 lines
Lookup for that would become simple:
structrte_mempool_cache*cache =mp->local_cache[lcore_id];Which compiles to something like:
off =lcore_id*sizeof(void*); // Shift operation off +=offsetof(cache_ptr_array);
ptr = LOAD(mp + off);
Quoted 7 lines
Yes, I suppose that would be faster.
I'll take a stab at it.Caches for all lcores must be allocated at mempool creation. Or we would need special handling to allocate/free the relevant per-lcore cache for all registered mempools whenever a new lcore is registered for use at runtime, e.g. by rte_thread_register()/unregister().
My initial thought was:
we can allocate caches for all lcores that are already enabled at mempool_init time.
If later, such core will be disabled - that's probably not a big deal, we can simply left the cache for it hanging around unused until mempool will be destroyed.
For lcores that will be enabled after mempool_init() we can use lazy allocation (at first access to it). But after another thought - that's probably not such good idea, as in principle rte_malloc() here can fail.
Quoted 56 lines
Quoted 56 lines
lib/mempool/rte_mempool.h struct __rte_cache_aligned rte_mempool { · line 266
Quoted 8 lines
lib/mempool/rte_mempool.h +0
-0};/**
lib/mempool/rte_mempool.h +0
-0@@ -265,6 +264,7 @@ struct __rte_cache_aligned rte_mempool {int32_t ops_index;struct rte_mempool_cache *local_cache;/**< Per-lcore local cache */
lib/mempool/rte_mempool.h struct __rte_cache_aligned rte_mempool { · line 267
Quoted 1 line
lib/mempool/rte_mempool.h +1
-0+uint32_t sizeof_cache_per_lcore;/**< Multiplier for indexing into the
Quoted 3 lines
local cache. */
uint32_tpopulated_size; /**< Number of populatedobjects. */
Quoted 1 line
struct
rte_mempool_objhdr_listelt_list; /**< List of objects inpool */
lib/mempool/rte_mempool.h rte_mempool_free(struct rte_mempool *mp); · line 1052
Quoted 4 lines
lib/mempool/rte_mempool.h +0
-0@@ -1050,8 +1050,7 @@ rte_mempool_free(struct rte_mempool *mp);* @param cache_size * If cache_size is non-zero, the rte_mempool library will try to * limit the accesses to the common lockless pool, by maintaining a
lib/mempool/rte_mempool.h rte_mempool_free(struct rte_mempool *mp);
Quoted 3 lines
lib/mempool/rte_mempool.h +1
-2-* per-lcore object cache. This argument must be lower or equal to -* RTE_MEMPOOL_CACHE_MAX_SIZE and n. +* per-lcore object cache. This argument must be lower or equal to n.
lib/mempool/rte_mempool.h rte_mempool_free(struct rte_mempool *mp);
Quoted 3 lines
lib/mempool/rte_mempool.h +0
-0* The access to the per-lcore table is of course * faster than the multi-producer/consumer pool. The cache can be * disabled if the cache_size argument is set to 0; it can be useful to
Quoted 26 lines
lib/mempool/rte_mempool.h +6
-4@@ -1371,15 +1370,17 @@ rte_mempool_cache_create(uint32_t size, int socket_id) static __rte_always_inline struct rte_mempool_cache *rte_mempool_default_cache (struct rte_mempool *mp,unsigned lcore_id) { -if (unlikely (mp->cache_size ==0 )) +if (unlikely (mp->local_cache ==NULL ))return NULL ;if (unlikely (lcore_id ==LCORE_ID_ANY ))return NULL ; -rte_mempool_trace_default_cache (mp, lcore_id, - &mp->local_cache[lcore_id]); -return &mp->local_cache[lcore_id]; +struct rte_mempool_cache *cache = (struct rte_mempool_cache *)RTE_PTR_ADD (mp->local_cache, + lcore_id * (size_t )mp->sizeof_cache_per_lcore); +rte_mempool_trace_default_cache (mp, lcore_id, cache); +__rte_assume (cache !=NULL ); +return cache; }/**
--
2.43.0
RE: [PATCH 6/6] mempool: no cache size limit
Quoted 52 lines
Quoted 1 line
Quoted 1 line
Review
Quoted 1 line
(quoted diff) +0
-0
b/lib/mempool/rte_mempool.hQuoted 4 lines
Quoted 4 lines
lib/mempool/rte_mempool.h struct __rte_cache_aligned rte_mempool_cache
Quoted 4 lines
index
5a81e53a9f..5fc7cdb79c100644lib/mempool/rte_mempool.h +0
-0@@ -105,8 +105,7 @@ struct __rte_cache_aligned rte_mempool_cache{
Quoted 10 lines
Quoted 10 lines
lib/mempool/rte_mempool.h struct __rte_cache_aligned rte_mempool_cache · line 109
Quoted 7 lines
lib/mempool/rte_mempool.h +1
-2} stats; /**< Statistics */#endif /** Cache objects */ - alignas(RTE_CACHE_LINE_SIZE )void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE ]; -RTE_CACHE_GUARD ; + alignas(RTE_CACHE_LINE_SIZE )void *objs[];LGTM to me in general.
Again, just as a thought:Quoted 8 lines
Quoted 8 lines
Would it be plausible to have a pointer per lcore instead? i.e.:
alignas(RTE_CACHE_LINE_SIZE)void *objs[RTE_MAX_LCORE]; That way we can avoid multiply op inrte_mempool_default_cache(), also it will allow us to allocate lcore cache on demand (only for enabled lcores).
As a downside -sizeof(rte_mempool_cache)will grow by (RTE_MAX_LCORE- 1) *sizeof(uintptr_t), but that's probably not abig
Quoted 11 lines
Quoted 1 line
deal.
The multiply op is not expensive.
This patch has:
structrte_mempool_cache*cache =(struct rte_mempool_cache *) RTE_PTR_ADD( mp->local_cache, lcore_id * (size_t)mp->sizeof_cache_per_lcore);Quoted 8 lines
Which compiles to something like:
ptr =LOAD(mp + offsetof(cache_ptr));
off =LOAD(mp + offsetof(sizeof_cache_per_lcore)); off *=lcore_id;
ptr += off;Yes, having an array of pointers to the caches in the mempool would
be a good
Review
Quoted 4 lines
alternative:
struct
__rte_cache_alignedrte_mempool{(quoted diff) +0
-1- struct rte_mempool_cache *local_cache; /**< Per-lcore local cache*/
Quoted 1 line
Quoted 22 lines
Quoted 6 lines
[...]
struct rte_mempool_debug_stats stats[RTE_MAX_LCORE + 1];#endif
(quoted diff) +2
-0+ /** Per-lcore local cache */ + struct rte_mempool_cache *local_cache[RTE_MAX_LCORE];};
Yes, you right, off-course it has to be that way.
Quoted 13 lines
Lookup for that would become simple:
structrte_mempool_cache*cache =mp->local_cache[lcore_id];Which compiles to something like:
off =lcore_id*sizeof(void*); // Shift operation off +=offsetof(cache_ptr_array);
ptr = LOAD(mp + off);Yes, I suppose that would be faster.
I'll take a stab at it.
I tried it, but the mempool perf test yields similar results on our build server (a virtual machine).
There are also a couple of disadvantages:
1. The table has a larger memory (and cache) footprint than the solution with the base pointer and multiplier. We knew that, and consider it insignificant.
2. If the mempool cache misses, the backend must be involved, and this needs to load mp->ops_index. With the base/multiplier variant, ops_index is in the same cache line as the local_cache pointer, and thus already hot in the cache. With the lcore_id indexed local_cache pointer array, ops_index is not hot in the cache. Obviously, we want to optimize for mempool cache hits, so this should be considered insignificant; I only mention it for completeness.
I think we should stick with the patch series as is.
If you want to play around with the lcore_id indexed local_cache pointer array, a patch is provided at the bottom of this email.
Quoted 83 lines
Quoted 2 lines
Caches for all lcores must be allocated at mempool creation. Or we
would need
Quoted 1 line
special handling to allocate/free the relevant per-lcore cache for
all registered
Quoted 1 line
mempools whenever a new lcore is registered for use at runtime, e.g.
by
Quoted 1 line
rte_thread_register()/unregister().
My initial thought was:
we can allocate caches for all lcores that are already enabled atmempool_inittime.
If later, such core will be disabled - that's probably not a big deal, we can simply left the cache for it
hanging around unused until mempool will be destroyed.
For lcores that will be enabled aftermempool_init()we can use lazy allocation (at first access to it).
But after another thought - that's probably not such good idea, as in principlerte_malloc()here can fail.Quoted 8 lines
Quoted 8 lines
lib/mempool/rte_mempool.h struct __rte_cache_aligned rte_mempool { · line 266
Quoted 8 lines
};
/**
lib/mempool/rte_mempool.h +0
-0@@ -265,6 +264,7 @@ struct __rte_cache_aligned rte_mempool {int32_t ops_index;struct rte_mempool_cache *local_cache;/**< Per-lcore local cache
Quoted 2 lines
Quoted 2 lines
*/
lib/mempool/rte_mempool.h struct __rte_cache_aligned rte_mempool { · line 267
Quoted 1 line
lib/mempool/rte_mempool.h +1
-0+uint32_t sizeof_cache_per_lcore;/**< Multiplier for indexing
Quoted 6 lines
Quoted 6 lines
into the
Quoted 3 lines
local cache. */
uint32_tpopulated_size; /**< Number of populatedobjects. */
Quoted 1 line
struct
rte_mempool_objhdr_listelt_list; /**< List ofobjects in
Quoted 4 lines
Quoted 4 lines
pool */
lib/mempool/rte_mempool.h rte_mempool_free(struct rte_mempool *mp); · line 1051
Quoted 3 lines
lib/mempool/rte_mempool.h +0
-0@@ -1050,8 +1050,7 @@ rte_mempool_free(struct rte_mempool *mp);* @param cache_size * If cache_size is non-zero, the rte_mempool library will try to
Quoted 1 line
Quoted 1 line
lib/mempool/rte_mempool.h rte_mempool_free(struct rte_mempool *mp); · line 1052
Quoted 1 line
lib/mempool/rte_mempool.h +0
-0* limit the accesses to the common lockless pool, by maintaining
Quoted 2 lines
Quoted 2 lines
a
Quoted 1 line
lib/mempool/rte_mempool.h +0
-1-* per-lcore object cache. This argument must be lower or equal to
Quoted 2 lines
Quoted 2 lines
lib/mempool/rte_mempool.h rte_mempool_free(struct rte_mempool *mp); · line 1053
Quoted 2 lines
lib/mempool/rte_mempool.h +1
-1-* RTE_MEMPOOL_CACHE_MAX_SIZE and n. +* per-lcore object cache. This argument must be lower or equal to
Quoted 3 lines
Quoted 3 lines
n.
Quoted 2 lines
lib/mempool/rte_mempool.h +0
-0* The access to the per-lcore table is of course * faster than the multi-producer/consumer pool. The cache can be
Quoted 3 lines
Quoted 3 lines
lib/mempool/rte_mempool.h rte_mempool_free(struct rte_mempool *mp); · line 1056
Quoted 1 line
lib/mempool/rte_mempool.h +0
-0* disabled if the cache_size argument is set to 0; it can be useful to
lib/mempool/rte_mempool.h rte_mempool_cache_create(uint32_t size,
Quoted 1 line
lib/mempool/rte_mempool.h +0
-0@@ -1371,15 +1370,17 @@ rte_mempool_cache_create(uint32_t size,int
Quoted 3 lines
Quoted 3 lines
lib/mempool/rte_mempool.h rte_mempool_cache_create(uint32_t size, · line 1372
Quoted 3 lines
lib/mempool/rte_mempool.h +0
-0socket_id) static __rte_always_inline struct rte_mempool_cache * rte_mempool_default_cache(struct rte_mempool *mp, unsigned
lcore_id)Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
lib/mempool/rte_mempool.h +6
-4{ -if (unlikely (mp->cache_size ==0 )) +if (unlikely (mp->local_cache ==NULL ))return NULL ;if (unlikely (lcore_id ==LCORE_ID_ANY ))return NULL ; -rte_mempool_trace_default_cache (mp, lcore_id, - &mp->local_cache[lcore_id]); -return &mp->local_cache[lcore_id]; +struct rte_mempool_cache *cache = (struct rte_mempool_cache *)RTE_PTR_ADD (mp->local_cache, + lcore_id * (size_t )mp->sizeof_cache_per_lcore); +rte_mempool_trace_default_cache (mp, lcore_id, cache); +__rte_assume (cache !=NULL ); +return cache; }
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
/**
--
2.43.0
doc/guides/rel_notes/release_26_11.rst +1 -1
@@ -105,7 +105,7 @@ API Changes as it is often used for the ``cache_size`` parameter when creating mempools. * mempool: Updated the ``rte_mempool`` structure as follows: - - Added the ``sizeof_cache_per_lcore`` field, for indexing into the per-lcore local cache. + - Changed the ``local_cache`` field to array of pointers to per-lcore local cache, for improved lookup performance. * mempool: Updated the ``rte_mempool_cache`` structure as follows: - Removed the deprecated and obsolete ``flushthresh`` field.
lib/mempool/rte_mempool.c +40 -42
154 lines not on this page · raw message
lib/mempool/rte_mempool.h +12 -13
@@ -263,14 +263,18 @@ struct __rte_cache_aligned rte_mempool {*/ int32_t ops_index; -struct rte_mempool_cache *local_cache;/**< Per-lcore local cache */ -uint32_t sizeof_cache_per_lcore;/**< Multiplier for indexing into the local cache. */ -uint32_t populated_size;/**< Number of populated objects. */ struct rte_mempool_objhdr_list elt_list;/**< List of objects in pool */ uint32_t nb_mem_chunks;/**< Number of memory chunks */ struct rte_mempool_memhdr_list mem_list;/**< List of memory chunks */ +/** +* Pointers to per-lcore local cache. +* Note: Cache line aligned for higher cache hit rate when only using the first few lcores. +*/ + alignas(RTE_CACHE_LINE_SIZE ) +struct rte_mempool_cache *local_cache[RTE_MAX_LCORE ]; +#ifdef RTE_LIBRTE_MEMPOOL_STATS /** Per-lcore statistics. *
@@ -1370,17 +1374,12 @@ rte_mempool_cache_create(uint32_t size, int socket_id) static __rte_always_inline struct rte_mempool_cache *rte_mempool_default_cache (struct rte_mempool *mp,unsigned lcore_id) { -if (unlikely (mp->local_cache ==NULL )) -return NULL ; -if (unlikely (lcore_id ==LCORE_ID_ANY ))return NULL ; -struct rte_mempool_cache *cache = (struct rte_mempool_cache *)RTE_PTR_ADD (mp->local_cache, - lcore_id * (size_t )mp->sizeof_cache_per_lcore); -rte_mempool_trace_default_cache (mp, lcore_id, cache); -__rte_assume (cache !=NULL ); -return cache; +rte_mempool_trace_default_cache (mp, lcore_id, + mp->local_cache[lcore_id]); +return mp->local_cache[lcore_id]; }/**
@@ -1449,8 +1448,8 @@ rte_mempool_do_generic_put(struct rte_mempool *mp, void * const *obj_table,* are more hot, from the upper half of the cache. */ __rte_assume (cache->len > cache->size /2 ); -rte_mempool_ops_enqueue_bulk (mp, &cache->objs[0 ], cache->size /2 ); -rte_memcpy (&cache->objs[0 ], &cache->objs[cache->size /2 ], +rte_mempool_ops_enqueue_bulk (mp, cache->objs, cache->size /2 ); +rte_memcpy (cache->objs, &cache->objs[cache->size /2 ],sizeof (void *) * (cache->len - cache->size /2 )); cache_objs = &cache->objs[cache->len - cache->size /2 ]; cache->len = cache->len - cache->size /2 + n;
RE: [PATCH 6/6] mempool: no cache size limit
Quoted 90 lines
Quoted 54 lines
Quoted 3 lines
Quoted 3 lines
Review
Quoted 3 lines
(quoted diff) +0
-0
b/lib/mempool/rte_mempool.hQuoted 4 lines
Quoted 4 lines
lib/mempool/rte_mempool.h struct __rte_cache_aligned rte_mempool_cache
Quoted 4 lines
index
5a81e53a9f..5fc7cdb79c100644lib/mempool/rte_mempool.h +0
-0@@ -105,8 +105,7 @@ struct __rte_cache_aligned rte_mempool_cache{
Quoted 10 lines
Quoted 10 lines
lib/mempool/rte_mempool.h struct __rte_cache_aligned rte_mempool_cache · line 109
Quoted 7 lines
lib/mempool/rte_mempool.h +1
-2} stats; /**< Statistics */#endif /** Cache objects */ - alignas(RTE_CACHE_LINE_SIZE )void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE ]; -RTE_CACHE_GUARD ; + alignas(RTE_CACHE_LINE_SIZE )void *objs[];LGTM to me in general.
Again, just as a thought:Quoted 8 lines
Quoted 8 lines
Would it be plausible to have a pointer per lcore instead? i.e.:
alignas(RTE_CACHE_LINE_SIZE)void *objs[RTE_MAX_LCORE]; That way we can avoid multiply op inrte_mempool_default_cache(), also it will allow us to allocate lcore cache on demand (only for enabled lcores).
As a downside -sizeof(rte_mempool_cache)will grow by (RTE_MAX_LCORE- 1) *sizeof(uintptr_t), but that's probably not abig
Quoted 11 lines
Quoted 1 line
deal.
The multiply op is not expensive.
This patch has:
structrte_mempool_cache*cache =(struct rte_mempool_cache *) RTE_PTR_ADD( mp->local_cache, lcore_id * (size_t)mp->sizeof_cache_per_lcore);Quoted 8 lines
Which compiles to something like:
ptr =LOAD(mp + offsetof(cache_ptr));
off =LOAD(mp + offsetof(sizeof_cache_per_lcore)); off *=lcore_id;
ptr += off;Yes, having an array of pointers to the caches in the mempool would
be a good
Review
Quoted 4 lines
alternative:
struct
__rte_cache_alignedrte_mempool{(quoted diff) +0
-1- struct rte_mempool_cache *local_cache; /**< Per-lcore local cache*/
Quoted 1 line
Quoted 22 lines
Quoted 6 lines
[...]
struct rte_mempool_debug_stats stats[RTE_MAX_LCORE + 1];#endif
(quoted diff) +2
-0+ /** Per-lcore local cache */ + struct rte_mempool_cache *local_cache[RTE_MAX_LCORE];};
Yes, you right, off-course it has to be that way.
Quoted 13 lines
Lookup for that would become simple:
structrte_mempool_cache*cache =mp->local_cache[lcore_id];Which compiles to something like:
off =lcore_id*sizeof(void*); // Shift operation off +=offsetof(cache_ptr_array);
ptr = LOAD(mp + off);Yes, I suppose that would be faster.
I'll take a stab at it.I tried it, but the mempool perf test yields similar results on our build server (a virtual machine).
There are also a couple of disadvantages:
1. The table has a larger memory (and cache) footprint than the solution with the base pointer and multiplier. We knew that, and consider it insignificant.
2. If the mempool cache misses, the backend must be involved, and this needs to loadmp->ops_index. With the base/multiplier variant,ops_indexis in the same cache line as thelocal_cachepointer, and thus already hot in the cache. With thelcore_idindexedlocal_cachepointer array,ops_indexis not hot in the cache. Obviously, we want to optimize for mempool cache hits, so this should be considered insignificant; I only mention it for completeness.
Quoted 1 line
I think we should stick with the patch series as is.
Ok, thanks for trying.
Overall the series looks good to me, so:
Quoted 3 lines
If you want to play around with the
lcore_idindexedlocal_cachepointer array, a patch is provided at the bottom of this email.
If I'll have time, I will run both versions on our boxes, though no hard promises.
Quoted 353 lines
Quoted 86 lines
Quoted 2 lines
Caches for all lcores must be allocated at mempool creation. Or we
would need
Quoted 1 line
special handling to allocate/free the relevant per-lcore cache for
all registered
Quoted 1 line
mempools whenever a new lcore is registered for use at runtime, e.g.
by
Quoted 1 line
rte_thread_register()/unregister().
My initial thought was:
we can allocate caches for all lcores that are already enabled atmempool_inittime.
If later, such core will be disabled - that's probably not a big deal, we can simply left the cache for it
hanging around unused until mempool will be destroyed.
For lcores that will be enabled aftermempool_init()we can use lazy allocation (at first access to it).
But after another thought - that's probably not such good idea, as in principlerte_malloc()here can fail.Quoted 8 lines
Quoted 8 lines
lib/mempool/rte_mempool.h struct __rte_cache_aligned rte_mempool { · line 266
Quoted 8 lines
};
/**
lib/mempool/rte_mempool.h +0
-0@@ -265,6 +264,7 @@ struct __rte_cache_aligned rte_mempool {int32_t ops_index;struct rte_mempool_cache *local_cache;/**< Per-lcore local cache
Quoted 2 lines
Quoted 2 lines
*/
lib/mempool/rte_mempool.h struct __rte_cache_aligned rte_mempool { · line 267
Quoted 1 line
lib/mempool/rte_mempool.h +1
-0+uint32_t sizeof_cache_per_lcore;/**< Multiplier for indexing
Quoted 6 lines
Quoted 6 lines
into the
Quoted 3 lines
local cache. */
uint32_tpopulated_size; /**< Number of populatedobjects. */
Quoted 1 line
struct
rte_mempool_objhdr_listelt_list; /**< List ofobjects in
Quoted 4 lines
Quoted 4 lines
pool */
lib/mempool/rte_mempool.h rte_mempool_free(struct rte_mempool *mp); · line 1051
Quoted 3 lines
lib/mempool/rte_mempool.h +0
-0@@ -1050,8 +1050,7 @@ rte_mempool_free(struct rte_mempool *mp);* @param cache_size * If cache_size is non-zero, the rte_mempool library will try to
Quoted 1 line
Quoted 1 line
lib/mempool/rte_mempool.h rte_mempool_free(struct rte_mempool *mp); · line 1052
Quoted 1 line
lib/mempool/rte_mempool.h +0
-0* limit the accesses to the common lockless pool, by maintaining
Quoted 2 lines
Quoted 2 lines
a
Quoted 1 line
lib/mempool/rte_mempool.h +0
-1-* per-lcore object cache. This argument must be lower or equal to
Quoted 2 lines
Quoted 2 lines
lib/mempool/rte_mempool.h rte_mempool_free(struct rte_mempool *mp); · line 1053
Quoted 2 lines
lib/mempool/rte_mempool.h +1
-1-* RTE_MEMPOOL_CACHE_MAX_SIZE and n. +* per-lcore object cache. This argument must be lower or equal to
Quoted 3 lines
Quoted 3 lines
n.
Quoted 2 lines
lib/mempool/rte_mempool.h +0
-0* The access to the per-lcore table is of course * faster than the multi-producer/consumer pool. The cache can be
Quoted 3 lines
Quoted 3 lines
lib/mempool/rte_mempool.h rte_mempool_free(struct rte_mempool *mp); · line 1056
Quoted 1 line
lib/mempool/rte_mempool.h +0
-0* disabled if the cache_size argument is set to 0; it can be useful to
lib/mempool/rte_mempool.h rte_mempool_cache_create(uint32_t size,
Quoted 1 line
lib/mempool/rte_mempool.h +0
-0@@ -1371,15 +1370,17 @@ rte_mempool_cache_create(uint32_t size,int
Quoted 3 lines
Quoted 3 lines
lib/mempool/rte_mempool.h rte_mempool_cache_create(uint32_t size, · line 1372
Quoted 3 lines
lib/mempool/rte_mempool.h +0
-0socket_id) static __rte_always_inline struct rte_mempool_cache * rte_mempool_default_cache(struct rte_mempool *mp, unsigned
lcore_id)Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
lib/mempool/rte_mempool.h +6
-4{ -if (unlikely (mp->cache_size ==0 )) +if (unlikely (mp->local_cache ==NULL ))return NULL ;if (unlikely (lcore_id ==LCORE_ID_ANY ))return NULL ; -rte_mempool_trace_default_cache (mp, lcore_id, - &mp->local_cache[lcore_id]); -return &mp->local_cache[lcore_id]; +struct rte_mempool_cache *cache = (struct rte_mempool_cache *)RTE_PTR_ADD (mp->local_cache, + lcore_id * (size_t )mp->sizeof_cache_per_lcore); +rte_mempool_trace_default_cache (mp, lcore_id, cache); +__rte_assume (cache !=NULL ); +return cache; }/**
--
2.43.0doc/guides/rel_notes/release_26_11.rst +0
-1@@ -105,7 +105,7 @@ API Changes as it is often used for the ``cache_size`` parameter when creating mempools. * mempool: Updated the ``rte_mempool`` structure as follows: - - Added the ``sizeof_cache_per_lcore`` field, for indexing into the per-lcorelocal cache.
+ - Changed the ``local_cache`` field to array of pointers to per-lcore local cache, for improved lookup performance.* mempool: Updated the ``rte_mempool_cache`` structure as follows:
- Removed the deprecated and obsolete ``flushthresh`` field.lib/mempool/rte_mempool.c +1
-1@@ -759,7 +759,7 @@ mempool_cache_init(struct rte_mempool_cache *cache, uint32_t size)/* * Create and initialize a cache for objects that are retrieved from and * returned to an underlying mempool. This structure is identical to the -* local_cache entry pointed to by the mempool structure. +* local_cache[lcore_id] pointed to by the mempool structure. */ RTE_EXPORT_SYMBOL (rte_mempool_cache_create)struct
rte_mempool_cache*lib/mempool/rte_mempool.c +1
-0@@ -930,6 +930,7 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, mp->elt_size = objsz.elt_size; mp->header_size = objsz.header_size; mp->trailer_size = objsz.trailer_size; +/* Size of default caches, zero means disabled. */ mp->cache_size = cache_size; mp->private_data_size = private_data_size;STAILQ_INIT(&mp->elt_list);lib/mempool/rte_mempool.c +4
-7@@ -953,17 +954,16 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, goto exit_unlock; } -/* local_cache pointer is only set if per-lcore local cache is present */ +/* local_cache pointers are only set if per-lcore local cache is present */ if (cache_size !=0 ) { - mp->local_cache = (struct rte_mempool_cache *) -RTE_PTR_ADD (mp,sizeof (struct rte_mempool ) + private_data_size); - mp->sizeof_cache_per_lcore = sizeof_cache_per_lcore; -/* Init all default caches. */ -struct rte_mempool_cache *cache = mp->local_cache; +struct rte_mempool_cache *cache = (struct rte_mempool_cache *) +RTE_PTR_ADD (mp,sizeof (struct rte_mempool ) + private_data_size);for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; - lcore_id++, cache =RTE_PTR_ADD (cache, sizeof_cache_per_lcore)) + lcore_id++, cache = RTE_PTR_ADD(cache,
sizeof_cache_per_lcore)) {lib/mempool/rte_mempool.c +7
-6+ mp->local_cache[lcore_id] = cache;mempool_cache_init (cache, cache_size); + } } te->data = mp;@@ -1026,17 +1026,16 @@ RTE_EXPORT_SYMBOL(rte_mempool_avail_count)unsigned int rte_mempool_avail_count (const struct rte_mempool *mp) { -unsigned int count; +unsigned count; +unsigned lcore_id; count =rte_mempool_ops_get_count (mp); -if (mp->local_cache ==NULL ) +if (mp->cache_size ==0 )return count; -const struct rte_mempool_cache *cache = mp->local_cache; -for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; - lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) - count += cache->len; +for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) + count += mp->local_cache[lcore_id]->len;/* * due to race condition (access to len is not locked), the
lib/mempool/rte_mempool.c +5
-5@@ -1064,11 +1063,11 @@ rte_mempool_stats_reset(struct rte_mempool *mp)#ifdef RTE_LIBRTE_MEMPOOL_STATS memset (&mp->stats,0 ,sizeof (mp->stats)); -if (mp->local_cache !=NULL ) { -struct rte_mempool_cache *cache = mp->local_cache; -for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; - lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) -memset (&cache->stats,0 ,sizeof (cache->stats)); +- if (mp->cache_size != 0) { +- for (unsigned int lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { +-memset (&mp->local_cache[lcore_id]->stats,0 , +-sizeof (mp->local_cache[lcore_id]->stats)); +- } }RTE_MEMPOOL_LOG(DEBUG, "<%s>@%p: statistics reset", mp->name, mp);lib/mempool/rte_mempool.c +6
-6@@ -1082,18 +1081,18 @@ rte_mempool_stats_reset(struct rte_mempool *mp)static unsigned rte_mempool_dump_cache (FILE *f,const struct rte_mempool *mp) { -unsigned int count =0 ; +unsigned lcore_id; +unsigned count =0 ; +unsigned cache_count;fprintf (f," internal cache infos (hide zero value items):\n" );fprintf (f," cache_size=%" PRIu32"\n" , mp->cache_size); -if (mp->local_cache ==NULL ) +if (mp->cache_size ==0 )return count; -const struct rte_mempool_cache *cache = mp->local_cache; -for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; - lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) { -unsigned int cache_count = cache->len; +for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) { + cache_count = mp->local_cache[lcore_id]->len;if (cache_count ==0 )continue; fprintf(f, " cache_count[%u]=%"PRIu32"\n",lib/mempool/rte_mempool.c +6
-4@@ -1234,12 +1233,14 @@ mempool_audit_cookies(struct rte_mempool *mp)static void mempool_audit_cache (const struct rte_mempool *mp) { -if (mp->local_cache ==NULL ) +unsigned lcore_id; + +if (mp->cache_size ==0 )return ; -const struct rte_mempool_cache *cache = mp->local_cache; -for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; - lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) { +for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) { +const struct rte_mempool_cache *cache; + cache = mp->local_cache[lcore_id];if (cache->size > mp->size) {RTE_MEMPOOL_LOG (CRIT ,"badness on cache[%u] size" , lcore_id);rte_panic("MEMPOOL: invalid cache[%u] size\n", lcore_id);lib/mempool/rte_mempool.c +6
-8@@ -1333,15 +1334,13 @@ rte_mempool_dump(FILE *f, struct rte_mempool *mp) sum.get_success_blks += mp->stats[lcore_id].get_success_blks; sum.get_fail_blks += mp->stats[lcore_id].get_fail_blks; } -if (mp->local_cache !=NULL ) { +if (mp->cache_size !=0 ) {/* Add the statistics stored in the mempool caches. */ -const struct rte_mempool_cache *cache = mp->local_cache; -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; - lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) { - sum.put_bulk += cache->stats.put_bulk; - sum.put_objs += cache->stats.put_objs; - sum.get_success_bulk += cache->stats.get_success_bulk; - sum.get_success_objs += cache->stats.get_success_objs; +for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) { + sum.put_bulk += mp->local_cache[lcore_id]->stats.put_bulk; + sum.put_objs += mp->local_cache[lcore_id]->stats.put_objs; + sum.get_success_bulk += mp->local_cache[lcore_id]- >stats.get_success_bulk; + sum.get_success_objs += mp->local_cache[lcore_id]-Quoted 1 line
stats.get_success_objs;
} } fprintf(f, " stats:\n");lib/mempool/rte_mempool.c +4
-5@@ -1638,11 +1637,10 @@ mempool_info_cb(struct rte_mempool *mp, void *arg) mp->populated_size); cache_count =0 ; -if (mp->local_cache !=NULL ) { -const struct rte_mempool_cache *cache = mp->local_cache; -for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; - lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) - cache_count += cache->len; +if (mp->cache_size >0 ) { +int lcore_id; +for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) + cache_count += mp->local_cache[lcore_id]->len; }rte_tel_data_add_dict_uint(info->d, "total_cache_count", cache_count); common_count = rte_mempool_ops_get_count(mp);lib/mempool/rte_mempool.h +0
-2@@ -263,14 +263,18 @@ struct __rte_cache_aligned rte_mempool {*/ int32_t ops_index; -struct rte_mempool_cache *local_cache;/**< Per-lcore local cache */ -uint32_t sizeof_cache_per_lcore;/**< Multiplier for indexing into the local cache. */
lib/mempool/rte_mempool.h +3
-1-uint32_t populated_size;/**< Number of populated objects. */ struct rte_mempool_objhdr_list elt_list;/**< List of objects in pool */ uint32_t nb_mem_chunks;/**< Number of memory chunks */ struct rte_mempool_memhdr_list mem_list;/**< List of memory chunks */ +/** +* Pointers to per-lcore local cache. +* Note: Cache line aligned for higher cache hit rate when only using the first few lcores.
lib/mempool/rte_mempool.h +7
-8+*/ + alignas(RTE_CACHE_LINE_SIZE ) +struct rte_mempool_cache *local_cache[RTE_MAX_LCORE ]; +#ifdef RTE_LIBRTE_MEMPOOL_STATS /** Per-lcore statistics. * @@ -1370,17 +1374,12 @@ rte_mempool_cache_create(uint32_t size, int socket_id) static __rte_always_inline struct rte_mempool_cache *rte_mempool_default_cache (struct rte_mempool *mp,unsigned lcore_id) { -if (unlikely (mp->local_cache ==NULL )) -return NULL ; -if (unlikely (lcore_id ==LCORE_ID_ANY ))return NULL ; -struct rte_mempool_cache *cache = (struct rte_mempool_cache *)RTE_PTR_ADD(mp->local_cache, - lcore_id * (size_t )mp->sizeof_cache_per_lcore); -rte_mempool_trace_default_cache (mp, lcore_id, cache); -__rte_assume (cache !=NULL ); -return cache; +rte_mempool_trace_default_cache (mp, lcore_id, + mp->local_cache[lcore_id]); +return mp->local_cache[lcore_id]; }/**
lib/mempool/rte_mempool.h +2
-2@@ -1449,8 +1448,8 @@ rte_mempool_do_generic_put(struct rte_mempool*mp, void * const *obj_table, * are more hot, from the upper half of the cache. */ __rte_assume (cache->len > cache->size /2 ); -rte_mempool_ops_enqueue_bulk (mp, &cache->objs[0 ], cache->size / 2); -rte_memcpy (&cache->objs[0 ], &cache->objs[cache->size /2 ], +rte_mempool_ops_enqueue_bulk (mp, cache->objs, cache->size /2 ); +rte_memcpy (cache->objs, &cache->objs[cache->size /2 ],sizeof (void *) * (cache->len - cache->size /2 ));cache_objs = &cache->objs[cache->len - cache->size / 2]; cache->len = cache->len - cache->size / 2 + n;
RE: [PATCH 6/6] mempool: no cache size limit
In reply to Morten BrørupQuoted 403 lines
Replaced the fixed size object array in the per-lcore local cache with a variable size array, allocated at mempool creation. For faster indexing into the per-lcore array of caches, pre-calculate the size (in bytes) of the per-lcore local cache.
Using a variable size array makes the
RTE_MEMPOOL_CACHE_MAX_SIZEbuild time configuration parameter superfluous,
but it was kept for compatibility purposes, as it is often used for thecache_sizeparameter when creating mempools.Signed-off-by Morten Brørup <[email protected]>---
Supersedes: patch-169235
("[v2] mempool: no cache size limit")--- app/test/test_mempool.c | 2 +- doc/guides/rel_notes/release_26_11.rst | 12 ++- lib/mempool/rte_mempool.c | 118 ++++++++++++++----------- lib/mempool/rte_mempool.h | 17 ++-- 4 files changed, 86 insertions(+), 63 deletions(-)app/test/test_mempool.c +1
-1@@ -193,7 +193,7 @@ static inttest_mempool_creation_with_exceeded_cache_size (void ) mp_cov = rte_mempool_create("test_cache_too_big", MEMPOOL_SIZE, MEMPOOL_ELT_SIZE, - RTE_MEMPOOL_CACHE_MAX_SIZE + 32, 0, + MEMPOOL_SIZE + 32, 0, NULL, NULL, my_obj_init, NULL,app/test/test_mempool.c +0
-0SOCKET_ID_ANY, 0);doc/guides/rel_notes/release_26_11.rst +3
-0@@ -97,10 +97,20 @@ API Changes Also, make sure to start the actual text at the margin. ======================================================= +* mempool: When creating a mempool, the cache size can be freely specified + (although still not exceed the number of elements), + and is no longer limited by the ``RTE_MEMPOOL_CACHE_MAX_SIZE`` build timeconfiguration parameter.
doc/guides/rel_notes/release_26_11.rst +6
-0+ Although ``RTE_MEMPOOL_CACHE_MAX_SIZE`` has lost its original meaning, + it was kept for compatibility purposes, + as it is often used for the ``cache_size`` parameter when creating mempools. + +* mempool: Updated the ``rte_mempool`` structure as follows: + - Added the ``sizeof_cache_per_lcore`` field, for indexing into the per-lcorelocal cache.
doc/guides/rel_notes/release_26_11.rst +1
-1+ * mempool: Updated the ``rte_mempool_cache`` structure as follows: - Removed the deprecated and obsolete ``flushthresh`` field. - Removed the ``unused`` field. - - Reduced the size of the ``objs`` array from``RTE_MEMPOOL_CACHE_MAX_SIZE`` * 2 to
``RTE_MEMPOOL_CACHE_MAX_SIZE``.doc/guides/rel_notes/release_26_11.rst +1
-0+ - Changed the ``objs`` array from fixed size to variable size. ABI Changes -----------lib/mempool/rte_mempool.c +4
-3@@ -759,20 +759,21 @@ mempool_cache_init(struct rte_mempool_cache *cache, uint32_t size)/* * Create and initialize a cache for objects that are retrieved from and * returned to an underlying mempool. This structure is identical to the -* local_cache[lcore_id] pointed to by the mempool structure. +* local_cache entry pointed to by the mempool structure. */ RTE_EXPORT_SYMBOL(rte_mempool_cache_create)struct rte_mempool_cache *rte_mempool_cache_create (uint32_t size,int socket_id) {struct rte_mempool_cache *cache; +size_t sizeof_cache =sizeof (struct rte_mempool_cache ) + size *sizeof (void *); -if (size ==0 || size >RTE_MEMPOOL_CACHE_MAX_SIZE ) { +if (size ==0 || sizeof_cache >UINT32_MAX ) { rte_errno =EINVAL ;return NULL ; } - cache =rte_zmalloc_socket ("MEMPOOL_CACHE" ,sizeof (*cache), + cache =rte_zmalloc_socket ("MEMPOOL_CACHE" , sizeof_cache,RTE_CACHE_LINE_SIZE , socket_id);lib/mempool/rte_mempool.c +1
-2if (cache ==NULL ) {RTE_MEMPOOL_LOG (ERR ,"Cannot allocate mempool cache." );@@ -811,10 +812,9 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, struct rte_mempool *mp = NULL;struct rte_tailq_entry *te =NULL ;const struct rte_memzone *mz =NULL ; -size_t mempool_size; +size_t mempool_size, sizeof_cache_per_lcore;unsigned int mz_flags =RTE_MEMZONE_1GB |RTE_MEMZONE_SIZE_HINT_ONLY ;struct rte_mempool_objsz objsz; -unsigned lcore_id;int ret;lib/mempool/rte_mempool.c +2
-0/* compilation-time checks */ @@ -822,6 +822,8 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, RTE_CACHE_LINE_MASK) != 0);RTE_BUILD_BUG_ON ((sizeof (struct rte_mempool_cache ) &RTE_CACHE_LINE_MASK ) !=0 ); +RTE_BUILD_BUG_ON (offsetof(struct rte_mempool_cache , objs) != +sizeof (struct rte_mempool_cache ));#ifdef RTE_LIBRTE_MEMPOOL_STATS RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &lib/mempool/rte_mempool.c +12
-1RTE_CACHE_LINE_MASK) != 0);@@ -838,7 +840,18 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, }/* asked cache too big */ -if (cache_size >RTE_MEMPOOL_CACHE_MAX_SIZE || + sizeof_cache_per_lcore =0 ; +if (cache_size !=0 ) { + sizeof_cache_per_lcore =sizeof (struct rte_mempool_cache ); + sizeof_cache_per_lcore +=RTE_CACHE_LINE_ROUNDUP (cache_size *sizeof (void *)); +/* +* Add padding, to guard against false sharing-like effects +* on systems with a next-N-lines hardware prefetcher, when +* accessing objects at the end of the cache. +*/ + sizeof_cache_per_lcore +=RTE_CACHE_GUARD_LINES *RTE_CACHE_LINE_SIZE ; + } + if (sizeof_cache_per_lcore > UINT32_MAX ||lib/mempool/rte_mempool.c +1
-2cache_size > n) { rte_errno =EINVAL ;return NULL ;@@ -890,8 +903,7 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, mempool_size =sizeof (struct rte_mempool ); mempool_size += private_data_size; -if (cache_size !=0 ) - mempool_size +=RTE_MAX_LCORE *sizeof (struct rte_mempool_cache); + mempool_size +=RTE_MAX_LCORE * sizeof_cache_per_lcore;ret = snprintf(mz_name,
sizeof(mz_name),RTE_MEMPOOL_MZ_FORMAT, name);lib/mempool/rte_mempool.c +0
-1if (ret <0 || ret >= (int )sizeof (mz_name)) {@@ -918,7 +930,6 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, mp->elt_size = objsz.elt_size; mp->header_size = objsz.header_size; mp->trailer_size = objsz.trailer_size; -/* Size of default caches, zero means disabled. */ mp->cache_size = cache_size; mp->private_data_size = private_data_size;lib/mempool/rte_mempool.c +9
-11STAILQ_INIT (&mp->elt_list);@@ -942,18 +953,17 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, goto exit_unlock; } -/* -* local_cache pointer is set even if cache_size is zero. -* The local_cache points to just past the private data. -*/ - mp->local_cache = (struct rte_mempool_cache *) -RTE_PTR_ADD (mp,sizeof (struct rte_mempool ) + private_data_size); - -/* Init all default caches. */ + /* local_cache pointer is only set if per-lcore local cache is present */ if (cache_size != 0) { -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) -mempool_cache_init (&mp->local_cache[lcore_id], - cache_size); + mp->local_cache = (struct rte_mempool_cache *) + RTE_PTR_ADD(mp, sizeof(struct rte_mempool) + private_data_size); + mp->sizeof_cache_per_lcore = sizeof_cache_per_lcore; + + /* Init all default caches. */ + struct rte_mempool_cache *cache = mp->local_cache; + for (unsigned int lcore_id = 0; lcore_id < RTE_MAX_LCORE; + lcore_id++, cache = RTE_PTR_ADD(cache,
sizeof_cache_per_lcore))lib/mempool/rte_mempool.c +7
-5+mempool_cache_init (cache, cache_size); } te->data = mp;@@ -1016,16 +1026,17 @@ RTE_EXPORT_SYMBOL(rte_mempool_avail_count)unsigned int rte_mempool_avail_count (const struct rte_mempool *mp) { -unsigned count; -unsigned lcore_id; +unsigned int count; count =rte_mempool_ops_get_count (mp); -if (mp->cache_size ==0 ) +if (mp->local_cache ==NULL )return count; -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) - count += mp->local_cache[lcore_id].len; +const struct rte_mempool_cache *cache = mp->local_cache; +for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; + lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) + count += cache->len;/* lib/mempool/rte_mempool.c +5
-5* due to race condition (access to len is not locked), the @@ -1053,11 +1064,11 @@ rte_mempool_stats_reset(struct rte_mempool *mp)#ifdef RTE_LIBRTE_MEMPOOL_STATS memset (&mp->stats,0 ,sizeof (mp->stats)); -if (mp->cache_size !=0 ) { -for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) { -memset (&mp->local_cache[lcore_id].stats,0 , -sizeof (mp->local_cache[lcore_id].stats)); - } +if (mp->local_cache !=NULL ) { +struct rte_mempool_cache *cache = mp->local_cache; +for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; + lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) +memset (&cache->stats,0 ,sizeof (cache->stats));lib/mempool/rte_mempool.c +0
-0} RTE_MEMPOOL_LOG(DEBUG, "<%s>@%p: statistics reset", mp->name,mp);
lib/mempool/rte_mempool.c +6
-6@@ -1071,18 +1082,18 @@ rte_mempool_stats_reset(struct rte_mempool *mp)static unsigned rte_mempool_dump_cache (FILE *f,const struct rte_mempool *mp) { -unsigned lcore_id; -unsigned count =0 ; -unsigned cache_count; +unsigned int count =0 ;fprintf (f," internal cache infos (hide zero value items):\n" );fprintf (f," cache_size=%" PRIu32"\n" , mp->cache_size); -if (mp->cache_size ==0 ) +if (mp->local_cache ==NULL )return count; -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) { - cache_count = mp->local_cache[lcore_id].len; +const struct rte_mempool_cache *cache = mp->local_cache; +for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; + lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) { +unsigned int cache_count = cache->len;if (cache_count ==0 )lib/mempool/rte_mempool.c +5
-7continue ; fprintf(f, " cache_count[%u]=%"PRIu32"\n",@@ -1223,15 +1234,13 @@ mempool_audit_cookies(struct rte_mempool *mp)static void mempool_audit_cache (const struct rte_mempool *mp) { -unsigned lcore_id; - -if (mp->cache_size ==0 ) +if (mp->local_cache ==NULL )return ; -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) { -const struct rte_mempool_cache *cache; - cache = &mp->local_cache[lcore_id]; -if (cache->size >RTE_DIM (cache->objs)) { +const struct rte_mempool_cache *cache = mp->local_cache; +for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; + lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) { +if (cache->size > mp->size) {RTE_MEMPOOL_LOG (CRIT ,"badness on cache[%u] size" , lcore_id);rte_panic("MEMPOOL: invalid cache[%u] size\n",
lcore_id);lib/mempool/rte_mempool.c +1
-6}@@ -1324,13 +1333,15 @@ rte_mempool_dump(FILE *f, struct rte_mempool *mp) sum.get_success_blks += mp->stats[lcore_id].get_success_blks; sum.get_fail_blks += mp->stats[lcore_id].get_fail_blks; } -if (mp->cache_size !=0 ) { +if (mp->local_cache !=NULL ) {/* Add the statistics stored in the mempool caches. */ -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) { - sum.put_bulk += mp- >local_cache[lcore_id].stats.put_bulk; - sum.put_objs += mp- >local_cache[lcore_id].stats.put_objs; - sum.get_success_bulk += mp- >local_cache[lcore_id].stats.get_success_bulk; - sum.get_success_objs += mp-Quoted 1 line
local_cache[lcore_id].stats.get_success_objs;
lib/mempool/rte_mempool.c +3
-0+const struct rte_mempool_cache *cache = mp->local_cache; + for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; + lcore_id++, cache = RTE_PTR_ADD(cache, mp-Quoted 1 line
sizeof_cache_per_lcore)) {lib/mempool/rte_mempool.c +9
-4+ sum.put_bulk += cache->stats.put_bulk; + sum.put_objs += cache->stats.put_objs; + sum.get_success_bulk += cache->stats.get_success_bulk; + sum.get_success_objs += cache->stats.get_success_objs; } }fprintf (f," stats:\n" );@@ -1627,10 +1638,11 @@ mempool_info_cb(struct rte_mempool *mp, void *arg) mp->populated_size); cache_count =0 ; -if (mp->cache_size >0 ) { -int lcore_id; -for (lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; lcore_id++) - cache_count += mp->local_cache[lcore_id].len; +if (mp->local_cache !=NULL ) { +const struct rte_mempool_cache *cache = mp->local_cache; +for (unsigned int lcore_id =0 ; lcore_id <RTE_MAX_LCORE ; + lcore_id++, cache =RTE_PTR_ADD (cache, mp- >sizeof_cache_per_lcore)) + cache_count += cache->len; }lib/mempool/rte_mempool.c +0
-0rte_tel_data_add_dict_uint (info->d,"total_cache_count" , cache_count); common_count =rte_mempool_ops_get_count (mp);lib/mempool/rte_mempool.h +1
-2@@ -105,8 +105,7 @@ struct __rte_cache_aligned rte_mempool_cache { } stats; /**< Statistics */#endif /** Cache objects */ - alignas(RTE_CACHE_LINE_SIZE )void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE ]; -RTE_CACHE_GUARD ; + alignas(RTE_CACHE_LINE_SIZE )void *objs[]; };/**
lib/mempool/rte_mempool.h +1
-0@@ -265,6 +264,7 @@ struct __rte_cache_aligned rte_mempool {int32_t ops_index;struct rte_mempool_cache *local_cache;/**< Per-lcore local cache */ +uint32_t sizeof_cache_per_lcore;/**< Multiplier for indexing into the local cache. */
lib/mempool/rte_mempool.h +7
-6uint32_t populated_size;/**< Number of populated objects. */ struct rte_mempool_objhdr_list elt_list;/**< List of objects in pool */ @@ -1050,8 +1050,7 @@ rte_mempool_free(struct rte_mempool *mp);* @param cache_size * If cache_size is non-zero, the rte_mempool library will try to * limit the accesses to the common lockless pool, by maintaining a -* per-lcore object cache. This argument must be lower or equal to -* RTE_MEMPOOL_CACHE_MAX_SIZE and n. +* per-lcore object cache. This argument must be lower or equal to n. * The access to the per-lcore table is of course * faster than the multi-producer/consumer pool. The cache can be * disabled if the cache_size argument is set to 0; it can be useful to @@ -1371,15 +1370,17 @@ rte_mempool_cache_create(uint32_t size, int socket_id) static __rte_always_inline struct rte_mempool_cache *rte_mempool_default_cache (struct rte_mempool *mp,unsigned lcore_id) { -if (unlikely (mp->cache_size ==0 )) +if (unlikely (mp->local_cache ==NULL ))return NULL ;if (unlikely (lcore_id ==LCORE_ID_ANY ))return NULL ; -rte_mempool_trace_default_cache (mp, lcore_id, - &mp->local_cache[lcore_id]); -return &mp->local_cache[lcore_id]; +struct rte_mempool_cache *cache = (struct rte_mempool_cache *)RTE_PTR_ADD (mp->local_cache, + lcore_id * (size_t )mp->sizeof_cache_per_lcore); +rte_mempool_trace_default_cache (mp, lcore_id, cache); +__rte_assume (cache !=NULL ); +return cache; }/**
--
2.43.0
Thomas Monjalon
AppliedQuoted 7 lines
Morten Brørup (6):
mempool: fix private data area size alignment net/sxe2: do not use deprecated mempool cache field
mempool: remove deprecated cache flush threshold field
mempool: various cleanups
mempool: optimize access to private data
mempool: no cache size limit
Applied, thanks.