197f17497SC.J. Collier/*-
297f17497SC.J. Collier *   BSD LICENSE
397f17497SC.J. Collier *
497f17497SC.J. Collier *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
597f17497SC.J. Collier *   All rights reserved.
697f17497SC.J. Collier *
797f17497SC.J. Collier *   Redistribution and use in source and binary forms, with or without
897f17497SC.J. Collier *   modification, are permitted provided that the following conditions
997f17497SC.J. Collier *   are met:
1097f17497SC.J. Collier *
1197f17497SC.J. Collier *     * Redistributions of source code must retain the above copyright
1297f17497SC.J. Collier *       notice, this list of conditions and the following disclaimer.
1397f17497SC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
1497f17497SC.J. Collier *       notice, this list of conditions and the following disclaimer in
1597f17497SC.J. Collier *       the documentation and/or other materials provided with the
1697f17497SC.J. Collier *       distribution.
1797f17497SC.J. Collier *     * Neither the name of Intel Corporation nor the names of its
1897f17497SC.J. Collier *       contributors may be used to endorse or promote products derived
1997f17497SC.J. Collier *       from this software without specific prior written permission.
2097f17497SC.J. Collier *
2197f17497SC.J. Collier *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2297f17497SC.J. Collier *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2397f17497SC.J. Collier *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2497f17497SC.J. Collier *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2597f17497SC.J. Collier *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2697f17497SC.J. Collier *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2797f17497SC.J. Collier *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2897f17497SC.J. Collier *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2997f17497SC.J. Collier *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3097f17497SC.J. Collier *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3197f17497SC.J. Collier *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3297f17497SC.J. Collier */
3397f17497SC.J. Collier
3497f17497SC.J. Collier#include "test.h"
3597f17497SC.J. Collier
3697f17497SC.J. Collier#include <unistd.h>
3797f17497SC.J. Collier#include <string.h>
3897f17497SC.J. Collier#include <rte_mempool.h>
3997f17497SC.J. Collier#include <rte_cycles.h>
4097f17497SC.J. Collier#include <rte_common.h>
4197f17497SC.J. Collier#include <rte_mbuf.h>
4297f17497SC.J. Collier#include <rte_distributor.h>
4397f17497SC.J. Collier
4497f17497SC.J. Collier#define ITER_POWER 20 /* log 2 of how many iterations we do when timing. */
4597f17497SC.J. Collier#define BURST 32
4697f17497SC.J. Collier#define BIG_BATCH 1024
4797f17497SC.J. Collier
4897f17497SC.J. Collier/* static vars - zero initialized by default */
4997f17497SC.J. Collierstatic volatile int quit;
5097f17497SC.J. Collierstatic volatile unsigned worker_idx;
5197f17497SC.J. Collier
5297f17497SC.J. Collierstruct worker_stats {
5397f17497SC.J. Collier	volatile unsigned handled_packets;
5497f17497SC.J. Collier} __rte_cache_aligned;
5597f17497SC.J. Collierstruct worker_stats worker_stats[RTE_MAX_LCORE];
5697f17497SC.J. Collier
5797f17497SC.J. Collier/* worker thread used for testing the time to do a round-trip of a cache
5897f17497SC.J. Collier * line between two cores and back again
5997f17497SC.J. Collier */
6043192222SLuca Boccassistatic int
6197f17497SC.J. Collierflip_bit(volatile uint64_t *arg)
6297f17497SC.J. Collier{
6397f17497SC.J. Collier	uint64_t old_val = 0;
6497f17497SC.J. Collier	while (old_val != 2) {
6597f17497SC.J. Collier		while (!*arg)
6697f17497SC.J. Collier			rte_pause();
6797f17497SC.J. Collier		old_val = *arg;
6897f17497SC.J. Collier		*arg = 0;
6997f17497SC.J. Collier	}
7043192222SLuca Boccassi	return 0;
7197f17497SC.J. Collier}
7297f17497SC.J. Collier
7397f17497SC.J. Collier/* test case to time the number of cycles to round-trip a cache line between
7497f17497SC.J. Collier * two cores and back again.
7597f17497SC.J. Collier */
7697f17497SC.J. Collierstatic void
7797f17497SC.J. Colliertime_cache_line_switch(void)
7897f17497SC.J. Collier{
7997f17497SC.J. Collier	/* allocate a full cache line for data, we use only first byte of it */
8097f17497SC.J. Collier	uint64_t data[RTE_CACHE_LINE_SIZE*3 / sizeof(uint64_t)];
8197f17497SC.J. Collier
8297f17497SC.J. Collier	unsigned i, slaveid = rte_get_next_lcore(rte_lcore_id(), 0, 0);
8397f17497SC.J. Collier	volatile uint64_t *pdata = &data[0];
8497f17497SC.J. Collier	*pdata = 1;
8597f17497SC.J. Collier	rte_eal_remote_launch((lcore_function_t *)flip_bit, &data[0], slaveid);
8697f17497SC.J. Collier	while (*pdata)
8797f17497SC.J. Collier		rte_pause();
8897f17497SC.J. Collier
8997f17497SC.J. Collier	const uint64_t start_time = rte_rdtsc();
9097f17497SC.J. Collier	for (i = 0; i < (1 << ITER_POWER); i++) {
9197f17497SC.J. Collier		while (*pdata)
9297f17497SC.J. Collier			rte_pause();
9397f17497SC.J. Collier		*pdata = 1;
9497f17497SC.J. Collier	}
9597f17497SC.J. Collier	const uint64_t end_time = rte_rdtsc();
9697f17497SC.J. Collier
9797f17497SC.J. Collier	while (*pdata)
9897f17497SC.J. Collier		rte_pause();
9997f17497SC.J. Collier	*pdata = 2;
10097f17497SC.J. Collier	rte_eal_wait_lcore(slaveid);
10197f17497SC.J. Collier	printf("==== Cache line switch test ===\n");
10297f17497SC.J. Collier	printf("Time for %u iterations = %"PRIu64" ticks\n", (1<<ITER_POWER),
10397f17497SC.J. Collier			end_time-start_time);
10497f17497SC.J. Collier	printf("Ticks per iteration = %"PRIu64"\n\n",
10597f17497SC.J. Collier			(end_time-start_time) >> ITER_POWER);
10697f17497SC.J. Collier}
10797f17497SC.J. Collier
10897f17497SC.J. Collier/* returns the total count of the number of packets handled by the worker
10997f17497SC.J. Collier * functions given below.
11097f17497SC.J. Collier */
11197f17497SC.J. Collierstatic unsigned
11297f17497SC.J. Colliertotal_packet_count(void)
11397f17497SC.J. Collier{
11497f17497SC.J. Collier	unsigned i, count = 0;
11597f17497SC.J. Collier	for (i = 0; i < worker_idx; i++)
11697f17497SC.J. Collier		count += worker_stats[i].handled_packets;
11797f17497SC.J. Collier	return count;
11897f17497SC.J. Collier}
11997f17497SC.J. Collier
12097f17497SC.J. Collier/* resets the packet counts for a new test */
12197f17497SC.J. Collierstatic void
12297f17497SC.J. Collierclear_packet_count(void)
12397f17497SC.J. Collier{
12497f17497SC.J. Collier	memset(&worker_stats, 0, sizeof(worker_stats));
12597f17497SC.J. Collier}
12697f17497SC.J. Collier
12797f17497SC.J. Collier/* this is the basic worker function for performance tests.
12897f17497SC.J. Collier * it does nothing but return packets and count them.
12997f17497SC.J. Collier */
13097f17497SC.J. Collierstatic int
13197f17497SC.J. Collierhandle_work(void *arg)
13297f17497SC.J. Collier{
13397f17497SC.J. Collier	struct rte_mbuf *pkt = NULL;
13497f17497SC.J. Collier	struct rte_distributor *d = arg;
13597f17497SC.J. Collier	unsigned count = 0;
13697f17497SC.J. Collier	unsigned id = __sync_fetch_and_add(&worker_idx, 1);
13797f17497SC.J. Collier
13897f17497SC.J. Collier	pkt = rte_distributor_get_pkt(d, id, NULL);
13997f17497SC.J. Collier	while (!quit) {
14097f17497SC.J. Collier		worker_stats[id].handled_packets++, count++;
14197f17497SC.J. Collier		pkt = rte_distributor_get_pkt(d, id, pkt);
14297f17497SC.J. Collier	}
14397f17497SC.J. Collier	worker_stats[id].handled_packets++, count++;
14497f17497SC.J. Collier	rte_distributor_return_pkt(d, id, pkt);
14597f17497SC.J. Collier	return 0;
14697f17497SC.J. Collier}
14797f17497SC.J. Collier
14897f17497SC.J. Collier/* this basic performance test just repeatedly sends in 32 packets at a time
14997f17497SC.J. Collier * to the distributor and verifies at the end that we got them all in the worker
15097f17497SC.J. Collier * threads and finally how long per packet the processing took.
15197f17497SC.J. Collier */
15297f17497SC.J. Collierstatic inline int
15397f17497SC.J. Collierperf_test(struct rte_distributor *d, struct rte_mempool *p)
15497f17497SC.J. Collier{
15597f17497SC.J. Collier	unsigned i;
15697f17497SC.J. Collier	uint64_t start, end;
15797f17497SC.J. Collier	struct rte_mbuf *bufs[BURST];
15897f17497SC.J. Collier
15997f17497SC.J. Collier	clear_packet_count();
16097f17497SC.J. Collier	if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
16197f17497SC.J. Collier		printf("Error getting mbufs from pool\n");
16297f17497SC.J. Collier		return -1;
16397f17497SC.J. Collier	}
16497f17497SC.J. Collier	/* ensure we have different hash value for each pkt */
16597f17497SC.J. Collier	for (i = 0; i < BURST; i++)
16697f17497SC.J. Collier		bufs[i]->hash.usr = i;
16797f17497SC.J. Collier
16897f17497SC.J. Collier	start = rte_rdtsc();
16997f17497SC.J. Collier	for (i = 0; i < (1<<ITER_POWER); i++)
17097f17497SC.J. Collier		rte_distributor_process(d, bufs, BURST);
17197f17497SC.J. Collier	end = rte_rdtsc();
17297f17497SC.J. Collier
17397f17497SC.J. Collier	do {
17497f17497SC.J. Collier		usleep(100);
17597f17497SC.J. Collier		rte_distributor_process(d, NULL, 0);
17697f17497SC.J. Collier	} while (total_packet_count() < (BURST << ITER_POWER));
17797f17497SC.J. Collier
17897f17497SC.J. Collier	printf("=== Performance test of distributor ===\n");
17997f17497SC.J. Collier	printf("Time per burst:  %"PRIu64"\n", (end - start) >> ITER_POWER);
18097f17497SC.J. Collier	printf("Time per packet: %"PRIu64"\n\n",
18197f17497SC.J. Collier			((end - start) >> ITER_POWER)/BURST);
18297f17497SC.J. Collier	rte_mempool_put_bulk(p, (void *)bufs, BURST);
18397f17497SC.J. Collier
18497f17497SC.J. Collier	for (i = 0; i < rte_lcore_count() - 1; i++)
18597f17497SC.J. Collier		printf("Worker %u handled %u packets\n", i,
18697f17497SC.J. Collier				worker_stats[i].handled_packets);
18797f17497SC.J. Collier	printf("Total packets: %u (%x)\n", total_packet_count(),
18897f17497SC.J. Collier			total_packet_count());
18997f17497SC.J. Collier	printf("=== Perf test done ===\n\n");
19097f17497SC.J. Collier
19197f17497SC.J. Collier	return 0;
19297f17497SC.J. Collier}
19397f17497SC.J. Collier
19497f17497SC.J. Collier/* Useful function which ensures that all worker functions terminate */
19597f17497SC.J. Collierstatic void
19697f17497SC.J. Collierquit_workers(struct rte_distributor *d, struct rte_mempool *p)
19797f17497SC.J. Collier{
19897f17497SC.J. Collier	const unsigned num_workers = rte_lcore_count() - 1;
19997f17497SC.J. Collier	unsigned i;
20097f17497SC.J. Collier	struct rte_mbuf *bufs[RTE_MAX_LCORE];
20197f17497SC.J. Collier	rte_mempool_get_bulk(p, (void *)bufs, num_workers);
20297f17497SC.J. Collier
20397f17497SC.J. Collier	quit = 1;
20497f17497SC.J. Collier	for (i = 0; i < num_workers; i++)
20597f17497SC.J. Collier		bufs[i]->hash.usr = i << 1;
20697f17497SC.J. Collier	rte_distributor_process(d, bufs, num_workers);
20797f17497SC.J. Collier
20897f17497SC.J. Collier	rte_mempool_put_bulk(p, (void *)bufs, num_workers);
20997f17497SC.J. Collier
21097f17497SC.J. Collier	rte_distributor_process(d, NULL, 0);
21197f17497SC.J. Collier	rte_eal_mp_wait_lcore();
21297f17497SC.J. Collier	quit = 0;
21397f17497SC.J. Collier	worker_idx = 0;
21497f17497SC.J. Collier}
21597f17497SC.J. Collier
21697f17497SC.J. Collierstatic int
21797f17497SC.J. Colliertest_distributor_perf(void)
21897f17497SC.J. Collier{
21997f17497SC.J. Collier	static struct rte_distributor *d;
22097f17497SC.J. Collier	static struct rte_mempool *p;
22197f17497SC.J. Collier
22297f17497SC.J. Collier	if (rte_lcore_count() < 2) {
22397f17497SC.J. Collier		printf("ERROR: not enough cores to test distributor\n");
22497f17497SC.J. Collier		return -1;
22597f17497SC.J. Collier	}
22697f17497SC.J. Collier
22797f17497SC.J. Collier	/* first time how long it takes to round-trip a cache line */
22897f17497SC.J. Collier	time_cache_line_switch();
22997f17497SC.J. Collier
23097f17497SC.J. Collier	if (d == NULL) {
23197f17497SC.J. Collier		d = rte_distributor_create("Test_perf", rte_socket_id(),
23297f17497SC.J. Collier				rte_lcore_count() - 1);
23397f17497SC.J. Collier		if (d == NULL) {
23497f17497SC.J. Collier			printf("Error creating distributor\n");
23597f17497SC.J. Collier			return -1;
23697f17497SC.J. Collier		}
23797f17497SC.J. Collier	} else {
23897f17497SC.J. Collier		rte_distributor_flush(d);
23997f17497SC.J. Collier		rte_distributor_clear_returns(d);
24097f17497SC.J. Collier	}
24197f17497SC.J. Collier
24297f17497SC.J. Collier	const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
24397f17497SC.J. Collier			(BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
24497f17497SC.J. Collier	if (p == NULL) {
24597f17497SC.J. Collier		p = rte_pktmbuf_pool_create("DPT_MBUF_POOL", nb_bufs, BURST,
24697f17497SC.J. Collier			0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
24797f17497SC.J. Collier		if (p == NULL) {
24897f17497SC.J. Collier			printf("Error creating mempool\n");
24997f17497SC.J. Collier			return -1;
25097f17497SC.J. Collier		}
25197f17497SC.J. Collier	}
25297f17497SC.J. Collier
25397f17497SC.J. Collier	rte_eal_mp_remote_launch(handle_work, d, SKIP_MASTER);
25497f17497SC.J. Collier	if (perf_test(d, p) < 0)
25597f17497SC.J. Collier		return -1;
25697f17497SC.J. Collier	quit_workers(d, p);
25797f17497SC.J. Collier
25897f17497SC.J. Collier	return 0;
25997f17497SC.J. Collier}
26097f17497SC.J. Collier
2615d4e5dcdSRicardo SalvetiREGISTER_TEST_COMMAND(distributor_perf_autotest, test_distributor_perf);
262