stack: introduce pile
PATCH 1/1 stack: introduce pile
Added a new high-performance lock-free "pile", using the Stack API. The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile time configurable bulk size is handled gracefully, but not as fast as complete bulks.
Performance examples, stack_pile_perf_autotest vs. stack_lf_autotest:
On a single core, pushing/popping 1 or 8 objects is similar speed. On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly faster. On four cores, pushing/popping 512 objects is 4x faster.
--- app/test/test_stack.c | 71 +++++- app/test/test_stack_perf.c | 15 +- config/rte_config.h | 3 + doc/guides/prog_guide/stack_lib.rst | 67 +++++- lib/mempool/rte_mempool.h | 2 +- lib/stack/meson.build | 3 +- lib/stack/rte_stack.c | 18 +- lib/stack/rte_stack.h | 79 +++++++ lib/stack/rte_stack_lf.h | 1 + lib/stack/rte_stack_pile.c | 35 +++ lib/stack/rte_stack_pile.h | 334 ++++++++++++++++++++++++++++ 11 files changed, 609 insertions(+), 19 deletions(-) create mode 100644 lib/stack/rte_stack_pile.c create mode 100644 lib/stack/rte_stack_pile.h
app/test/test_stack.c +61 -10
@@ -11,8 +11,8 @@#include "test.h" -#define STACK_SIZE 4096 -#define MAX_BULK 32 +#define STACK_SIZE 65536 +#define MAX_BULK 512static int test_stack_push_pop (struct rte_stack *s,void **obj_table,unsigned int bulk_sz)
@@ -81,13 +81,37 @@ test_stack_push_pop(struct rte_stack *s, void **obj_table, unsigned int bulk_sz) } } -for (i =0 ; i <STACK_SIZE ; i++) { -if (obj_table[i] != popped_objs[STACK_SIZE - i -1 ]) { -printf ("[%s():%u] Incorrect value %p at index 0x%x\n" , - __func__, __LINE__, - popped_objs[STACK_SIZE - i -1 ], i); -rte_free (popped_objs); -return -1 ; +if (!(s->flags &RTE_STACK_F_PILE )) { +/* Normal stack. */ +lifo: +for (i =0 ; i <STACK_SIZE ; i++) { +if (obj_table[i] != popped_objs[STACK_SIZE - i -1 ]) { +printf ("[%s():%u] Incorrect value %p at index 0x%x\n" , + __func__, __LINE__, + popped_objs[STACK_SIZE - i -1 ], i); +rte_free (popped_objs); +return -1 ; + } + } + }else { +/* Pile. Ordering not strictly LIFO. */ +if (bulk_sz <RTE_STACK_PILE_BULK_SIZE ) + goto lifo; +if ((bulk_sz & (RTE_STACK_PILE_BULK_SIZE -1 )) ==0 ) { +for (i =0 ; i <STACK_SIZE ; i +=RTE_STACK_PILE_BULK_SIZE ) { +if (memcmp (&obj_table[i], + &popped_objs[STACK_SIZE -RTE_STACK_PILE_BULK_SIZE - + i], +sizeof (void *) *RTE_STACK_PILE_BULK_SIZE ) !=0 ) { +printf ("[%s():%u] Incorrect values %p at 0x%x, bulk %u\n" , + __func__, __LINE__, + popped_objs[STACK_SIZE -RTE_STACK_PILE_BULK_SIZE - + i], + i, bulk_sz); +rte_free (popped_objs); +return -1 ; + } + } } }
@@ -152,12 +176,24 @@ test_stack_basic(uint32_t flags) goto fail_test; } - ret =rte_stack_push (s, obj_table,2 *STACK_SIZE ); + ret =rte_stack_push (s, obj_table,STACK_SIZE ); +if (ret ==0 ) { +printf ("[%s():%u] All objects push failed\n" , + __func__, __LINE__); + goto fail_test; + } + ret =rte_stack_push (s, obj_table,STACK_SIZE );if (ret !=0 ) {printf ("[%s():%u] Excess objects push succeeded\n" , __func__, __LINE__); goto fail_test; } + ret =rte_stack_pop (s, obj_table,STACK_SIZE ); +if (ret ==0 ) { +printf ("[%s():%u] All objects pop failed\n" , + __func__, __LINE__); + goto fail_test; + } ret =rte_stack_pop (s, obj_table,1 );if (ret !=0 ) {
@@ -167,8 +203,12 @@ test_stack_basic(uint32_t flags) } ret =0 ; + goto done; fail_test: + ret =-1 ; + +done:rte_stack_free (s);rte_free (obj_table);
@@ -384,5 +424,16 @@ test_lf_stack(void)#endif } +static int +test_pile (void ) +{ +#if defined(RTE_STACK_PILE_SUPPORTED ) +return __test_stack (RTE_STACK_F_PILE ); +#else +return TEST_SKIPPED ; +#endif +} +REGISTER_FAST_TEST (stack_autotest,NOHUGE_SKIP ,ASAN_OK , test_stack);REGISTER_FAST_TEST (stack_lf_autotest,NOHUGE_SKIP ,ASAN_OK , test_lf_stack); +REGISTER_FAST_TEST (stack_pile_autotest,NOHUGE_SKIP ,ASAN_OK , test_pile);
app/test/test_stack_perf.c +13 -2
@@ -14,14 +14,14 @@#include "test.h" #define STACK_NAME "STACK_PERF" -#define MAX_BURST 32 +#define MAX_BURST 512#define STACK_SIZE (RTE_MAX_LCORE * MAX_BURST)/* * Push/pop bulk sizes, marked volatile so they aren't treated as compile-time * constants. */ -static volatile unsigned int bulk_sizes[] = {8 ,MAX_BURST }; +static volatile unsigned int bulk_sizes[] = {1 ,8 ,32 ,MAX_BURST };static RTE_ATOMIC (uint32_t ) lcore_barrier;
@@ -354,5 +354,16 @@ test_lf_stack_perf(void)#endif } +static int +test_pile_perf (void ) +{ +#if defined(RTE_STACK_PILE_SUPPORTED ) +return __test_stack_perf (RTE_STACK_F_PILE ); +#else +return TEST_SKIPPED ; +#endif +} +REGISTER_PERF_TEST (stack_perf_autotest, test_stack_perf);REGISTER_PERF_TEST (stack_lf_perf_autotest, test_lf_stack_perf); +REGISTER_PERF_TEST (stack_pile_perf_autotest, test_pile_perf);
config/rte_config.h +3 -0
@@ -64,6 +64,9 @@#define RTE_MBUF_DEFAULT_MEMPOOL_OPS "ring_mp_mc"/* RTE_MBUF_HISTORY_DEBUG is not set */ +/* stack defines */ +#define RTE_STACK_PILE_BULK_SIZE 32 +/* ether defines */ #define RTE_MAX_QUEUES_PER_PORT 1024#define RTE_ETHDEV_RXTX_CALLBACKS 1
doc/guides/prog_guide/stack_lib.rst +63 -4
95 lines not on this page · raw message
lib/mempool/rte_mempool.h +1 -1
@@ -718,7 +718,7 @@ struct __rte_cache_aligned rte_mempool_ops {rte_mempool_dequeue_contig_blocks_t dequeue_contig_blocks; }; -#define RTE_MEMPOOL_MAX_OPS_IDX 16/**< Max registered ops structs */ +#define RTE_MEMPOOL_MAX_OPS_IDX 32/**< Max registered ops structs */ /** * Structure storing the table of registered ops structs, each of which contain
lib/stack/meson.build +2 -1
@@ -1,7 +1,7 @@ # SPDX-License-Identifier: BSD-3-Clause # Copyright(c) 2019 Intel Corporation -sources = files('rte_stack.c', 'rte_stack_std.c', 'rte_stack_lf.c') +sources = files('rte_stack.c', 'rte_stack_std.c', 'rte_stack_lf.c', 'rte_stack_pile.c') headers = files('rte_stack.h') # subheaders, not for direct inclusion by apps indirect_headers += files(
@@ -10,4 +10,5 @@ indirect_headers += files( 'rte_stack_lf_generic.h', 'rte_stack_lf_c11.h', 'rte_stack_lf_stubs.h', + 'rte_stack_pile.h', )
lib/stack/rte_stack.c +17 -1
@@ -1,5 +1,6 @@/* SPDX-License-Identifier: BSD-3-Clause * Copyright(c) 2019 Intel Corporation +* Copyright(c) 2026 SmartShare Systems */ #include <stdalign.h>
@@ -32,6 +33,8 @@ rte_stack_init(struct rte_stack *s, unsigned int count, uint32_t flags)if (flags &RTE_STACK_F_LF )rte_stack_lf_init (s, count); +else if (flags &RTE_STACK_F_PILE ) +rte_stack_pile_init (s, count);else rte_stack_std_init (s); }
@@ -41,6 +44,8 @@ rte_stack_get_memsize(unsigned int count, uint32_t flags) {if (flags &RTE_STACK_F_LF )return rte_stack_lf_get_memsize (count); +else if (flags &RTE_STACK_F_PILE ) +return rte_stack_pile_get_memsize (count);else return rte_stack_std_get_memsize (count); }
@@ -58,7 +63,11 @@ rte_stack_create(const char *name, unsigned int count, int socket_id,unsigned int sz;int ret; -if (flags & ~(RTE_STACK_F_LF )) { +if (flags & ~(RTE_STACK_F_LF |RTE_STACK_F_PILE )) { +STACK_LOG_ERR ("Unsupported stack flags %#x" , flags); +return NULL ; + } +if ((flags &RTE_STACK_F_LF ) && (flags &RTE_STACK_F_PILE )) {STACK_LOG_ERR ("Unsupported stack flags %#x" , flags);return NULL ; }
@@ -73,6 +82,13 @@ rte_stack_create(const char *name, unsigned int count, int socket_id, return NULL; }#endif +#if !defined(RTE_STACK_PILE_SUPPORTED ) +if (flags &RTE_STACK_F_PILE ) { +STACK_LOG_ERR ("Pile is not supported on your platform" ); + rte_errno =ENOTSUP ; +return NULL ; + } +#endif sz =rte_stack_get_memsize (count, flags);
lib/stack/rte_stack.h +79 -0
139 lines not on this page · raw message
lib/stack/rte_stack_lf.h +1 -0
@@ -79,6 +79,7 @@ __rte_stack_lf_pop(struct rte_stack *s, void **obj_table, unsigned int n)return 0 ;/* Pop n used elements */ +__rte_assume (obj_table !=NULL ); first =__rte_stack_lf_pop_elems (&s->stack_lf.used, n, obj_table, &last);if (unlikely (first ==NULL ))
lib/stack/rte_stack_pile.c new file +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2026 SmartShare Systems +*/ + +#include "rte_stack.h" + +void +rte_stack_pile_init (struct rte_stack *s,unsigned int count) +{ +unsigned int bulk = (count +RTE_STACK_PILE_BULK_SIZE -1 ) /RTE_STACK_PILE_BULK_SIZE ; +struct rte_stack_pile_bulk_elem *bulk_elems = + (struct rte_stack_pile_bulk_elem *)(s->stack_pile.elems); +struct rte_stack_lf_elem *solo_elems = (struct rte_stack_lf_elem *)&bulk_elems[bulk]; +unsigned int i; + +for (i =0 ; i < bulk; i++) +__rte_stack_pile_bulk_push_elems (&s->stack_pile.free_bulk, + &bulk_elems[i], &bulk_elems[i],1 ); +for (i =0 ; i < count; i++) +__rte_stack_lf_push_elems (&s->stack_pile.free_solo, + &solo_elems[i], &solo_elems[i],1 ); +} + +ssize_t +rte_stack_pile_get_memsize (unsigned int count) +{ +unsigned int bulk = (count +RTE_STACK_PILE_BULK_SIZE -1 ) /RTE_STACK_PILE_BULK_SIZE ; + ssize_t sz = offsetof(struct rte_stack, stack_pile.elems); + sz += bulk *sizeof (struct rte_stack_pile_bulk_elem ); + sz += count *sizeof (struct rte_stack_lf_elem ); + sz =RTE_CACHE_LINE_ROUNDUP (sz); + sz +=RTE_CACHE_GUARD_LINES *RTE_CACHE_LINE_SIZE ; + +return sz; +}
lib/stack/rte_stack_pile.h new file +334 -0
334 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 10 lines · Morten Brørup
Added a new high-performance lock-free "pile", using the Stack API. The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile time configurable bulk size is handled gracefully, but not as fast as complete bulks.
Quoted 9 lines
Performance examples,
stack_pile_perf_autotestvs.stack_lf_autotest:On a single core, pushing/popping 1 or 8 objects is similar speed. On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly faster. On four cores, pushing/popping 512 objects is 4x faster.
How does it differ, or what are the benefits vs the standard stack. The LF stack I'd view as a strange edge case, since even with the standard MP/MC ring mempool driver you still have atomics on the fast-path and you will still get stalls if a thread gets context switched out in the middle of a mempool operation.
/Bruce
Quoted 16 lines
From: Bruce Richardson [mailto:[email protected]]
Sent: Wednesday, 12 August 2026 16.34Quoted 12 lines · Morten Brørup
Added a new high-performance lock-free "pile", using the Stack API. The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile time configurable bulk size is handled gracefully, but not as fast as complete bulks.
Performance examples,
stack_pile_perf_autotestvs.stack_lf_autotest:
Quoted 8 lines
Quoted 7 lines
On a single core, pushing/popping 1 or 8 objects is similar speed. On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly faster. On four cores, pushing/popping 512 objects is 4x faster.
How does it differ, or what are the benefits vs the standard stack.
The standard stack is generally faster on single core.
Only pushing/popping 512 objects is slightly faster than the standard stack, but maybe the standard stack could be microoptimized to fix that.
On four cores, it is slightly faster than the standard stack with multiple objects:
stack_perf_autotest
Average cycles per object push/pop (bulk size: 1): 3806.65 Average cycles per object push/pop (bulk size: 8): 554.77 Average cycles per object push/pop (bulk size: 32): 144.70 Average cycles per object push/pop (bulk size: 512): 13.52
stack_pile_perf_autotest
Average cycles per object push/pop (bulk size: 1): 4012.39 Average cycles per object push/pop (bulk size: 8): 459.82 Average cycles per object push/pop (bulk size: 32): 122.40 Average cycles per object push/pop (bulk size: 512): 9.60
Quoted 9 lines
The
LF
stack I'd view as a strange edge case, since even with the standard MP/MC
ring mempool driver you still have atomics on the fast-path and you will
still get stalls if a thread gets context switched out in the middle of a
mempool operation.
Yes, the no-stall guarantee is a key benefit of the lock-free stack and the pile.
Quoted 53 lines · Morten Brørup
Quoted 16 lines
From: Bruce Richardson [mailto:[email protected]]
Sent: Wednesday, 12 August 2026 16.34Quoted 12 lines · Morten Brørup
Added a new high-performance lock-free "pile", using the Stack API. The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile time configurable bulk size is handled gracefully, but not as fast as complete bulks.
Performance examples,
stack_pile_perf_autotestvs.stack_lf_autotest:Quoted 8 lines
Quoted 7 lines
On a single core, pushing/popping 1 or 8 objects is similar speed. On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly faster. On four cores, pushing/popping 512 objects is 4x faster.
How does it differ, or what are the benefits vs the standard stack.
The standard stack is generally faster on single core.
Only pushing/popping 512 objects is slightly faster than the standard stack, but maybe the standard stack could be microoptimized to fix that.On four cores, it is slightly faster than the standard stack with multiple objects:
stack_perf_autotest
Average cycles per object push/pop (bulk size: 1): 3806.65 Average cycles per object push/pop (bulk size: 8): 554.77 Average cycles per object push/pop (bulk size: 32): 144.70 Average cycles per object push/pop (bulk size: 512): 13.52
stack_pile_perf_autotest
Average cycles per object push/pop (bulk size: 1): 4012.39 Average cycles per object push/pop (bulk size: 8): 459.82 Average cycles per object push/pop (bulk size: 32): 122.40 Average cycles per object push/pop (bulk size: 512): 9.60Quoted 9 lines
The
LF
stack I'd view as a strange edge case, since even with the standard MP/MC
ring mempool driver you still have atomics on the fast-path and you will
still get stalls if a thread gets context switched out in the middle of a
mempool operation.
Quoted 2 lines
Yes, the no-stall guarantee is a key benefit of the lock-free stack and the pile.
Ok, that is good. Next question, rather than adding a whole new mempool driver implementation, can we use this to replace one of the existing stack implementations? Do we need to have so many mempool stack drivers generally?
/Bruce
Quoted 67 lines
From: Bruce Richardson [mailto:[email protected]]
Sent: Wednesday, 12 August 2026 18.16Quoted 5 lines · Morten Brørup
Quoted 5 lines
From: Bruce Richardson [mailto:[email protected]]
Sent: Wednesday, 12 August 2026 16.34Quoted 1 line · Morten Brørup
Added a new high-performance lock-free "pile", using the Stack
API.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile
time
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
configurable bulk size is handled gracefully, but not as fast as complete bulks.
Performance examples,
stack_pile_perf_autotestvs.
stack_lf_autotest:Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
On a single core, pushing/popping 1 or 8 objects is similar
speed.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly
faster.
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines
On four cores, pushing/popping 512 objects is 4x faster.
How does it differ, or what are the benefits vs the standard stack.
The standard stack is generally faster on single core. Only pushing/popping 512 objects is slightly faster than the standard
stack, but maybe the standard stack could be microoptimized to fix that.
Quoted 2 lines
On four cores, it is slightly faster than the standard stack with
multiple objects:
Quoted 7 lines
stack_perf_autotest
Average cycles per object push/pop (bulk size: 1): 3806.65 Average cycles per object push/pop (bulk size: 8): 554.77 Average cycles per object push/pop (bulk size: 32): 144.70 Average cycles per object push/pop (bulk size: 512): 13.52Quoted 14 lines
stack_pile_perf_autotest
Average cycles per object push/pop (bulk size: 1): 4012.39 Average cycles per object push/pop (bulk size: 8): 459.82 Average cycles per object push/pop (bulk size: 32): 122.40 Average cycles per object push/pop (bulk size: 512): 9.60Quoted 7 lines
The
LF
stack I'd view as a strange edge case, since even with the standard MP/MC
ring mempool driver you still have atomics on the fast-path and you will
still get stalls if a thread gets context switched out in themiddle of
Quoted 4 lines
Quoted 2 lines
a
mempool operation.Yes, the no-stall guarantee is a key benefit of the lock-free stack
Quoted 8 lines
and the pile.
Quoted 1 line
Ok, that is good. Next question, rather than adding a whole new mempool driver implementation, can we use this to replace one of the existing stack
implementations? Do we need to have so many mempool stack drivers generally?
We could consider replacing the lock-free stack implementation by the pile implementation, but the pile is not strictly LIFO, so it seems risky.
For a mempool holding mbufs, there's probably no harm in the difference. But for a mempool holding some other object type, it could be harmful. I don't know. IMO, not worth the risk.
Good idea, though!
I'm usually in favor of cleaning up. Just not this one. :-)
Quoted 76 lines · Morten Brørup
Quoted 67 lines
From: Bruce Richardson [mailto:[email protected]]
Sent: Wednesday, 12 August 2026 18.16Quoted 5 lines · Morten Brørup
Quoted 5 lines
From: Bruce Richardson [mailto:[email protected]]
Sent: Wednesday, 12 August 2026 16.34Quoted 1 line · Morten Brørup
Added a new high-performance lock-free "pile", using the Stack
API.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile
time
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
configurable bulk size is handled gracefully, but not as fast as complete bulks.
Performance examples,
stack_pile_perf_autotestvs.
stack_lf_autotest:Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
On a single core, pushing/popping 1 or 8 objects is similar
speed.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly
faster.
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines
On four cores, pushing/popping 512 objects is 4x faster.
How does it differ, or what are the benefits vs the standard stack.
The standard stack is generally faster on single core. Only pushing/popping 512 objects is slightly faster than the standard
stack, but maybe the standard stack could be microoptimized to fix that.
Quoted 2 lines
On four cores, it is slightly faster than the standard stack with
multiple objects:
Quoted 7 lines
stack_perf_autotest
Average cycles per object push/pop (bulk size: 1): 3806.65 Average cycles per object push/pop (bulk size: 8): 554.77 Average cycles per object push/pop (bulk size: 32): 144.70 Average cycles per object push/pop (bulk size: 512): 13.52Quoted 14 lines
stack_pile_perf_autotest
Average cycles per object push/pop (bulk size: 1): 4012.39 Average cycles per object push/pop (bulk size: 8): 459.82 Average cycles per object push/pop (bulk size: 32): 122.40 Average cycles per object push/pop (bulk size: 512): 9.60Quoted 7 lines
The
LF
stack I'd view as a strange edge case, since even with the standard MP/MC
ring mempool driver you still have atomics on the fast-path and you will
still get stalls if a thread gets context switched out in themiddle of
Quoted 4 lines
Quoted 2 lines
a
mempool operation.Yes, the no-stall guarantee is a key benefit of the lock-free stack
Quoted 8 lines
and the pile.
Quoted 1 line
Ok, that is good. Next question, rather than adding a whole new mempool driver implementation, can we use this to replace one of the existing stack
implementations? Do we need to have so many mempool stack drivers generally?
Quoted 9 lines
We could consider replacing the lock-free stack implementation by the pile implementation, but the pile is not strictly LIFO, so it seems risky.
For a mempool holding mbufs, there's probably no harm in the difference. But for a mempool holding some other object type, it could be harmful. I don't know. IMO, not worth the risk.
Good idea, though!
I'm usually in favor of cleaning up. Just not this one. :-)
Having not reviewed the code yet, I can't fully comment, but I would look for a strong differentiator before adding yet another stack implementation to DPDK. Just because it's not strictly LIFO doesn't rule-out it being a drop-in replacement for the existing stack_lf implementation, for example, so long as it's pretty close. We have a habit of accumulating lots of different implementations of things with slight variances, leading both to having lots of stuff to maintain and also then to confusion from users as to what to use. Therefore, as we look to have "better" implementations of structures, I believe that we should always replace old with new, rather than just adding new alongside old all the time - unless there is a very strong reason not to.
Hope to review your new implementation in the coming days, and hope to have more feedback then.
Thanks,
/Bruce
Quoted 20 lines · Morten Brørup
Added a new high-performance lock-free "pile", using the Stack API. The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile time configurable bulk size is handled gracefully, but not as fast as complete bulks.
Performance examples,
stack_pile_perf_autotestvs.stack_lf_autotest:On a single core, pushing/popping 1 or 8 objects is similar speed. On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly faster. On four cores, pushing/popping 512 objects is 4x faster.
Signed-off-by Morten Brørup <[email protected]>
Quoted 16 lines
--- app/test/test_stack.c | 71 +++++- app/test/test_stack_perf.c | 15 +- config/rte_config.h | 3 + doc/guides/prog_guide/stack_lib.rst | 67 +++++- lib/mempool/rte_mempool.h | 2 +- lib/stack/meson.build | 3 +- lib/stack/rte_stack.c | 18 +- lib/stack/rte_stack.h | 79 +++++++ lib/stack/rte_stack_lf.h | 1 + lib/stack/rte_stack_pile.c | 35 +++ lib/stack/rte_stack_pile.h | 334 ++++++++++++++++++++++++++++ 11 files changed, 609 insertions(+), 19 deletions(-) create mode 100644 lib/stack/rte_stack_pile.c create mode 100644 lib/stack/rte_stack_pile.h
Looking at this a little closer, and thinking some more, here are some of my further thoughts/ideas on this:
* For most cases using a mempool, I can't see having non-strict LIFO behaviour being an issue, and there is nothing in the mempool API that makes any ordering guarantees about what buffers get given by get/put, and in fact we can't make any guarantees because of the fact of multiple cores doing allocs and frees. Therefore, for the mempool driver, I believe one pre-emptible implementation is enough, so therefore the pile mempool driver should just replace the current LF one.
* For apps which may want to use the stack structs directly, not through a mempool, I can see that having defined ordering behaviour may be beneficial. However, if multiple cores are involved, then we can never guarantee ordering, I believe, so I'm not sure its worth trying to enforce strict LIFO for such cases. [If you need the same elements back in the correct order from a core, then use a regular stack without sharing it].
* IF we decide that we really, really want LIFO ordering across multiple cores - despite the likely random ordering of allocs/frees between those cores, I still think that this implementation should replace the LF stack. If we reverse the order of elements on enqueue (or dequeue) then we should be closer to correct LIFO ordering - and fully lifo if allocs and frees are based on multiples of the burst size.
I suppose for me the main question to be resolved is - do we have scenarios where we a) have multi-core operation on the stacks and b) absolutely must have strict LIFO ordering? In the absense of that, I'd very much be in favour of replacing the existing LF implementation completely with this one.
/Bruce
Quoted 63 lines · Morten Brørup
Quoted 20 lines
Added a new high-performance lock-free "pile", using the Stack API. The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile time configurable bulk size is handled gracefully, but not as fast as complete bulks.
Performance examples,
stack_pile_perf_autotestvs.stack_lf_autotest:On a single core, pushing/popping 1 or 8 objects is similar speed. On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly faster. On four cores, pushing/popping 512 objects is 4x faster.
Signed-off-by Morten Brørup <[email protected]>Quoted 16 lines
--- app/test/test_stack.c | 71 +++++- app/test/test_stack_perf.c | 15 +- config/rte_config.h | 3 + doc/guides/prog_guide/stack_lib.rst | 67 +++++- lib/mempool/rte_mempool.h | 2 +- lib/stack/meson.build | 3 +- lib/stack/rte_stack.c | 18 +- lib/stack/rte_stack.h | 79 +++++++ lib/stack/rte_stack_lf.h | 1 + lib/stack/rte_stack_pile.c | 35 +++ lib/stack/rte_stack_pile.h | 334 ++++++++++++++++++++++++++++ 11 files changed, 609 insertions(+), 19 deletions(-) create mode 100644 lib/stack/rte_stack_pile.c create mode 100644 lib/stack/rte_stack_pile.hLooking at this a little closer, and thinking some more, here are some of my further thoughts/ideas on this:
* For most cases using a mempool, I can't see having non-strict LIFO behaviour being an issue, and there is nothing in the mempool API that makes any ordering guarantees about what buffers get given by get/put, and in fact we can't make any guarantees because of the fact of multiple cores doing allocs and frees. Therefore, for the mempool driver, I believe one pre-emptible implementation is enough, so therefore the pile mempool driver should just replace the current LF one.
* For apps which may want to use the stack structs directly, not through a mempool, I can see that having defined ordering behaviour may be beneficial. However, if multiple cores are involved, then we can never guarantee ordering, I believe, so I'm not sure its worth trying to enforce strict LIFO for such cases. [If you need the same elements back in the correct order from a core, then use a regular stack without sharing it].
* IF we decide that we really, really want LIFO ordering across multiple cores - despite the likely random ordering of allocs/frees between those cores, I still think that this implementation should replace the LF stack. If we reverse the order of elements on enqueue (or dequeue) then we should be closer to correct LIFO ordering - and fully lifo if allocs and frees are based on multiples of the burst size.
Quoted 5 lines
I suppose for me the main question to be resolved is - do we have scenarios where we a) have multi-core operation on the stacks and b) absolutely must have strict LIFO ordering? In the absense of that, I'd very much be in favour of replacing the existing LF implementation completely with this one.
I am agree with Bruce - it is probably worth to replace current lfstack implementation with this new one, as new one is proved to be way much faster.
One more observation: instead of making RTE_STACK_PILE_BULK_SIZE compile time constant, why not to make it configurable variable (at lfstack instance creation time)?
Then in theory we can support current behavior too - user can create a pile with no bulks, and all requests will go via solo code-path.
That way we'll have good perf improvement, while preserving old-behavior compatibility for those who needs it.
Though have to admit, I don't know any off-hand who will need it :)
Quoted 86 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Tuesday, 18 August 2026 10.12Quoted 8 lines · Morten Brørup
Quoted 7 lines
Added a new high-performance lock-free "pile", using the Stack API. The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile
time
Quoted 4 lines
Quoted 4 lines
configurable bulk size is handled gracefully, but not as fast as complete bulks.
Performance examples,
stack_pile_perf_autotestvs.
stack_lf_autotest:Quoted 6 lines
Quoted 6 lines
On a single core, pushing/popping 1 or 8 objects is similar speed. On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly
faster.
Quoted 15 lines
Quoted 15 lines
On four cores, pushing/popping 512 objects is 4x faster.
Signed-off-by Morten Brørup <[email protected]>--- app/test/test_stack.c | 71 +++++- app/test/test_stack_perf.c | 15 +- config/rte_config.h | 3 + doc/guides/prog_guide/stack_lib.rst | 67 +++++- lib/mempool/rte_mempool.h | 2 +- lib/stack/meson.build | 3 +- lib/stack/rte_stack.c | 18 +- lib/stack/rte_stack.h | 79 +++++++ lib/stack/rte_stack_lf.h | 1 + lib/stack/rte_stack_pile.c | 35 +++ lib/stack/rte_stack_pile.h | 334++++++++++++++++++++++++++++
Quoted 5 lines
Quoted 4 lines
11 files changed, 609 insertions(+), 19 deletions(-) create mode 100644 lib/stack/rte_stack_pile.c create mode 100644 lib/stack/rte_stack_pile.hLooking at this a little closer, and thinking some more, here are
some of
Quoted 4 lines
my further thoughts/ideas on this:
* For most cases using a mempool, I can't see having non-strict LIFO behaviour being an issue, and there is nothing in the mempool API
that
Quoted 1 line
makes any ordering guarantees about what buffers get given by
get/put,
Quoted 1 line
and in fact we can't make any guarantees because of the fact of
multiple
Quoted 2 lines
cores doing allocs and frees. Therefore, for the mempool driver, I believe one pre-emptible implementation is enough, so therefore the
pile
Quoted 2 lines
mempool driver should just replace the current LF one.
* For apps which may want to use the stack structs directly, notthrough a
Quoted 2 lines
mempool, I can see that having defined ordering behaviour may be beneficial. However, if multiple cores are involved, then we can
never
Quoted 2 lines
guarantee ordering, I believe, so I'm not sure its worth trying to enforce strict LIFO for such cases. [If you need the same elements
back
Quoted 1 line
in the correct order from a core, then use a regular stack without
sharing
Quoted 2 lines
it].
* IF we decide that we really, really want LIFO ordering acrossmultiple
Quoted 1 line
cores - despite the likely random ordering of allocs/frees between
those
Quoted 2 lines
cores, I still think that this implementation should replace the LF stack. If we reverse the order of elements on enqueue (or dequeue)
then
Quoted 1 line
we should be closer to correct LIFO ordering - and fully lifo if
allocs
Quoted 3 lines
and frees are based on multiples of the burst size.
I suppose for me the main question to be resolved is - do we have
scenarios
Quoted 1 line
where we a) have multi-core operation on the stacks and b) absolutely
must
Quoted 1 line
have strict LIFO ordering? In the absense of that, I'd very much be
Quoted 8 lines
in
Quoted 1 line
favour of replacing the existing LF implementation completely with
this
Quoted 1 line
one.
I am agree with Bruce - it is probably worth to replace current lfstack implementation with this new one,
as new one is proved to be way much faster.
I agree too.
Quoted 4 lines
One more observation: instead of making
RTE_STACK_PILE_BULK_SIZEcompile time constant,
why not to make it configurable variable (at lfstack instance creation time)?
Build time constant allows compiler optimization (e.g. loop unrolling).
Quoted 6 lines
Then in theory we can support current behavior too - user can create a pile with no bulks,
and all requests will go via solo code-path.
That way we'll have good perf improvement, while preserving old- behavior compatibility for those
who needs it.
I'd rather have two purpose-built variants than one variant with an integer parameter which only actual purpose is being used as a Boolean parameter to switch between two different modes of operation.
Quoted 1 line
Though have to admit, I don't know any off-hand who will need it :)
Exactly!
DPDK has many questionable knobs for runtime configurability.
E.g. a lot of the thresholds in the drivers are configurable.
For which purposes?
If they were fixed (or build time configurable), the compiler could optimize more.
If there are use case driven needs for different modes of operation, a Boolean or enum (or an array of flags) would suffice. Like we have discussed for the mempool cache algorithm; the number of use cases with different requirements is limited.
Quoted 24 lines
Added a new high-performance lock-free "pile", using the Stack API. The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile time configurable bulk size is handled gracefully, but not as fast as complete bulks.
Performance examples,
stack_pile_perf_autotestvs.stack_lf_autotest:On a single core, pushing/popping 1 or 8 objects is similar speed. On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly faster. On four cores, pushing/popping 512 objects is 4x faster.
Signed-off-by Morten Brørup <[email protected]>---
...
Quoted 65 lines
lib/stack/rte_stack_pile.h new file +24
-0+ +/** +* Pop several objects from the pile (lock-free, MT-safe). +* +* @param s +* A pointer to the pile structure. +* @param obj_table +* A pointer to a table of void * pointers (objects). +* @param n +* The number of objects to pull from the pile. +* @return +* Actual number of objects popped (either 0 or *n*). +*/ +static __rte_always_inline unsigned int +__rte_stack_pile_pop (struct rte_stack *s, +void **obj_table, +unsigned int n) +{ +RTE_ASSERT (s !=NULL ); +RTE_ASSERT (obj_table !=NULL ); + +struct rte_stack_pile *pile = &s->stack_pile; +struct rte_stack_pile_bulk_elem *bulk_first =NULL , *bulk_last =NULL ; + struct rte_stack_lf_elem *solo_first = NULL, *solo_last = NULL,
*tmp_solo;(quoted diff) +1
-0+ alignas(RTE_CACHE_LINE_SIZE) void*obj_frag[RTE_STACK_PILE_BULK_SIZE];
lib/stack/rte_stack_pile.h new file +13
-0+struct rte_stack_pile_bulk_elem *frag =NULL ; +unsigned int n_bulk = n /RTE_STACK_PILE_BULK_SIZE ; +unsigned int n_solo = n & (RTE_STACK_PILE_BULK_SIZE -1 ); +unsigned int i; + +if (unlikely (n_bulk ==0 )) { +if (unlikely (n_solo ==0 )) +return 0 ; + goto solo; + } + +bulk: +/* Fetch n_bulk * RTE_STACK_PILE_BULK_SIZE objects as bulk elements. */
(quoted diff) +1
-0+ bulk_first = __rte_stack_pile_bulk_pop_elems(&pile->bulk, n_bulk,
obj_table,&bulk_last);lib/stack/rte_stack_pile.h new file +4
-0+if (unlikely (bulk_first ==NULL )) { +/* +* Not available. +* Retry with fewer bulk elements; objects to be fetched as solo elements instead.
lib/stack/rte_stack_pile.h new file +16
-0+*/ + n_solo +=RTE_STACK_PILE_BULK_SIZE ; + n_bulk--; +if (n_bulk >0 ) + goto bulk; +else + goto solo; + } + +if (likely (n_solo ==0 )) + goto done; + +solo: +/* Fetch n_solo objects as solo elements. */ + solo_first = __rte_stack_lf_pop_elems(&pile->solo, n_solo, + &obj_table[n_bulk * RTE_STACK_PILE_BULK_SIZE],
&solo_last);
lib/stack/rte_stack_pile.h line 237
Quoted 2 lines
lib/stack/rte_stack_pile.h new file +2
-0+if (solo_first !=NULL ) + goto done;
A question: does it mean that if user asked to pop just one elem, it can fail If solo list is empty, while there are plenty of elements in bulk section?
Quoted 107 lines
lib/stack/rte_stack_pile.h new file +4
-0+ +/* Solo elements not available. Try fragmentation. */ +if (unlikely (n_solo >=RTE_STACK_PILE_BULK_SIZE )) + goto fail;/* Ran out of bulk elements above. Don't try to fetch one more. */
lib/stack/rte_stack_pile.h new file +15
-0+ +/* Fetch a fragmentation element as a bulk element. */ + frag =__rte_stack_pile_bulk_pop_elems (&pile->bulk,1 , obj_frag,NULL ); +if (unlikely (frag ==NULL )) + goto fail; + +/* Get n_solo objects from the fragmentation element. */ +__rte_assume (n_solo >0 ); +__rte_assume (n_solo <RTE_STACK_PILE_BULK_SIZE ); +for (i =0 ; i < n_solo; i++) + obj_table[n_bulk *RTE_STACK_PILE_BULK_SIZE + i] = obj_frag[i]; + +/* Fetch free elements for the excess objects. */ +__rte_assume (RTE_STACK_PILE_BULK_SIZE - n_solo >0 ); + __rte_assume(RTE_STACK_PILE_BULK_SIZE - n_solo <
RTE_STACK_PILE_BULK_SIZE);lib/stack/rte_stack_pile.h new file +10
-0+ solo_first =__rte_stack_lf_pop_elems (&pile->free_solo, +RTE_STACK_PILE_BULK_SIZE - n_solo,NULL , &solo_last); +if (unlikely (solo_first ==NULL )) + goto fail; + +/* Construct the solo elements from the excess objects. */ + tmp_solo = solo_first; +__rte_assume (n_solo >0 ); +__rte_assume (n_solo <RTE_STACK_PILE_BULK_SIZE ); + for (i = n_solo; i < RTE_STACK_PILE_BULK_SIZE; i++, tmp_solo = tmp_solo-Quoted 1 line
next)
lib/stack/rte_stack_pile.h new file +8
-0+ tmp_solo->data = obj_frag[i]; + +/* Push the excess objects as solo elements. */ +__rte_stack_lf_push_elems (&pile->solo, solo_first, solo_last, +RTE_STACK_PILE_BULK_SIZE - n_solo); + n_solo =0 ; + +/* Add the fragmentation element to the bulk elements, so it can be freed with them. */
lib/stack/rte_stack_pile.h new file +11
-0+if (n_bulk >0 ) + bulk_last->next = frag; +else + bulk_first = frag; + bulk_last = frag; + n_bulk++; + +done: +/* Success. Free the elements. */ +if (n_bulk >0 ) + __rte_stack_pile_bulk_push_elems(&pile->free_bulk, bulk_first,
bulk_last,n_bulk);(quoted diff) +2
-0+ if (n_solo > 0) + __rte_stack_lf_push_elems(&pile->free_solo, solo_first,
solo_last,n_solo);lib/stack/rte_stack_pile.h new file +8
-0+ +return n; + +fail: +/* Failed. Roll back. */ +if (frag !=NULL ) { +/* +* No further action than this is required to roll the fragmentation
lib/stack/rte_stack_pile.h new file +12
-0+* element back into the pile of bulk elements, as the objects in +* the fragmentation element are intact. +*/ +if (n_bulk >0 ) + bulk_last->next = frag; +else + bulk_first = frag; + bulk_last = frag; + n_bulk +=1 ; + } +if (n_bulk >0 ) + __rte_stack_pile_bulk_push_elems(&pile->bulk, bulk_first,
bulk_last,n_bulk);lib/stack/rte_stack_pile.h new file +27
-1+ + return 0; +} + +/** +* @internal Initialize a pile stack. +* +* @param s +* A pointer to the stack structure. +* @param count +* The size of the stack. +*/ +void +rte_stack_pile_init (struct rte_stack *s,unsigned int count); + +/** +* @internal Return the memory required for a pile stack. +* +* @param count +* The size of the stack. +* @return +* The bytes to allocate for a pile stack. +*/ +ssize_t +rte_stack_pile_get_memsize (unsigned int count); + +#endif /* _RTE_STACK_PILE_H_ */ --2.43.0
Quoted 93 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Tuesday, 25 August 2026 09.05Quoted 22 lines
Added a new high-performance lock-free "pile", using the Stack API. The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile time configurable bulk size is handled gracefully, but not as fast as complete bulks.
Performance examples,
stack_pile_perf_autotestvs.stack_lf_autotest:On a single core, pushing/popping 1 or 8 objects is similar speed. On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly faster. On four cores, pushing/popping 512 objects is 4x faster.
Signed-off-by Morten Brørup <[email protected]>---
...
Quoted 13 lines
lib/stack/rte_stack_pile.h new file +13
-0+ +/** +* Pop several objects from the pile (lock-free, MT-safe). +* +* @param s +* A pointer to the pile structure. +* @param obj_table +* A pointer to a table of void * pointers (objects). +* @param n +* The number of objects to pull from the pile. +* @return +* Actual number of objects popped (either 0 or *n*). +*/ lib/stack/rte_stack_pile.h
Quoted 10 lines
lib/stack/rte_stack_pile.h new file +10
-0+static __rte_always_inline unsigned int +__rte_stack_pile_pop (struct rte_stack *s, +void **obj_table, +unsigned int n) +{ +RTE_ASSERT (s !=NULL ); +RTE_ASSERT (obj_table !=NULL ); + +struct rte_stack_pile *pile = &s->stack_pile; + struct rte_stack_pile_bulk_elem *bulk_first = NULL, *bulk_last =NULL;
Quoted 7 lines
(quoted diff) +1
-0+ struct rte_stack_lf_elem *solo_first = NULL, *solo_last = NULL,
*tmp_solo;(quoted diff) +1
-0+ alignas(RTE_CACHE_LINE_SIZE) void*obj_frag[RTE_STACK_PILE_BULK_SIZE];
lib/stack/rte_stack_pile.h new file +3
-0+struct rte_stack_pile_bulk_elem *frag =NULL ; +unsigned int n_bulk = n /RTE_STACK_PILE_BULK_SIZE ; +unsigned int n_solo = n & (RTE_STACK_PILE_BULK_SIZE -1 );lib/stack/rte_stack_pile.h
Quoted 10 lines
lib/stack/rte_stack_pile.h new file +10
-0+unsigned int i; + +if (unlikely (n_bulk ==0 )) { +if (unlikely (n_solo ==0 )) +return 0 ; + goto solo; + } + +bulk: +/* Fetch n_bulk * RTE_STACK_PILE_BULK_SIZE objects as bulk elements.
lib/stack/rte_stack_pile.h
Quoted 7 lines
*/
(quoted diff) +1
-0+ bulk_first = __rte_stack_pile_bulk_pop_elems(&pile->bulk, n_bulk,
obj_table,&bulk_last);lib/stack/rte_stack_pile.h new file +4
-0+if (unlikely (bulk_first ==NULL )) { +/* +* Not available. +* Retry with fewer bulk elements; objects to be fetched as solo
Quoted 17 lines
elements instead.
lib/stack/rte_stack_pile.h new file +16
-0+*/ + n_solo +=RTE_STACK_PILE_BULK_SIZE ; + n_bulk--; +if (n_bulk >0 ) + goto bulk; +else + goto solo; + } + +if (likely (n_solo ==0 )) + goto done; + +solo: +/* Fetch n_solo objects as solo elements. */ + solo_first = __rte_stack_lf_pop_elems(&pile->solo, n_solo, + &obj_table[n_bulk * RTE_STACK_PILE_BULK_SIZE],
Quoted 8 lines
lib/stack/rte_stack_pile.h line 237
Quoted 3 lines
&solo_last);lib/stack/rte_stack_pile.h new file +2
-0+if (solo_first !=NULL ) + goto done;A question: does it mean that if user asked to pop just one elem, it can fail
If solo list is empty, while there are plenty of elements in bulk section?
No.
In that case, it proceeds to fragmentation:
It fetches a bulk element, returns the one asked object to the caller, and puts the remaining objects of the bulk into the solo section.
Quoted 115 lines
lib/stack/rte_stack_pile.h
Quoted 5 lines
lib/stack/rte_stack_pile.h new file +4
-0+ +/* Solo elements not available. Try fragmentation. */ +if (unlikely (n_solo >=RTE_STACK_PILE_BULK_SIZE )) + goto fail;/* Ran out of bulk elements above. Don't try to fetch
lib/stack/rte_stack_pile.h
Quoted 4 lines
one more. */
lib/stack/rte_stack_pile.h new file +3
-0+ +/* Fetch a fragmentation element as a bulk element. */ + frag = __rte_stack_pile_bulk_pop_elems(&pile->bulk, 1, obj_frag,NULL);
lib/stack/rte_stack_pile.h
Quoted 8 lines
lib/stack/rte_stack_pile.h new file +8
-0+if (unlikely (frag ==NULL )) + goto fail; + +/* Get n_solo objects from the fragmentation element. */ +__rte_assume (n_solo >0 ); +__rte_assume (n_solo <RTE_STACK_PILE_BULK_SIZE ); +for (i =0 ; i < n_solo; i++) + obj_table[n_bulk * RTE_STACK_PILE_BULK_SIZE + i] =obj_frag[i];
Quoted 5 lines
lib/stack/rte_stack_pile.h new file +4
-0+ +/* Fetch free elements for the excess objects. */ +__rte_assume (RTE_STACK_PILE_BULK_SIZE - n_solo >0 ); + __rte_assume(RTE_STACK_PILE_BULK_SIZE - n_solo <
RTE_STACK_PILE_BULK_SIZE);lib/stack/rte_stack_pile.h
Quoted 10 lines
lib/stack/rte_stack_pile.h new file +10
-0+ solo_first =__rte_stack_lf_pop_elems (&pile->free_solo, +RTE_STACK_PILE_BULK_SIZE - n_solo,NULL , &solo_last); +if (unlikely (solo_first ==NULL )) + goto fail; + +/* Construct the solo elements from the excess objects. */ + tmp_solo = solo_first; +__rte_assume (n_solo >0 ); +__rte_assume (n_solo <RTE_STACK_PILE_BULK_SIZE ); + for (i = n_solo; i < RTE_STACK_PILE_BULK_SIZE; i++, tmp_solo =tmp_solo-
lib/stack/rte_stack_pile.h
Quoted 9 lines
Quoted 1 line
next)
lib/stack/rte_stack_pile.h new file +8
-0+ tmp_solo->data = obj_frag[i]; + +/* Push the excess objects as solo elements. */ +__rte_stack_lf_push_elems (&pile->solo, solo_first, solo_last, +RTE_STACK_PILE_BULK_SIZE - n_solo); + n_solo =0 ; + +/* Add the fragmentation element to the bulk elements, so it can be
lib/stack/rte_stack_pile.h
Quoted 12 lines
freed with them. */
lib/stack/rte_stack_pile.h new file +11
-0+if (n_bulk >0 ) + bulk_last->next = frag; +else + bulk_first = frag; + bulk_last = frag; + n_bulk++; + +done: +/* Success. Free the elements. */ +if (n_bulk >0 ) + __rte_stack_pile_bulk_push_elems(&pile->free_bulk,
bulk_first,Quoted 13 lines
bulk_last,n_bulk);(quoted diff) +2
-0+ if (n_solo > 0) + __rte_stack_lf_push_elems(&pile->free_solo, solo_first,
solo_last,n_solo);lib/stack/rte_stack_pile.h new file +8
-0+ +return n; + +fail: +/* Failed. Roll back. */ +if (frag !=NULL ) { +/* +* No further action than this is required to roll the fragmentation
Review
Quoted 1 line
(quoted diff) +1
-0+ * element back into the pile of bulk elements, as theobjects in
Quoted 41 lines
lib/stack/rte_stack_pile.h new file +11
-0+* the fragmentation element are intact. +*/ +if (n_bulk >0 ) + bulk_last->next = frag; +else + bulk_first = frag; + bulk_last = frag; + n_bulk +=1 ; + } +if (n_bulk >0 ) + __rte_stack_pile_bulk_push_elems(&pile->bulk, bulk_first,
bulk_last,n_bulk);lib/stack/rte_stack_pile.h new file +27
-1+ + return 0; +} + +/** +* @internal Initialize a pile stack. +* +* @param s +* A pointer to the stack structure. +* @param count +* The size of the stack. +*/ +void +rte_stack_pile_init (struct rte_stack *s,unsigned int count); + +/** +* @internal Return the memory required for a pile stack. +* +* @param count +* The size of the stack. +* @return +* The bytes to allocate for a pile stack. +*/ +ssize_t +rte_stack_pile_get_memsize (unsigned int count); + +#endif /* _RTE_STACK_PILE_H_ */ --2.43.0
Quoted 100 lines
Quoted 91 lines
Quoted 22 lines
Added a new high-performance lock-free "pile", using the Stack API. The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile time configurable bulk size is handled gracefully, but not as fast as complete bulks.
Performance examples,
stack_pile_perf_autotestvs.stack_lf_autotest:On a single core, pushing/popping 1 or 8 objects is similar speed. On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly faster. On four cores, pushing/popping 512 objects is 4x faster.
Signed-off-by Morten Brørup <[email protected]>---
...
Quoted 13 lines
lib/stack/rte_stack_pile.h new file +13
-0+ +/** +* Pop several objects from the pile (lock-free, MT-safe). +* +* @param s +* A pointer to the pile structure. +* @param obj_table +* A pointer to a table of void * pointers (objects). +* @param n +* The number of objects to pull from the pile. +* @return +* Actual number of objects popped (either 0 or *n*). +*/ lib/stack/rte_stack_pile.h
Quoted 10 lines
lib/stack/rte_stack_pile.h new file +10
-0+static __rte_always_inline unsigned int +__rte_stack_pile_pop (struct rte_stack *s, +void **obj_table, +unsigned int n) +{ +RTE_ASSERT (s !=NULL ); +RTE_ASSERT (obj_table !=NULL ); + +struct rte_stack_pile *pile = &s->stack_pile; + struct rte_stack_pile_bulk_elem *bulk_first = NULL, *bulk_last =NULL;
Quoted 7 lines
(quoted diff) +1
-0+ struct rte_stack_lf_elem *solo_first = NULL, *solo_last = NULL,
*tmp_solo;(quoted diff) +1
-0+ alignas(RTE_CACHE_LINE_SIZE) void*obj_frag[RTE_STACK_PILE_BULK_SIZE];
lib/stack/rte_stack_pile.h new file +3
-0+struct rte_stack_pile_bulk_elem *frag =NULL ; +unsigned int n_bulk = n /RTE_STACK_PILE_BULK_SIZE ; +unsigned int n_solo = n & (RTE_STACK_PILE_BULK_SIZE -1 );lib/stack/rte_stack_pile.h
Quoted 10 lines
lib/stack/rte_stack_pile.h new file +10
-0+unsigned int i; + +if (unlikely (n_bulk ==0 )) { +if (unlikely (n_solo ==0 )) +return 0 ; + goto solo; + } + +bulk: +/* Fetch n_bulk * RTE_STACK_PILE_BULK_SIZE objects as bulk elements.
lib/stack/rte_stack_pile.h
Quoted 7 lines
*/
(quoted diff) +1
-0+ bulk_first = __rte_stack_pile_bulk_pop_elems(&pile->bulk, n_bulk,
obj_table,&bulk_last);lib/stack/rte_stack_pile.h new file +4
-0+if (unlikely (bulk_first ==NULL )) { +/* +* Not available. +* Retry with fewer bulk elements; objects to be fetched as solo
Quoted 17 lines
elements instead.
lib/stack/rte_stack_pile.h new file +16
-0+*/ + n_solo +=RTE_STACK_PILE_BULK_SIZE ; + n_bulk--; +if (n_bulk >0 ) + goto bulk; +else + goto solo; + } + +if (likely (n_solo ==0 )) + goto done; + +solo: +/* Fetch n_solo objects as solo elements. */ + solo_first = __rte_stack_lf_pop_elems(&pile->solo, n_solo, + &obj_table[n_bulk * RTE_STACK_PILE_BULK_SIZE],Quoted 8 lines
lib/stack/rte_stack_pile.h line 237
Quoted 3 lines
&solo_last);lib/stack/rte_stack_pile.h new file +2
-0+if (solo_first !=NULL ) + goto done;A question: does it mean that if user asked to pop just one elem, it can fail
If solo list is empty, while there are plenty of elements in bulk section?
Quoted 4 lines
No.
In that case, it proceeds to fragmentation:
It fetches a bulk element, returns the one asked object to the caller, and puts the remaining objects of the bulk into the solo section.
Yep, now I do see fragmentation block below. My bad, I didn't read till the end of the function.
Quoted 115 lines
Quoted 115 lines
lib/stack/rte_stack_pile.h
Quoted 5 lines
lib/stack/rte_stack_pile.h new file +4
-0+ +/* Solo elements not available. Try fragmentation. */ +if (unlikely (n_solo >=RTE_STACK_PILE_BULK_SIZE )) + goto fail;/* Ran out of bulk elements above. Don't try to fetch
lib/stack/rte_stack_pile.h
Quoted 4 lines
one more. */
lib/stack/rte_stack_pile.h new file +3
-0+ +/* Fetch a fragmentation element as a bulk element. */ + frag = __rte_stack_pile_bulk_pop_elems(&pile->bulk, 1, obj_frag,NULL);
lib/stack/rte_stack_pile.h
Quoted 8 lines
lib/stack/rte_stack_pile.h new file +8
-0+if (unlikely (frag ==NULL )) + goto fail; + +/* Get n_solo objects from the fragmentation element. */ +__rte_assume (n_solo >0 ); +__rte_assume (n_solo <RTE_STACK_PILE_BULK_SIZE ); +for (i =0 ; i < n_solo; i++) + obj_table[n_bulk * RTE_STACK_PILE_BULK_SIZE + i] =obj_frag[i];
Quoted 5 lines
lib/stack/rte_stack_pile.h new file +4
-0+ +/* Fetch free elements for the excess objects. */ +__rte_assume (RTE_STACK_PILE_BULK_SIZE - n_solo >0 ); + __rte_assume(RTE_STACK_PILE_BULK_SIZE - n_solo <
RTE_STACK_PILE_BULK_SIZE);lib/stack/rte_stack_pile.h
Quoted 10 lines
lib/stack/rte_stack_pile.h new file +10
-0+ solo_first =__rte_stack_lf_pop_elems (&pile->free_solo, +RTE_STACK_PILE_BULK_SIZE - n_solo,NULL , &solo_last); +if (unlikely (solo_first ==NULL )) + goto fail; + +/* Construct the solo elements from the excess objects. */ + tmp_solo = solo_first; +__rte_assume (n_solo >0 ); +__rte_assume (n_solo <RTE_STACK_PILE_BULK_SIZE ); + for (i = n_solo; i < RTE_STACK_PILE_BULK_SIZE; i++, tmp_solo =tmp_solo-
lib/stack/rte_stack_pile.h
Quoted 9 lines
Quoted 1 line
next)
lib/stack/rte_stack_pile.h new file +8
-0+ tmp_solo->data = obj_frag[i]; + +/* Push the excess objects as solo elements. */ +__rte_stack_lf_push_elems (&pile->solo, solo_first, solo_last, +RTE_STACK_PILE_BULK_SIZE - n_solo); + n_solo =0 ; + +/* Add the fragmentation element to the bulk elements, so it can be
lib/stack/rte_stack_pile.h
Quoted 12 lines
freed with them. */
lib/stack/rte_stack_pile.h new file +11
-0+if (n_bulk >0 ) + bulk_last->next = frag; +else + bulk_first = frag; + bulk_last = frag; + n_bulk++; + +done: +/* Success. Free the elements. */ +if (n_bulk >0 ) + __rte_stack_pile_bulk_push_elems(&pile->free_bulk,
bulk_first,Quoted 13 lines
bulk_last,n_bulk);(quoted diff) +2
-0+ if (n_solo > 0) + __rte_stack_lf_push_elems(&pile->free_solo, solo_first,
solo_last,n_solo);lib/stack/rte_stack_pile.h new file +8
-0+ +return n; + +fail: +/* Failed. Roll back. */ +if (frag !=NULL ) { +/* +* No further action than this is required to roll the fragmentation
Review
Quoted 1 line
(quoted diff) +1
-0+ * element back into the pile of bulk elements, as theobjects in
Quoted 41 lines
lib/stack/rte_stack_pile.h new file +11
-0+* the fragmentation element are intact. +*/ +if (n_bulk >0 ) + bulk_last->next = frag; +else + bulk_first = frag; + bulk_last = frag; + n_bulk +=1 ; + } +if (n_bulk >0 ) + __rte_stack_pile_bulk_push_elems(&pile->bulk, bulk_first,
bulk_last,n_bulk);lib/stack/rte_stack_pile.h new file +27
-1+ + return 0; +} + +/** +* @internal Initialize a pile stack. +* +* @param s +* A pointer to the stack structure. +* @param count +* The size of the stack. +*/ +void +rte_stack_pile_init (struct rte_stack *s,unsigned int count); + +/** +* @internal Return the memory required for a pile stack. +* +* @param count +* The size of the stack. +* @return +* The bytes to allocate for a pile stack. +*/ +ssize_t +rte_stack_pile_get_memsize (unsigned int count); + +#endif /* _RTE_STACK_PILE_H_ */ --2.43.0
Quoted 38 lines
lib/stack/rte_stack.h +14
-0@@ -1,5 +1,6 @@/* SPDX-License-Identifier: BSD-3-Clause * Copyright(c) 2019 Intel Corporation +* Copyright(c) 2026 SmartShare Systems */ /** @@ -28,11 +29,47 @@#define RTE_STACK_NAMESIZE (RTE_MEMZONE_NAMESIZE - \ sizeof(RTE_STACK_MZ_PREFIX) + 1) +static_assert (((sizeof (void *) *RTE_STACK_PILE_BULK_SIZE ) &RTE_CACHE_LINE_MASK ) ==0 , +"Pile bulk size must be divisible by CPU cache line size" ); +static_assert (RTE_IS_POWER_OF_2 (RTE_STACK_PILE_BULK_SIZE ), +"Pile bulk size must be power of 2" ); + +/* Note: Also used as solo (single-object) pile element. */ struct rte_stack_lf_elem {void *data;/**< Data pointer */ struct rte_stack_lf_elem *next;/**< Next pointer */ }; +/* +* Bulk (multi-object) pile element. +* Inherited from the rte_stack_lf_elem (single-object) class, +* and extended with an array for holding a bulk of object pointers. +*/ +struct rte_stack_pile_bulk_elem { +/* The first part must be ABI compatible with the rte_stack_lf_elem parent class. */
lib/stack/rte_stack.h +1
-0+void *data;/**< Unused, for rte_stack_lf_elem
Quoted 1 line
compatibility */
Why simply not:
struct rte_stack_pile_bulk_elem {
struct rte_stack_lf_elem elem;
....
};?
That way you just need a static_assert that elem always first (offset == 0). Wouldn't need that clumsy asserts below.
Quoted 94 lines
lib/stack/rte_stack.h +4
-0+struct rte_stack_pile_bulk_elem *next;/**< Next pointer */ +/* The second part differs. */ + alignas(RTE_CACHE_LINE_SIZE ) +void *objs[RTE_STACK_PILE_BULK_SIZE ];/**< Bulk (multi-object) pointers */
lib/stack/rte_stack.h +43
-0+}; + +static_assert (sizeof (struct rte_stack_lf_elem ) == +sizeof (struct rte_stack_lf_elem *) +sizeof (void *), +"Parent type has changed" ); +static_assert(RTE_SIZEOF_FIELD(struct rte_stack_lf_elem, next) == + RTE_SIZEOF_FIELD(struct rte_stack_pile_bulk_elem, next), + "Inherited type mismatch"); +static_assert (offsetof(struct rte_stack_lf_elem , next) == + offsetof(struct rte_stack_pile_bulk_elem , next), +"Inherited type mismatch" ); +static_assert(RTE_SIZEOF_FIELD(struct rte_stack_lf_elem, data) == + RTE_SIZEOF_FIELD(struct rte_stack_pile_bulk_elem, data), + "Inherited type mismatch"); +static_assert (offsetof(struct rte_stack_lf_elem , data) == + offsetof(struct rte_stack_pile_bulk_elem , data), +"Inherited type mismatch" ); + struct __rte_aligned(16) rte_stack_lf_head {struct rte_stack_lf_elem *top;/**< Stack top */ uint64_t cnt;/**< Modification counter for avoiding ABA problem */ @@ -51,12 +88,36 @@ struct rte_stack_lf_list {struct rte_stack_lf {/** LIFO list of elements */ alignas(RTE_CACHE_LINE_SIZE )struct rte_stack_lf_list used; +RTE_CACHE_GUARD ;/** LIFO list of free elements */ alignas(RTE_CACHE_LINE_SIZE )struct rte_stack_lf_list free; +RTE_CACHE_GUARD ;/** LIFO elements */ alignas(RTE_CACHE_LINE_SIZE )struct rte_stack_lf_elem elems[]; }; +/* Pile structure containing three lock-free LIFO-like lists: +* - A list of elements, each element holding a bulk of pointers to objects. +* - A list of elements, each element holding one pointer to an object. +* - A list of free linked-list elements. +*/ +struct rte_stack_pile { +/** LIFO list of bulk (multi-object) elements */ + alignas(RTE_CACHE_LINE_SIZE )struct rte_stack_lf_list bulk; +RTE_CACHE_GUARD ; +/** LIFO list of solo (single-object) elements */ + alignas(RTE_CACHE_LINE_SIZE )struct rte_stack_lf_list solo; +RTE_CACHE_GUARD ; +/** LIFO list of free bulk elements */ + alignas(RTE_CACHE_LINE_SIZE )struct rte_stack_lf_list free_bulk; +RTE_CACHE_GUARD ; +/** LIFO list of free solo elements */ + alignas(RTE_CACHE_LINE_SIZE )struct rte_stack_lf_list free_solo; +RTE_CACHE_GUARD ; +/** LIFO elements follow, first bulk, then solo */ + alignas(RTE_CACHE_LINE_SIZE )void *elems[]; +}; +/* Structure containing the LIFO, its current length, and a lock for mutual * exclusion. */ @@ -78,6 +139,7 @@ struct __rte_cache_aligned rte_stack {uint32_t flags;/**< Flags supplied at creation. */ union {struct rte_stack_lf stack_lf;/**< Lock-free LIFO structure. */ +struct rte_stack_pile stack_pile;/**< Lock-free pile (LIFO-like) structure. */
lib/stack/rte_stack.h +7
-0struct rte_stack_std stack_std;/**< LIFO structure. */ }; };@@ -88,8 +150,19 @@ struct __rte_cache_aligned rte_stack {*/ #define RTE_STACK_F_LF 0x0001 +/** +* The stack-like pile uses lock-free push and pop functions. +* It is optimized for bulks of objects, and is not strictly LIFO. +* This flag is only supported on x86_64 or arm64 platforms, currently. +* +* @warning +* @b EXPERIMENTAL: this API may change, or be removed, without prior notice.
+ */lib/stack/rte_stack.h +3
-0+#define RTE_STACK_F_PILE 0x0002 +#include "rte_stack_std.h" #include "rte_stack_lf.h" +#include "rte_stack_pile.h" #ifdef __cplusplusextern "C" {
lib/stack/rte_stack.h rte_stack_push(struct rte_stack *s, void * const · line 193
Quoted 6 lines
lib/stack/rte_stack.h +2
-0@@ -115,6 +188,8 @@ rte_stack_push(struct rte_stack *s, void * const *obj_table, unsigned int n)if (s->flags &RTE_STACK_F_LF )return __rte_stack_lf_push (s, obj_table, n); +else if (s->flags &RTE_STACK_F_PILE ) +return __rte_stack_pile_push (s, obj_table, n);
I understand that you followed the common pattern, but might be consider putting rte_scak_pile-* funcions into .c?
pile push/pop functions are quite big and heavily branched, do
we really want all that code to be always inlined for any stack pushs/pops? Again, we can still keep inlned version in internal header for mempool driver usage.
Quoted 322 lines
lib/stack/rte_stack.h +0
-0else return __rte_stack_std_push (s, obj_table, n);}
lib/stack/rte_stack.h +2
-0@@ -139,6 +214,8 @@ rte_stack_pop(struct rte_stack *s, void **obj_table, unsigned int n)if (s->flags &RTE_STACK_F_LF )return __rte_stack_lf_pop (s, obj_table, n); +else if (s->flags &RTE_STACK_F_PILE ) +return __rte_stack_pile_pop (s, obj_table, n);else return __rte_stack_std_pop (s, obj_table, n);}
lib/stack/rte_stack.h +2
-0@@ -158,6 +235,8 @@ rte_stack_count(struct rte_stack *s)if (s->flags &RTE_STACK_F_LF )return __rte_stack_lf_count (s); +else if (s->flags &RTE_STACK_F_PILE ) +return __rte_stack_pile_count (s);else return __rte_stack_std_count (s); }lib/stack/rte_stack_lf.h +1
-0@@ -79,6 +79,7 @@ __rte_stack_lf_pop(struct rte_stack *s, void **obj_table, unsigned int n)return 0 ;/* Pop n used elements */ +__rte_assume (obj_table !=NULL ); first =__rte_stack_lf_pop_elems (&s->stack_lf.used, n, obj_table, &last);lib/stack/rte_stack_lf.h +0
-0if (unlikely (first ==NULL ))lib/stack/rte_stack_pile.c new file +10
-0@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2026 SmartShare Systems +*/ + +#include "rte_stack.h" + +void +rte_stack_pile_init (struct rte_stack *s,unsigned int count) +{ + unsigned int bulk = (count + RTE_STACK_PILE_BULK_SIZE - 1) /
RTE_STACK_PILE_BULK_SIZE;lib/stack/rte_stack_pile.c new file +3
-0+struct rte_stack_pile_bulk_elem *bulk_elems = + (struct rte_stack_pile_bulk_elem *)(s->stack_pile.elems); + struct rte_stack_lf_elem *solo_elems = (struct rte_stack_lf_elem*)&bulk_elems[bulk];
lib/stack/rte_stack_pile.c new file +14
-0+unsigned int i; + +for (i =0 ; i < bulk; i++) +__rte_stack_pile_bulk_push_elems (&s->stack_pile.free_bulk, + &bulk_elems[i], &bulk_elems[i],1 ); +for (i =0 ; i < count; i++) +__rte_stack_lf_push_elems (&s->stack_pile.free_solo, + &solo_elems[i], &solo_elems[i],1 ); +} + +ssize_t +rte_stack_pile_get_memsize (unsigned int count) +{ + unsigned int bulk = (count + RTE_STACK_PILE_BULK_SIZE - 1) /
RTE_STACK_PILE_BULK_SIZE;lib/stack/rte_stack_pile.c new file +8
-0+ ssize_t sz = offsetof(struct rte_stack, stack_pile.elems); + sz += bulk *sizeof (struct rte_stack_pile_bulk_elem ); + sz += count *sizeof (struct rte_stack_lf_elem ); + sz =RTE_CACHE_LINE_ROUNDUP (sz); + sz +=RTE_CACHE_GUARD_LINES *RTE_CACHE_LINE_SIZE ; + +return sz; +}lib/stack/rte_stack_pile.h new file +28
-0@@ -0,0 +1,334 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2026 SmartShare Systems +*/ + +#ifndef _RTE_STACK_PILE_H_ +#define _RTE_STACK_PILE_H_ + +#include <rte_memcpy.h> + +#include "rte_stack_lf.h" +#ifdef RTE_STACK_LF_SUPPORTED +/** +* Indicates that RTE_STACK_F_PILE is supported. +*/ +#define RTE_STACK_PILE_SUPPORTED +#endif + +static __rte_always_inline unsigned int +__rte_stack_pile_count (struct rte_stack *s) +{ +/* stack_lf_push() and stack_lf_pop() do not update the list's contents +* and stack_lf->len atomically, which can cause the list to appear +* shorter than it actually is if this function is called while other +* threads are modifying the list. +* +* However, given the inherently approximate nature of the get_count +* callback -- even if the list and its size were updated atomically, +* the size could change between when get_count executes and when the
lib/stack/rte_stack_pile.h new file +10
-0+* value is returned to the caller -- this is acceptable. +* +* The stack_lf->len updates are placed such that the list may appear to +* have fewer elements than it does, but will never appear to have more +* elements. If the mempool is near-empty to the point that this is a +* concern, the user should consider increasing the mempool size. +*/ +#ifdef RTE_USE_C11_MEM_MODEL + return RTE_MIN((unsigned int)s->capacity, + (unsigned int)rte_atomic_load_explicit(&s-Quoted 1 line
stack_pile.bulk.len,
lib/stack/rte_stack_pile.h new file +1
-0+ rte_memory_order_relaxed) *
RTE_STACK_PILE_BULK_SIZE+lib/stack/rte_stack_pile.h new file +1
-0+ (unsigned int)rte_atomic_load_explicit(&s-Quoted 1 line
stack_pile.solo.len,
lib/stack/rte_stack_pile.h new file +5
-0+ rte_memory_order_relaxed)); +#else /* FIXME: Remove if removed from lock-free stack. */ + /* NOTE: review for potential ordering optimization */ + return RTE_MIN((unsigned int)s->capacity, + (unsigned int)rte_atomic_load_explicit(&s-Quoted 1 line
stack_pile.bulk.len,
lib/stack/rte_stack_pile.h new file +1
-0+ rte_memory_order_seq_cst) *
RTE_STACK_PILE_BULK_SIZE+lib/stack/rte_stack_pile.h new file +1
-0+ (unsigned int)rte_atomic_load_explicit(&s-Quoted 1 line
stack_pile.solo.len,
lib/stack/rte_stack_pile.h new file +23
-0+ rte_memory_order_seq_cst)); +#endif +} + +static __rte_always_inline void +__rte_stack_pile_bulk_push_elems (struct rte_stack_lf_list *list, +struct rte_stack_pile_bulk_elem *first, +struct rte_stack_pile_bulk_elem *last, +unsigned int num) +{ +__rte_stack_lf_push_elems (list, + (struct rte_stack_lf_elem *)first, + (struct rte_stack_lf_elem *)last, + num); +} + +static __rte_always_inline struct rte_stack_pile_bulk_elem * +__rte_stack_pile_bulk_pop_elems (struct rte_stack_lf_list *list, +unsigned int num, +void **obj_table, +struct rte_stack_pile_bulk_elem **last) +{ + struct rte_stack_pile_bulk_elem *first = (struct rte_stack_pile_bulk_elem*)
lib/stack/rte_stack_pile.h new file +16
-0+__rte_stack_lf_pop_elems (list, num,NULL , + (struct rte_stack_lf_elem **)last); +if (first ==NULL ) +return NULL ; + +if (obj_table !=NULL ) { +/* +* Traverse the list to copy the bulks. +* Note: +* Done here to minimize the time spent in the retry loop in +* __rte_stack_lf_pop_elems(), +* and to avoid modifying __rte_stack_lf_pop_elems(). +*/ +struct rte_stack_pile_bulk_elem *tmp = first; +for (unsigned int i =0 ; i < num; i++, tmp = tmp->next) + rte_memcpy(&obj_table[i * RTE_STACK_PILE_BULK_SIZE],
tmp->objs,lib/stack/rte_stack_pile.h new file +1
-0+ sizeof(void *) *
RTE_STACK_PILE_BULK_SIZE);lib/stack/rte_stack_pile.h new file +27
-0+ } + +return first; +} + +/** +* Push several objects on the pile (lock-free, MT-safe). +* +* @param s +* A pointer to the pile structure. +* @param obj_table +* A pointer to a table of void * pointers (objects). +* @param n +* The number of objects to push on the pile from the obj_table. +* @return +* Actual number of objects pushed (either 0 or *n*). +*/ +static __rte_always_inline unsigned int +__rte_stack_pile_push (struct rte_stack *s, +void *const *obj_table, +unsigned int n) +{ +RTE_ASSERT (s !=NULL ); +RTE_ASSERT (obj_table !=NULL ); + +struct rte_stack_pile *pile = &s->stack_pile; + struct rte_stack_pile_bulk_elem *bulk_first = NULL, *bulk_last = NULL,
*tmp_bulk;lib/stack/rte_stack_pile.h new file +1
-0+ struct rte_stack_lf_elem *solo_first = NULL, *solo_last = NULL,
*tmp_solo;lib/stack/rte_stack_pile.h new file +12
-0+unsigned int n_bulk = n /RTE_STACK_PILE_BULK_SIZE ; +unsigned int n_solo = n & (RTE_STACK_PILE_BULK_SIZE -1 ); +unsigned int i; + +if (unlikely (n_bulk ==0 )) { +if (unlikely (n_solo ==0 )) +return 0 ; + goto solo; + } + +/* Allocate n_bulk elements from the free list. */ + bulk_first = __rte_stack_pile_bulk_pop_elems(&pile->free_bulk, n_bulk,NULL,
&bulk_last);lib/stack/rte_stack_pile.h new file +9
-0+if (unlikely (bulk_first ==NULL )) +return 0 ;/* Failed. */ + +if (likely (n_solo ==0 )) + goto bulk; + +solo: +/* Allocate n_solo elements from the free list. */ + solo_first = __rte_stack_lf_pop_elems(&pile->free_solo, n_solo, NULL,
&solo_last);lib/stack/rte_stack_pile.h new file +29
-0+if (unlikely (solo_first ==NULL )) { +/* Failed. Roll back. */ +if (n_bulk >0 ) +__rte_stack_pile_bulk_push_elems (&pile->free_bulk, + bulk_first, bulk_last, n_bulk); +return 0 ; + } + +/* +* Construct the solo elements. +* Copy objects in reverse order. +*/ + tmp_solo = solo_first; +__rte_assume (n_solo >0 ); +__rte_assume (n_solo <RTE_STACK_PILE_BULK_SIZE ); +for (i =0 ; i < n_solo; i++, tmp_solo = tmp_solo->next) + tmp_solo->data = obj_table[n_bulk *RTE_STACK_PILE_BULK_SIZE + n_solo - i - 1]; + +/* Push them to the solo list. */ +__rte_stack_lf_push_elems (&pile->solo, solo_first, solo_last, n_solo); + +if (unlikely (n_bulk ==0 )) +return n;/* Done. */ + +bulk: +/* +* Construct the bulk elements. +* Copy bulks in reverse order, but ignore the object order within each bulk.
lib/stack/rte_stack_pile.h new file +5
-0+*/ + tmp_bulk = bulk_first; +__rte_assume (n_bulk >0 ); +for (i =0 ; i < n_bulk; i++, tmp_bulk = tmp_bulk->next) + rte_memcpy(tmp_bulk->objs, &obj_table[(n_bulk - i - 1) *
RTE_STACK_PILE_BULK_SIZE],lib/stack/rte_stack_pile.h new file +4
-0+ sizeof(void *) * RTE_STACK_PILE_BULK_SIZE); + +/* Push them to the bulk list. */ + __rte_stack_pile_bulk_push_elems(&pile->bulk, bulk_first, bulk_last,
n_bulk);lib/stack/rte_stack_pile.h new file +27
-0+ +return n; +} + +/** +* Pop several objects from the pile (lock-free, MT-safe). +* +* @param s +* A pointer to the pile structure. +* @param obj_table +* A pointer to a table of void * pointers (objects). +* @param n +* The number of objects to pull from the pile. +* @return +* Actual number of objects popped (either 0 or *n*). +*/ +static __rte_always_inline unsigned int +__rte_stack_pile_pop (struct rte_stack *s, +void **obj_table, +unsigned int n) +{ +RTE_ASSERT (s !=NULL ); +RTE_ASSERT (obj_table !=NULL ); + +struct rte_stack_pile *pile = &s->stack_pile; +struct rte_stack_pile_bulk_elem *bulk_first =NULL , *bulk_last =NULL ; + struct rte_stack_lf_elem *solo_first = NULL, *solo_last = NULL,
*tmp_solo;lib/stack/rte_stack_pile.h new file +1
-0+ alignas(RTE_CACHE_LINE_SIZE) void*obj_frag[RTE_STACK_PILE_BULK_SIZE];
lib/stack/rte_stack_pile.h new file +13
-0+struct rte_stack_pile_bulk_elem *frag =NULL ; +unsigned int n_bulk = n /RTE_STACK_PILE_BULK_SIZE ; +unsigned int n_solo = n & (RTE_STACK_PILE_BULK_SIZE -1 ); +unsigned int i; + +if (unlikely (n_bulk ==0 )) { +if (unlikely (n_solo ==0 )) +return 0 ; + goto solo; + } + +bulk: +/* Fetch n_bulk * RTE_STACK_PILE_BULK_SIZE objects as bulk elements. */
lib/stack/rte_stack_pile.h new file +1
-0+ bulk_first = __rte_stack_pile_bulk_pop_elems(&pile->bulk, n_bulk,
obj_table,&bulk_last);
lib/stack/rte_stack_pile.h line 216
Quoted 1 line
lib/stack/rte_stack_pile.h new file +1
-0+if (unlikely (bulk_first ==NULL )) {
That's quite heavy construction...
If it fail, might be go straight to solo is better?
As an alternative: introduction _burst_ version of _lf_pop_elems - it would really help here:
bot in terms of performance and code compaction.
Quoted 27 lines
lib/stack/rte_stack_pile.h new file +3
-0+/* +* Not available. +* Retry with fewer bulk elements; objects to be fetched as solo elements instead.
lib/stack/rte_stack_pile.h new file +16
-0+*/ + n_solo +=RTE_STACK_PILE_BULK_SIZE ; + n_bulk--; +if (n_bulk >0 ) + goto bulk; +else + goto solo; + } + +if (likely (n_solo ==0 )) + goto done; + +solo: +/* Fetch n_solo objects as solo elements. */ + solo_first = __rte_stack_lf_pop_elems(&pile->solo, n_solo, + &obj_table[n_bulk * RTE_STACK_PILE_BULK_SIZE],
&solo_last);lib/stack/rte_stack_pile.h new file +6
-0+if (solo_first !=NULL ) + goto done; + +/* Solo elements not available. Try fragmentation. */ +if (unlikely (n_solo >=RTE_STACK_PILE_BULK_SIZE )) + goto fail;/* Ran out of bulk elements above. Don't try to fetch
Quoted 2 lines
one more. */
+
Fragmentation procedure is quite big piece of code, my suggestion would be to put it into separate inline helper function (for readability and ease to maintain). As a generic comment - there are a lot fo gotos, probably some of them unavoidable, but makes code really hard to follow, please consider splitting big functions into smaller chunks whenever possible.
Quoted 101 lines
lib/stack/rte_stack_pile.h new file +14
-0+/* Fetch a fragmentation element as a bulk element. */ + frag =__rte_stack_pile_bulk_pop_elems (&pile->bulk,1 , obj_frag,NULL ); +if (unlikely (frag ==NULL )) + goto fail; + +/* Get n_solo objects from the fragmentation element. */ +__rte_assume (n_solo >0 ); +__rte_assume (n_solo <RTE_STACK_PILE_BULK_SIZE ); +for (i =0 ; i < n_solo; i++) + obj_table[n_bulk *RTE_STACK_PILE_BULK_SIZE + i] = obj_frag[i]; + +/* Fetch free elements for the excess objects. */ +__rte_assume (RTE_STACK_PILE_BULK_SIZE - n_solo >0 ); + __rte_assume(RTE_STACK_PILE_BULK_SIZE - n_solo <
RTE_STACK_PILE_BULK_SIZE);lib/stack/rte_stack_pile.h new file +10
-0+ solo_first =__rte_stack_lf_pop_elems (&pile->free_solo, +RTE_STACK_PILE_BULK_SIZE - n_solo,NULL , &solo_last); +if (unlikely (solo_first ==NULL )) + goto fail; + +/* Construct the solo elements from the excess objects. */ + tmp_solo = solo_first; +__rte_assume (n_solo >0 ); +__rte_assume (n_solo <RTE_STACK_PILE_BULK_SIZE ); + for (i = n_solo; i < RTE_STACK_PILE_BULK_SIZE; i++, tmp_solo = tmp_solo-Quoted 1 line
next)
lib/stack/rte_stack_pile.h new file +8
-0+ tmp_solo->data = obj_frag[i]; + +/* Push the excess objects as solo elements. */ +__rte_stack_lf_push_elems (&pile->solo, solo_first, solo_last, +RTE_STACK_PILE_BULK_SIZE - n_solo); + n_solo =0 ; + +/* Add the fragmentation element to the bulk elements, so it can be freed with them. */
lib/stack/rte_stack_pile.h new file +11
-0+if (n_bulk >0 ) + bulk_last->next = frag; +else + bulk_first = frag; + bulk_last = frag; + n_bulk++; + +done: +/* Success. Free the elements. */ +if (n_bulk >0 ) + __rte_stack_pile_bulk_push_elems(&pile->free_bulk, bulk_first,
bulk_last,n_bulk);lib/stack/rte_stack_pile.h new file +2
-0+if (n_solo >0 ) + __rte_stack_lf_push_elems(&pile->free_solo, solo_first,
solo_last,n_solo);lib/stack/rte_stack_pile.h new file +8
-0+ +return n; + +fail: +/* Failed. Roll back. */ +if (frag !=NULL ) { +/* +* No further action than this is required to roll the fragmentation
lib/stack/rte_stack_pile.h new file +12
-0+* element back into the pile of bulk elements, as the objects in +* the fragmentation element are intact. +*/ +if (n_bulk >0 ) + bulk_last->next = frag; +else + bulk_first = frag; + bulk_last = frag; + n_bulk +=1 ; + } +if (n_bulk >0 ) + __rte_stack_pile_bulk_push_elems(&pile->bulk, bulk_first,
bulk_last,n_bulk);lib/stack/rte_stack_pile.h new file +27
-1+ + return 0; +} + +/** +* @internal Initialize a pile stack. +* +* @param s +* A pointer to the stack structure. +* @param count +* The size of the stack. +*/ +void +rte_stack_pile_init (struct rte_stack *s,unsigned int count); + +/** +* @internal Return the memory required for a pile stack. +* +* @param count +* The size of the stack. +* @return +* The bytes to allocate for a pile stack. +*/ +ssize_t +rte_stack_pile_get_memsize (unsigned int count); + +#endif /* _RTE_STACK_PILE_H_ */ --2.43.0
v3 cover introduce pile stack and mempool driver
In reply to Morten BrørupAdded a new high-performance lock-free "pile", using the Stack API, and an accompanying mempool driver.
Morten Brørup (2):
stack: introduce pile
mempool: introduce pile driver
app/test/test_stack.c | 71 ++++- app/test/test_stack_perf.c | 15 +- config/rte_config.h | 3 + doc/guides/mempool/stack.rst | 12 +- doc/guides/prog_guide/stack_lib.rst | 67 +++- drivers/mempool/stack/rte_mempool_stack.c | 90 +++++- lib/mempool/rte_mempool.h | 4 +- lib/mempool/rte_mempool_ops.c | 27 +- lib/stack/meson.build | 3 +- lib/stack/rte_stack.c | 18 +- lib/stack/rte_stack.h | 65 ++++ lib/stack/rte_stack_lf.h | 22 ++ lib/stack/rte_stack_lf_c11.h | 20 +- lib/stack/rte_stack_lf_generic.h | 19 +- lib/stack/rte_stack_lf_stubs.h | 4 +- lib/stack/rte_stack_pile.c | 35 +++ lib/stack/rte_stack_pile.h | 359 ++++++++++++++++++++++ 17 files changed, 761 insertions(+), 73 deletions(-) create mode 100644 lib/stack/rte_stack_pile.c create mode 100644 lib/stack/rte_stack_pile.h
Signature · 1 line
2.43.0
PATCH v3 1/2 stack: introduce pile
Added a new high-performance lock-free "pile", using the Stack API. The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile time configurable bulk size is handled gracefully, but not as fast as complete bulks.
Performance examples, stack_pile_perf_autotest vs. stack_lf_autotest:
On a single core, pushing/popping 1 or 8 objects is similar speed. On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly faster. On four cores, pushing/popping 512 objects is 4x faster.
---
v3:
* Embedded rte_stack_lf_elem parent class as first field in rte_stack_pile_bulk_elem inherited class, instead of copying all its fields. (Konstantin)
* Moved fragmentation (in pop function) into separate inline function. (Konstantin)
* Removed memory model #ifdefs by adding __rte_stack_lf_elems_count() function to the lock-free stack.
* Added TODO about potential improvement of bulk pop retry by implementing burst variant of pop_elems().
* Improved bulk pop retry algorithm by peeking at availability. v2:
* There is no v2.
--- app/test/test_stack.c | 71 +++++- app/test/test_stack_perf.c | 15 +- config/rte_config.h | 3 + doc/guides/prog_guide/stack_lib.rst | 67 +++++- lib/mempool/rte_mempool.h | 2 +- lib/stack/meson.build | 3 +- lib/stack/rte_stack.c | 18 +- lib/stack/rte_stack.h | 65 +++++ lib/stack/rte_stack_lf.h | 22 ++ lib/stack/rte_stack_lf_c11.h | 20 +- lib/stack/rte_stack_lf_generic.h | 19 +- lib/stack/rte_stack_lf_stubs.h | 4 +- lib/stack/rte_stack_pile.c | 35 +++ lib/stack/rte_stack_pile.h | 359 ++++++++++++++++++++++++++++ 14 files changed, 647 insertions(+), 56 deletions(-) create mode 100644 lib/stack/rte_stack_pile.c create mode 100644 lib/stack/rte_stack_pile.h
app/test/test_stack.c +61 -10
@@ -11,8 +11,8 @@#include "test.h" -#define STACK_SIZE 4096 -#define MAX_BULK 32 +#define STACK_SIZE 65536 +#define MAX_BULK 512static int test_stack_push_pop (struct rte_stack *s,void **obj_table,unsigned int bulk_sz)
@@ -81,13 +81,37 @@ test_stack_push_pop(struct rte_stack *s, void **obj_table, unsigned int bulk_sz) } } -for (i =0 ; i <STACK_SIZE ; i++) { -if (obj_table[i] != popped_objs[STACK_SIZE - i -1 ]) { -printf ("[%s():%u] Incorrect value %p at index 0x%x\n" , - __func__, __LINE__, - popped_objs[STACK_SIZE - i -1 ], i); -rte_free (popped_objs); -return -1 ; +if (!(s->flags &RTE_STACK_F_PILE )) { +/* Normal stack. */ +lifo: +for (i =0 ; i <STACK_SIZE ; i++) { +if (obj_table[i] != popped_objs[STACK_SIZE - i -1 ]) { +printf ("[%s():%u] Incorrect value %p at index 0x%x\n" , + __func__, __LINE__, + popped_objs[STACK_SIZE - i -1 ], i); +rte_free (popped_objs); +return -1 ; + } + } + }else { +/* Pile. Ordering not strictly LIFO. */ +if (bulk_sz <RTE_STACK_PILE_BULK_SIZE ) + goto lifo; +if ((bulk_sz & (RTE_STACK_PILE_BULK_SIZE -1 )) ==0 ) { +for (i =0 ; i <STACK_SIZE ; i +=RTE_STACK_PILE_BULK_SIZE ) { +if (memcmp (&obj_table[i], + &popped_objs[STACK_SIZE -RTE_STACK_PILE_BULK_SIZE - + i], +sizeof (void *) *RTE_STACK_PILE_BULK_SIZE ) !=0 ) { +printf ("[%s():%u] Incorrect values %p at 0x%x, bulk %u\n" , + __func__, __LINE__, + popped_objs[STACK_SIZE -RTE_STACK_PILE_BULK_SIZE - + i], + i, bulk_sz); +rte_free (popped_objs); +return -1 ; + } + } } }
@@ -152,12 +176,24 @@ test_stack_basic(uint32_t flags) goto fail_test; } - ret =rte_stack_push (s, obj_table,2 *STACK_SIZE ); + ret =rte_stack_push (s, obj_table,STACK_SIZE ); +if (ret ==0 ) { +printf ("[%s():%u] All objects push failed\n" , + __func__, __LINE__); + goto fail_test; + } + ret =rte_stack_push (s, obj_table,STACK_SIZE );if (ret !=0 ) {printf ("[%s():%u] Excess objects push succeeded\n" , __func__, __LINE__); goto fail_test; } + ret =rte_stack_pop (s, obj_table,STACK_SIZE ); +if (ret ==0 ) { +printf ("[%s():%u] All objects pop failed\n" , + __func__, __LINE__); + goto fail_test; + } ret =rte_stack_pop (s, obj_table,1 );if (ret !=0 ) {
@@ -167,8 +203,12 @@ test_stack_basic(uint32_t flags) } ret =0 ; + goto done; fail_test: + ret =-1 ; + +done:rte_stack_free (s);rte_free (obj_table);
@@ -384,5 +424,16 @@ test_lf_stack(void)#endif } +static int +test_pile (void ) +{ +#if defined(RTE_STACK_PILE_SUPPORTED ) +return __test_stack (RTE_STACK_F_PILE ); +#else +return TEST_SKIPPED ; +#endif +} +REGISTER_FAST_TEST (stack_autotest,NOHUGE_SKIP ,ASAN_OK , test_stack);REGISTER_FAST_TEST (stack_lf_autotest,NOHUGE_SKIP ,ASAN_OK , test_lf_stack); +REGISTER_FAST_TEST (stack_pile_autotest,NOHUGE_SKIP ,ASAN_OK , test_pile);
app/test/test_stack_perf.c +13 -2
@@ -14,14 +14,14 @@#include "test.h" #define STACK_NAME "STACK_PERF" -#define MAX_BURST 32 +#define MAX_BURST 512#define STACK_SIZE (RTE_MAX_LCORE * MAX_BURST)/* * Push/pop bulk sizes, marked volatile so they aren't treated as compile-time * constants. */ -static volatile unsigned int bulk_sizes[] = {8 ,MAX_BURST }; +static volatile unsigned int bulk_sizes[] = {1 ,8 ,32 ,MAX_BURST };static RTE_ATOMIC (uint32_t ) lcore_barrier;
@@ -354,5 +354,16 @@ test_lf_stack_perf(void)#endif } +static int +test_pile_perf (void ) +{ +#if defined(RTE_STACK_PILE_SUPPORTED ) +return __test_stack_perf (RTE_STACK_F_PILE ); +#else +return TEST_SKIPPED ; +#endif +} +REGISTER_PERF_TEST (stack_perf_autotest, test_stack_perf);REGISTER_PERF_TEST (stack_lf_perf_autotest, test_lf_stack_perf); +REGISTER_PERF_TEST (stack_pile_perf_autotest, test_pile_perf);
config/rte_config.h +3 -0
@@ -64,6 +64,9 @@#define RTE_MBUF_DEFAULT_MEMPOOL_OPS "ring_mp_mc"/* RTE_MBUF_HISTORY_DEBUG is not set */ +/* stack defines */ +#define RTE_STACK_PILE_BULK_SIZE 32 +/* ether defines */ #define RTE_MAX_QUEUES_PER_PORT 1024#define RTE_ETHDEV_RXTX_CALLBACKS 1
doc/guides/prog_guide/stack_lib.rst +63 -4
95 lines not on this page · raw message
lib/mempool/rte_mempool.h +1 -1
@@ -718,7 +718,7 @@ struct __rte_cache_aligned rte_mempool_ops {rte_mempool_dequeue_contig_blocks_t dequeue_contig_blocks; }; -#define RTE_MEMPOOL_MAX_OPS_IDX 16/**< Max registered ops structs */ +#define RTE_MEMPOOL_MAX_OPS_IDX 32/**< Max registered ops structs */ /** * Structure storing the table of registered ops structs, each of which contain
lib/stack/meson.build +2 -1
@@ -1,7 +1,7 @@ # SPDX-License-Identifier: BSD-3-Clause # Copyright(c) 2019 Intel Corporation -sources = files('rte_stack.c', 'rte_stack_std.c', 'rte_stack_lf.c') +sources = files('rte_stack.c', 'rte_stack_std.c', 'rte_stack_lf.c', 'rte_stack_pile.c') headers = files('rte_stack.h') # subheaders, not for direct inclusion by apps indirect_headers += files(
@@ -10,4 +10,5 @@ indirect_headers += files( 'rte_stack_lf_generic.h', 'rte_stack_lf_c11.h', 'rte_stack_lf_stubs.h', + 'rte_stack_pile.h', )
lib/stack/rte_stack.c +17 -1
@@ -1,5 +1,6 @@/* SPDX-License-Identifier: BSD-3-Clause * Copyright(c) 2019 Intel Corporation +* Copyright(c) 2026 SmartShare Systems */ #include <stdalign.h>
@@ -32,6 +33,8 @@ rte_stack_init(struct rte_stack *s, unsigned int count, uint32_t flags)if (flags &RTE_STACK_F_LF )rte_stack_lf_init (s, count); +else if (flags &RTE_STACK_F_PILE ) +rte_stack_pile_init (s, count);else rte_stack_std_init (s); }
@@ -41,6 +44,8 @@ rte_stack_get_memsize(unsigned int count, uint32_t flags) {if (flags &RTE_STACK_F_LF )return rte_stack_lf_get_memsize (count); +else if (flags &RTE_STACK_F_PILE ) +return rte_stack_pile_get_memsize (count);else return rte_stack_std_get_memsize (count); }
@@ -58,7 +63,11 @@ rte_stack_create(const char *name, unsigned int count, int socket_id,unsigned int sz;int ret; -if (flags & ~(RTE_STACK_F_LF )) { +if (flags & ~(RTE_STACK_F_LF |RTE_STACK_F_PILE )) { +STACK_LOG_ERR ("Unsupported stack flags %#x" , flags); +return NULL ; + } +if ((flags &RTE_STACK_F_LF ) && (flags &RTE_STACK_F_PILE )) {STACK_LOG_ERR ("Unsupported stack flags %#x" , flags);return NULL ; }
@@ -73,6 +82,13 @@ rte_stack_create(const char *name, unsigned int count, int socket_id, return NULL; }#endif +#if !defined(RTE_STACK_PILE_SUPPORTED ) +if (flags &RTE_STACK_F_PILE ) { +STACK_LOG_ERR ("Pile is not supported on your platform" ); + rte_errno =ENOTSUP ; +return NULL ; + } +#endif sz =rte_stack_get_memsize (count, flags);
lib/stack/rte_stack.h +65 -0
125 lines not on this page · raw message
lib/stack/rte_stack_lf.h +22 -0
@@ -20,6 +20,27 @@#define RTE_STACK_LF_SUPPORTED #endif +static __rte_always_inline unsigned int +__rte_stack_lf_count (struct rte_stack *s) +{ +/* stack_lf_push() and stack_lf_pop() do not update the list's contents +* and stack_lf->len atomically, which can cause the list to appear +* shorter than it actually is if this function is called while other +* threads are modifying the list. +* +* However, given the inherently approximate nature of the get_count +* callback -- even if the list and its size were updated atomically, +* the size could change between when get_count executes and when the +* value is returned to the caller -- this is acceptable. +* +* The stack_lf->len updates are placed such that the list may appear to +* have fewer elements than it does, but will never appear to have more +* elements. If the mempool is near-empty to the point that this is a +* concern, the user should consider increasing the mempool size. +*/ +return __rte_stack_lf_elems_count (&s->stack_lf.used); +} +/** * @internal Push several objects on the lock-free stack (MT-safe). *
@@ -79,6 +100,7 @@ __rte_stack_lf_pop(struct rte_stack *s, void **obj_table, unsigned int n)return 0 ;/* Pop n used elements */ +__rte_assume (obj_table !=NULL ); first =__rte_stack_lf_pop_elems (&s->stack_lf.used, n, obj_table, &last);if (unlikely (first ==NULL ))
lib/stack/rte_stack_lf_c11.h +2 -18
@@ -36,25 +36,9 @@ rte_atomic128_cmp_exchange(rte_int128_t *dst,#endif /* RTE_TOOLCHAIN_MSVC */ static __rte_always_inline unsigned int -__rte_stack_lf_count (struct rte_stack *s) +__rte_stack_lf_elems_count (struct rte_stack_lf_list *list) { -/* stack_lf_push() and stack_lf_pop() do not update the list's contents -* and stack_lf->len atomically, which can cause the list to appear -* shorter than it actually is if this function is called while other -* threads are modifying the list. -* -* However, given the inherently approximate nature of the get_count -* callback -- even if the list and its size were updated atomically, -* the size could change between when get_count executes and when the -* value is returned to the caller -- this is acceptable. -* -* The stack_lf->len updates are placed such that the list may appear to -* have fewer elements than it does, but will never appear to have more -* elements. If the mempool is near-empty to the point that this is a -* concern, the user should consider increasing the mempool size. -*/ -return (unsigned int )rte_atomic_load_explicit (&s->stack_lf.used.len, - rte_memory_order_relaxed); +return (unsigned int )rte_atomic_load_explicit (&list->len, rte_memory_order_relaxed); }static __rte_always_inline void
lib/stack/rte_stack_lf_generic.h +2 -17
@@ -9,25 +9,10 @@#include <rte_prefetch.h> static __rte_always_inline unsigned int -__rte_stack_lf_count (struct rte_stack *s) +__rte_stack_lf_elems_count (struct rte_stack_lf_list *list) { -/* stack_lf_push() and stack_lf_pop() do not update the list's contents -* and stack_lf->len atomically, which can cause the list to appear -* shorter than it actually is if this function is called while other -* threads are modifying the list. -* -* However, given the inherently approximate nature of the get_count -* callback -- even if the list and its size were updated atomically, -* the size could change between when get_count executes and when the -* value is returned to the caller -- this is acceptable. -* -* The stack_lf->len updates are placed such that the list may appear to -* have fewer elements than it does, but will never appear to have more -* elements. If the mempool is near-empty to the point that this is a -* concern, the user should consider increasing the mempool size. -*/ /* NOTE: review for potential ordering optimization */ -return rte_atomic_load_explicit (&s->stack_lf.used.len, rte_memory_order_seq_cst); +return (unsigned int )rte_atomic_load_explicit (&list->len, rte_memory_order_seq_cst); }static __rte_always_inline void
lib/stack/rte_stack_lf_stubs.h +2 -2
@@ -8,9 +8,9 @@#include <rte_common.h> static __rte_always_inline unsigned int -__rte_stack_lf_count (struct rte_stack *s) +__rte_stack_lf_elems_count (struct rte_stack_lf_list *list) { -RTE_SET_USED (s); +RTE_SET_USED (list);return 0 ; }
lib/stack/rte_stack_pile.c new file +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2026 SmartShare Systems +*/ + +#include "rte_stack.h" + +void +rte_stack_pile_init (struct rte_stack *s,unsigned int count) +{ +unsigned int bulk = (count +RTE_STACK_PILE_BULK_SIZE -1 ) /RTE_STACK_PILE_BULK_SIZE ; +struct rte_stack_pile_bulk_elem *bulk_elems = + (struct rte_stack_pile_bulk_elem *)(s->stack_pile.elems); +struct rte_stack_lf_elem *solo_elems = (struct rte_stack_lf_elem *)&bulk_elems[bulk]; +unsigned int i; + +for (i =0 ; i < bulk; i++) +__rte_stack_pile_bulk_push_elems (&s->stack_pile.free_bulk, + &bulk_elems[i], &bulk_elems[i],1 ); +for (i =0 ; i < count; i++) +__rte_stack_lf_push_elems (&s->stack_pile.free_solo, + &solo_elems[i], &solo_elems[i],1 ); +} + +ssize_t +rte_stack_pile_get_memsize (unsigned int count) +{ +unsigned int bulk = (count +RTE_STACK_PILE_BULK_SIZE -1 ) /RTE_STACK_PILE_BULK_SIZE ; + ssize_t sz = offsetof(struct rte_stack, stack_pile.elems); + sz += bulk *sizeof (struct rte_stack_pile_bulk_elem ); + sz += count *sizeof (struct rte_stack_lf_elem ); + sz =RTE_CACHE_LINE_ROUNDUP (sz); + sz +=RTE_CACHE_GUARD_LINES *RTE_CACHE_LINE_SIZE ; + +return sz; +}
lib/stack/rte_stack_pile.h new file +359 -0
359 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 12 lines
Added a new high-performance lock-free "pile", using the Stack API. The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile time configurable bulk size is handled gracefully, but not as fast as complete bulks.
Quoted 8 lines
Performance examples,
stack_pile_perf_autotestvs.stack_lf_autotest:On a single core, pushing/popping 1 or 8 objects is similar speed. On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly faster. On four cores, pushing/popping 512 objects is 4x faster.
The code itself looks ok to me, thought I still think it is worth to consider moving lf_pile (and lf_stack) DP implementation in .c, to avoid each rte_stack_pus/pop to inline all three of them.
My speculation is that the perf diff for bulk enqueue/dequeue because of that would be negligible, while both are quite big for inlining them always (specially lf_pile).
Quoted 2 lines
Signed-off-by Morten Brørup <[email protected]>---
Quoted 22 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Monday, 31 August 2026 10.50Quoted 10 lines
Added a new high-performance lock-free "pile", using the Stack API. The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile time configurable bulk size is handled gracefully, but not as fast as complete bulks.
Quoted 8 lines
Performance examples,
stack_pile_perf_autotestvs.stack_lf_autotest:On a single core, pushing/popping 1 or 8 objects is similar speed. On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly faster. On four cores, pushing/popping 512 objects is 4x faster.
Quoted 7 lines
The code itself looks ok to me, thought I still think it is worth to consider
movinglf_pile(andlf_stack) DP implementation in .c, to avoid each rte_stack_pus/pop
to inline all three of them.
If a use case knows the selected stack implement at build time, it can call the implementation's push/pop functions directly. I updated the mempool stack driver v3 patch [1] to do this for all three stack implementations.
[1]: https://patchwork.dpdk.org/project/dpdk/patch/[email protected]/
Quoted 5 lines
My speculation is that the perf diff for bulk enqueue/dequeue because of
that would be negligible, while both are quite big for inlining them always
(speciallylf_pile).
If the number of objects being pushed/popped is known at build time, the compiler can optimize the functions when inlined.
I tried experimented with conditional inlining depending on the number of objects being known at build time, but I wasn't really pleased with it.
Quoted 31 lines
Quoted 23 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Monday, 31 August 2026 10.50Quoted 10 lines
Added a new high-performance lock-free "pile", using the Stack API. The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile time configurable bulk size is handled gracefully, but not as fast as complete bulks.
Quoted 8 lines
Performance examples,
stack_pile_perf_autotestvs.stack_lf_autotest:On a single core, pushing/popping 1 or 8 objects is similar speed. On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly faster. On four cores, pushing/popping 512 objects is 4x faster.
Quoted 7 lines
The code itself looks ok to me, thought I still think it is worth to consider
movinglf_pile(andlf_stack) DP implementation in .c, to avoid each rte_stack_pus/pop
to inline all three of them.
Quoted 4 lines
If a use case knows the selected stack implement at build time, it can call the implementation's push/pop functions directly.
I updated the mempool stack driver v3 patch [1] to do this for all three stack implementations.
That's good thing for sure.
Though I am talking about:
#ifdef __cplusplus
extern "C" {
@@ -115,6 +174,8 @@ rte_stack_push(struct rte_stack *s, void * const *obj_table, unsigned int n)
if (s->flags & RTE_STACK_F_LF)
return __rte_stack_lf_push(s, obj_table, n);
+ else if (s->flags & RTE_STACK_F_PILE)
+ return __rte_stack_pile_push(s, obj_table, n);
else
return __rte_stack_std_push(s, obj_table, n);
}
@@ -139,6 +200,8 @@ rte_stack_pop(struct rte_stack *s, void **obj_table, unsigned int n)
if (s->flags & RTE_STACK_F_LF)
return __rte_stack_lf_pop(s, obj_table, n);
+ else if (s->flags & RTE_STACK_F_PILE)
+ return __rte_stack_pile_pop(s, obj_table, n);
else
return __rte_stack_std_pop(s, obj_table, n);
}
In rte_stack.h
We still can have our __rte_stack_pile_push/po as inline functions in the internal headers, so mempool (and whoever else needs them) can include them directly.
My suggestion to have rte_stack_pile_pop() in .c and invoke it (not-inlined one) from generic rte_stack_pop().
Quoted 14 lines
[1]: https://patchwork.dpdk.org/project/dpdk/patch/20260827135556.522443-3-
[email protected]/Quoted 5 lines
My speculation is that the perf diff for bulk enqueue/dequeue because of
that would be negligible, while both are quite big for inlining them always
(speciallylf_pile).If the number of objects being pushed/popped is known at build time, the compiler can optimize the functions when inlined.
I tried experimented with conditional inlining depending on the number of objects being known at build time, but I wasn't really pleased with it.
Quoted 72 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Monday, 31 August 2026 11.38Quoted 4 lines
Quoted 4 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Monday, 31 August 2026 10.50Quoted 1 line
Added a new high-performance lock-free "pile", using the Stack
API.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile
time
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
configurable bulk size is handled gracefully, but not as fast as complete bulks.
Performance examples,
stack_pile_perf_autotestvs.
stack_lf_autotest:Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
On a single core, pushing/popping 1 or 8 objects is similar
speed.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly
faster.
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
On four cores, pushing/popping 512 objects is 4x faster.
The code itself looks ok to me, thought I still think it is worth
to
Quoted 2 lines
Quoted 2 lines
consider
movinglf_pile(andlf_stack) DP implementation in .c, to avoideach
Quoted 4 lines
Quoted 2 lines
rte_stack_pus/pop
to inline all three of them.If a use case knows the selected stack implement at build time, it
can call the
Quoted 2 lines
implementation's push/pop functions directly.
I updated the mempool stack driver v3 patch [1] to do this for allthree stack
Quoted 1 line
implementations.
That's good thing for sure.
Though I am talking about:
#ifdef__cplusplus
extern "C" {lib/stack/rte_stack.h +2
-0@@ -115,6 +174,8 @@ rte_stack_push(struct rte_stack *s, void * const *obj_table, unsigned int n)if (s->flags &RTE_STACK_F_LF )return __rte_stack_lf_push (s, obj_table, n); +else if (s->flags &RTE_STACK_F_PILE ) +return __rte_stack_pile_push (s, obj_table, n);else return __rte_stack_std_push (s, obj_table, n);}
lib/stack/rte_stack.h +2
-0@@ -139,6 +200,8 @@ rte_stack_pop(struct rte_stack *s, void **obj_table, unsigned int n)if (s->flags &RTE_STACK_F_LF )return __rte_stack_lf_pop (s, obj_table, n); +else if (s->flags &RTE_STACK_F_PILE ) +return __rte_stack_pile_pop (s, obj_table, n);else return __rte_stack_std_pop (s, obj_table, n);}
Quoted 7 lines
In
rte_stack.h
We still can have our __rte_stack_pile_push/po as inline functions in the internal headers,
so mempool (and whoever else needs them) can include them directly. My suggestion to haverte_stack_pile_pop()in .c and invoke it (not- inlined one) from
genericrte_stack_pop().
I get it, and I agree it would reduce the footprint of the compiled code.
But it would eliminate compiler optimizations for build time known sizes, e.g. n=1 or n=BURST_SIZE, so I prefer keeping them inlined.
De-inlining could also ruin branch prediction when used with multiple stacks.
Let's say the same number of objects is always dequeued from a specific stack; the branch predictor would learn this. Now, if the non-inlined function is called to dequeue a different number of objects from another stack, it would trip up the branch predictor.
BTW,
The rings use the same design pattern, where the enqueue/dequeue implementation is selected at run-time based on ring->prod/cons.sync_type, all inlined for the benefit of the optimizer when various parameters (element size, number of objects) are known at build time, and for the benefit of the branch predictor.
Quoted 21 lines
Quoted 2 lines
[1]:
https://patchwork.dpdk.org/project/dpdk/patch/20260827135556.522443-3-
Quoted 3 lines
Quoted 1 line
My speculation is that the perf diff for bulk enqueue/dequeue
because
Quoted 2 lines
Quoted 2 lines
of
that would be negligible, while both are quite big for inliningthem
Quoted 4 lines
Quoted 2 lines
always
(speciallylf_pile).If the number of objects being pushed/popped is known at build time,
the
Quoted 3 lines
compiler can optimize the functions when inlined.
I tried experimented with conditional inlining depending on the
number of
Quoted 1 line
objects being known at build time, but I wasn't really pleased with
it.
lib/stack/rte_stack_pile.h line 254
Quoted 2 lines · On Thu, 27 Aug 2026 13:55:55 +0000 Morten Brørup <[email protected]>…
lib/stack/rte_stack_pile.h new file +2
-0+__rte_stack_pile_pop(struct rte_stack *s, + void **obj_table,
NAK to using always_inline on a function this big
It generates worse code in a lot of cases because of register spill.
Quoted 11 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Monday, 31 August 2026 18.06lib/stack/rte_stack_pile.h line 254
Quoted 2 lines · On Thu, 27 Aug 2026 13:55:55 +0000 Morten Brørup <[email protected]>…
lib/stack/rte_stack_pile.h new file +2
-0+__rte_stack_pile_pop(struct rte_stack *s, + void **obj_table,NAK to using
always_inlineon a function this big
It generates worse code in a lot of cases because of register spill.
OK, I will change it to "inline", and let the compiler decide. Then we don't have to discuss which is better. :-)
I'll change the pull function too.
BTW, it's not quite as big as it looks.
Here's the function without comments, assertions and empty lines:
static __rte_always_inline unsigned int
__rte_stack_pile_pop(struct rte_stack *s,
void **obj_table,
unsigned int n)
{
struct rte_stack_pile *pile = &s->stack_pile;
struct rte_stack_pile_bulk_elem *bulk_first = NULL, *bulk_last = NULL;
struct rte_stack_lf_elem *solo_first = NULL, *solo_last = NULL;
unsigned int n_bulk = n / RTE_STACK_PILE_BULK_SIZE;
unsigned int n_solo = n & (RTE_STACK_PILE_BULK_SIZE - 1);
if (unlikely(n_bulk == 0)) {
if (unlikely(n_solo == 0))
return 0;
goto solo;
}
bulk:
bulk_first = __rte_stack_pile_bulk_pop_elems(&pile->bulk, n_bulk, obj_table, &bulk_last);
if (unlikely(bulk_first == NULL)) {
unsigned int delta_bulk = n_bulk - __rte_stack_lf_elems_count(&pile->bulk);
if (unlikely((int)delta_bulk <= 0))
delta_bulk = 1;
n_bulk -= delta_bulk;
n_solo += RTE_STACK_PILE_BULK_SIZE * delta_bulk;
if (n_bulk == 0)
goto solo;
goto bulk;
}
obj_table += n_bulk * RTE_STACK_PILE_BULK_SIZE;
if (likely(n_solo == 0))
goto done;
solo:
solo_first = __rte_stack_lf_pop_elems(&pile->solo, n_solo,
obj_table, &solo_last);
if (solo_first != NULL)
goto done;
if (unlikely(n_solo >= RTE_STACK_PILE_BULK_SIZE))
goto fail;
if (unlikely(__rte_stack_pile_pop_frag(pile, obj_table, n_solo) == 0))
goto fail;
done:
if (bulk_first != NULL)
__rte_stack_pile_bulk_push_elems(&pile->free_bulk, bulk_first, bulk_last, n_bulk);
if (solo_first != NULL)
__rte_stack_lf_push_elems(&pile->free_solo, solo_first, solo_last, n_solo);
return n;
fail:
if (bulk_first != NULL)
__rte_stack_pile_bulk_push_elems(&pile->bulk, bulk_first, bulk_last, n_bulk);
return 0;
}PATCH v3 2/2 mempool: introduce pile driver
In reply to Morten BrørupAdded a new "pile" mempool driver, based on the high-performance lock-free "pile" stack variant.
Changed other stack drivers to call their specific push/pop functions, instead of calling the generic stack push/pop API.
---
v3:
* Call other stack types' specific push/pop functions. v2:
* There is no v2.
--- doc/guides/mempool/stack.rst | 12 ++- drivers/mempool/stack/rte_mempool_stack.c | 90 ++++++++++++++++++++--- lib/mempool/rte_mempool.h | 2 + lib/mempool/rte_mempool_ops.c | 27 +++++-- 4 files changed, 114 insertions(+), 17 deletions(-)
doc/guides/mempool/stack.rst +11 -1
@@ -1,5 +1,6 @@ .. SPDX-License-Identifier: BSD-3-Clause Copyright(c) 2020 Intel Corporation. + Copyright(c) 2026 SmartShare Systems. Stack Mempool Driver ====================
@@ -28,6 +29,12 @@ can be selected as described in :ref:`Mempool_Handlers`: The underlying **rte_stack** operates in lock-free mode. For more information please refer to :ref:`Stack_Library_LF_Stack`. +- ``pile`` + + The underlying **rte_stack** operates in lock-free mode, + and is optimized for bulks of objects. + For more information please refer to :ref:`Stack_Library_Pile`. + The standard stack outperforms the lock-free stack on average, however the standard stack is non-preemptive: if a mempool user is preempted while holding the stack lock, that thread will block all other mempool accesses until it
@@ -35,9 +42,12 @@ returns and releases the lock. As a result, an application using the standard stack whose threads can be preempted can suffer from brief, infrequent performance hiccups. -The lock-free stack, by design, is not susceptible to this problem; one thread can +The lock-free stack and the pile, by design, are not susceptible to this problem; one thread can be preempted at any point during a push or pop operation and will not impede the progress of any other thread. +The pile is not LIFO per object, but per bulk of objects. +Although the pile is optimized for bulks of objects, it can handle any request size. + For a more detailed description of the stack implementations, please refer to :doc:`/prog_guide/stack_lib`.
drivers/mempool/stack/rte_mempool_stack.c +80 -10
128 lines not on this page · raw message
lib/mempool/rte_mempool.h +2 -0
@@ -979,6 +979,8 @@ rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name,* - >=0: Success; return the index of the ops struct in the table. * - -EINVAL - some missing callbacks while registering ops struct. * - -ENOSPC - the maximum number of ops structs has been reached. +* - -ENAMETOOLONG - the name of the ops is too long. +* - -EEXIST - the name of the ops is already registered. */ int rte_mempool_register_ops (const struct rte_mempool_ops *ops);
lib/mempool/rte_mempool_ops.c +21 -6
@@ -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,21 @@ rte_mempool_register_ops(const struct rte_mempool_ops *h)return -EINVAL ; } -if (strlen (h->name) >=sizeof (ops->name) -1 ) { +if (strlen (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" , +RTE_MEMPOOL_LOG (ERR ,"%s(): mempool_ops <%s>: name too long" , __func__, h->name); - rte_errno =EEXIST ; -return -EEXIST ; +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++;
@@ -68,6 +77,9 @@ rte_mempool_register_ops(const struct rte_mempool_ops *h) ops->get_info = h->get_info; ops->dequeue_contig_blocks = h->dequeue_contig_blocks; +RTE_MEMPOOL_LOG (DEBUG , +"Registered mempool_ops[%u] <%s>" , ops_index, h->name); +rte_spinlock_unlock (&rte_mempool_ops_table.sl);return ops_index;
@@ -185,8 +197,11 @@ rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name, } } -if (ops ==NULL ) +if (ops ==NULL ) { +RTE_MEMPOOL_LOG (ERR , +"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
v4 cover introduce pile stack and mempool driver
In reply to Morten BrørupAdded a new high-performance lock-free "pile", using the Stack API, and an accompanying mempool driver.
Morten Brørup (2):
stack: introduce pile
mempool: introduce pile driver
app/test/test_stack.c | 71 ++++- app/test/test_stack_perf.c | 15 +- config/rte_config.h | 3 + doc/guides/mempool/stack.rst | 12 +- doc/guides/prog_guide/stack_lib.rst | 67 +++- drivers/mempool/stack/rte_mempool_stack.c | 90 +++++- lib/mempool/rte_mempool.h | 4 +- lib/mempool/rte_mempool_ops.c | 27 +- lib/stack/meson.build | 3 +- lib/stack/rte_stack.c | 18 +- lib/stack/rte_stack.h | 65 ++++ lib/stack/rte_stack_lf.h | 22 ++ lib/stack/rte_stack_lf_c11.h | 20 +- lib/stack/rte_stack_lf_generic.h | 19 +- lib/stack/rte_stack_lf_stubs.h | 4 +- lib/stack/rte_stack_pile.c | 35 +++ lib/stack/rte_stack_pile.h | 359 ++++++++++++++++++++++ 17 files changed, 761 insertions(+), 73 deletions(-) create mode 100644 lib/stack/rte_stack_pile.c create mode 100644 lib/stack/rte_stack_pile.h
Signature · 1 line
2.43.0
PATCH v4 1/2 stack: introduce pile
Added a new high-performance lock-free "pile", using the Stack API. The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile time configurable bulk size is handled gracefully, but not as fast as complete bulks.
Performance examples, stack_pile_perf_autotest vs. stack_lf_autotest:
On a single core, pushing/popping 1 or 8 objects is similar speed. On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly faster. On four cores, pushing/popping 512 objects is 4x faster.
---
v4:
* Use inline instead of __rte_always_inline for push/pop functions, and let the compiler decide. (Konstantin, Stephen)
* Use inline instead of __rte_noinline for fragmentation (in pop function), and let the compiler decide.
v3:
* Embedded rte_stack_lf_elem parent class as first field in rte_stack_pile_bulk_elem inherited class, instead of copying all its fields. (Konstantin)
* Moved fragmentation (in pop function) into separate inline function. (Konstantin)
* Removed memory model #ifdefs by adding __rte_stack_lf_elems_count() function to the lock-free stack.
* Added TODO about potential improvement of bulk pop retry by implementing burst variant of pop_elems().
* Improved bulk pop retry algorithm by peeking at availability. v2:
* There is no v2.
--- app/test/test_stack.c | 71 +++++- app/test/test_stack_perf.c | 15 +- config/rte_config.h | 3 + doc/guides/prog_guide/stack_lib.rst | 67 +++++- lib/mempool/rte_mempool.h | 2 +- lib/stack/meson.build | 3 +- lib/stack/rte_stack.c | 18 +- lib/stack/rte_stack.h | 65 +++++ lib/stack/rte_stack_lf.h | 22 ++ lib/stack/rte_stack_lf_c11.h | 20 +- lib/stack/rte_stack_lf_generic.h | 19 +- lib/stack/rte_stack_lf_stubs.h | 4 +- lib/stack/rte_stack_pile.c | 35 +++ lib/stack/rte_stack_pile.h | 359 ++++++++++++++++++++++++++++ 14 files changed, 647 insertions(+), 56 deletions(-) create mode 100644 lib/stack/rte_stack_pile.c create mode 100644 lib/stack/rte_stack_pile.h
app/test/test_stack.c +61 -10
@@ -11,8 +11,8 @@#include "test.h" -#define STACK_SIZE 4096 -#define MAX_BULK 32 +#define STACK_SIZE 65536 +#define MAX_BULK 512static int test_stack_push_pop (struct rte_stack *s,void **obj_table,unsigned int bulk_sz)
@@ -81,13 +81,37 @@ test_stack_push_pop(struct rte_stack *s, void **obj_table, unsigned int bulk_sz) } } -for (i =0 ; i <STACK_SIZE ; i++) { -if (obj_table[i] != popped_objs[STACK_SIZE - i -1 ]) { -printf ("[%s():%u] Incorrect value %p at index 0x%x\n" , - __func__, __LINE__, - popped_objs[STACK_SIZE - i -1 ], i); -rte_free (popped_objs); -return -1 ; +if (!(s->flags &RTE_STACK_F_PILE )) { +/* Normal stack. */ +lifo: +for (i =0 ; i <STACK_SIZE ; i++) { +if (obj_table[i] != popped_objs[STACK_SIZE - i -1 ]) { +printf ("[%s():%u] Incorrect value %p at index 0x%x\n" , + __func__, __LINE__, + popped_objs[STACK_SIZE - i -1 ], i); +rte_free (popped_objs); +return -1 ; + } + } + }else { +/* Pile. Ordering not strictly LIFO. */ +if (bulk_sz <RTE_STACK_PILE_BULK_SIZE ) + goto lifo; +if ((bulk_sz & (RTE_STACK_PILE_BULK_SIZE -1 )) ==0 ) { +for (i =0 ; i <STACK_SIZE ; i +=RTE_STACK_PILE_BULK_SIZE ) { +if (memcmp (&obj_table[i], + &popped_objs[STACK_SIZE -RTE_STACK_PILE_BULK_SIZE - + i], +sizeof (void *) *RTE_STACK_PILE_BULK_SIZE ) !=0 ) { +printf ("[%s():%u] Incorrect values %p at 0x%x, bulk %u\n" , + __func__, __LINE__, + popped_objs[STACK_SIZE -RTE_STACK_PILE_BULK_SIZE - + i], + i, bulk_sz); +rte_free (popped_objs); +return -1 ; + } + } } }
@@ -152,12 +176,24 @@ test_stack_basic(uint32_t flags) goto fail_test; } - ret =rte_stack_push (s, obj_table,2 *STACK_SIZE ); + ret =rte_stack_push (s, obj_table,STACK_SIZE ); +if (ret ==0 ) { +printf ("[%s():%u] All objects push failed\n" , + __func__, __LINE__); + goto fail_test; + } + ret =rte_stack_push (s, obj_table,STACK_SIZE );if (ret !=0 ) {printf ("[%s():%u] Excess objects push succeeded\n" , __func__, __LINE__); goto fail_test; } + ret =rte_stack_pop (s, obj_table,STACK_SIZE ); +if (ret ==0 ) { +printf ("[%s():%u] All objects pop failed\n" , + __func__, __LINE__); + goto fail_test; + } ret =rte_stack_pop (s, obj_table,1 );if (ret !=0 ) {
@@ -167,8 +203,12 @@ test_stack_basic(uint32_t flags) } ret =0 ; + goto done; fail_test: + ret =-1 ; + +done:rte_stack_free (s);rte_free (obj_table);
@@ -384,5 +424,16 @@ test_lf_stack(void)#endif } +static int +test_pile (void ) +{ +#if defined(RTE_STACK_PILE_SUPPORTED ) +return __test_stack (RTE_STACK_F_PILE ); +#else +return TEST_SKIPPED ; +#endif +} +REGISTER_FAST_TEST (stack_autotest,NOHUGE_SKIP ,ASAN_OK , test_stack);REGISTER_FAST_TEST (stack_lf_autotest,NOHUGE_SKIP ,ASAN_OK , test_lf_stack); +REGISTER_FAST_TEST (stack_pile_autotest,NOHUGE_SKIP ,ASAN_OK , test_pile);
app/test/test_stack_perf.c +13 -2
@@ -14,14 +14,14 @@#include "test.h" #define STACK_NAME "STACK_PERF" -#define MAX_BURST 32 +#define MAX_BURST 512#define STACK_SIZE (RTE_MAX_LCORE * MAX_BURST)/* * Push/pop bulk sizes, marked volatile so they aren't treated as compile-time * constants. */ -static volatile unsigned int bulk_sizes[] = {8 ,MAX_BURST }; +static volatile unsigned int bulk_sizes[] = {1 ,8 ,32 ,MAX_BURST };static RTE_ATOMIC (uint32_t ) lcore_barrier;
@@ -354,5 +354,16 @@ test_lf_stack_perf(void)#endif } +static int +test_pile_perf (void ) +{ +#if defined(RTE_STACK_PILE_SUPPORTED ) +return __test_stack_perf (RTE_STACK_F_PILE ); +#else +return TEST_SKIPPED ; +#endif +} +REGISTER_PERF_TEST (stack_perf_autotest, test_stack_perf);REGISTER_PERF_TEST (stack_lf_perf_autotest, test_lf_stack_perf); +REGISTER_PERF_TEST (stack_pile_perf_autotest, test_pile_perf);
config/rte_config.h +3 -0
@@ -64,6 +64,9 @@#define RTE_MBUF_DEFAULT_MEMPOOL_OPS "ring_mp_mc"/* RTE_MBUF_HISTORY_DEBUG is not set */ +/* stack defines */ +#define RTE_STACK_PILE_BULK_SIZE 32 +/* ether defines */ #define RTE_MAX_QUEUES_PER_PORT 1024#define RTE_ETHDEV_RXTX_CALLBACKS 1
doc/guides/prog_guide/stack_lib.rst +63 -4
95 lines not on this page · raw message
lib/mempool/rte_mempool.h +1 -1
@@ -718,7 +718,7 @@ struct __rte_cache_aligned rte_mempool_ops {rte_mempool_dequeue_contig_blocks_t dequeue_contig_blocks; }; -#define RTE_MEMPOOL_MAX_OPS_IDX 16/**< Max registered ops structs */ +#define RTE_MEMPOOL_MAX_OPS_IDX 32/**< Max registered ops structs */ /** * Structure storing the table of registered ops structs, each of which contain
lib/stack/meson.build +2 -1
@@ -1,7 +1,7 @@ # SPDX-License-Identifier: BSD-3-Clause # Copyright(c) 2019 Intel Corporation -sources = files('rte_stack.c', 'rte_stack_std.c', 'rte_stack_lf.c') +sources = files('rte_stack.c', 'rte_stack_std.c', 'rte_stack_lf.c', 'rte_stack_pile.c') headers = files('rte_stack.h') # subheaders, not for direct inclusion by apps indirect_headers += files(
@@ -10,4 +10,5 @@ indirect_headers += files( 'rte_stack_lf_generic.h', 'rte_stack_lf_c11.h', 'rte_stack_lf_stubs.h', + 'rte_stack_pile.h', )
lib/stack/rte_stack.c +17 -1
@@ -1,5 +1,6 @@/* SPDX-License-Identifier: BSD-3-Clause * Copyright(c) 2019 Intel Corporation +* Copyright(c) 2026 SmartShare Systems */ #include <stdalign.h>
@@ -32,6 +33,8 @@ rte_stack_init(struct rte_stack *s, unsigned int count, uint32_t flags)if (flags &RTE_STACK_F_LF )rte_stack_lf_init (s, count); +else if (flags &RTE_STACK_F_PILE ) +rte_stack_pile_init (s, count);else rte_stack_std_init (s); }
@@ -41,6 +44,8 @@ rte_stack_get_memsize(unsigned int count, uint32_t flags) {if (flags &RTE_STACK_F_LF )return rte_stack_lf_get_memsize (count); +else if (flags &RTE_STACK_F_PILE ) +return rte_stack_pile_get_memsize (count);else return rte_stack_std_get_memsize (count); }
@@ -58,7 +63,11 @@ rte_stack_create(const char *name, unsigned int count, int socket_id,unsigned int sz;int ret; -if (flags & ~(RTE_STACK_F_LF )) { +if (flags & ~(RTE_STACK_F_LF |RTE_STACK_F_PILE )) { +STACK_LOG_ERR ("Unsupported stack flags %#x" , flags); +return NULL ; + } +if ((flags &RTE_STACK_F_LF ) && (flags &RTE_STACK_F_PILE )) {STACK_LOG_ERR ("Unsupported stack flags %#x" , flags);return NULL ; }
@@ -73,6 +82,13 @@ rte_stack_create(const char *name, unsigned int count, int socket_id, return NULL; }#endif +#if !defined(RTE_STACK_PILE_SUPPORTED ) +if (flags &RTE_STACK_F_PILE ) { +STACK_LOG_ERR ("Pile is not supported on your platform" ); + rte_errno =ENOTSUP ; +return NULL ; + } +#endif sz =rte_stack_get_memsize (count, flags);
lib/stack/rte_stack.h +65 -0
125 lines not on this page · raw message
lib/stack/rte_stack_lf.h +22 -0
@@ -20,6 +20,27 @@#define RTE_STACK_LF_SUPPORTED #endif +static __rte_always_inline unsigned int +__rte_stack_lf_count (struct rte_stack *s) +{ +/* stack_lf_push() and stack_lf_pop() do not update the list's contents +* and stack_lf->len atomically, which can cause the list to appear +* shorter than it actually is if this function is called while other +* threads are modifying the list. +* +* However, given the inherently approximate nature of the get_count +* callback -- even if the list and its size were updated atomically, +* the size could change between when get_count executes and when the +* value is returned to the caller -- this is acceptable. +* +* The stack_lf->len updates are placed such that the list may appear to +* have fewer elements than it does, but will never appear to have more +* elements. If the mempool is near-empty to the point that this is a +* concern, the user should consider increasing the mempool size. +*/ +return __rte_stack_lf_elems_count (&s->stack_lf.used); +} +/** * @internal Push several objects on the lock-free stack (MT-safe). *
@@ -79,6 +100,7 @@ __rte_stack_lf_pop(struct rte_stack *s, void **obj_table, unsigned int n)return 0 ;/* Pop n used elements */ +__rte_assume (obj_table !=NULL ); first =__rte_stack_lf_pop_elems (&s->stack_lf.used, n, obj_table, &last);if (unlikely (first ==NULL ))
lib/stack/rte_stack_lf_c11.h +2 -18
@@ -36,25 +36,9 @@ rte_atomic128_cmp_exchange(rte_int128_t *dst,#endif /* RTE_TOOLCHAIN_MSVC */ static __rte_always_inline unsigned int -__rte_stack_lf_count (struct rte_stack *s) +__rte_stack_lf_elems_count (struct rte_stack_lf_list *list) { -/* stack_lf_push() and stack_lf_pop() do not update the list's contents -* and stack_lf->len atomically, which can cause the list to appear -* shorter than it actually is if this function is called while other -* threads are modifying the list. -* -* However, given the inherently approximate nature of the get_count -* callback -- even if the list and its size were updated atomically, -* the size could change between when get_count executes and when the -* value is returned to the caller -- this is acceptable. -* -* The stack_lf->len updates are placed such that the list may appear to -* have fewer elements than it does, but will never appear to have more -* elements. If the mempool is near-empty to the point that this is a -* concern, the user should consider increasing the mempool size. -*/ -return (unsigned int )rte_atomic_load_explicit (&s->stack_lf.used.len, - rte_memory_order_relaxed); +return (unsigned int )rte_atomic_load_explicit (&list->len, rte_memory_order_relaxed); }static __rte_always_inline void
lib/stack/rte_stack_lf_generic.h +2 -17
@@ -9,25 +9,10 @@#include <rte_prefetch.h> static __rte_always_inline unsigned int -__rte_stack_lf_count (struct rte_stack *s) +__rte_stack_lf_elems_count (struct rte_stack_lf_list *list) { -/* stack_lf_push() and stack_lf_pop() do not update the list's contents -* and stack_lf->len atomically, which can cause the list to appear -* shorter than it actually is if this function is called while other -* threads are modifying the list. -* -* However, given the inherently approximate nature of the get_count -* callback -- even if the list and its size were updated atomically, -* the size could change between when get_count executes and when the -* value is returned to the caller -- this is acceptable. -* -* The stack_lf->len updates are placed such that the list may appear to -* have fewer elements than it does, but will never appear to have more -* elements. If the mempool is near-empty to the point that this is a -* concern, the user should consider increasing the mempool size. -*/ /* NOTE: review for potential ordering optimization */ -return rte_atomic_load_explicit (&s->stack_lf.used.len, rte_memory_order_seq_cst); +return (unsigned int )rte_atomic_load_explicit (&list->len, rte_memory_order_seq_cst); }static __rte_always_inline void
lib/stack/rte_stack_lf_stubs.h +2 -2
@@ -8,9 +8,9 @@#include <rte_common.h> static __rte_always_inline unsigned int -__rte_stack_lf_count (struct rte_stack *s) +__rte_stack_lf_elems_count (struct rte_stack_lf_list *list) { -RTE_SET_USED (s); +RTE_SET_USED (list);return 0 ; }
lib/stack/rte_stack_pile.c new file +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2026 SmartShare Systems +*/ + +#include "rte_stack.h" + +void +rte_stack_pile_init (struct rte_stack *s,unsigned int count) +{ +unsigned int bulk = (count +RTE_STACK_PILE_BULK_SIZE -1 ) /RTE_STACK_PILE_BULK_SIZE ; +struct rte_stack_pile_bulk_elem *bulk_elems = + (struct rte_stack_pile_bulk_elem *)(s->stack_pile.elems); +struct rte_stack_lf_elem *solo_elems = (struct rte_stack_lf_elem *)&bulk_elems[bulk]; +unsigned int i; + +for (i =0 ; i < bulk; i++) +__rte_stack_pile_bulk_push_elems (&s->stack_pile.free_bulk, + &bulk_elems[i], &bulk_elems[i],1 ); +for (i =0 ; i < count; i++) +__rte_stack_lf_push_elems (&s->stack_pile.free_solo, + &solo_elems[i], &solo_elems[i],1 ); +} + +ssize_t +rte_stack_pile_get_memsize (unsigned int count) +{ +unsigned int bulk = (count +RTE_STACK_PILE_BULK_SIZE -1 ) /RTE_STACK_PILE_BULK_SIZE ; + ssize_t sz = offsetof(struct rte_stack, stack_pile.elems); + sz += bulk *sizeof (struct rte_stack_pile_bulk_elem ); + sz += count *sizeof (struct rte_stack_lf_elem ); + sz =RTE_CACHE_LINE_ROUNDUP (sz); + sz +=RTE_CACHE_GUARD_LINES *RTE_CACHE_LINE_SIZE ; + +return sz; +}
lib/stack/rte_stack_pile.h new file +359 -0
359 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v4 2/2 mempool: introduce pile driver
In reply to Morten BrørupAdded a new "pile" mempool driver, based on the high-performance lock-free "pile" stack variant.
Changed other stack drivers to call their specific push/pop functions, instead of calling the generic stack push/pop API.
---
v4:
* No changes.
v3:
* Call other stack types' specific push/pop functions. v2:
* There is no v2.
--- doc/guides/mempool/stack.rst | 12 ++- drivers/mempool/stack/rte_mempool_stack.c | 90 ++++++++++++++++++++--- lib/mempool/rte_mempool.h | 2 + lib/mempool/rte_mempool_ops.c | 27 +++++-- 4 files changed, 114 insertions(+), 17 deletions(-)
doc/guides/mempool/stack.rst +11 -1
@@ -1,5 +1,6 @@ .. SPDX-License-Identifier: BSD-3-Clause Copyright(c) 2020 Intel Corporation. + Copyright(c) 2026 SmartShare Systems. Stack Mempool Driver ====================
@@ -28,6 +29,12 @@ can be selected as described in :ref:`Mempool_Handlers`: The underlying **rte_stack** operates in lock-free mode. For more information please refer to :ref:`Stack_Library_LF_Stack`. +- ``pile`` + + The underlying **rte_stack** operates in lock-free mode, + and is optimized for bulks of objects. + For more information please refer to :ref:`Stack_Library_Pile`. + The standard stack outperforms the lock-free stack on average, however the standard stack is non-preemptive: if a mempool user is preempted while holding the stack lock, that thread will block all other mempool accesses until it
@@ -35,9 +42,12 @@ returns and releases the lock. As a result, an application using the standard stack whose threads can be preempted can suffer from brief, infrequent performance hiccups. -The lock-free stack, by design, is not susceptible to this problem; one thread can +The lock-free stack and the pile, by design, are not susceptible to this problem; one thread can be preempted at any point during a push or pop operation and will not impede the progress of any other thread. +The pile is not LIFO per object, but per bulk of objects. +Although the pile is optimized for bulks of objects, it can handle any request size. + For a more detailed description of the stack implementations, please refer to :doc:`/prog_guide/stack_lib`.
drivers/mempool/stack/rte_mempool_stack.c +80 -10
128 lines not on this page · raw message
lib/mempool/rte_mempool.h +2 -0
@@ -979,6 +979,8 @@ rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name,* - >=0: Success; return the index of the ops struct in the table. * - -EINVAL - some missing callbacks while registering ops struct. * - -ENOSPC - the maximum number of ops structs has been reached. +* - -ENAMETOOLONG - the name of the ops is too long. +* - -EEXIST - the name of the ops is already registered. */ int rte_mempool_register_ops (const struct rte_mempool_ops *ops);
lib/mempool/rte_mempool_ops.c +21 -6
@@ -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,21 @@ rte_mempool_register_ops(const struct rte_mempool_ops *h)return -EINVAL ; } -if (strlen (h->name) >=sizeof (ops->name) -1 ) { +if (strlen (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" , +RTE_MEMPOOL_LOG (ERR ,"%s(): mempool_ops <%s>: name too long" , __func__, h->name); - rte_errno =EEXIST ; -return -EEXIST ; +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++;
@@ -68,6 +77,9 @@ rte_mempool_register_ops(const struct rte_mempool_ops *h) ops->get_info = h->get_info; ops->dequeue_contig_blocks = h->dequeue_contig_blocks; +RTE_MEMPOOL_LOG (DEBUG , +"Registered mempool_ops[%u] <%s>" , ops_index, h->name); +rte_spinlock_unlock (&rte_mempool_ops_table.sl);return ops_index;
@@ -185,8 +197,11 @@ rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name, } } -if (ops ==NULL ) +if (ops ==NULL ) { +RTE_MEMPOOL_LOG (ERR , +"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 v4 2/2] mempool: introduce pile driver
Quoted 8 lines · On Tue, 1 Sep 2026 06:43:31 +0000 Morten Brørup <[email protected]>…
Added a new "pile" mempool driver, based on the high-performance lock-free "pile" stack variant.
Changed other stack drivers to call their specific push/pop functions, instead of calling the generic stack push/pop API.
Signed-off-by Morten Brørup <[email protected]>---
FYI - pile driver is a good funny name https://en.wikipedia.org/wiki/Pile_driver
Did a AI review with Fable 5.1 as experiment
Review of [PATCH v4 0/2] stack/mempool: introduce pile
Applied on top of d55ccd4 ("pci: remove deprecated catch-all flag"), built with -Dwerror=true, and ran stack_pile_autotest, stack_autotest and mempool_autotest; all pass on x86_64.
Patch 1/2: stack: introduce pile
Warning: the pile does not honour the capacity given to rte_stack_create(). Bulk and solo elements are separate resources (ceil(count / RTE_STACK_PILE_BULK_SIZE) bulk elements plus count solo elements), so the pile can hold objects in both pools at once. A pile created with count = 32 accepts 32 single-object pushes, then a further push of 32 succeeds, giving 64 objects stored while rte_stack_free_count() reports 0. Because count is rounded up to a whole number of bulk elements, a single push can also exceed
capacity: a pile created with count = 40 accepts one push of 64. The RTE_MIN() in __rte_stack_pile_count() hides this rather than preventing it:
return RTE_MIN((unsigned int)s->capacity, __rte_stack_lf_elems_count(&s->stack_pile.bulk) * RTE_STACK_PILE_BULK_SIZE + __rte_stack_lf_elems_count(&s->stack_pile.solo));
This is harmless for the mempool driver, which never holds more than mp->size objects, but RTE_STACK_F_PILE is a public flag on an API documented as a bounded stack, and callers that rely on push failing at capacity (as test_stack_basic() does) get different behaviour depending on how the objects were pushed. Either account for capacity explicitly, or document in the RTE_STACK_F_PILE Doxygen and in stack_lib.rst that the pile is only bounded when used through the mempool driver.
Warning: the change to RTE_MEMPOOL_MAX_OPS_IDX in lib/mempool/rte_mempool.h is unrelated to the stack library, is not mentioned in the commit message, and is only needed by patch 2. After this series there are 17 RTE_MEMPOOL_REGISTER_OPS() sites in the tree, so without the bump the pile driver's constructor fails with -ENOSPC. Please move it into the mempool patch (or a separate preparatory mempool patch) and say why in the log.
Warning: no release note. A new public stack flag and a new mempool driver both need an entry in doc/guides/rel_notes/release_26_11.rst.
Info: the deterministic tests only cover bulk sizes 1 and MAX_BULK. The fragmentation path (__rte_stack_pile_pop_frag) and the bulk-pop retry path are only reached through the random-size multi-thread test, which does not check contents. A short sequence such as push 32 then pop 5, pop 27, and push 5 then push 32 then pop 37 would cover both paths with a known expected result.
Info: in test_stack_push_pop() the "goto lifo" jumps backwards into the body of the preceding if block. It is legal C, but restructuring so that the LIFO check is a helper called from both branches would be easier to read.
Patch 2/2: mempool: introduce pile driver
Warning: the changes to rte_mempool_register_ops() and rte_mempool_set_ops_byname() (duplicate-name rejection, the strlen limit changing from >= to >, the new -ENAMETOOLONG return, log level changes) are unrelated to the pile driver and are not mentioned in the commit message. The strlen change also alters behaviour: names of exactly RTE_MEMPOOL_OPS_NAMESIZE - 1 characters were previously rejected and are now accepted. These belong in a separate mempool patch with their own explanation.
Review-Result: WARNING
v5 cover introduce pile stack and mempool driver
In reply to Morten BrørupAdded a new high-performance lock-free "pile", using the Stack API, and an accompanying mempool driver.
Morten Brørup (2):
stack: introduce pile
mempool: introduce pile driver
app/test/test_stack.c | 263 +++++++++++++++- app/test/test_stack_perf.c | 15 +- config/rte_config.h | 3 + doc/guides/mempool/stack.rst | 12 +- doc/guides/prog_guide/stack_lib.rst | 68 +++- doc/guides/rel_notes/release_26_11.rst | 7 + drivers/mempool/stack/rte_mempool_stack.c | 90 +++++- lib/mempool/rte_mempool.h | 2 +- lib/stack/meson.build | 3 +- lib/stack/rte_stack.c | 18 +- lib/stack/rte_stack.h | 66 ++++ lib/stack/rte_stack_lf.h | 22 ++ lib/stack/rte_stack_lf_c11.h | 20 +- lib/stack/rte_stack_lf_generic.h | 19 +- lib/stack/rte_stack_lf_stubs.h | 4 +- lib/stack/rte_stack_pile.c | 35 +++ lib/stack/rte_stack_pile.h | 359 ++++++++++++++++++++++ 17 files changed, 943 insertions(+), 63 deletions(-) create mode 100644 lib/stack/rte_stack_pile.c create mode 100644 lib/stack/rte_stack_pile.h
Signature · 1 line
2.43.0
PATCH v5 1/2 stack: introduce pile
Added a new high-performance lock-free "pile", using the Stack API. The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile time configurable bulk size is handled gracefully, but not as fast as complete bulks.
Performance examples, stack_pile_perf_autotest vs. stack_lf_autotest:
On a single core, pushing/popping 1 or 8 objects is similar speed. On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly faster. On four cores, pushing/popping 512 objects is 4x faster.
---
v5:
* Added release note. (AI advanced model)
* Moved increase of max number of mempool drivers from stack patch to mempool driver patch, where it belongs.
* Added to the stack library documentation that the pile is not strictly bounded by its size. (AI advanced model)
* Added to the description of RTE_STACK_F_PILE that the pile is not strictly bounded by its size. (AI advanced model)
* Added explicit fragmentation test. (AI advanced model)
* Added explicit retry test. (AI advanced model)
v4:
* Use inline instead of __rte_always_inline for push/pop functions, and let the compiler decide. (Konstantin, Stephen)
* Use inline instead of __rte_noinline for fragmentation (in pop function), and let the compiler decide.
v3:
* Embedded rte_stack_lf_elem parent class as first field in rte_stack_pile_bulk_elem inherited class, instead of copying all its fields. (Konstantin)
* Moved fragmentation (in pop function) into separate inline function. (Konstantin)
* Removed memory model #ifdefs by adding __rte_stack_lf_elems_count() function to the lock-free stack.
* Added TODO about potential improvement of bulk pop retry by implementing burst variant of pop_elems().
* Improved bulk pop retry algorithm by peeking at availability. v2:
* There is no v2.
--- app/test/test_stack.c | 263 +++++++++++++++++- app/test/test_stack_perf.c | 15 +- config/rte_config.h | 3 + doc/guides/prog_guide/stack_lib.rst | 68 ++++- doc/guides/rel_notes/release_26_11.rst | 6 + lib/stack/meson.build | 3 +- lib/stack/rte_stack.c | 18 +- lib/stack/rte_stack.h | 66 +++++ lib/stack/rte_stack_lf.h | 22 ++ lib/stack/rte_stack_lf_c11.h | 20 +- lib/stack/rte_stack_lf_generic.h | 19 +- lib/stack/rte_stack_lf_stubs.h | 4 +- lib/stack/rte_stack_pile.c | 35 +++ lib/stack/rte_stack_pile.h | 359 +++++++++++++++++++++++++ 14 files changed, 850 insertions(+), 51 deletions(-) create mode 100644 lib/stack/rte_stack_pile.c create mode 100644 lib/stack/rte_stack_pile.h
app/test/test_stack.c +257 -6
@@ -11,8 +11,8 @@#include "test.h" -#define STACK_SIZE 4096 -#define MAX_BULK 32 +#define STACK_SIZE 65536 +#define MAX_BULK 512static int test_stack_push_pop (struct rte_stack *s,void **obj_table,unsigned int bulk_sz)
@@ -81,11 +81,220 @@ test_stack_push_pop(struct rte_stack *s, void **obj_table, unsigned int bulk_sz) } } -for (i =0 ; i <STACK_SIZE ; i++) { -if (obj_table[i] != popped_objs[STACK_SIZE - i -1 ]) { +if (!(s->flags &RTE_STACK_F_PILE )) { +/* Normal stack. */ +lifo: +for (i =0 ; i <STACK_SIZE ; i++) { +if (obj_table[i] != popped_objs[STACK_SIZE - i -1 ]) { +printf ("[%s():%u] Incorrect value %p at index 0x%x\n" , + __func__, __LINE__, + popped_objs[STACK_SIZE - i -1 ], i); +rte_free (popped_objs); +return -1 ; + } + } + }else { +/* Pile. Ordering not strictly LIFO. */ +if (bulk_sz <RTE_STACK_PILE_BULK_SIZE ) + goto lifo; +if ((bulk_sz & (RTE_STACK_PILE_BULK_SIZE -1 )) ==0 ) { +for (i =0 ; i <STACK_SIZE ; i +=RTE_STACK_PILE_BULK_SIZE ) { +if (memcmp (&obj_table[i], + &popped_objs[STACK_SIZE -RTE_STACK_PILE_BULK_SIZE - + i], +sizeof (void *) *RTE_STACK_PILE_BULK_SIZE ) !=0 ) { +printf ("[%s():%u] Incorrect values %p at 0x%x, bulk %u\n" , + __func__, __LINE__, + popped_objs[STACK_SIZE -RTE_STACK_PILE_BULK_SIZE - + i], + i, bulk_sz); +rte_free (popped_objs); +return -1 ; + } + } + } + } + +rte_free (popped_objs); + +return 0 ; +} + +static int +test_stack_pile_fragmentation (struct rte_stack *s,void **obj_table) +{ +unsigned int ret; +void **popped_objs; + + popped_objs =rte_calloc (NULL ,STACK_SIZE ,sizeof (void *),0 ); +if (popped_objs ==NULL ) { +printf ("[%s():%u] failed to calloc %zu bytes\n" , + __func__, __LINE__,STACK_SIZE *sizeof (void *)); +return -1 ; + } + + ret =rte_stack_push (s, &obj_table[0 ],RTE_STACK_PILE_BULK_SIZE ); +if (ret !=RTE_STACK_PILE_BULK_SIZE ) { +printf ("[%s():%u] push returned: %d (expected %u)\n" , + __func__, __LINE__, ret,RTE_STACK_PILE_BULK_SIZE ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_count (s) !=RTE_STACK_PILE_BULK_SIZE ) { +printf ("[%s():%u] stack count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +RTE_STACK_PILE_BULK_SIZE ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_free_count (s) !=STACK_SIZE -RTE_STACK_PILE_BULK_SIZE ) { +printf ("[%s():%u] stack free count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +STACK_SIZE -RTE_STACK_PILE_BULK_SIZE ); +rte_free (popped_objs); +return -1 ; + } + + ret =rte_stack_pop (s, &popped_objs[RTE_STACK_PILE_BULK_SIZE -3 ],3 ); +if (ret !=3 ) { +printf ("[%s():%u] pop returned: %d (expected %u)\n" , + __func__, __LINE__, ret,3 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_count (s) !=RTE_STACK_PILE_BULK_SIZE -3 ) { +printf ("[%s():%u] stack count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +RTE_STACK_PILE_BULK_SIZE -3 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_free_count (s) !=STACK_SIZE - (RTE_STACK_PILE_BULK_SIZE -3 )) { +printf ("[%s():%u] stack free count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +STACK_SIZE - (RTE_STACK_PILE_BULK_SIZE -3 )); +rte_free (popped_objs); +return -1 ; + } + + ret =rte_stack_pop (s, &popped_objs[0 ],RTE_STACK_PILE_BULK_SIZE -3 ); +if (ret !=RTE_STACK_PILE_BULK_SIZE -3 ) { +printf ("[%s():%u] pop returned: %d (expected %u)\n" , + __func__, __LINE__, ret,RTE_STACK_PILE_BULK_SIZE -3 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_count (s) !=0 ) { +printf ("[%s():%u] stack count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +0 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_free_count (s) !=STACK_SIZE ) { +printf ("[%s():%u] stack free count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +STACK_SIZE ); +rte_free (popped_objs); +return -1 ; + } + +if (memcmp (obj_table, popped_objs,sizeof (void *) *RTE_STACK_PILE_BULK_SIZE ) !=0 ) { +printf ("[%s():%u] Incorrect values\n" , + __func__, __LINE__); +rte_free (popped_objs); +return -1 ; + } + +rte_free (popped_objs); + +return 0 ; +} + +static int +test_stack_pile_retry (struct rte_stack *s,void **obj_table) +{ +unsigned int i, ret; +void **popped_objs; + + popped_objs =rte_calloc (NULL ,STACK_SIZE ,sizeof (void *),0 ); +if (popped_objs ==NULL ) { +printf ("[%s():%u] failed to calloc %zu bytes\n" , + __func__, __LINE__,STACK_SIZE *sizeof (void *)); +return -1 ; + } + + ret =rte_stack_push (s, &obj_table[0 ],3 ); +if (ret !=3 ) { +printf ("[%s():%u] push returned: %d (expected %u)\n" , + __func__, __LINE__, ret,3 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_count (s) !=3 ) { +printf ("[%s():%u] stack count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +3 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_free_count (s) !=STACK_SIZE -3 ) { +printf ("[%s():%u] stack free count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +STACK_SIZE -3 ); +rte_free (popped_objs); +return -1 ; + } + + ret =rte_stack_push (s, &obj_table[3 ],RTE_STACK_PILE_BULK_SIZE -3 ); +if (ret !=RTE_STACK_PILE_BULK_SIZE -3 ) { +printf ("[%s():%u] push returned: %d (expected %u)\n" , + __func__, __LINE__, ret,RTE_STACK_PILE_BULK_SIZE -3 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_count (s) !=RTE_STACK_PILE_BULK_SIZE ) { +printf ("[%s():%u] stack count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +RTE_STACK_PILE_BULK_SIZE ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_free_count (s) !=STACK_SIZE -RTE_STACK_PILE_BULK_SIZE ) { +printf ("[%s():%u] stack free count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +STACK_SIZE -RTE_STACK_PILE_BULK_SIZE ); +rte_free (popped_objs); +return -1 ; + } + + ret =rte_stack_pop (s, &popped_objs[0 ],RTE_STACK_PILE_BULK_SIZE ); +if (ret !=RTE_STACK_PILE_BULK_SIZE ) { +printf ("[%s():%u] pop returned: %d (expected %u)\n" , + __func__, __LINE__, ret,RTE_STACK_PILE_BULK_SIZE ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_count (s) !=0 ) { +printf ("[%s():%u] stack count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +0 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_free_count (s) !=STACK_SIZE ) { +printf ("[%s():%u] stack free count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +STACK_SIZE ); +rte_free (popped_objs); +return -1 ; + } + +for (i =0 ; i <RTE_STACK_PILE_BULK_SIZE ; i++) { +if (obj_table[i] != popped_objs[RTE_STACK_PILE_BULK_SIZE - i -1 ]) {printf ("[%s():%u] Incorrect value %p at index 0x%x\n" , __func__, __LINE__, - popped_objs[STACK_SIZE - i -1 ], i); + popped_objs[RTE_STACK_PILE_BULK_SIZE - i -1 ], i);rte_free (popped_objs);return -1 ; }
@@ -152,12 +361,39 @@ test_stack_basic(uint32_t flags) goto fail_test; } - ret =rte_stack_push (s, obj_table,2 *STACK_SIZE ); +if (s->flags &RTE_STACK_F_PILE ) { + ret =test_stack_pile_fragmentation (s, obj_table); +if (ret) { +printf ("[%s():%u] Pile push/pop fragmentation failed\n" , + __func__, __LINE__); + goto fail_test; + } + ret =test_stack_pile_retry (s, obj_table); +if (ret) { +printf ("[%s():%u] Pile push/pop retry failed\n" , + __func__, __LINE__); + goto fail_test; + } + } + + ret =rte_stack_push (s, obj_table,STACK_SIZE ); +if (ret ==0 ) { +printf ("[%s():%u] All objects push failed\n" , + __func__, __LINE__); + goto fail_test; + } + ret =rte_stack_push (s, obj_table,STACK_SIZE );if (ret !=0 ) {printf ("[%s():%u] Excess objects push succeeded\n" , __func__, __LINE__); goto fail_test; } + ret =rte_stack_pop (s, obj_table,STACK_SIZE ); +if (ret ==0 ) { +printf ("[%s():%u] All objects pop failed\n" , + __func__, __LINE__); + goto fail_test; + } ret =rte_stack_pop (s, obj_table,1 );if (ret !=0 ) {
@@ -167,8 +403,12 @@ test_stack_basic(uint32_t flags) } ret =0 ; + goto done; fail_test: + ret =-1 ; + +done:rte_stack_free (s);rte_free (obj_table);
@@ -384,5 +624,16 @@ test_lf_stack(void)#endif } +static int +test_pile (void ) +{ +#if defined(RTE_STACK_PILE_SUPPORTED ) +return __test_stack (RTE_STACK_F_PILE ); +#else +return TEST_SKIPPED ; +#endif +} +REGISTER_FAST_TEST (stack_autotest,NOHUGE_SKIP ,ASAN_OK , test_stack);REGISTER_FAST_TEST (stack_lf_autotest,NOHUGE_SKIP ,ASAN_OK , test_lf_stack); +REGISTER_FAST_TEST (stack_pile_autotest,NOHUGE_SKIP ,ASAN_OK , test_pile);
app/test/test_stack_perf.c +13 -2
@@ -14,14 +14,14 @@#include "test.h" #define STACK_NAME "STACK_PERF" -#define MAX_BURST 32 +#define MAX_BURST 512#define STACK_SIZE (RTE_MAX_LCORE * MAX_BURST)/* * Push/pop bulk sizes, marked volatile so they aren't treated as compile-time * constants. */ -static volatile unsigned int bulk_sizes[] = {8 ,MAX_BURST }; +static volatile unsigned int bulk_sizes[] = {1 ,8 ,32 ,MAX_BURST };static RTE_ATOMIC (uint32_t ) lcore_barrier;
@@ -354,5 +354,16 @@ test_lf_stack_perf(void)#endif } +static int +test_pile_perf (void ) +{ +#if defined(RTE_STACK_PILE_SUPPORTED ) +return __test_stack_perf (RTE_STACK_F_PILE ); +#else +return TEST_SKIPPED ; +#endif +} +REGISTER_PERF_TEST (stack_perf_autotest, test_stack_perf);REGISTER_PERF_TEST (stack_lf_perf_autotest, test_lf_stack_perf); +REGISTER_PERF_TEST (stack_pile_perf_autotest, test_pile_perf);
config/rte_config.h +3 -0
@@ -64,6 +64,9 @@#define RTE_MBUF_DEFAULT_MEMPOOL_OPS "ring_mp_mc"/* RTE_MBUF_HISTORY_DEBUG is not set */ +/* stack defines */ +#define RTE_STACK_PILE_BULK_SIZE 32 +/* ether defines */ #define RTE_MAX_QUEUES_PER_PORT 1024#define RTE_ETHDEV_RXTX_CALLBACKS 1
doc/guides/prog_guide/stack_lib.rst +64 -4
96 lines not on this page · raw message
doc/guides/rel_notes/release_26_11.rst +6 -0
@@ -55,6 +55,12 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* stack: Introduced "pile", a lock-free, stack-like implementation, + optimized for bulk operations. + The pile is only LIFO on bulk level, not on object level; i.e. arrays of bulks are + pushed and popped in LIFO manner, but objects within each bulk are not ordered + as expected by a stack. + Furthermore, it is not strictly bounded by its size, but might hold more objects. Removed Items -------------
lib/stack/meson.build +2 -1
@@ -1,7 +1,7 @@ # SPDX-License-Identifier: BSD-3-Clause # Copyright(c) 2019 Intel Corporation -sources = files('rte_stack.c', 'rte_stack_std.c', 'rte_stack_lf.c') +sources = files('rte_stack.c', 'rte_stack_std.c', 'rte_stack_lf.c', 'rte_stack_pile.c') headers = files('rte_stack.h') # subheaders, not for direct inclusion by apps indirect_headers += files(
@@ -10,4 +10,5 @@ indirect_headers += files( 'rte_stack_lf_generic.h', 'rte_stack_lf_c11.h', 'rte_stack_lf_stubs.h', + 'rte_stack_pile.h', )
lib/stack/rte_stack.c +17 -1
@@ -1,5 +1,6 @@/* SPDX-License-Identifier: BSD-3-Clause * Copyright(c) 2019 Intel Corporation +* Copyright(c) 2026 SmartShare Systems */ #include <stdalign.h>
@@ -32,6 +33,8 @@ rte_stack_init(struct rte_stack *s, unsigned int count, uint32_t flags)if (flags &RTE_STACK_F_LF )rte_stack_lf_init (s, count); +else if (flags &RTE_STACK_F_PILE ) +rte_stack_pile_init (s, count);else rte_stack_std_init (s); }
@@ -41,6 +44,8 @@ rte_stack_get_memsize(unsigned int count, uint32_t flags) {if (flags &RTE_STACK_F_LF )return rte_stack_lf_get_memsize (count); +else if (flags &RTE_STACK_F_PILE ) +return rte_stack_pile_get_memsize (count);else return rte_stack_std_get_memsize (count); }
@@ -58,7 +63,11 @@ rte_stack_create(const char *name, unsigned int count, int socket_id,unsigned int sz;int ret; -if (flags & ~(RTE_STACK_F_LF )) { +if (flags & ~(RTE_STACK_F_LF |RTE_STACK_F_PILE )) { +STACK_LOG_ERR ("Unsupported stack flags %#x" , flags); +return NULL ; + } +if ((flags &RTE_STACK_F_LF ) && (flags &RTE_STACK_F_PILE )) {STACK_LOG_ERR ("Unsupported stack flags %#x" , flags);return NULL ; }
@@ -73,6 +82,13 @@ rte_stack_create(const char *name, unsigned int count, int socket_id, return NULL; }#endif +#if !defined(RTE_STACK_PILE_SUPPORTED ) +if (flags &RTE_STACK_F_PILE ) { +STACK_LOG_ERR ("Pile is not supported on your platform" ); + rte_errno =ENOTSUP ; +return NULL ; + } +#endif sz =rte_stack_get_memsize (count, flags);
lib/stack/rte_stack.h +66 -0
126 lines not on this page · raw message
lib/stack/rte_stack_lf.h +22 -0
@@ -20,6 +20,27 @@#define RTE_STACK_LF_SUPPORTED #endif +static __rte_always_inline unsigned int +__rte_stack_lf_count (struct rte_stack *s) +{ +/* stack_lf_push() and stack_lf_pop() do not update the list's contents +* and stack_lf->len atomically, which can cause the list to appear +* shorter than it actually is if this function is called while other +* threads are modifying the list. +* +* However, given the inherently approximate nature of the get_count +* callback -- even if the list and its size were updated atomically, +* the size could change between when get_count executes and when the +* value is returned to the caller -- this is acceptable. +* +* The stack_lf->len updates are placed such that the list may appear to +* have fewer elements than it does, but will never appear to have more +* elements. If the mempool is near-empty to the point that this is a +* concern, the user should consider increasing the mempool size. +*/ +return __rte_stack_lf_elems_count (&s->stack_lf.used); +} +/** * @internal Push several objects on the lock-free stack (MT-safe). *
@@ -79,6 +100,7 @@ __rte_stack_lf_pop(struct rte_stack *s, void **obj_table, unsigned int n)return 0 ;/* Pop n used elements */ +__rte_assume (obj_table !=NULL ); first =__rte_stack_lf_pop_elems (&s->stack_lf.used, n, obj_table, &last);if (unlikely (first ==NULL ))
lib/stack/rte_stack_lf_c11.h +2 -18
@@ -36,25 +36,9 @@ rte_atomic128_cmp_exchange(rte_int128_t *dst,#endif /* RTE_TOOLCHAIN_MSVC */ static __rte_always_inline unsigned int -__rte_stack_lf_count (struct rte_stack *s) +__rte_stack_lf_elems_count (struct rte_stack_lf_list *list) { -/* stack_lf_push() and stack_lf_pop() do not update the list's contents -* and stack_lf->len atomically, which can cause the list to appear -* shorter than it actually is if this function is called while other -* threads are modifying the list. -* -* However, given the inherently approximate nature of the get_count -* callback -- even if the list and its size were updated atomically, -* the size could change between when get_count executes and when the -* value is returned to the caller -- this is acceptable. -* -* The stack_lf->len updates are placed such that the list may appear to -* have fewer elements than it does, but will never appear to have more -* elements. If the mempool is near-empty to the point that this is a -* concern, the user should consider increasing the mempool size. -*/ -return (unsigned int )rte_atomic_load_explicit (&s->stack_lf.used.len, - rte_memory_order_relaxed); +return (unsigned int )rte_atomic_load_explicit (&list->len, rte_memory_order_relaxed); }static __rte_always_inline void
lib/stack/rte_stack_lf_generic.h +2 -17
@@ -9,25 +9,10 @@#include <rte_prefetch.h> static __rte_always_inline unsigned int -__rte_stack_lf_count (struct rte_stack *s) +__rte_stack_lf_elems_count (struct rte_stack_lf_list *list) { -/* stack_lf_push() and stack_lf_pop() do not update the list's contents -* and stack_lf->len atomically, which can cause the list to appear -* shorter than it actually is if this function is called while other -* threads are modifying the list. -* -* However, given the inherently approximate nature of the get_count -* callback -- even if the list and its size were updated atomically, -* the size could change between when get_count executes and when the -* value is returned to the caller -- this is acceptable. -* -* The stack_lf->len updates are placed such that the list may appear to -* have fewer elements than it does, but will never appear to have more -* elements. If the mempool is near-empty to the point that this is a -* concern, the user should consider increasing the mempool size. -*/ /* NOTE: review for potential ordering optimization */ -return rte_atomic_load_explicit (&s->stack_lf.used.len, rte_memory_order_seq_cst); +return (unsigned int )rte_atomic_load_explicit (&list->len, rte_memory_order_seq_cst); }static __rte_always_inline void
lib/stack/rte_stack_lf_stubs.h +2 -2
@@ -8,9 +8,9 @@#include <rte_common.h> static __rte_always_inline unsigned int -__rte_stack_lf_count (struct rte_stack *s) +__rte_stack_lf_elems_count (struct rte_stack_lf_list *list) { -RTE_SET_USED (s); +RTE_SET_USED (list);return 0 ; }
lib/stack/rte_stack_pile.c new file +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2026 SmartShare Systems +*/ + +#include "rte_stack.h" + +void +rte_stack_pile_init (struct rte_stack *s,unsigned int count) +{ +unsigned int bulk = (count +RTE_STACK_PILE_BULK_SIZE -1 ) /RTE_STACK_PILE_BULK_SIZE ; +struct rte_stack_pile_bulk_elem *bulk_elems = + (struct rte_stack_pile_bulk_elem *)(s->stack_pile.elems); +struct rte_stack_lf_elem *solo_elems = (struct rte_stack_lf_elem *)&bulk_elems[bulk]; +unsigned int i; + +for (i =0 ; i < bulk; i++) +__rte_stack_pile_bulk_push_elems (&s->stack_pile.free_bulk, + &bulk_elems[i], &bulk_elems[i],1 ); +for (i =0 ; i < count; i++) +__rte_stack_lf_push_elems (&s->stack_pile.free_solo, + &solo_elems[i], &solo_elems[i],1 ); +} + +ssize_t +rte_stack_pile_get_memsize (unsigned int count) +{ +unsigned int bulk = (count +RTE_STACK_PILE_BULK_SIZE -1 ) /RTE_STACK_PILE_BULK_SIZE ; + ssize_t sz = offsetof(struct rte_stack, stack_pile.elems); + sz += bulk *sizeof (struct rte_stack_pile_bulk_elem ); + sz += count *sizeof (struct rte_stack_lf_elem ); + sz =RTE_CACHE_LINE_ROUNDUP (sz); + sz +=RTE_CACHE_GUARD_LINES *RTE_CACHE_LINE_SIZE ; + +return sz; +}
lib/stack/rte_stack_pile.h new file +359 -0
359 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v5 2/2 mempool: introduce pile driver
In reply to Morten BrørupAdded a new "pile" mempool driver, based on the high-performance lock-free "pile" stack variant.
With this driver added, the max number of mempool drivers (RTE_MEMPOOL_MAX_OPS_IDX) would be exceeded, so it was increased from 16 to 32.
Changed the other stack drivers to call their specific push/pop functions, instead of calling the generic stack push/pop API.
---
v5:
* Added release note. (AI advanced model)
* Moved increase of max number of mempool drivers from stack patch to mempool driver patch, where it belongs.
* Mentioned increase of max number of mempool drivers in patch description.
* Removed the rte_mempool_register_ops() and rte_mempool_set_ops_byname() changes.
They are included in the "mempool: various cleanups" patch in the "mempool: cleanup, fixes, improvements and optimizations" series.
v4:
* No changes.
v3:
* Call other stack types' specific push/pop functions.
v2:
* There is no v2.
--- doc/guides/mempool/stack.rst | 12 ++- doc/guides/rel_notes/release_26_11.rst | 1 + drivers/mempool/stack/rte_mempool_stack.c | 90 ++++++++++++++++++++--- lib/mempool/rte_mempool.h | 2 +- 4 files changed, 93 insertions(+), 12 deletions(-)
doc/guides/mempool/stack.rst +11 -1
@@ -1,5 +1,6 @@ .. SPDX-License-Identifier: BSD-3-Clause Copyright(c) 2020 Intel Corporation. + Copyright(c) 2026 SmartShare Systems. Stack Mempool Driver ====================
@@ -28,6 +29,12 @@ can be selected as described in :ref:`Mempool_Handlers`: The underlying **rte_stack** operates in lock-free mode. For more information please refer to :ref:`Stack_Library_LF_Stack`. +- ``pile`` + + The underlying **rte_stack** operates in lock-free mode, + and is optimized for bulks of objects. + For more information please refer to :ref:`Stack_Library_Pile`. + The standard stack outperforms the lock-free stack on average, however the standard stack is non-preemptive: if a mempool user is preempted while holding the stack lock, that thread will block all other mempool accesses until it
@@ -35,9 +42,12 @@ returns and releases the lock. As a result, an application using the standard stack whose threads can be preempted can suffer from brief, infrequent performance hiccups. -The lock-free stack, by design, is not susceptible to this problem; one thread can +The lock-free stack and the pile, by design, are not susceptible to this problem; one thread can be preempted at any point during a push or pop operation and will not impede the progress of any other thread. +The pile is not LIFO per object, but per bulk of objects. +Although the pile is optimized for bulks of objects, it can handle any request size. + For a more detailed description of the stack implementations, please refer to :doc:`/prog_guide/stack_lib`.
doc/guides/rel_notes/release_26_11.rst +1 -0
@@ -61,6 +61,7 @@ New Features pushed and popped in LIFO manner, but objects within each bulk are not ordered as expected by a stack. Furthermore, it is not strictly bounded by its size, but might hold more objects. +* mempool: Added "pile" driver, using the lock-free "pile" stack-like implementation. Removed Items -------------
drivers/mempool/stack/rte_mempool_stack.c +80 -10
128 lines not on this page · raw message
lib/mempool/rte_mempool.h +1 -1
@@ -718,7 +718,7 @@ struct __rte_cache_aligned rte_mempool_ops {rte_mempool_dequeue_contig_blocks_t dequeue_contig_blocks; }; -#define RTE_MEMPOOL_MAX_OPS_IDX 16/**< Max registered ops structs */ +#define RTE_MEMPOOL_MAX_OPS_IDX 32/**< Max registered ops structs */ /** * Structure storing the table of registered ops structs, each of which contain
Signature · 1 line
2.43.0
v6 cover introduce pile stack and mempool driver
In reply to Morten BrørupAdded a new high-performance lock-free "pile", using the Stack API, and an accompanying mempool driver.
Morten Brørup (2):
stack: introduce pile
mempool: introduce pile driver
app/test/test_stack.c | 263 +++++++++++++++- app/test/test_stack_perf.c | 15 +- config/rte_config.h | 3 + doc/guides/mempool/stack.rst | 12 +- doc/guides/prog_guide/stack_lib.rst | 68 +++- doc/guides/rel_notes/release_26_11.rst | 7 + drivers/mempool/stack/rte_mempool_stack.c | 90 +++++- lib/mempool/rte_mempool.h | 2 +- lib/stack/meson.build | 3 +- lib/stack/rte_stack.c | 18 +- lib/stack/rte_stack.h | 66 ++++ lib/stack/rte_stack_lf.h | 22 ++ lib/stack/rte_stack_lf_c11.h | 20 +- lib/stack/rte_stack_lf_generic.h | 19 +- lib/stack/rte_stack_lf_stubs.h | 4 +- lib/stack/rte_stack_pile.c | 35 +++ lib/stack/rte_stack_pile.h | 359 ++++++++++++++++++++++ 17 files changed, 943 insertions(+), 63 deletions(-) create mode 100644 lib/stack/rte_stack_pile.c create mode 100644 lib/stack/rte_stack_pile.h
Signature · 1 line
2.43.0
PATCH v6 1/2 stack: introduce pile
Added a new high-performance lock-free "pile", using the Stack API. The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile time configurable bulk size is handled gracefully, but not as fast as complete bulks.
Performance examples, stack_pile_perf_autotest vs. stack_lf_autotest:
On a single core, pushing/popping 1 or 8 objects is similar speed. On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly faster. On four cores, pushing/popping 512 objects is 4x faster.
---
v6:
* Fix pile fragmentation test. (CI)
v5:
* Added release note. (AI advanced model)
* Moved increase of max number of mempool drivers from stack patch to mempool driver patch, where it belongs.
* Added to the stack library documentation that the pile is not strictly bounded by its size. (AI advanced model)
* Added to the description of RTE_STACK_F_PILE that the pile is not strictly bounded by its size. (AI advanced model)
* Added explicit fragmentation test. (AI advanced model)
* Added explicit retry test. (AI advanced model)
v4:
* Use inline instead of __rte_always_inline for push/pop functions, and let the compiler decide. (Konstantin, Stephen)
* Use inline instead of __rte_noinline for fragmentation (in pop function), and let the compiler decide.
v3:
* Embedded rte_stack_lf_elem parent class as first field in rte_stack_pile_bulk_elem inherited class, instead of copying all its fields. (Konstantin)
* Moved fragmentation (in pop function) into separate inline function. (Konstantin)
* Removed memory model #ifdefs by adding __rte_stack_lf_elems_count() function to the lock-free stack.
* Added TODO about potential improvement of bulk pop retry by implementing burst variant of pop_elems().
* Improved bulk pop retry algorithm by peeking at availability. v2:
* There is no v2.
--- app/test/test_stack.c | 263 +++++++++++++++- app/test/test_stack_perf.c | 15 +- config/rte_config.h | 3 + doc/guides/mempool/stack.rst | 12 +- doc/guides/prog_guide/stack_lib.rst | 68 +++- doc/guides/rel_notes/release_26_11.rst | 6 + drivers/mempool/stack/rte_mempool_stack.c | 90 +++++- lib/mempool/rte_mempool.h | 2 +- lib/stack/meson.build | 3 +- lib/stack/rte_stack.c | 18 +- lib/stack/rte_stack.h | 66 ++++ lib/stack/rte_stack_lf.h | 22 ++ lib/stack/rte_stack_lf_c11.h | 20 +- lib/stack/rte_stack_lf_generic.h | 19 +- lib/stack/rte_stack_lf_stubs.h | 4 +- lib/stack/rte_stack_pile.c | 35 +++ lib/stack/rte_stack_pile.h | 359 ++++++++++++++++++++++ 17 files changed, 942 insertions(+), 63 deletions(-) create mode 100644 lib/stack/rte_stack_pile.c create mode 100644 lib/stack/rte_stack_pile.h
app/test/test_stack.c +257 -6
@@ -11,8 +11,8 @@#include "test.h" -#define STACK_SIZE 4096 -#define MAX_BULK 32 +#define STACK_SIZE 65536 +#define MAX_BULK 512static int test_stack_push_pop (struct rte_stack *s,void **obj_table,unsigned int bulk_sz)
@@ -81,11 +81,220 @@ test_stack_push_pop(struct rte_stack *s, void **obj_table, unsigned int bulk_sz) } } -for (i =0 ; i <STACK_SIZE ; i++) { -if (obj_table[i] != popped_objs[STACK_SIZE - i -1 ]) { +if (!(s->flags &RTE_STACK_F_PILE )) { +/* Normal stack. */ +lifo: +for (i =0 ; i <STACK_SIZE ; i++) { +if (obj_table[i] != popped_objs[STACK_SIZE - i -1 ]) { +printf ("[%s():%u] Incorrect value %p at index 0x%x\n" , + __func__, __LINE__, + popped_objs[STACK_SIZE - i -1 ], i); +rte_free (popped_objs); +return -1 ; + } + } + }else { +/* Pile. Ordering not strictly LIFO. */ +if (bulk_sz <RTE_STACK_PILE_BULK_SIZE ) + goto lifo; +if ((bulk_sz & (RTE_STACK_PILE_BULK_SIZE -1 )) ==0 ) { +for (i =0 ; i <STACK_SIZE ; i +=RTE_STACK_PILE_BULK_SIZE ) { +if (memcmp (&obj_table[i], + &popped_objs[STACK_SIZE -RTE_STACK_PILE_BULK_SIZE - + i], +sizeof (void *) *RTE_STACK_PILE_BULK_SIZE ) !=0 ) { +printf ("[%s():%u] Incorrect values %p at 0x%x, bulk %u\n" , + __func__, __LINE__, + popped_objs[STACK_SIZE -RTE_STACK_PILE_BULK_SIZE - + i], + i, bulk_sz); +rte_free (popped_objs); +return -1 ; + } + } + } + } + +rte_free (popped_objs); + +return 0 ; +} + +static int +test_stack_pile_fragmentation (struct rte_stack *s,void **obj_table) +{ +unsigned int ret; +void **popped_objs; + + popped_objs =rte_calloc (NULL ,STACK_SIZE ,sizeof (void *),0 ); +if (popped_objs ==NULL ) { +printf ("[%s():%u] failed to calloc %zu bytes\n" , + __func__, __LINE__,STACK_SIZE *sizeof (void *)); +return -1 ; + } + + ret =rte_stack_push (s, &obj_table[0 ],RTE_STACK_PILE_BULK_SIZE ); +if (ret !=RTE_STACK_PILE_BULK_SIZE ) { +printf ("[%s():%u] push returned: %d (expected %u)\n" , + __func__, __LINE__, ret,RTE_STACK_PILE_BULK_SIZE ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_count (s) !=RTE_STACK_PILE_BULK_SIZE ) { +printf ("[%s():%u] stack count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +RTE_STACK_PILE_BULK_SIZE ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_free_count (s) !=STACK_SIZE -RTE_STACK_PILE_BULK_SIZE ) { +printf ("[%s():%u] stack free count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +STACK_SIZE -RTE_STACK_PILE_BULK_SIZE ); +rte_free (popped_objs); +return -1 ; + } + + ret =rte_stack_pop (s, &popped_objs[0 ],3 ); +if (ret !=3 ) { +printf ("[%s():%u] pop returned: %d (expected %u)\n" , + __func__, __LINE__, ret,3 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_count (s) !=RTE_STACK_PILE_BULK_SIZE -3 ) { +printf ("[%s():%u] stack count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +RTE_STACK_PILE_BULK_SIZE -3 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_free_count (s) !=STACK_SIZE - (RTE_STACK_PILE_BULK_SIZE -3 )) { +printf ("[%s():%u] stack free count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +STACK_SIZE - (RTE_STACK_PILE_BULK_SIZE -3 )); +rte_free (popped_objs); +return -1 ; + } + + ret =rte_stack_pop (s, &popped_objs[3 ],RTE_STACK_PILE_BULK_SIZE -3 ); +if (ret !=RTE_STACK_PILE_BULK_SIZE -3 ) { +printf ("[%s():%u] pop returned: %d (expected %u)\n" , + __func__, __LINE__, ret,RTE_STACK_PILE_BULK_SIZE -3 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_count (s) !=0 ) { +printf ("[%s():%u] stack count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +0 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_free_count (s) !=STACK_SIZE ) { +printf ("[%s():%u] stack free count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +STACK_SIZE ); +rte_free (popped_objs); +return -1 ; + } + +if (memcmp (obj_table, popped_objs,sizeof (void *) *RTE_STACK_PILE_BULK_SIZE ) !=0 ) { +printf ("[%s():%u] Incorrect values\n" , + __func__, __LINE__); +rte_free (popped_objs); +return -1 ; + } + +rte_free (popped_objs); + +return 0 ; +} + +static int +test_stack_pile_retry (struct rte_stack *s,void **obj_table) +{ +unsigned int i, ret; +void **popped_objs; + + popped_objs =rte_calloc (NULL ,STACK_SIZE ,sizeof (void *),0 ); +if (popped_objs ==NULL ) { +printf ("[%s():%u] failed to calloc %zu bytes\n" , + __func__, __LINE__,STACK_SIZE *sizeof (void *)); +return -1 ; + } + + ret =rte_stack_push (s, &obj_table[0 ],3 ); +if (ret !=3 ) { +printf ("[%s():%u] push returned: %d (expected %u)\n" , + __func__, __LINE__, ret,3 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_count (s) !=3 ) { +printf ("[%s():%u] stack count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +3 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_free_count (s) !=STACK_SIZE -3 ) { +printf ("[%s():%u] stack free count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +STACK_SIZE -3 ); +rte_free (popped_objs); +return -1 ; + } + + ret =rte_stack_push (s, &obj_table[3 ],RTE_STACK_PILE_BULK_SIZE -3 ); +if (ret !=RTE_STACK_PILE_BULK_SIZE -3 ) { +printf ("[%s():%u] push returned: %d (expected %u)\n" , + __func__, __LINE__, ret,RTE_STACK_PILE_BULK_SIZE -3 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_count (s) !=RTE_STACK_PILE_BULK_SIZE ) { +printf ("[%s():%u] stack count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +RTE_STACK_PILE_BULK_SIZE ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_free_count (s) !=STACK_SIZE -RTE_STACK_PILE_BULK_SIZE ) { +printf ("[%s():%u] stack free count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +STACK_SIZE -RTE_STACK_PILE_BULK_SIZE ); +rte_free (popped_objs); +return -1 ; + } + + ret =rte_stack_pop (s, &popped_objs[0 ],RTE_STACK_PILE_BULK_SIZE ); +if (ret !=RTE_STACK_PILE_BULK_SIZE ) { +printf ("[%s():%u] pop returned: %d (expected %u)\n" , + __func__, __LINE__, ret,RTE_STACK_PILE_BULK_SIZE ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_count (s) !=0 ) { +printf ("[%s():%u] stack count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +0 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_free_count (s) !=STACK_SIZE ) { +printf ("[%s():%u] stack free count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +STACK_SIZE ); +rte_free (popped_objs); +return -1 ; + } + +for (i =0 ; i <RTE_STACK_PILE_BULK_SIZE ; i++) { +if (obj_table[i] != popped_objs[RTE_STACK_PILE_BULK_SIZE - i -1 ]) {printf ("[%s():%u] Incorrect value %p at index 0x%x\n" , __func__, __LINE__, - popped_objs[STACK_SIZE - i -1 ], i); + popped_objs[RTE_STACK_PILE_BULK_SIZE - i -1 ], i);rte_free (popped_objs);return -1 ; }
@@ -152,12 +361,39 @@ test_stack_basic(uint32_t flags) goto fail_test; } - ret =rte_stack_push (s, obj_table,2 *STACK_SIZE ); +if (s->flags &RTE_STACK_F_PILE ) { + ret =test_stack_pile_fragmentation (s, obj_table); +if (ret) { +printf ("[%s():%u] Pile push/pop fragmentation failed\n" , + __func__, __LINE__); + goto fail_test; + } + ret =test_stack_pile_retry (s, obj_table); +if (ret) { +printf ("[%s():%u] Pile push/pop retry failed\n" , + __func__, __LINE__); + goto fail_test; + } + } + + ret =rte_stack_push (s, obj_table,STACK_SIZE ); +if (ret ==0 ) { +printf ("[%s():%u] All objects push failed\n" , + __func__, __LINE__); + goto fail_test; + } + ret =rte_stack_push (s, obj_table,STACK_SIZE );if (ret !=0 ) {printf ("[%s():%u] Excess objects push succeeded\n" , __func__, __LINE__); goto fail_test; } + ret =rte_stack_pop (s, obj_table,STACK_SIZE ); +if (ret ==0 ) { +printf ("[%s():%u] All objects pop failed\n" , + __func__, __LINE__); + goto fail_test; + } ret =rte_stack_pop (s, obj_table,1 );if (ret !=0 ) {
@@ -167,8 +403,12 @@ test_stack_basic(uint32_t flags) } ret =0 ; + goto done; fail_test: + ret =-1 ; + +done:rte_stack_free (s);rte_free (obj_table);
@@ -384,5 +624,16 @@ test_lf_stack(void)#endif } +static int +test_pile (void ) +{ +#if defined(RTE_STACK_PILE_SUPPORTED ) +return __test_stack (RTE_STACK_F_PILE ); +#else +return TEST_SKIPPED ; +#endif +} +REGISTER_FAST_TEST (stack_autotest,NOHUGE_SKIP ,ASAN_OK , test_stack);REGISTER_FAST_TEST (stack_lf_autotest,NOHUGE_SKIP ,ASAN_OK , test_lf_stack); +REGISTER_FAST_TEST (stack_pile_autotest,NOHUGE_SKIP ,ASAN_OK , test_pile);
app/test/test_stack_perf.c +13 -2
@@ -14,14 +14,14 @@#include "test.h" #define STACK_NAME "STACK_PERF" -#define MAX_BURST 32 +#define MAX_BURST 512#define STACK_SIZE (RTE_MAX_LCORE * MAX_BURST)/* * Push/pop bulk sizes, marked volatile so they aren't treated as compile-time * constants. */ -static volatile unsigned int bulk_sizes[] = {8 ,MAX_BURST }; +static volatile unsigned int bulk_sizes[] = {1 ,8 ,32 ,MAX_BURST };static RTE_ATOMIC (uint32_t ) lcore_barrier;
@@ -354,5 +354,16 @@ test_lf_stack_perf(void)#endif } +static int +test_pile_perf (void ) +{ +#if defined(RTE_STACK_PILE_SUPPORTED ) +return __test_stack_perf (RTE_STACK_F_PILE ); +#else +return TEST_SKIPPED ; +#endif +} +REGISTER_PERF_TEST (stack_perf_autotest, test_stack_perf);REGISTER_PERF_TEST (stack_lf_perf_autotest, test_lf_stack_perf); +REGISTER_PERF_TEST (stack_pile_perf_autotest, test_pile_perf);
config/rte_config.h +3 -0
@@ -64,6 +64,9 @@#define RTE_MBUF_DEFAULT_MEMPOOL_OPS "ring_mp_mc"/* RTE_MBUF_HISTORY_DEBUG is not set */ +/* stack defines */ +#define RTE_STACK_PILE_BULK_SIZE 32 +/* ether defines */ #define RTE_MAX_QUEUES_PER_PORT 1024#define RTE_ETHDEV_RXTX_CALLBACKS 1
doc/guides/mempool/stack.rst +11 -1
@@ -1,5 +1,6 @@ .. SPDX-License-Identifier: BSD-3-Clause Copyright(c) 2020 Intel Corporation. + Copyright(c) 2026 SmartShare Systems. Stack Mempool Driver ====================
@@ -28,6 +29,12 @@ can be selected as described in :ref:`Mempool_Handlers`: The underlying **rte_stack** operates in lock-free mode. For more information please refer to :ref:`Stack_Library_LF_Stack`. +- ``pile`` + + The underlying **rte_stack** operates in lock-free mode, + and is optimized for bulks of objects. + For more information please refer to :ref:`Stack_Library_Pile`. + The standard stack outperforms the lock-free stack on average, however the standard stack is non-preemptive: if a mempool user is preempted while holding the stack lock, that thread will block all other mempool accesses until it
@@ -35,9 +42,12 @@ returns and releases the lock. As a result, an application using the standard stack whose threads can be preempted can suffer from brief, infrequent performance hiccups. -The lock-free stack, by design, is not susceptible to this problem; one thread can +The lock-free stack and the pile, by design, are not susceptible to this problem; one thread can be preempted at any point during a push or pop operation and will not impede the progress of any other thread. +The pile is not LIFO per object, but per bulk of objects. +Although the pile is optimized for bulks of objects, it can handle any request size. + For a more detailed description of the stack implementations, please refer to :doc:`/prog_guide/stack_lib`.
doc/guides/prog_guide/stack_lib.rst +64 -4
96 lines not on this page · raw message
doc/guides/rel_notes/release_26_11.rst +6 -0
@@ -55,6 +55,12 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* stack: Introduced "pile", a lock-free, stack-like implementation, + optimized for bulk operations. + The pile is only LIFO on bulk level, not on object level; i.e. arrays of bulks are + pushed and popped in LIFO manner, but objects within each bulk are not ordered + as expected by a stack. + Furthermore, it is not strictly bounded by its size, but might hold more objects. Removed Items -------------
drivers/mempool/stack/rte_mempool_stack.c +80 -10
128 lines not on this page · raw message
lib/mempool/rte_mempool.h +1 -1
@@ -718,7 +718,7 @@ struct __rte_cache_aligned rte_mempool_ops {rte_mempool_dequeue_contig_blocks_t dequeue_contig_blocks; }; -#define RTE_MEMPOOL_MAX_OPS_IDX 16/**< Max registered ops structs */ +#define RTE_MEMPOOL_MAX_OPS_IDX 32/**< Max registered ops structs */ /** * Structure storing the table of registered ops structs, each of which contain
lib/stack/meson.build +2 -1
@@ -1,7 +1,7 @@ # SPDX-License-Identifier: BSD-3-Clause # Copyright(c) 2019 Intel Corporation -sources = files('rte_stack.c', 'rte_stack_std.c', 'rte_stack_lf.c') +sources = files('rte_stack.c', 'rte_stack_std.c', 'rte_stack_lf.c', 'rte_stack_pile.c') headers = files('rte_stack.h') # subheaders, not for direct inclusion by apps indirect_headers += files(
@@ -10,4 +10,5 @@ indirect_headers += files( 'rte_stack_lf_generic.h', 'rte_stack_lf_c11.h', 'rte_stack_lf_stubs.h', + 'rte_stack_pile.h', )
lib/stack/rte_stack.c +17 -1
@@ -1,5 +1,6 @@/* SPDX-License-Identifier: BSD-3-Clause * Copyright(c) 2019 Intel Corporation +* Copyright(c) 2026 SmartShare Systems */ #include <stdalign.h>
@@ -32,6 +33,8 @@ rte_stack_init(struct rte_stack *s, unsigned int count, uint32_t flags)if (flags &RTE_STACK_F_LF )rte_stack_lf_init (s, count); +else if (flags &RTE_STACK_F_PILE ) +rte_stack_pile_init (s, count);else rte_stack_std_init (s); }
@@ -41,6 +44,8 @@ rte_stack_get_memsize(unsigned int count, uint32_t flags) {if (flags &RTE_STACK_F_LF )return rte_stack_lf_get_memsize (count); +else if (flags &RTE_STACK_F_PILE ) +return rte_stack_pile_get_memsize (count);else return rte_stack_std_get_memsize (count); }
@@ -58,7 +63,11 @@ rte_stack_create(const char *name, unsigned int count, int socket_id,unsigned int sz;int ret; -if (flags & ~(RTE_STACK_F_LF )) { +if (flags & ~(RTE_STACK_F_LF |RTE_STACK_F_PILE )) { +STACK_LOG_ERR ("Unsupported stack flags %#x" , flags); +return NULL ; + } +if ((flags &RTE_STACK_F_LF ) && (flags &RTE_STACK_F_PILE )) {STACK_LOG_ERR ("Unsupported stack flags %#x" , flags);return NULL ; }
@@ -73,6 +82,13 @@ rte_stack_create(const char *name, unsigned int count, int socket_id, return NULL; }#endif +#if !defined(RTE_STACK_PILE_SUPPORTED ) +if (flags &RTE_STACK_F_PILE ) { +STACK_LOG_ERR ("Pile is not supported on your platform" ); + rte_errno =ENOTSUP ; +return NULL ; + } +#endif sz =rte_stack_get_memsize (count, flags);
lib/stack/rte_stack.h +66 -0
126 lines not on this page · raw message
lib/stack/rte_stack_lf.h +22 -0
@@ -20,6 +20,27 @@#define RTE_STACK_LF_SUPPORTED #endif +static __rte_always_inline unsigned int +__rte_stack_lf_count (struct rte_stack *s) +{ +/* stack_lf_push() and stack_lf_pop() do not update the list's contents +* and stack_lf->len atomically, which can cause the list to appear +* shorter than it actually is if this function is called while other +* threads are modifying the list. +* +* However, given the inherently approximate nature of the get_count +* callback -- even if the list and its size were updated atomically, +* the size could change between when get_count executes and when the +* value is returned to the caller -- this is acceptable. +* +* The stack_lf->len updates are placed such that the list may appear to +* have fewer elements than it does, but will never appear to have more +* elements. If the mempool is near-empty to the point that this is a +* concern, the user should consider increasing the mempool size. +*/ +return __rte_stack_lf_elems_count (&s->stack_lf.used); +} +/** * @internal Push several objects on the lock-free stack (MT-safe). *
@@ -79,6 +100,7 @@ __rte_stack_lf_pop(struct rte_stack *s, void **obj_table, unsigned int n)return 0 ;/* Pop n used elements */ +__rte_assume (obj_table !=NULL ); first =__rte_stack_lf_pop_elems (&s->stack_lf.used, n, obj_table, &last);if (unlikely (first ==NULL ))
lib/stack/rte_stack_lf_c11.h +2 -18
@@ -36,25 +36,9 @@ rte_atomic128_cmp_exchange(rte_int128_t *dst,#endif /* RTE_TOOLCHAIN_MSVC */ static __rte_always_inline unsigned int -__rte_stack_lf_count (struct rte_stack *s) +__rte_stack_lf_elems_count (struct rte_stack_lf_list *list) { -/* stack_lf_push() and stack_lf_pop() do not update the list's contents -* and stack_lf->len atomically, which can cause the list to appear -* shorter than it actually is if this function is called while other -* threads are modifying the list. -* -* However, given the inherently approximate nature of the get_count -* callback -- even if the list and its size were updated atomically, -* the size could change between when get_count executes and when the -* value is returned to the caller -- this is acceptable. -* -* The stack_lf->len updates are placed such that the list may appear to -* have fewer elements than it does, but will never appear to have more -* elements. If the mempool is near-empty to the point that this is a -* concern, the user should consider increasing the mempool size. -*/ -return (unsigned int )rte_atomic_load_explicit (&s->stack_lf.used.len, - rte_memory_order_relaxed); +return (unsigned int )rte_atomic_load_explicit (&list->len, rte_memory_order_relaxed); }static __rte_always_inline void
lib/stack/rte_stack_lf_generic.h +2 -17
@@ -9,25 +9,10 @@#include <rte_prefetch.h> static __rte_always_inline unsigned int -__rte_stack_lf_count (struct rte_stack *s) +__rte_stack_lf_elems_count (struct rte_stack_lf_list *list) { -/* stack_lf_push() and stack_lf_pop() do not update the list's contents -* and stack_lf->len atomically, which can cause the list to appear -* shorter than it actually is if this function is called while other -* threads are modifying the list. -* -* However, given the inherently approximate nature of the get_count -* callback -- even if the list and its size were updated atomically, -* the size could change between when get_count executes and when the -* value is returned to the caller -- this is acceptable. -* -* The stack_lf->len updates are placed such that the list may appear to -* have fewer elements than it does, but will never appear to have more -* elements. If the mempool is near-empty to the point that this is a -* concern, the user should consider increasing the mempool size. -*/ /* NOTE: review for potential ordering optimization */ -return rte_atomic_load_explicit (&s->stack_lf.used.len, rte_memory_order_seq_cst); +return (unsigned int )rte_atomic_load_explicit (&list->len, rte_memory_order_seq_cst); }static __rte_always_inline void
lib/stack/rte_stack_lf_stubs.h +2 -2
@@ -8,9 +8,9 @@#include <rte_common.h> static __rte_always_inline unsigned int -__rte_stack_lf_count (struct rte_stack *s) +__rte_stack_lf_elems_count (struct rte_stack_lf_list *list) { -RTE_SET_USED (s); +RTE_SET_USED (list);return 0 ; }
lib/stack/rte_stack_pile.c new file +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2026 SmartShare Systems +*/ + +#include "rte_stack.h" + +void +rte_stack_pile_init (struct rte_stack *s,unsigned int count) +{ +unsigned int bulk = (count +RTE_STACK_PILE_BULK_SIZE -1 ) /RTE_STACK_PILE_BULK_SIZE ; +struct rte_stack_pile_bulk_elem *bulk_elems = + (struct rte_stack_pile_bulk_elem *)(s->stack_pile.elems); +struct rte_stack_lf_elem *solo_elems = (struct rte_stack_lf_elem *)&bulk_elems[bulk]; +unsigned int i; + +for (i =0 ; i < bulk; i++) +__rte_stack_pile_bulk_push_elems (&s->stack_pile.free_bulk, + &bulk_elems[i], &bulk_elems[i],1 ); +for (i =0 ; i < count; i++) +__rte_stack_lf_push_elems (&s->stack_pile.free_solo, + &solo_elems[i], &solo_elems[i],1 ); +} + +ssize_t +rte_stack_pile_get_memsize (unsigned int count) +{ +unsigned int bulk = (count +RTE_STACK_PILE_BULK_SIZE -1 ) /RTE_STACK_PILE_BULK_SIZE ; + ssize_t sz = offsetof(struct rte_stack, stack_pile.elems); + sz += bulk *sizeof (struct rte_stack_pile_bulk_elem ); + sz += count *sizeof (struct rte_stack_lf_elem ); + sz =RTE_CACHE_LINE_ROUNDUP (sz); + sz +=RTE_CACHE_GUARD_LINES *RTE_CACHE_LINE_SIZE ; + +return sz; +}
lib/stack/rte_stack_pile.h new file +359 -0
359 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v6 2/2 mempool: introduce pile driver
In reply to Morten BrørupAdded a new "pile" mempool driver, based on the high-performance lock-free "pile" stack variant.
With this driver added, the max number of mempool drivers (RTE_MEMPOOL_MAX_OPS_IDX) would be exceeded, so it was increased from 16 to 32.
Changed the other stack drivers to call their specific push/pop functions, instead of calling the generic stack push/pop API.
---
v6:
* No changes.
v5:
* Added release note. (AI advanced model)
* Moved increase of max number of mempool drivers from stack patch to mempool driver patch, where it belongs.
* Mentioned increase of max number of mempool drivers in patch description.
* Removed the rte_mempool_register_ops() and rte_mempool_set_ops_byname() changes.
They are included in the "mempool: various cleanups" patch in the "mempool: cleanup, fixes, improvements and optimizations" series.
v4:
* No changes.
v3:
* Call other stack types' specific push/pop functions.
v2:
* There is no v2.
--- doc/guides/rel_notes/release_26_11.rst | 1 + 1 file changed, 1 insertion(+)
doc/guides/rel_notes/release_26_11.rst +1 -0
@@ -61,6 +61,7 @@ New Features pushed and popped in LIFO manner, but objects within each bulk are not ordered as expected by a stack. Furthermore, it is not strictly bounded by its size, but might hold more objects. +* mempool: Added "pile" driver, using the lock-free "pile" stack-like implementation. Removed Items -------------
Signature · 1 line
2.43.0
v7 cover introduce pile stack and mempool driver
In reply to Morten BrørupAdded a new high-performance lock-free "pile", using the Stack API, and an accompanying mempool driver.
Morten Brørup (2):
stack: introduce pile
mempool: introduce pile driver
app/test/test_stack.c | 263 +++++++++++++++- app/test/test_stack_perf.c | 15 +- config/rte_config.h | 3 + doc/guides/mempool/stack.rst | 12 +- doc/guides/prog_guide/stack_lib.rst | 68 +++- doc/guides/rel_notes/release_26_11.rst | 7 + drivers/mempool/stack/rte_mempool_stack.c | 90 +++++- lib/mempool/rte_mempool.h | 2 +- lib/stack/meson.build | 3 +- lib/stack/rte_stack.c | 18 +- lib/stack/rte_stack.h | 66 ++++ lib/stack/rte_stack_lf.h | 22 ++ lib/stack/rte_stack_lf_c11.h | 20 +- lib/stack/rte_stack_lf_generic.h | 19 +- lib/stack/rte_stack_lf_stubs.h | 4 +- lib/stack/rte_stack_pile.c | 35 +++ lib/stack/rte_stack_pile.h | 359 ++++++++++++++++++++++ 17 files changed, 943 insertions(+), 63 deletions(-) create mode 100644 lib/stack/rte_stack_pile.c create mode 100644 lib/stack/rte_stack_pile.h
Signature · 1 line
2.43.0
PATCH v7 1/2 stack: introduce pile
Added a new high-performance lock-free "pile", using the Stack API. The pile behaves roughly like a stack, but is not strictly LIFO.
The pile is optimized for pushing/popping bulks of objects, which it does significantly faster than the lock-free stack.
Pushing/popping a number of objects not divisible by the compile time configurable bulk size is handled gracefully, but not as fast as complete bulks.
Performance examples, stack_pile_perf_autotest vs. stack_lf_autotest:
On a single core, pushing/popping 1 or 8 objects is similar speed. On a single core, pushing/popping 32 objects is 2x faster. On a single core, pushing/popping 512 objects is 10x faster.
On four cores, pushing/popping 1, 8 or 32 objects is slightly faster. On four cores, pushing/popping 512 objects is 4x faster.
---
v7:
* Actually moved increase of max number of mempool drivers from stack patch to mempool driver patch, where it belongs.
v6:
* Fix pile fragmentation test. (CI)
v5:
* Added release note. (AI advanced model)
* Moved increase of max number of mempool drivers from stack patch to mempool driver patch, where it belongs.
* Added to the stack library documentation that the pile is not strictly bounded by its size. (AI advanced model)
* Added to the description of RTE_STACK_F_PILE that the pile is not strictly bounded by its size. (AI advanced model)
* Added explicit fragmentation test. (AI advanced model)
* Added explicit retry test. (AI advanced model)
v4:
* Use inline instead of __rte_always_inline for push/pop functions, and let the compiler decide. (Konstantin, Stephen)
* Use inline instead of __rte_noinline for fragmentation (in pop function), and let the compiler decide.
v3:
* Embedded rte_stack_lf_elem parent class as first field in rte_stack_pile_bulk_elem inherited class, instead of copying all its fields. (Konstantin)
* Moved fragmentation (in pop function) into separate inline function. (Konstantin)
* Removed memory model #ifdefs by adding __rte_stack_lf_elems_count() function to the lock-free stack.
* Added TODO about potential improvement of bulk pop retry by implementing burst variant of pop_elems().
* Improved bulk pop retry algorithm by peeking at availability. v2:
* There is no v2.
--- app/test/test_stack.c | 263 +++++++++++++++++- app/test/test_stack_perf.c | 15 +- config/rte_config.h | 3 + doc/guides/prog_guide/stack_lib.rst | 68 ++++- doc/guides/rel_notes/release_26_11.rst | 6 + lib/stack/meson.build | 3 +- lib/stack/rte_stack.c | 18 +- lib/stack/rte_stack.h | 66 +++++ lib/stack/rte_stack_lf.h | 22 ++ lib/stack/rte_stack_lf_c11.h | 20 +- lib/stack/rte_stack_lf_generic.h | 19 +- lib/stack/rte_stack_lf_stubs.h | 4 +- lib/stack/rte_stack_pile.c | 35 +++ lib/stack/rte_stack_pile.h | 359 +++++++++++++++++++++++++ 14 files changed, 850 insertions(+), 51 deletions(-) create mode 100644 lib/stack/rte_stack_pile.c create mode 100644 lib/stack/rte_stack_pile.h
app/test/test_stack.c +257 -6
@@ -11,8 +11,8 @@#include "test.h" -#define STACK_SIZE 4096 -#define MAX_BULK 32 +#define STACK_SIZE 65536 +#define MAX_BULK 512static int test_stack_push_pop (struct rte_stack *s,void **obj_table,unsigned int bulk_sz)
@@ -81,11 +81,220 @@ test_stack_push_pop(struct rte_stack *s, void **obj_table, unsigned int bulk_sz) } } -for (i =0 ; i <STACK_SIZE ; i++) { -if (obj_table[i] != popped_objs[STACK_SIZE - i -1 ]) { +if (!(s->flags &RTE_STACK_F_PILE )) { +/* Normal stack. */ +lifo: +for (i =0 ; i <STACK_SIZE ; i++) { +if (obj_table[i] != popped_objs[STACK_SIZE - i -1 ]) { +printf ("[%s():%u] Incorrect value %p at index 0x%x\n" , + __func__, __LINE__, + popped_objs[STACK_SIZE - i -1 ], i); +rte_free (popped_objs); +return -1 ; + } + } + }else { +/* Pile. Ordering not strictly LIFO. */ +if (bulk_sz <RTE_STACK_PILE_BULK_SIZE ) + goto lifo; +if ((bulk_sz & (RTE_STACK_PILE_BULK_SIZE -1 )) ==0 ) { +for (i =0 ; i <STACK_SIZE ; i +=RTE_STACK_PILE_BULK_SIZE ) { +if (memcmp (&obj_table[i], + &popped_objs[STACK_SIZE -RTE_STACK_PILE_BULK_SIZE - + i], +sizeof (void *) *RTE_STACK_PILE_BULK_SIZE ) !=0 ) { +printf ("[%s():%u] Incorrect values %p at 0x%x, bulk %u\n" , + __func__, __LINE__, + popped_objs[STACK_SIZE -RTE_STACK_PILE_BULK_SIZE - + i], + i, bulk_sz); +rte_free (popped_objs); +return -1 ; + } + } + } + } + +rte_free (popped_objs); + +return 0 ; +} + +static int +test_stack_pile_fragmentation (struct rte_stack *s,void **obj_table) +{ +unsigned int ret; +void **popped_objs; + + popped_objs =rte_calloc (NULL ,STACK_SIZE ,sizeof (void *),0 ); +if (popped_objs ==NULL ) { +printf ("[%s():%u] failed to calloc %zu bytes\n" , + __func__, __LINE__,STACK_SIZE *sizeof (void *)); +return -1 ; + } + + ret =rte_stack_push (s, &obj_table[0 ],RTE_STACK_PILE_BULK_SIZE ); +if (ret !=RTE_STACK_PILE_BULK_SIZE ) { +printf ("[%s():%u] push returned: %d (expected %u)\n" , + __func__, __LINE__, ret,RTE_STACK_PILE_BULK_SIZE ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_count (s) !=RTE_STACK_PILE_BULK_SIZE ) { +printf ("[%s():%u] stack count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +RTE_STACK_PILE_BULK_SIZE ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_free_count (s) !=STACK_SIZE -RTE_STACK_PILE_BULK_SIZE ) { +printf ("[%s():%u] stack free count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +STACK_SIZE -RTE_STACK_PILE_BULK_SIZE ); +rte_free (popped_objs); +return -1 ; + } + + ret =rte_stack_pop (s, &popped_objs[0 ],3 ); +if (ret !=3 ) { +printf ("[%s():%u] pop returned: %d (expected %u)\n" , + __func__, __LINE__, ret,3 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_count (s) !=RTE_STACK_PILE_BULK_SIZE -3 ) { +printf ("[%s():%u] stack count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +RTE_STACK_PILE_BULK_SIZE -3 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_free_count (s) !=STACK_SIZE - (RTE_STACK_PILE_BULK_SIZE -3 )) { +printf ("[%s():%u] stack free count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +STACK_SIZE - (RTE_STACK_PILE_BULK_SIZE -3 )); +rte_free (popped_objs); +return -1 ; + } + + ret =rte_stack_pop (s, &popped_objs[3 ],RTE_STACK_PILE_BULK_SIZE -3 ); +if (ret !=RTE_STACK_PILE_BULK_SIZE -3 ) { +printf ("[%s():%u] pop returned: %d (expected %u)\n" , + __func__, __LINE__, ret,RTE_STACK_PILE_BULK_SIZE -3 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_count (s) !=0 ) { +printf ("[%s():%u] stack count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +0 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_free_count (s) !=STACK_SIZE ) { +printf ("[%s():%u] stack free count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +STACK_SIZE ); +rte_free (popped_objs); +return -1 ; + } + +if (memcmp (obj_table, popped_objs,sizeof (void *) *RTE_STACK_PILE_BULK_SIZE ) !=0 ) { +printf ("[%s():%u] Incorrect values\n" , + __func__, __LINE__); +rte_free (popped_objs); +return -1 ; + } + +rte_free (popped_objs); + +return 0 ; +} + +static int +test_stack_pile_retry (struct rte_stack *s,void **obj_table) +{ +unsigned int i, ret; +void **popped_objs; + + popped_objs =rte_calloc (NULL ,STACK_SIZE ,sizeof (void *),0 ); +if (popped_objs ==NULL ) { +printf ("[%s():%u] failed to calloc %zu bytes\n" , + __func__, __LINE__,STACK_SIZE *sizeof (void *)); +return -1 ; + } + + ret =rte_stack_push (s, &obj_table[0 ],3 ); +if (ret !=3 ) { +printf ("[%s():%u] push returned: %d (expected %u)\n" , + __func__, __LINE__, ret,3 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_count (s) !=3 ) { +printf ("[%s():%u] stack count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +3 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_free_count (s) !=STACK_SIZE -3 ) { +printf ("[%s():%u] stack free count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +STACK_SIZE -3 ); +rte_free (popped_objs); +return -1 ; + } + + ret =rte_stack_push (s, &obj_table[3 ],RTE_STACK_PILE_BULK_SIZE -3 ); +if (ret !=RTE_STACK_PILE_BULK_SIZE -3 ) { +printf ("[%s():%u] push returned: %d (expected %u)\n" , + __func__, __LINE__, ret,RTE_STACK_PILE_BULK_SIZE -3 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_count (s) !=RTE_STACK_PILE_BULK_SIZE ) { +printf ("[%s():%u] stack count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +RTE_STACK_PILE_BULK_SIZE ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_free_count (s) !=STACK_SIZE -RTE_STACK_PILE_BULK_SIZE ) { +printf ("[%s():%u] stack free count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +STACK_SIZE -RTE_STACK_PILE_BULK_SIZE ); +rte_free (popped_objs); +return -1 ; + } + + ret =rte_stack_pop (s, &popped_objs[0 ],RTE_STACK_PILE_BULK_SIZE ); +if (ret !=RTE_STACK_PILE_BULK_SIZE ) { +printf ("[%s():%u] pop returned: %d (expected %u)\n" , + __func__, __LINE__, ret,RTE_STACK_PILE_BULK_SIZE ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_count (s) !=0 ) { +printf ("[%s():%u] stack count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +0 ); +rte_free (popped_objs); +return -1 ; + } +if (rte_stack_free_count (s) !=STACK_SIZE ) { +printf ("[%s():%u] stack free count: %u (expected %u)\n" , + __func__, __LINE__,rte_stack_count (s), +STACK_SIZE ); +rte_free (popped_objs); +return -1 ; + } + +for (i =0 ; i <RTE_STACK_PILE_BULK_SIZE ; i++) { +if (obj_table[i] != popped_objs[RTE_STACK_PILE_BULK_SIZE - i -1 ]) {printf ("[%s():%u] Incorrect value %p at index 0x%x\n" , __func__, __LINE__, - popped_objs[STACK_SIZE - i -1 ], i); + popped_objs[RTE_STACK_PILE_BULK_SIZE - i -1 ], i);rte_free (popped_objs);return -1 ; }
@@ -152,12 +361,39 @@ test_stack_basic(uint32_t flags) goto fail_test; } - ret =rte_stack_push (s, obj_table,2 *STACK_SIZE ); +if (s->flags &RTE_STACK_F_PILE ) { + ret =test_stack_pile_fragmentation (s, obj_table); +if (ret) { +printf ("[%s():%u] Pile push/pop fragmentation failed\n" , + __func__, __LINE__); + goto fail_test; + } + ret =test_stack_pile_retry (s, obj_table); +if (ret) { +printf ("[%s():%u] Pile push/pop retry failed\n" , + __func__, __LINE__); + goto fail_test; + } + } + + ret =rte_stack_push (s, obj_table,STACK_SIZE ); +if (ret ==0 ) { +printf ("[%s():%u] All objects push failed\n" , + __func__, __LINE__); + goto fail_test; + } + ret =rte_stack_push (s, obj_table,STACK_SIZE );if (ret !=0 ) {printf ("[%s():%u] Excess objects push succeeded\n" , __func__, __LINE__); goto fail_test; } + ret =rte_stack_pop (s, obj_table,STACK_SIZE ); +if (ret ==0 ) { +printf ("[%s():%u] All objects pop failed\n" , + __func__, __LINE__); + goto fail_test; + } ret =rte_stack_pop (s, obj_table,1 );if (ret !=0 ) {
@@ -167,8 +403,12 @@ test_stack_basic(uint32_t flags) } ret =0 ; + goto done; fail_test: + ret =-1 ; + +done:rte_stack_free (s);rte_free (obj_table);
@@ -384,5 +624,16 @@ test_lf_stack(void)#endif } +static int +test_pile (void ) +{ +#if defined(RTE_STACK_PILE_SUPPORTED ) +return __test_stack (RTE_STACK_F_PILE ); +#else +return TEST_SKIPPED ; +#endif +} +REGISTER_FAST_TEST (stack_autotest,NOHUGE_SKIP ,ASAN_OK , test_stack);REGISTER_FAST_TEST (stack_lf_autotest,NOHUGE_SKIP ,ASAN_OK , test_lf_stack); +REGISTER_FAST_TEST (stack_pile_autotest,NOHUGE_SKIP ,ASAN_OK , test_pile);
app/test/test_stack_perf.c +13 -2
@@ -14,14 +14,14 @@#include "test.h" #define STACK_NAME "STACK_PERF" -#define MAX_BURST 32 +#define MAX_BURST 512#define STACK_SIZE (RTE_MAX_LCORE * MAX_BURST)/* * Push/pop bulk sizes, marked volatile so they aren't treated as compile-time * constants. */ -static volatile unsigned int bulk_sizes[] = {8 ,MAX_BURST }; +static volatile unsigned int bulk_sizes[] = {1 ,8 ,32 ,MAX_BURST };static RTE_ATOMIC (uint32_t ) lcore_barrier;
@@ -354,5 +354,16 @@ test_lf_stack_perf(void)#endif } +static int +test_pile_perf (void ) +{ +#if defined(RTE_STACK_PILE_SUPPORTED ) +return __test_stack_perf (RTE_STACK_F_PILE ); +#else +return TEST_SKIPPED ; +#endif +} +REGISTER_PERF_TEST (stack_perf_autotest, test_stack_perf);REGISTER_PERF_TEST (stack_lf_perf_autotest, test_lf_stack_perf); +REGISTER_PERF_TEST (stack_pile_perf_autotest, test_pile_perf);
config/rte_config.h +3 -0
@@ -64,6 +64,9 @@#define RTE_MBUF_DEFAULT_MEMPOOL_OPS "ring_mp_mc"/* RTE_MBUF_HISTORY_DEBUG is not set */ +/* stack defines */ +#define RTE_STACK_PILE_BULK_SIZE 32 +/* ether defines */ #define RTE_MAX_QUEUES_PER_PORT 1024#define RTE_ETHDEV_RXTX_CALLBACKS 1
doc/guides/prog_guide/stack_lib.rst +64 -4
96 lines not on this page · raw message
doc/guides/rel_notes/release_26_11.rst +6 -0
@@ -55,6 +55,12 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* stack: Introduced "pile", a lock-free, stack-like implementation, + optimized for bulk operations. + The pile is only LIFO on bulk level, not on object level; i.e. arrays of bulks are + pushed and popped in LIFO manner, but objects within each bulk are not ordered + as expected by a stack. + Furthermore, it is not strictly bounded by its size, but might hold more objects. Removed Items -------------
lib/stack/meson.build +2 -1
@@ -1,7 +1,7 @@ # SPDX-License-Identifier: BSD-3-Clause # Copyright(c) 2019 Intel Corporation -sources = files('rte_stack.c', 'rte_stack_std.c', 'rte_stack_lf.c') +sources = files('rte_stack.c', 'rte_stack_std.c', 'rte_stack_lf.c', 'rte_stack_pile.c') headers = files('rte_stack.h') # subheaders, not for direct inclusion by apps indirect_headers += files(
@@ -10,4 +10,5 @@ indirect_headers += files( 'rte_stack_lf_generic.h', 'rte_stack_lf_c11.h', 'rte_stack_lf_stubs.h', + 'rte_stack_pile.h', )
lib/stack/rte_stack.c +17 -1
@@ -1,5 +1,6 @@/* SPDX-License-Identifier: BSD-3-Clause * Copyright(c) 2019 Intel Corporation +* Copyright(c) 2026 SmartShare Systems */ #include <stdalign.h>
@@ -32,6 +33,8 @@ rte_stack_init(struct rte_stack *s, unsigned int count, uint32_t flags)if (flags &RTE_STACK_F_LF )rte_stack_lf_init (s, count); +else if (flags &RTE_STACK_F_PILE ) +rte_stack_pile_init (s, count);else rte_stack_std_init (s); }
@@ -41,6 +44,8 @@ rte_stack_get_memsize(unsigned int count, uint32_t flags) {if (flags &RTE_STACK_F_LF )return rte_stack_lf_get_memsize (count); +else if (flags &RTE_STACK_F_PILE ) +return rte_stack_pile_get_memsize (count);else return rte_stack_std_get_memsize (count); }
@@ -58,7 +63,11 @@ rte_stack_create(const char *name, unsigned int count, int socket_id,unsigned int sz;int ret; -if (flags & ~(RTE_STACK_F_LF )) { +if (flags & ~(RTE_STACK_F_LF |RTE_STACK_F_PILE )) { +STACK_LOG_ERR ("Unsupported stack flags %#x" , flags); +return NULL ; + } +if ((flags &RTE_STACK_F_LF ) && (flags &RTE_STACK_F_PILE )) {STACK_LOG_ERR ("Unsupported stack flags %#x" , flags);return NULL ; }
@@ -73,6 +82,13 @@ rte_stack_create(const char *name, unsigned int count, int socket_id, return NULL; }#endif +#if !defined(RTE_STACK_PILE_SUPPORTED ) +if (flags &RTE_STACK_F_PILE ) { +STACK_LOG_ERR ("Pile is not supported on your platform" ); + rte_errno =ENOTSUP ; +return NULL ; + } +#endif sz =rte_stack_get_memsize (count, flags);
lib/stack/rte_stack.h +66 -0
126 lines not on this page · raw message
lib/stack/rte_stack_lf.h +22 -0
@@ -20,6 +20,27 @@#define RTE_STACK_LF_SUPPORTED #endif +static __rte_always_inline unsigned int +__rte_stack_lf_count (struct rte_stack *s) +{ +/* stack_lf_push() and stack_lf_pop() do not update the list's contents +* and stack_lf->len atomically, which can cause the list to appear +* shorter than it actually is if this function is called while other +* threads are modifying the list. +* +* However, given the inherently approximate nature of the get_count +* callback -- even if the list and its size were updated atomically, +* the size could change between when get_count executes and when the +* value is returned to the caller -- this is acceptable. +* +* The stack_lf->len updates are placed such that the list may appear to +* have fewer elements than it does, but will never appear to have more +* elements. If the mempool is near-empty to the point that this is a +* concern, the user should consider increasing the mempool size. +*/ +return __rte_stack_lf_elems_count (&s->stack_lf.used); +} +/** * @internal Push several objects on the lock-free stack (MT-safe). *
@@ -79,6 +100,7 @@ __rte_stack_lf_pop(struct rte_stack *s, void **obj_table, unsigned int n)return 0 ;/* Pop n used elements */ +__rte_assume (obj_table !=NULL ); first =__rte_stack_lf_pop_elems (&s->stack_lf.used, n, obj_table, &last);if (unlikely (first ==NULL ))
lib/stack/rte_stack_lf_c11.h +2 -18
@@ -36,25 +36,9 @@ rte_atomic128_cmp_exchange(rte_int128_t *dst,#endif /* RTE_TOOLCHAIN_MSVC */ static __rte_always_inline unsigned int -__rte_stack_lf_count (struct rte_stack *s) +__rte_stack_lf_elems_count (struct rte_stack_lf_list *list) { -/* stack_lf_push() and stack_lf_pop() do not update the list's contents -* and stack_lf->len atomically, which can cause the list to appear -* shorter than it actually is if this function is called while other -* threads are modifying the list. -* -* However, given the inherently approximate nature of the get_count -* callback -- even if the list and its size were updated atomically, -* the size could change between when get_count executes and when the -* value is returned to the caller -- this is acceptable. -* -* The stack_lf->len updates are placed such that the list may appear to -* have fewer elements than it does, but will never appear to have more -* elements. If the mempool is near-empty to the point that this is a -* concern, the user should consider increasing the mempool size. -*/ -return (unsigned int )rte_atomic_load_explicit (&s->stack_lf.used.len, - rte_memory_order_relaxed); +return (unsigned int )rte_atomic_load_explicit (&list->len, rte_memory_order_relaxed); }static __rte_always_inline void
lib/stack/rte_stack_lf_generic.h +2 -17
@@ -9,25 +9,10 @@#include <rte_prefetch.h> static __rte_always_inline unsigned int -__rte_stack_lf_count (struct rte_stack *s) +__rte_stack_lf_elems_count (struct rte_stack_lf_list *list) { -/* stack_lf_push() and stack_lf_pop() do not update the list's contents -* and stack_lf->len atomically, which can cause the list to appear -* shorter than it actually is if this function is called while other -* threads are modifying the list. -* -* However, given the inherently approximate nature of the get_count -* callback -- even if the list and its size were updated atomically, -* the size could change between when get_count executes and when the -* value is returned to the caller -- this is acceptable. -* -* The stack_lf->len updates are placed such that the list may appear to -* have fewer elements than it does, but will never appear to have more -* elements. If the mempool is near-empty to the point that this is a -* concern, the user should consider increasing the mempool size. -*/ /* NOTE: review for potential ordering optimization */ -return rte_atomic_load_explicit (&s->stack_lf.used.len, rte_memory_order_seq_cst); +return (unsigned int )rte_atomic_load_explicit (&list->len, rte_memory_order_seq_cst); }static __rte_always_inline void
lib/stack/rte_stack_lf_stubs.h +2 -2
@@ -8,9 +8,9 @@#include <rte_common.h> static __rte_always_inline unsigned int -__rte_stack_lf_count (struct rte_stack *s) +__rte_stack_lf_elems_count (struct rte_stack_lf_list *list) { -RTE_SET_USED (s); +RTE_SET_USED (list);return 0 ; }
lib/stack/rte_stack_pile.c new file +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2026 SmartShare Systems +*/ + +#include "rte_stack.h" + +void +rte_stack_pile_init (struct rte_stack *s,unsigned int count) +{ +unsigned int bulk = (count +RTE_STACK_PILE_BULK_SIZE -1 ) /RTE_STACK_PILE_BULK_SIZE ; +struct rte_stack_pile_bulk_elem *bulk_elems = + (struct rte_stack_pile_bulk_elem *)(s->stack_pile.elems); +struct rte_stack_lf_elem *solo_elems = (struct rte_stack_lf_elem *)&bulk_elems[bulk]; +unsigned int i; + +for (i =0 ; i < bulk; i++) +__rte_stack_pile_bulk_push_elems (&s->stack_pile.free_bulk, + &bulk_elems[i], &bulk_elems[i],1 ); +for (i =0 ; i < count; i++) +__rte_stack_lf_push_elems (&s->stack_pile.free_solo, + &solo_elems[i], &solo_elems[i],1 ); +} + +ssize_t +rte_stack_pile_get_memsize (unsigned int count) +{ +unsigned int bulk = (count +RTE_STACK_PILE_BULK_SIZE -1 ) /RTE_STACK_PILE_BULK_SIZE ; + ssize_t sz = offsetof(struct rte_stack, stack_pile.elems); + sz += bulk *sizeof (struct rte_stack_pile_bulk_elem ); + sz += count *sizeof (struct rte_stack_lf_elem ); + sz =RTE_CACHE_LINE_ROUNDUP (sz); + sz +=RTE_CACHE_GUARD_LINES *RTE_CACHE_LINE_SIZE ; + +return sz; +}
lib/stack/rte_stack_pile.h new file +359 -0
359 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v7 2/2 mempool: introduce pile driver
In reply to Morten BrørupAdded a new "pile" mempool driver, based on the high-performance lock-free "pile" stack variant.
With this driver added, the max number of mempool drivers (RTE_MEMPOOL_MAX_OPS_IDX) would be exceeded, so it was increased from 16 to 32.
Changed the other stack drivers to call their specific push/pop functions, instead of calling the generic stack push/pop API.
---
v7:
* Actually moved increase of max number of mempool drivers from stack patch to mempool driver patch, where it belongs.
v6:
* No changes.
v5:
* Added release note. (AI advanced model)
* Moved increase of max number of mempool drivers from stack patch to mempool driver patch, where it belongs.
* Mentioned increase of max number of mempool drivers in patch description.
* Removed the rte_mempool_register_ops() and rte_mempool_set_ops_byname() changes.
They are included in the "mempool: various cleanups" patch in the "mempool: cleanup, fixes, improvements and optimizations" series.
v4:
* No changes.
v3:
* Call other stack types' specific push/pop functions.
v2:
* There is no v2.
--- doc/guides/mempool/stack.rst | 12 ++- doc/guides/rel_notes/release_26_11.rst | 1 + drivers/mempool/stack/rte_mempool_stack.c | 90 ++++++++++++++++++++--- lib/mempool/rte_mempool.h | 2 +- 4 files changed, 93 insertions(+), 12 deletions(-)
doc/guides/mempool/stack.rst +11 -1
@@ -1,5 +1,6 @@ .. SPDX-License-Identifier: BSD-3-Clause Copyright(c) 2020 Intel Corporation. + Copyright(c) 2026 SmartShare Systems. Stack Mempool Driver ====================
@@ -28,6 +29,12 @@ can be selected as described in :ref:`Mempool_Handlers`: The underlying **rte_stack** operates in lock-free mode. For more information please refer to :ref:`Stack_Library_LF_Stack`. +- ``pile`` + + The underlying **rte_stack** operates in lock-free mode, + and is optimized for bulks of objects. + For more information please refer to :ref:`Stack_Library_Pile`. + The standard stack outperforms the lock-free stack on average, however the standard stack is non-preemptive: if a mempool user is preempted while holding the stack lock, that thread will block all other mempool accesses until it
@@ -35,9 +42,12 @@ returns and releases the lock. As a result, an application using the standard stack whose threads can be preempted can suffer from brief, infrequent performance hiccups. -The lock-free stack, by design, is not susceptible to this problem; one thread can +The lock-free stack and the pile, by design, are not susceptible to this problem; one thread can be preempted at any point during a push or pop operation and will not impede the progress of any other thread. +The pile is not LIFO per object, but per bulk of objects. +Although the pile is optimized for bulks of objects, it can handle any request size. + For a more detailed description of the stack implementations, please refer to :doc:`/prog_guide/stack_lib`.
doc/guides/rel_notes/release_26_11.rst +1 -0
@@ -61,6 +61,7 @@ New Features pushed and popped in LIFO manner, but objects within each bulk are not ordered as expected by a stack. Furthermore, it is not strictly bounded by its size, but might hold more objects. +* mempool: Added "pile" driver, using the lock-free "pile" stack-like implementation. Removed Items -------------
drivers/mempool/stack/rte_mempool_stack.c +80 -10
128 lines not on this page · raw message
lib/mempool/rte_mempool.h +1 -1
@@ -718,7 +718,7 @@ struct __rte_cache_aligned rte_mempool_ops {rte_mempool_dequeue_contig_blocks_t dequeue_contig_blocks; }; -#define RTE_MEMPOOL_MAX_OPS_IDX 16/**< Max registered ops structs */ +#define RTE_MEMPOOL_MAX_OPS_IDX 32/**< Max registered ops structs */ /** * Structure storing the table of registered ops structs, each of which contain
Signature · 1 line
2.43.0
Thomas Monjalon
AppliedRe: [PATCH v7 0/2] introduce pile stack and mempool driver
In reply to Morten Brørup14/09/2026 17:58, Morten Brørup:
Quoted 6 lines
Added a new high-performance lock-free "pile", using the Stack API, and an accompanying mempool driver.
Morten Brørup (2):
stack: introduce pile
mempool: introduce pile driver
Applied, thanks.
Re: [PATCH v7 0/2] introduce pile stack and mempool driver
Quoted 8 lines · Thomas Monjalon
14/09/2026 17:58, Morten Brørup:
Quoted 6 lines
Added a new high-performance lock-free "pile", using the Stack API, and an accompanying mempool driver.
Morten Brørup (2):
stack: introduce pile
mempool: introduce pile driverApplied, thanks.
Hello,
Debug build fails on Ubuntu 24.04 (aarch64) with this patchset. Bug:
https://bugs.dpdk.org/show_bug.cgi?id=2061
Regards,
Ali
RE: [PATCH v7 0/2] introduce pile stack and mempool driver
Quoted 14 lines
From: Ali Alnubani [mailto:[email protected]]
Sent: Monday, 19 October 2026 15.56Quoted 8 lines · Thomas Monjalon
14/09/2026 17:58, Morten Brørup:
Quoted 6 lines
Added a new high-performance lock-free "pile", using the Stack API, and an accompanying mempool driver.
Morten Brørup (2):
stack: introduce pile
mempool: introduce pile driverApplied, thanks.
Quoted 8 lines
Hello,
Debug build fails on Ubuntu 24.04 (aarch64) with this patchset. Bug:
https://bugs.dpdk.org/show_bug.cgi?id=2061
Regards,
Ali
I am able to reproduce.
Have working workaround.
Will submit tomorrow.
-Morten
RE: [PATCH v7 0/2] introduce pile stack and mempool driver
In reply to Ali AlnubaniQuoted 22 lines
From: Morten Brørup
Sent: Monday, 19 October 2026 18.40Quoted 7 lines
From: Ali Alnubani [mailto:[email protected]]
Sent: Monday, 19 October 2026 15.56Quoted 2 lines · Thomas Monjalon
14/09/2026 17:58, Morten Brørup:
Quoted 1 line
Added a new high-performance lock-free "pile", using the Stack
API,
Quoted 11 lines
Quoted 6 lines
Quoted 5 lines
and an accompanying mempool driver.
Morten Brørup (2):
stack: introduce pile
mempool: introduce pile driverApplied, thanks.
Hello,
Debug build fails on Ubuntu 24.04 (aarch64) with this patchset. Bug:
Quoted 8 lines
Quoted 4 lines
https://bugs.dpdk.org/show_bug.cgi?id=2061
Regards,
AliI am able to reproduce.
Have working workaround.
Will submit tomorrow.
Patch sent:
https://patchwork.dpdk.org/project/dpdk/patch/[email protected]/
-Morten