15129044dSC.J. Collier/*-
25129044dSC.J. Collier *   BSD LICENSE
35129044dSC.J. Collier *
45129044dSC.J. Collier *   Copyright(c) 2015 Akamai Technologies.
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 "test.h"
355129044dSC.J. Collier
365129044dSC.J. Collier#include <stdio.h>
375129044dSC.J. Collier#include <unistd.h>
385129044dSC.J. Collier#include <inttypes.h>
395129044dSC.J. Collier#include <rte_cycles.h>
405129044dSC.J. Collier#include <rte_timer.h>
415129044dSC.J. Collier#include <rte_common.h>
425129044dSC.J. Collier#include <rte_lcore.h>
435129044dSC.J. Collier#include <rte_random.h>
445129044dSC.J. Collier#include <rte_malloc.h>
455129044dSC.J. Collier
465129044dSC.J. Collier#undef TEST_TIMER_RACECOND_VERBOSE
475129044dSC.J. Collier
485129044dSC.J. Collier#ifdef RTE_EXEC_ENV_LINUXAPP
495129044dSC.J. Collier#define usec_delay(us) usleep(us)
505129044dSC.J. Collier#else
515129044dSC.J. Collier#define usec_delay(us) rte_delay_us(us)
525129044dSC.J. Collier#endif
535129044dSC.J. Collier
545129044dSC.J. Collier#define BILLION (1UL << 30)
555129044dSC.J. Collier
565129044dSC.J. Collier#define TEST_DURATION_S 20 /* in seconds */
575129044dSC.J. Collier#define N_TIMERS    50
585129044dSC.J. Collier
595129044dSC.J. Collierstatic struct rte_timer timer[N_TIMERS];
605129044dSC.J. Collierstatic unsigned timer_lcore_id[N_TIMERS];
615129044dSC.J. Collier
625129044dSC.J. Collierstatic unsigned master;
635129044dSC.J. Collierstatic volatile unsigned stop_slaves;
645129044dSC.J. Collier
655129044dSC.J. Collierstatic int reload_timer(struct rte_timer *tim);
665129044dSC.J. Collier
675129044dSC.J. Collierstatic void
685129044dSC.J. Colliertimer_cb(struct rte_timer *tim, void *arg __rte_unused)
695129044dSC.J. Collier{
705129044dSC.J. Collier	/* Simulate slow callback function, 100 us. */
715129044dSC.J. Collier	rte_delay_us(100);
725129044dSC.J. Collier
735129044dSC.J. Collier#ifdef TEST_TIMER_RACECOND_VERBOSE
745129044dSC.J. Collier	if (tim == &timer[0])
755129044dSC.J. Collier		printf("------------------------------------------------\n");
765129044dSC.J. Collier	printf("timer_cb: core %u timer %lu\n",
775129044dSC.J. Collier		rte_lcore_id(), tim - timer);
785129044dSC.J. Collier#endif
795129044dSC.J. Collier	(void)reload_timer(tim);
805129044dSC.J. Collier}
815129044dSC.J. Collier
825129044dSC.J. CollierRTE_DEFINE_PER_LCORE(unsigned, n_reset_collisions);
835129044dSC.J. Collier
845129044dSC.J. Collierstatic int
855129044dSC.J. Collierreload_timer(struct rte_timer *tim)
865129044dSC.J. Collier{
875129044dSC.J. Collier	/* Make timer expire roughly when the TSC hits the next BILLION
885129044dSC.J. Collier	 * multiple. Add in timer's index to make them expire in nearly
895129044dSC.J. Collier	 * sorted order. This makes all timers somewhat synchronized,
905129044dSC.J. Collier	 * firing ~2-3 times per second, assuming 2-3 GHz TSCs.
915129044dSC.J. Collier	 */
925129044dSC.J. Collier	uint64_t ticks = BILLION - (rte_get_timer_cycles() % BILLION) +
935129044dSC.J. Collier	    (tim - timer);
945129044dSC.J. Collier	int ret;
955129044dSC.J. Collier
965129044dSC.J. Collier	ret = rte_timer_reset(tim, ticks, PERIODICAL, master, timer_cb, NULL);
975129044dSC.J. Collier	if (ret != 0) {
985129044dSC.J. Collier#ifdef TEST_TIMER_RACECOND_VERBOSE
995129044dSC.J. Collier		printf("- core %u failed to reset timer %lu (OK)\n",
1005129044dSC.J. Collier			rte_lcore_id(), tim - timer);
1015129044dSC.J. Collier#endif
1025129044dSC.J. Collier		RTE_PER_LCORE(n_reset_collisions) += 1;
1035129044dSC.J. Collier	}
1045129044dSC.J. Collier	return ret;
1055129044dSC.J. Collier}
1065129044dSC.J. Collier
1075129044dSC.J. Collierstatic int
1085129044dSC.J. Collierslave_main_loop(__attribute__((unused)) void *arg)
1095129044dSC.J. Collier{
1105129044dSC.J. Collier	unsigned lcore_id = rte_lcore_id();
1115129044dSC.J. Collier	unsigned i;
1125129044dSC.J. Collier
1135129044dSC.J. Collier	RTE_PER_LCORE(n_reset_collisions) = 0;
1145129044dSC.J. Collier
1155129044dSC.J. Collier	printf("Starting main loop on core %u\n", lcore_id);
1165129044dSC.J. Collier
1175129044dSC.J. Collier	while (!stop_slaves) {
1185129044dSC.J. Collier		/* Wait until the timer manager is running.
1195129044dSC.J. Collier		 * We know it's running when we see timer[0] NOT pending.
1205129044dSC.J. Collier		 */
1215129044dSC.J. Collier		if (rte_timer_pending(&timer[0])) {
1225129044dSC.J. Collier			rte_pause();
1235129044dSC.J. Collier			continue;
1245129044dSC.J. Collier		}
1255129044dSC.J. Collier
1265129044dSC.J. Collier		/* Now, go cause some havoc!
1275129044dSC.J. Collier		 * Reload our timers.
1285129044dSC.J. Collier		 */
1295129044dSC.J. Collier		for (i = 0; i < N_TIMERS; i++) {
1305129044dSC.J. Collier			if (timer_lcore_id[i] == lcore_id)
1315129044dSC.J. Collier				(void)reload_timer(&timer[i]);
1325129044dSC.J. Collier		}
1335129044dSC.J. Collier		usec_delay(100*1000); /* sleep 100 ms */
1345129044dSC.J. Collier	}
1355129044dSC.J. Collier
1365129044dSC.J. Collier	if (RTE_PER_LCORE(n_reset_collisions) != 0) {
1375129044dSC.J. Collier		printf("- core %u, %u reset collisions (OK)\n",
1385129044dSC.J. Collier			lcore_id, RTE_PER_LCORE(n_reset_collisions));
1395129044dSC.J. Collier	}
1405129044dSC.J. Collier	return 0;
1415129044dSC.J. Collier}
1425129044dSC.J. Collier
1435129044dSC.J. Collierstatic int
1445129044dSC.J. Colliertest_timer_racecond(void)
1455129044dSC.J. Collier{
1465129044dSC.J. Collier	int ret;
1475129044dSC.J. Collier	uint64_t hz;
1485129044dSC.J. Collier	uint64_t cur_time;
1495129044dSC.J. Collier	uint64_t end_time;
1505129044dSC.J. Collier	int64_t diff = 0;
1515129044dSC.J. Collier	unsigned lcore_id;
1525129044dSC.J. Collier	unsigned i;
1535129044dSC.J. Collier
1545129044dSC.J. Collier	master = lcore_id = rte_lcore_id();
1555129044dSC.J. Collier	hz = rte_get_timer_hz();
1565129044dSC.J. Collier
1575129044dSC.J. Collier	/* init and start timers */
1585129044dSC.J. Collier	for (i = 0; i < N_TIMERS; i++) {
1595129044dSC.J. Collier		rte_timer_init(&timer[i]);
1605129044dSC.J. Collier		ret = reload_timer(&timer[i]);
1615129044dSC.J. Collier		TEST_ASSERT(ret == 0, "reload_timer failed");
1625129044dSC.J. Collier
1635129044dSC.J. Collier		/* Distribute timers to slaves.
1645129044dSC.J. Collier		 * Note that we assign timer[0] to the master.
1655129044dSC.J. Collier		 */
1665129044dSC.J. Collier		timer_lcore_id[i] = lcore_id;
1675129044dSC.J. Collier		lcore_id = rte_get_next_lcore(lcore_id, 1, 1);
1685129044dSC.J. Collier	}
1695129044dSC.J. Collier
1705129044dSC.J. Collier	/* calculate the "end of test" time */
1715129044dSC.J. Collier	cur_time = rte_get_timer_cycles();
1725129044dSC.J. Collier	end_time = cur_time + (hz * TEST_DURATION_S);
1735129044dSC.J. Collier
1745129044dSC.J. Collier	/* start slave cores */
1755129044dSC.J. Collier	stop_slaves = 0;
1765129044dSC.J. Collier	printf("Start timer manage race condition test (%u seconds)\n",
1775129044dSC.J. Collier			TEST_DURATION_S);
1785129044dSC.J. Collier	rte_eal_mp_remote_launch(slave_main_loop, NULL, SKIP_MASTER);
1795129044dSC.J. Collier
1805129044dSC.J. Collier	while (diff >= 0) {
1815129044dSC.J. Collier		/* run the timers */
1825129044dSC.J. Collier		rte_timer_manage();
1835129044dSC.J. Collier
1845129044dSC.J. Collier		/* wait 100 ms */
1855129044dSC.J. Collier		usec_delay(100*1000);
1865129044dSC.J. Collier
1875129044dSC.J. Collier		cur_time = rte_get_timer_cycles();
1885129044dSC.J. Collier		diff = end_time - cur_time;
1895129044dSC.J. Collier	}
1905129044dSC.J. Collier
1915129044dSC.J. Collier	/* stop slave cores */
1925129044dSC.J. Collier	printf("Stopping timer manage race condition test\n");
1935129044dSC.J. Collier	stop_slaves = 1;
1945129044dSC.J. Collier	rte_eal_mp_wait_lcore();
1955129044dSC.J. Collier
1965129044dSC.J. Collier	/* stop timers */
1975129044dSC.J. Collier	for (i = 0; i < N_TIMERS; i++) {
1985129044dSC.J. Collier		ret = rte_timer_stop(&timer[i]);
1995129044dSC.J. Collier		TEST_ASSERT(ret == 0, "rte_timer_stop failed");
2005129044dSC.J. Collier	}
2015129044dSC.J. Collier
2025129044dSC.J. Collier	return TEST_SUCCESS;
2035129044dSC.J. Collier}
2045129044dSC.J. Collier
2059ecc306dSRicardo SalvetiREGISTER_TEST_COMMAND(timer_racecond_autotest, test_timer_racecond);
206