1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <rte_cycles.h>
11 #include <rte_timer.h>
12 #include <rte_common.h>
13 #include <rte_lcore.h>
14 #include <rte_random.h>
15 #include <rte_malloc.h>
16 #include <rte_pause.h>
18 #define MAX_ITERATIONS 1000000
20 int outstanding_count = 0;
23 timer_cb(struct rte_timer *t __rte_unused, void *param __rte_unused)
28 #define DELAY_SECONDS 1
30 #ifdef RTE_EXEC_ENV_LINUX
31 #define do_delay() usleep(10)
33 #define do_delay() rte_pause()
39 unsigned iterations = 100;
41 struct rte_timer *tms;
42 uint64_t start_tsc, end_tsc, delay_start;
43 unsigned lcore_id = rte_lcore_id();
45 tms = rte_malloc(NULL, sizeof(*tms) * MAX_ITERATIONS, 0);
47 for (i = 0; i < MAX_ITERATIONS; i++)
48 rte_timer_init(&tms[i]);
50 const uint64_t ticks = rte_get_timer_hz() * DELAY_SECONDS;
51 const uint64_t ticks_per_ms = rte_get_tsc_hz()/1000;
52 const uint64_t ticks_per_us = ticks_per_ms/1000;
54 while (iterations <= MAX_ITERATIONS) {
56 printf("Appending %u timers\n", iterations);
57 start_tsc = rte_rdtsc();
58 for (i = 0; i < iterations; i++)
59 rte_timer_reset(&tms[i], ticks, SINGLE, lcore_id,
61 end_tsc = rte_rdtsc();
62 printf("Time for %u timers: %"PRIu64" (%"PRIu64"ms), ", iterations,
63 end_tsc-start_tsc, (end_tsc-start_tsc+ticks_per_ms/2)/(ticks_per_ms));
64 printf("Time per timer: %"PRIu64" (%"PRIu64"us)\n",
65 (end_tsc-start_tsc)/iterations,
66 ((end_tsc-start_tsc)/iterations+ticks_per_us/2)/(ticks_per_us));
67 outstanding_count = iterations;
68 delay_start = rte_get_timer_cycles();
69 while (rte_get_timer_cycles() < delay_start + ticks)
72 start_tsc = rte_rdtsc();
73 while (outstanding_count)
75 end_tsc = rte_rdtsc();
76 printf("Time for %u callbacks: %"PRIu64" (%"PRIu64"ms), ", iterations,
77 end_tsc-start_tsc, (end_tsc-start_tsc+ticks_per_ms/2)/(ticks_per_ms));
78 printf("Time per callback: %"PRIu64" (%"PRIu64"us)\n",
79 (end_tsc-start_tsc)/iterations,
80 ((end_tsc-start_tsc)/iterations+ticks_per_us/2)/(ticks_per_us));
82 printf("Resetting %u timers\n", iterations);
83 start_tsc = rte_rdtsc();
84 for (i = 0; i < iterations; i++)
85 rte_timer_reset(&tms[i], rte_rand() % ticks, SINGLE, lcore_id,
87 end_tsc = rte_rdtsc();
88 printf("Time for %u timers: %"PRIu64" (%"PRIu64"ms), ", iterations,
89 end_tsc-start_tsc, (end_tsc-start_tsc+ticks_per_ms/2)/(ticks_per_ms));
90 printf("Time per timer: %"PRIu64" (%"PRIu64"us)\n",
91 (end_tsc-start_tsc)/iterations,
92 ((end_tsc-start_tsc)/iterations+ticks_per_us/2)/(ticks_per_us));
93 outstanding_count = iterations;
95 delay_start = rte_get_timer_cycles();
96 while (rte_get_timer_cycles() < delay_start + ticks)
100 if (outstanding_count != 0) {
101 printf("Error: outstanding callback count = %d\n", outstanding_count);
109 printf("All timers processed ok\n");
111 /* measure time to poll an empty timer list */
112 start_tsc = rte_rdtsc();
113 for (i = 0; i < iterations; i++)
115 end_tsc = rte_rdtsc();
116 printf("\nTime per rte_timer_manage with zero timers: %"PRIu64" cycles\n",
117 (end_tsc - start_tsc + iterations/2) / iterations);
119 /* measure time to poll a timer list with timers, but without
120 * calling any callbacks */
121 rte_timer_reset(&tms[0], ticks * 100, SINGLE, lcore_id,
123 start_tsc = rte_rdtsc();
124 for (i = 0; i < iterations; i++)
126 end_tsc = rte_rdtsc();
127 printf("Time per rte_timer_manage with zero callbacks: %"PRIu64" cycles\n",
128 (end_tsc - start_tsc + iterations/2) / iterations);
134 REGISTER_TEST_COMMAND(timer_perf_autotest, test_timer_perf);