15129044dSC.J. Collier/*-
25129044dSC.J. Collier *   BSD LICENSE
35129044dSC.J. Collier *
45129044dSC.J. Collier *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
55129044dSC.J. Collier *   All rights reserved.
65129044dSC.J. Collier *
75129044dSC.J. Collier *   Redistribution and use in source and binary forms, with or without
85129044dSC.J. Collier *   modification, are permitted provided that the following conditions
95129044dSC.J. Collier *   are met:
105129044dSC.J. Collier *
115129044dSC.J. Collier *     * Redistributions of source code must retain the above copyright
125129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer.
135129044dSC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
145129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer in
155129044dSC.J. Collier *       the documentation and/or other materials provided with the
165129044dSC.J. Collier *       distribution.
175129044dSC.J. Collier *     * Neither the name of Intel Corporation nor the names of its
185129044dSC.J. Collier *       contributors may be used to endorse or promote products derived
195129044dSC.J. Collier *       from this software without specific prior written permission.
205129044dSC.J. Collier *
215129044dSC.J. Collier *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225129044dSC.J. Collier *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
235129044dSC.J. Collier *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
245129044dSC.J. Collier *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
255129044dSC.J. Collier *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265129044dSC.J. Collier *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
275129044dSC.J. Collier *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285129044dSC.J. Collier *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
295129044dSC.J. Collier *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305129044dSC.J. Collier *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315129044dSC.J. Collier *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325129044dSC.J. Collier */
335129044dSC.J. Collier
345129044dSC.J. Collier#include <string.h>
355129044dSC.J. Collier#include <stdio.h>
365129044dSC.J. Collier#include <stdlib.h>
375129044dSC.J. Collier#include <stdint.h>
385129044dSC.J. Collier#include <inttypes.h>
395129044dSC.J. Collier#include <stdarg.h>
405129044dSC.J. Collier#include <errno.h>
415129044dSC.J. Collier#include <sys/queue.h>
425129044dSC.J. Collier
435129044dSC.J. Collier#include <rte_common.h>
445129044dSC.J. Collier#include <rte_log.h>
455129044dSC.J. Collier#include <rte_debug.h>
465129044dSC.J. Collier#include <rte_memory.h>
475129044dSC.J. Collier#include <rte_memzone.h>
485129044dSC.J. Collier#include <rte_launch.h>
495129044dSC.J. Collier#include <rte_cycles.h>
505129044dSC.J. Collier#include <rte_eal.h>
515129044dSC.J. Collier#include <rte_per_lcore.h>
525129044dSC.J. Collier#include <rte_lcore.h>
535129044dSC.J. Collier#include <rte_atomic.h>
545129044dSC.J. Collier#include <rte_branch_prediction.h>
555129044dSC.J. Collier#include <rte_mempool.h>
565129044dSC.J. Collier#include <rte_spinlock.h>
575129044dSC.J. Collier#include <rte_malloc.h>
585129044dSC.J. Collier
595129044dSC.J. Collier#include "test.h"
605129044dSC.J. Collier
615129044dSC.J. Collier/*
625129044dSC.J. Collier * Mempool performance
635129044dSC.J. Collier * =======
645129044dSC.J. Collier *
655129044dSC.J. Collier *    Each core get *n_keep* objects per bulk of *n_get_bulk*. Then,
665129044dSC.J. Collier *    objects are put back in the pool per bulk of *n_put_bulk*.
675129044dSC.J. Collier *
685129044dSC.J. Collier *    This sequence is done during TIME_S seconds.
695129044dSC.J. Collier *
705129044dSC.J. Collier *    This test is done on the following configurations:
715129044dSC.J. Collier *
725129044dSC.J. Collier *    - Cores configuration (*cores*)
735129044dSC.J. Collier *
745129044dSC.J. Collier *      - One core with cache
755129044dSC.J. Collier *      - Two cores with cache
765129044dSC.J. Collier *      - Max. cores with cache
775129044dSC.J. Collier *      - One core without cache
785129044dSC.J. Collier *      - Two cores without cache
795129044dSC.J. Collier *      - Max. cores without cache
80809f0800SChristian Ehrhardt *      - One core with user-owned cache
81809f0800SChristian Ehrhardt *      - Two cores with user-owned cache
82809f0800SChristian Ehrhardt *      - Max. cores with user-owned cache
835129044dSC.J. Collier *
845129044dSC.J. Collier *    - Bulk size (*n_get_bulk*, *n_put_bulk*)
855129044dSC.J. Collier *
865129044dSC.J. Collier *      - Bulk get from 1 to 32
875129044dSC.J. Collier *      - Bulk put from 1 to 32
885129044dSC.J. Collier *
895129044dSC.J. Collier *    - Number of kept objects (*n_keep*)
905129044dSC.J. Collier *
915129044dSC.J. Collier *      - 32
925129044dSC.J. Collier *      - 128
935129044dSC.J. Collier */
945129044dSC.J. Collier
955129044dSC.J. Collier#define N 65536
965129044dSC.J. Collier#define TIME_S 5
975129044dSC.J. Collier#define MEMPOOL_ELT_SIZE 2048
985129044dSC.J. Collier#define MAX_KEEP 128
995129044dSC.J. Collier#define MEMPOOL_SIZE ((rte_lcore_count()*(MAX_KEEP+RTE_MEMPOOL_CACHE_MAX_SIZE))-1)
1005129044dSC.J. Collier
101809f0800SChristian Ehrhardt#define LOG_ERR() printf("test failed at %s():%d\n", __func__, __LINE__)
102809f0800SChristian Ehrhardt#define RET_ERR() do {							\
103809f0800SChristian Ehrhardt		LOG_ERR();						\
104809f0800SChristian Ehrhardt		return -1;						\
105809f0800SChristian Ehrhardt	} while (0)
106809f0800SChristian Ehrhardt#define GOTO_ERR(var, label) do {					\
107809f0800SChristian Ehrhardt		LOG_ERR();						\
108809f0800SChristian Ehrhardt		var = -1;						\
109809f0800SChristian Ehrhardt		goto label;						\
110809f0800SChristian Ehrhardt	} while (0)
111809f0800SChristian Ehrhardt
1125129044dSC.J. Collierstatic struct rte_mempool *mp;
1135129044dSC.J. Collierstatic struct rte_mempool *mp_cache, *mp_nocache;
114809f0800SChristian Ehrhardtstatic int use_external_cache;
115809f0800SChristian Ehrhardtstatic unsigned external_cache_size = RTE_MEMPOOL_CACHE_MAX_SIZE;
1165129044dSC.J. Collier
1175129044dSC.J. Collierstatic rte_atomic32_t synchro;
1185129044dSC.J. Collier
1195129044dSC.J. Collier/* number of objects in one bulk operation (get or put) */
1205129044dSC.J. Collierstatic unsigned n_get_bulk;
1215129044dSC.J. Collierstatic unsigned n_put_bulk;
1225129044dSC.J. Collier
1235129044dSC.J. Collier/* number of objects retrived from mempool before putting them back */
1245129044dSC.J. Collierstatic unsigned n_keep;
1255129044dSC.J. Collier
1265129044dSC.J. Collier/* number of enqueues / dequeues */
1275129044dSC.J. Collierstruct mempool_test_stats {
128809f0800SChristian Ehrhardt	uint64_t enq_count;
1295129044dSC.J. Collier} __rte_cache_aligned;
1305129044dSC.J. Collier
1315129044dSC.J. Collierstatic struct mempool_test_stats stats[RTE_MAX_LCORE];
1325129044dSC.J. Collier
1335129044dSC.J. Collier/*
1345129044dSC.J. Collier * save the object number in the first 4 bytes of object data. All
1355129044dSC.J. Collier * other bytes are set to 0.
1365129044dSC.J. Collier */
1375129044dSC.J. Collierstatic void
1385129044dSC.J. Colliermy_obj_init(struct rte_mempool *mp, __attribute__((unused)) void *arg,
1395129044dSC.J. Collier	    void *obj, unsigned i)
1405129044dSC.J. Collier{
1415129044dSC.J. Collier	uint32_t *objnum = obj;
1425129044dSC.J. Collier	memset(obj, 0, mp->elt_size);
1435129044dSC.J. Collier	*objnum = i;
1445129044dSC.J. Collier}
1455129044dSC.J. Collier
1465129044dSC.J. Collierstatic int
1475129044dSC.J. Collierper_lcore_mempool_test(__attribute__((unused)) void *arg)
1485129044dSC.J. Collier{
1495129044dSC.J. Collier	void *obj_table[MAX_KEEP];
1505129044dSC.J. Collier	unsigned i, idx;
1515129044dSC.J. Collier	unsigned lcore_id = rte_lcore_id();
152809f0800SChristian Ehrhardt	int ret = 0;
1535129044dSC.J. Collier	uint64_t start_cycles, end_cycles;
1545129044dSC.J. Collier	uint64_t time_diff = 0, hz = rte_get_timer_hz();
155809f0800SChristian Ehrhardt	struct rte_mempool_cache *cache;
156809f0800SChristian Ehrhardt
157809f0800SChristian Ehrhardt	if (use_external_cache) {
158809f0800SChristian Ehrhardt		/* Create a user-owned mempool cache. */
159809f0800SChristian Ehrhardt		cache = rte_mempool_cache_create(external_cache_size,
160809f0800SChristian Ehrhardt						 SOCKET_ID_ANY);
161809f0800SChristian Ehrhardt		if (cache == NULL)
162809f0800SChristian Ehrhardt			RET_ERR();
163809f0800SChristian Ehrhardt	} else {
164809f0800SChristian Ehrhardt		/* May be NULL if cache is disabled. */
165809f0800SChristian Ehrhardt		cache = rte_mempool_default_cache(mp, lcore_id);
166809f0800SChristian Ehrhardt	}
1675129044dSC.J. Collier
1685129044dSC.J. Collier	/* n_get_bulk and n_put_bulk must be divisors of n_keep */
1695129044dSC.J. Collier	if (((n_keep / n_get_bulk) * n_get_bulk) != n_keep)
170809f0800SChristian Ehrhardt		GOTO_ERR(ret, out);
1715129044dSC.J. Collier	if (((n_keep / n_put_bulk) * n_put_bulk) != n_keep)
172809f0800SChristian Ehrhardt		GOTO_ERR(ret, out);
1735129044dSC.J. Collier
1745129044dSC.J. Collier	stats[lcore_id].enq_count = 0;
1755129044dSC.J. Collier
1765129044dSC.J. Collier	/* wait synchro for slaves */
1775129044dSC.J. Collier	if (lcore_id != rte_get_master_lcore())
1785129044dSC.J. Collier		while (rte_atomic32_read(&synchro) == 0);
1795129044dSC.J. Collier
1805129044dSC.J. Collier	start_cycles = rte_get_timer_cycles();
1815129044dSC.J. Collier
1825129044dSC.J. Collier	while (time_diff/hz < TIME_S) {
1835129044dSC.J. Collier		for (i = 0; likely(i < (N/n_keep)); i++) {
1845129044dSC.J. Collier			/* get n_keep objects by bulk of n_bulk */
1855129044dSC.J. Collier			idx = 0;
1865129044dSC.J. Collier			while (idx < n_keep) {
187809f0800SChristian Ehrhardt				ret = rte_mempool_generic_get(mp,
188809f0800SChristian Ehrhardt							      &obj_table[idx],
189809f0800SChristian Ehrhardt							      n_get_bulk,
190809f0800SChristian Ehrhardt							      cache, 0);
1915129044dSC.J. Collier				if (unlikely(ret < 0)) {
1925129044dSC.J. Collier					rte_mempool_dump(stdout, mp);
1935129044dSC.J. Collier					/* in this case, objects are lost... */
194809f0800SChristian Ehrhardt					GOTO_ERR(ret, out);
1955129044dSC.J. Collier				}
1965129044dSC.J. Collier				idx += n_get_bulk;
1975129044dSC.J. Collier			}
1985129044dSC.J. Collier
1995129044dSC.J. Collier			/* put the objects back */
2005129044dSC.J. Collier			idx = 0;
2015129044dSC.J. Collier			while (idx < n_keep) {
202809f0800SChristian Ehrhardt				rte_mempool_generic_put(mp, &obj_table[idx],
203809f0800SChristian Ehrhardt							n_put_bulk,
204809f0800SChristian Ehrhardt							cache, 0);
2055129044dSC.J. Collier				idx += n_put_bulk;
2065129044dSC.J. Collier			}
2075129044dSC.J. Collier		}
2085129044dSC.J. Collier		end_cycles = rte_get_timer_cycles();
2095129044dSC.J. Collier		time_diff = end_cycles - start_cycles;
2105129044dSC.J. Collier		stats[lcore_id].enq_count += N;
2115129044dSC.J. Collier	}
2125129044dSC.J. Collier
213809f0800SChristian Ehrhardtout:
214809f0800SChristian Ehrhardt	if (use_external_cache) {
215809f0800SChristian Ehrhardt		rte_mempool_cache_flush(cache, mp);
216809f0800SChristian Ehrhardt		rte_mempool_cache_free(cache);
217809f0800SChristian Ehrhardt	}
218809f0800SChristian Ehrhardt
219809f0800SChristian Ehrhardt	return ret;
2205129044dSC.J. Collier}
2215129044dSC.J. Collier
2225129044dSC.J. Collier/* launch all the per-lcore test, and display the result */
2235129044dSC.J. Collierstatic int
2245129044dSC.J. Collierlaunch_cores(unsigned cores)
2255129044dSC.J. Collier{
2265129044dSC.J. Collier	unsigned lcore_id;
227809f0800SChristian Ehrhardt	uint64_t rate;
2285129044dSC.J. Collier	int ret;
2295129044dSC.J. Collier	unsigned cores_save = cores;
2305129044dSC.J. Collier
2315129044dSC.J. Collier	rte_atomic32_set(&synchro, 0);
2325129044dSC.J. Collier
2335129044dSC.J. Collier	/* reset stats */
2345129044dSC.J. Collier	memset(stats, 0, sizeof(stats));
2355129044dSC.J. Collier
2365129044dSC.J. Collier	printf("mempool_autotest cache=%u cores=%u n_get_bulk=%u "
2375129044dSC.J. Collier	       "n_put_bulk=%u n_keep=%u ",
238809f0800SChristian Ehrhardt	       use_external_cache ?
239809f0800SChristian Ehrhardt		   external_cache_size : (unsigned) mp->cache_size,
240809f0800SChristian Ehrhardt	       cores, n_get_bulk, n_put_bulk, n_keep);
2415129044dSC.J. Collier
242809f0800SChristian Ehrhardt	if (rte_mempool_avail_count(mp) != MEMPOOL_SIZE) {
2435129044dSC.J. Collier		printf("mempool is not full\n");
2445129044dSC.J. Collier		return -1;
2455129044dSC.J. Collier	}
2465129044dSC.J. Collier
2475129044dSC.J. Collier	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2485129044dSC.J. Collier		if (cores == 1)
2495129044dSC.J. Collier			break;
2505129044dSC.J. Collier		cores--;
2515129044dSC.J. Collier		rte_eal_remote_launch(per_lcore_mempool_test,
2525129044dSC.J. Collier				      NULL, lcore_id);
2535129044dSC.J. Collier	}
2545129044dSC.J. Collier
2555129044dSC.J. Collier	/* start synchro and launch test on master */
2565129044dSC.J. Collier	rte_atomic32_set(&synchro, 1);
2575129044dSC.J. Collier
2585129044dSC.J. Collier	ret = per_lcore_mempool_test(NULL);
2595129044dSC.J. Collier
2605129044dSC.J. Collier	cores = cores_save;
2615129044dSC.J. Collier	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2625129044dSC.J. Collier		if (cores == 1)
2635129044dSC.J. Collier			break;
2645129044dSC.J. Collier		cores--;
2655129044dSC.J. Collier		if (rte_eal_wait_lcore(lcore_id) < 0)
2665129044dSC.J. Collier			ret = -1;
2675129044dSC.J. Collier	}
2685129044dSC.J. Collier
2695129044dSC.J. Collier	if (ret < 0) {
2705129044dSC.J. Collier		printf("per-lcore test returned -1\n");
2715129044dSC.J. Collier		return -1;
2725129044dSC.J. Collier	}
2735129044dSC.J. Collier
2745129044dSC.J. Collier	rate = 0;
2755129044dSC.J. Collier	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
2765129044dSC.J. Collier		rate += (stats[lcore_id].enq_count / TIME_S);
2775129044dSC.J. Collier
278809f0800SChristian Ehrhardt	printf("rate_persec=%" PRIu64 "\n", rate);
2795129044dSC.J. Collier
2805129044dSC.J. Collier	return 0;
2815129044dSC.J. Collier}
2825129044dSC.J. Collier
2835129044dSC.J. Collier/* for a given number of core, launch all test cases */
2845129044dSC.J. Collierstatic int
2855129044dSC.J. Collierdo_one_mempool_test(unsigned cores)
2865129044dSC.J. Collier{
2875129044dSC.J. Collier	unsigned bulk_tab_get[] = { 1, 4, 32, 0 };
2885129044dSC.J. Collier	unsigned bulk_tab_put[] = { 1, 4, 32, 0 };
2895129044dSC.J. Collier	unsigned keep_tab[] = { 32, 128, 0 };
2905129044dSC.J. Collier	unsigned *get_bulk_ptr;
2915129044dSC.J. Collier	unsigned *put_bulk_ptr;
2925129044dSC.J. Collier	unsigned *keep_ptr;
2935129044dSC.J. Collier	int ret;
2945129044dSC.J. Collier
2955129044dSC.J. Collier	for (get_bulk_ptr = bulk_tab_get; *get_bulk_ptr; get_bulk_ptr++) {
2965129044dSC.J. Collier		for (put_bulk_ptr = bulk_tab_put; *put_bulk_ptr; put_bulk_ptr++) {
2975129044dSC.J. Collier			for (keep_ptr = keep_tab; *keep_ptr; keep_ptr++) {
2985129044dSC.J. Collier
2995129044dSC.J. Collier				n_get_bulk = *get_bulk_ptr;
3005129044dSC.J. Collier				n_put_bulk = *put_bulk_ptr;
3015129044dSC.J. Collier				n_keep = *keep_ptr;
3025129044dSC.J. Collier				ret = launch_cores(cores);
3035129044dSC.J. Collier
3045129044dSC.J. Collier				if (ret < 0)
3055129044dSC.J. Collier					return -1;
3065129044dSC.J. Collier			}
3075129044dSC.J. Collier		}
3085129044dSC.J. Collier	}
3095129044dSC.J. Collier	return 0;
3105129044dSC.J. Collier}
3115129044dSC.J. Collier
3125129044dSC.J. Collierstatic int
3135129044dSC.J. Colliertest_mempool_perf(void)
3145129044dSC.J. Collier{
3155129044dSC.J. Collier	rte_atomic32_init(&synchro);
3165129044dSC.J. Collier
3175129044dSC.J. Collier	/* create a mempool (without cache) */
3185129044dSC.J. Collier	if (mp_nocache == NULL)
3195129044dSC.J. Collier		mp_nocache = rte_mempool_create("perf_test_nocache", MEMPOOL_SIZE,
3205129044dSC.J. Collier						MEMPOOL_ELT_SIZE, 0, 0,
3215129044dSC.J. Collier						NULL, NULL,
3225129044dSC.J. Collier						my_obj_init, NULL,
3235129044dSC.J. Collier						SOCKET_ID_ANY, 0);
3245129044dSC.J. Collier	if (mp_nocache == NULL)
3255129044dSC.J. Collier		return -1;
3265129044dSC.J. Collier
3275129044dSC.J. Collier	/* create a mempool (with cache) */
3285129044dSC.J. Collier	if (mp_cache == NULL)
3295129044dSC.J. Collier		mp_cache = rte_mempool_create("perf_test_cache", MEMPOOL_SIZE,
3305129044dSC.J. Collier					      MEMPOOL_ELT_SIZE,
3315129044dSC.J. Collier					      RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
3325129044dSC.J. Collier					      NULL, NULL,
3335129044dSC.J. Collier					      my_obj_init, NULL,
3345129044dSC.J. Collier					      SOCKET_ID_ANY, 0);
3355129044dSC.J. Collier	if (mp_cache == NULL)
3365129044dSC.J. Collier		return -1;
3375129044dSC.J. Collier
3385129044dSC.J. Collier	/* performance test with 1, 2 and max cores */
3395129044dSC.J. Collier	printf("start performance test (without cache)\n");
3405129044dSC.J. Collier	mp = mp_nocache;
3415129044dSC.J. Collier
3425129044dSC.J. Collier	if (do_one_mempool_test(1) < 0)
3435129044dSC.J. Collier		return -1;
3445129044dSC.J. Collier
3455129044dSC.J. Collier	if (do_one_mempool_test(2) < 0)
3465129044dSC.J. Collier		return -1;
3475129044dSC.J. Collier
3485129044dSC.J. Collier	if (do_one_mempool_test(rte_lcore_count()) < 0)
3495129044dSC.J. Collier		return -1;
3505129044dSC.J. Collier
3515129044dSC.J. Collier	/* performance test with 1, 2 and max cores */
3525129044dSC.J. Collier	printf("start performance test (with cache)\n");
3535129044dSC.J. Collier	mp = mp_cache;
3545129044dSC.J. Collier
355809f0800SChristian Ehrhardt	if (do_one_mempool_test(1) < 0)
356809f0800SChristian Ehrhardt		return -1;
357809f0800SChristian Ehrhardt
358809f0800SChristian Ehrhardt	if (do_one_mempool_test(2) < 0)
359809f0800SChristian Ehrhardt		return -1;
360809f0800SChristian Ehrhardt
361809f0800SChristian Ehrhardt	if (do_one_mempool_test(rte_lcore_count()) < 0)
362809f0800SChristian Ehrhardt		return -1;
363809f0800SChristian Ehrhardt
364809f0800SChristian Ehrhardt	/* performance test with 1, 2 and max cores */
365809f0800SChristian Ehrhardt	printf("start performance test (with user-owned cache)\n");
366809f0800SChristian Ehrhardt	mp = mp_nocache;
367809f0800SChristian Ehrhardt	use_external_cache = 1;
368809f0800SChristian Ehrhardt
3695129044dSC.J. Collier	if (do_one_mempool_test(1) < 0)
3705129044dSC.J. Collier		return -1;
3715129044dSC.J. Collier
3725129044dSC.J. Collier	if (do_one_mempool_test(2) < 0)
3735129044dSC.J. Collier		return -1;
3745129044dSC.J. Collier
3755129044dSC.J. Collier	if (do_one_mempool_test(rte_lcore_count()) < 0)
3765129044dSC.J. Collier		return -1;
3775129044dSC.J. Collier
3785129044dSC.J. Collier	rte_mempool_list_dump(stdout);
3795129044dSC.J. Collier
3805129044dSC.J. Collier	return 0;
3815129044dSC.J. Collier}
3825129044dSC.J. Collier
3839ecc306dSRicardo SalvetiREGISTER_TEST_COMMAND(mempool_perf_autotest, test_mempool_perf);
384