Re: [PATCH v2] mempool: introduce statistics reset function

fengchengwen
2026-02-27T09:20:16Z

Acked-by: Chengwen Feng <fengchengwen@huawei.com>

Another question:
How about the overload of mempool stats, If the cost is low, can we
remove the debug macro? As far as I know, some DPDK applications enable
mempool statistics (maybe self-impl) in the production environment,
which is mainly used to help locate problems.

On 2/24/2026 5:28 PM, Morten Brørup wrote:
> Populating a mempool with objects is accounted for in the statistics.
> When analyzing mempool cache statistics, this may distort the data.
> In order to simplify mempool cache statistics analysis, a mempool
> statistics reset function was added.
>
> Furthermore, details about average burst sizes and mempool cache miss
> rates were added to the statistics shown when dumping a mempool.
>
> Signed-off-by: Morten Brørup <mb@smartsharesystems.com>
> ---
> v2:
> * Added detailed usage instructions to the function description. (Andrew)
> * Added DEBUG log message to the function. (Andrew)
> ---
> lib/mempool/mempool_trace.h | 7 ++++
> lib/mempool/mempool_trace_points.c | 4 +++
> lib/mempool/rte_mempool.c | 51 ++++++++++++++++++++++++++++--
> lib/mempool/rte_mempool.h | 29 +++++++++++++++++
> 4 files changed, 89 insertions(+), 2 deletions(-)
>
> diff --git a/lib/mempool/mempool_trace.h b/lib/mempool/mempool_trace.h
> index c595a3116b..23cda1473c 100644
> --- a/lib/mempool/mempool_trace.h
> +++ b/lib/mempool/mempool_trace.h
> @@ -104,6 +104,13 @@ RTE_TRACE_POINT(
> rte_trace_point_emit_string(mempool->name);
> )
>
> +RTE_TRACE_POINT(
> + rte_mempool_trace_stats_reset,
> + RTE_TRACE_POINT_ARGS(struct rte_mempool *mempool),
> + rte_trace_point_emit_ptr(mempool);
> + rte_trace_point_emit_string(mempool->name);
> +)
> +
> RTE_TRACE_POINT(
> rte_mempool_trace_cache_create,
> RTE_TRACE_POINT_ARGS(uint32_t size, int socket_id,
> diff --git a/lib/mempool/mempool_trace_points.c b/lib/mempool/mempool_trace_points.c
> index ec465780f4..8249981502 100644
> --- a/lib/mempool/mempool_trace_points.c
> +++ b/lib/mempool/mempool_trace_points.c
> @@ -60,6 +60,10 @@ RTE_TRACE_POINT_REGISTER(rte_mempool_trace_populate_default,
> RTE_TRACE_POINT_REGISTER(rte_mempool_trace_populate_anon,
> lib.mempool.populate.anon)
>
> +RTE_EXPORT_EXPERIMENTAL_SYMBOL(__rte_mempool_trace_stats_reset, 26.03)
> +RTE_TRACE_POINT_REGISTER(rte_mempool_trace_stats_reset,
> + lib.mempool.stats_reset)
> +
> RTE_TRACE_POINT_REGISTER(rte_mempool_trace_cache_create,
> lib.mempool.cache_create)
>
> diff --git a/lib/mempool/rte_mempool.c b/lib/mempool/rte_mempool.c
> index 3042d94c14..d33ea15157 100644
> --- a/lib/mempool/rte_mempool.c
> +++ b/lib/mempool/rte_mempool.c
> @@ -1049,6 +1049,29 @@ rte_mempool_in_use_count(const struct rte_mempool *mp)
> return mp->size - rte_mempool_avail_count(mp);
> }
>
> +/* Reset the statistics of a mempool. */
> +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_mempool_stats_reset, 26.03)
> +void
> +rte_mempool_stats_reset(struct rte_mempool *mp)
> +{
> + RTE_ASSERT(mp != NULL);
> +
> +#ifdef RTE_LIBRTE_MEMPOOL_STATS
> + memset(&mp->stats, 0, sizeof(mp->stats));
> + if (mp->cache_size != 0) {
> + for (unsigned int lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
> + memset(&mp->local_cache[lcore_id].stats, 0,
> + sizeof(mp->local_cache[lcore_id].stats));
> + }
> + }
> +
> + RTE_MEMPOOL_LOG(DEBUG, "<%s>@%p: statistics reset", mp->name, mp);
> + rte_mempool_trace_stats_reset(mp);
> +#else
> + RTE_SET_USED(mp);
> +#endif
> +}
> +
> /* dump the cache status */
> static unsigned
> rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
> @@ -1327,10 +1350,34 @@ rte_mempool_dump(FILE *f, struct rte_mempool *mp)
> fprintf(f, " get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
> fprintf(f, " get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
> if (info.contig_block_size > 0) {
> - fprintf(f, " get_success_blks=%"PRIu64"\n",
> - sum.get_success_blks);
> + fprintf(f, " get_success_blks=%"PRIu64"\n", sum.get_success_blks);
> fprintf(f, " get_fail_blks=%"PRIu64"\n", sum.get_fail_blks);
> }
> + fprintf(f, " avg objs/bulk put=%#Lf, get=%#Lf, get_fail=%#Lf\n",
> + sum.put_bulk != 0 ? (long double)sum.put_objs / sum.put_bulk : 0,
> + sum.get_success_bulk != 0 ?
> + (long double)sum.get_success_objs / sum.get_success_bulk : 0,
> + sum.get_fail_bulk != 0 ?
> + (long double)sum.get_fail_objs / sum.get_fail_bulk : 0);
> + fprintf(f, " avg common_pool objs/bulk put=%#Lf, get=%#Lf\n",
> + sum.put_common_pool_bulk != 0 ?
> + (long double)sum.put_common_pool_objs / sum.put_common_pool_bulk : 0,
> + sum.get_common_pool_bulk != 0 ?
> + (long double)sum.get_common_pool_objs / sum.get_common_pool_bulk : 0);
> + fprintf(f, " avg cache miss rate put_objs=%s%#Lf, get_objs=%s%#Lf\n",
> + sum.put_common_pool_objs != 0 ? "1/" : "",
> + sum.put_common_pool_objs != 0 ?
> + (long double)sum.put_objs / sum.put_common_pool_objs : 0,
> + sum.get_common_pool_objs != 0 ? "1/" : "",
> + sum.get_common_pool_objs != 0 ?
> + (long double)sum.get_success_objs / sum.get_common_pool_objs : 0);
> + fprintf(f, " avg cache miss rate put_bulk=%s%#Lf, get_bulk=%s%#Lf\n",
> + sum.put_common_pool_bulk != 0 ? "1/" : "",
> + sum.put_common_pool_bulk != 0 ?
> + (long double)sum.put_bulk / sum.put_common_pool_bulk : 0,
> + sum.get_common_pool_bulk != 0 ? "1/" : "",
> + sum.get_common_pool_bulk != 0 ?
> + (long double)sum.get_success_bulk / sum.get_common_pool_bulk : 0);
> #else
> fprintf(f, " no statistics available\n");
> #endif
> diff --git a/lib/mempool/rte_mempool.h b/lib/mempool/rte_mempool.h
> index 1144dca58a..0b62837534 100644
> --- a/lib/mempool/rte_mempool.h
> +++ b/lib/mempool/rte_mempool.h
> @@ -1288,6 +1288,35 @@ uint32_t rte_mempool_obj_iter(struct rte_mempool *mp,
> uint32_t rte_mempool_mem_iter(struct rte_mempool *mp,
> rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg);
>
> +/**
> + * @warning
> + * @b EXPERIMENTAL: This API may change, or be removed, without prior notice.
> + *
> + * Reset the statistics of a mempool.
> + *
> + * This function is intended for use when analyzing mempool statistics
> + * without counting any mempool operations performed during application
> + * initialization.
> + * For example, populating the mempool counts as put operations into the
> + * common pool, and setting up ethdev Rx queues counts as get operations.
> + *
> + * This function should only be called after application initialization,
> + * before the data path is started; otherwise, the mempool statistics may
> + * become inconsistent.
> + *
> + * For a perfectly clean slate, the local caches of the mempools used
> + * during application initialization should be flushed before resetting
> + * the mempool statistics.
> + * For example, mbuf pools used by ethdev Rx queues.
> + *
> + * @see rte_mempool_cache_flush()
> + *
> + * @param mp
> + * A pointer to the mempool structure.
> + */
> +__rte_experimental
> +void rte_mempool_stats_reset(struct rte_mempool *mp);
> +
> /**
> * Dump the status of the mempool to a file.
> *