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 <stdio.h>
3797f17497SC.J. Collier#include <unistd.h>
3897f17497SC.J. Collier#include <inttypes.h>
3997f17497SC.J. Collier#include <rte_cycles.h>
4097f17497SC.J. Collier#include <rte_timer.h>
4197f17497SC.J. Collier#include <rte_common.h>
4297f17497SC.J. Collier#include <rte_lcore.h>
4397f17497SC.J. Collier#include <rte_random.h>
4497f17497SC.J. Collier#include <rte_malloc.h>
4597f17497SC.J. Collier
4697f17497SC.J. Collier#define MAX_ITERATIONS 1000000
4797f17497SC.J. Collier
4897f17497SC.J. Collierint outstanding_count = 0;
4997f17497SC.J. Collier
5097f17497SC.J. Collierstatic void
5197f17497SC.J. Colliertimer_cb(struct rte_timer *t __rte_unused, void *param __rte_unused)
5297f17497SC.J. Collier{
5397f17497SC.J. Collier	outstanding_count--;
5497f17497SC.J. Collier}
5597f17497SC.J. Collier
5697f17497SC.J. Collier#define DELAY_SECONDS 1
5797f17497SC.J. Collier
5897f17497SC.J. Collier#ifdef RTE_EXEC_ENV_LINUXAPP
5997f17497SC.J. Collier#define do_delay() usleep(10)
6097f17497SC.J. Collier#else
6197f17497SC.J. Collier#define do_delay() rte_pause()
6297f17497SC.J. Collier#endif
6397f17497SC.J. Collier
6497f17497SC.J. Collierstatic int
6597f17497SC.J. Colliertest_timer_perf(void)
6697f17497SC.J. Collier{
6797f17497SC.J. Collier	unsigned iterations = 100;
6897f17497SC.J. Collier	unsigned i;
6997f17497SC.J. Collier	struct rte_timer *tms;
7097f17497SC.J. Collier	uint64_t start_tsc, end_tsc, delay_start;
7197f17497SC.J. Collier	unsigned lcore_id = rte_lcore_id();
7297f17497SC.J. Collier
7397f17497SC.J. Collier	tms = rte_malloc(NULL, sizeof(*tms) * MAX_ITERATIONS, 0);
7497f17497SC.J. Collier
7597f17497SC.J. Collier	for (i = 0; i < MAX_ITERATIONS; i++)
7697f17497SC.J. Collier		rte_timer_init(&tms[i]);
7797f17497SC.J. Collier
7897f17497SC.J. Collier	const uint64_t ticks = rte_get_timer_hz() * DELAY_SECONDS;
7997f17497SC.J. Collier	const uint64_t ticks_per_ms = rte_get_tsc_hz()/1000;
8097f17497SC.J. Collier	const uint64_t ticks_per_us = ticks_per_ms/1000;
8197f17497SC.J. Collier
8297f17497SC.J. Collier	while (iterations <= MAX_ITERATIONS) {
8397f17497SC.J. Collier
8497f17497SC.J. Collier		printf("Appending %u timers\n", iterations);
8597f17497SC.J. Collier		start_tsc = rte_rdtsc();
8697f17497SC.J. Collier		for (i = 0; i < iterations; i++)
8797f17497SC.J. Collier			rte_timer_reset(&tms[i], ticks, SINGLE, lcore_id,
8897f17497SC.J. Collier					timer_cb, NULL);
8997f17497SC.J. Collier		end_tsc = rte_rdtsc();
9097f17497SC.J. Collier		printf("Time for %u timers: %"PRIu64" (%"PRIu64"ms), ", iterations,
9197f17497SC.J. Collier				end_tsc-start_tsc, (end_tsc-start_tsc+ticks_per_ms/2)/(ticks_per_ms));
9297f17497SC.J. Collier		printf("Time per timer: %"PRIu64" (%"PRIu64"us)\n",
9397f17497SC.J. Collier				(end_tsc-start_tsc)/iterations,
9497f17497SC.J. Collier				((end_tsc-start_tsc)/iterations+ticks_per_us/2)/(ticks_per_us));
9597f17497SC.J. Collier		outstanding_count = iterations;
9697f17497SC.J. Collier		delay_start = rte_get_timer_cycles();
9797f17497SC.J. Collier		while (rte_get_timer_cycles() < delay_start + ticks)
9897f17497SC.J. Collier			do_delay();
9997f17497SC.J. Collier
10097f17497SC.J. Collier		start_tsc = rte_rdtsc();
10197f17497SC.J. Collier		while (outstanding_count)
10297f17497SC.J. Collier			rte_timer_manage();
10397f17497SC.J. Collier		end_tsc = rte_rdtsc();
10497f17497SC.J. Collier		printf("Time for %u callbacks: %"PRIu64" (%"PRIu64"ms), ", iterations,
10597f17497SC.J. Collier				end_tsc-start_tsc, (end_tsc-start_tsc+ticks_per_ms/2)/(ticks_per_ms));
10697f17497SC.J. Collier		printf("Time per callback: %"PRIu64" (%"PRIu64"us)\n",
10797f17497SC.J. Collier				(end_tsc-start_tsc)/iterations,
10897f17497SC.J. Collier				((end_tsc-start_tsc)/iterations+ticks_per_us/2)/(ticks_per_us));
10997f17497SC.J. Collier
11097f17497SC.J. Collier		printf("Resetting %u timers\n", iterations);
11197f17497SC.J. Collier		start_tsc = rte_rdtsc();
11297f17497SC.J. Collier		for (i = 0; i < iterations; i++)
11397f17497SC.J. Collier			rte_timer_reset(&tms[i], rte_rand() % ticks, SINGLE, lcore_id,
11497f17497SC.J. Collier					timer_cb, NULL);
11597f17497SC.J. Collier		end_tsc = rte_rdtsc();
11697f17497SC.J. Collier		printf("Time for %u timers: %"PRIu64" (%"PRIu64"ms), ", iterations,
11797f17497SC.J. Collier				end_tsc-start_tsc, (end_tsc-start_tsc+ticks_per_ms/2)/(ticks_per_ms));
11897f17497SC.J. Collier		printf("Time per timer: %"PRIu64" (%"PRIu64"us)\n",
11997f17497SC.J. Collier				(end_tsc-start_tsc)/iterations,
12097f17497SC.J. Collier				((end_tsc-start_tsc)/iterations+ticks_per_us/2)/(ticks_per_us));
12197f17497SC.J. Collier		outstanding_count = iterations;
12297f17497SC.J. Collier
12397f17497SC.J. Collier		delay_start = rte_get_timer_cycles();
12497f17497SC.J. Collier		while (rte_get_timer_cycles() < delay_start + ticks)
12597f17497SC.J. Collier			do_delay();
12697f17497SC.J. Collier
12797f17497SC.J. Collier		rte_timer_manage();
12897f17497SC.J. Collier		if (outstanding_count != 0) {
12997f17497SC.J. Collier			printf("Error: outstanding callback count = %d\n", outstanding_count);
13097f17497SC.J. Collier			return -1;
13197f17497SC.J. Collier		}
13297f17497SC.J. Collier
13397f17497SC.J. Collier		iterations *= 10;
13497f17497SC.J. Collier		printf("\n");
13597f17497SC.J. Collier	}
13697f17497SC.J. Collier
13797f17497SC.J. Collier	printf("All timers processed ok\n");
13897f17497SC.J. Collier
13997f17497SC.J. Collier	/* measure time to poll an empty timer list */
14097f17497SC.J. Collier	start_tsc = rte_rdtsc();
14197f17497SC.J. Collier	for (i = 0; i < iterations; i++)
14297f17497SC.J. Collier		rte_timer_manage();
14397f17497SC.J. Collier	end_tsc = rte_rdtsc();
14497f17497SC.J. Collier	printf("\nTime per rte_timer_manage with zero timers: %"PRIu64" cycles\n",
14597f17497SC.J. Collier			(end_tsc - start_tsc + iterations/2) / iterations);
14697f17497SC.J. Collier
14797f17497SC.J. Collier	/* measure time to poll a timer list with timers, but without
14897f17497SC.J. Collier	 * calling any callbacks */
14997f17497SC.J. Collier	rte_timer_reset(&tms[0], ticks * 100, SINGLE, lcore_id,
15097f17497SC.J. Collier			timer_cb, NULL);
15197f17497SC.J. Collier	start_tsc = rte_rdtsc();
15297f17497SC.J. Collier	for (i = 0; i < iterations; i++)
15397f17497SC.J. Collier		rte_timer_manage();
15497f17497SC.J. Collier	end_tsc = rte_rdtsc();
15597f17497SC.J. Collier	printf("Time per rte_timer_manage with zero callbacks: %"PRIu64" cycles\n",
15697f17497SC.J. Collier			(end_tsc - start_tsc + iterations/2) / iterations);
15797f17497SC.J. Collier
15839157ec0SLuca Boccassi	rte_free(tms);
15997f17497SC.J. Collier	return 0;
16097f17497SC.J. Collier}
16197f17497SC.J. Collier
1625d4e5dcdSRicardo SalvetiREGISTER_TEST_COMMAND(timer_perf_autotest, test_timer_perf);
163