Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : */
4 : :
5 : : #include "test.h"
6 : :
7 : : #include <stdio.h>
8 : : #include <unistd.h>
9 : : #include <inttypes.h>
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>
17 : :
18 : : #define MAX_ITERATIONS 1000000
19 : :
20 : : int outstanding_count = 0;
21 : :
22 : : static void
23 : 0 : timer_cb(struct rte_timer *t __rte_unused, void *param __rte_unused)
24 : : {
25 : 0 : outstanding_count--;
26 : 0 : }
27 : :
28 : : #define DELAY_SECONDS 1
29 : :
30 : : #ifdef RTE_EXEC_ENV_LINUX
31 : : #define do_delay() usleep(10)
32 : : #else
33 : : #define do_delay() rte_pause()
34 : : #endif
35 : :
36 : : static int
37 : 0 : test_timer_perf(void)
38 : : {
39 : : unsigned iterations = 100;
40 : : unsigned i;
41 : : struct rte_timer *tms;
42 : : uint64_t start_tsc, end_tsc, delay_start;
43 : : unsigned lcore_id = rte_lcore_id();
44 : : int ret;
45 : :
46 : 0 : ret = rte_timer_subsystem_init();
47 [ # # ]: 0 : if (ret < 0 && ret != -EALREADY) {
48 : : printf("timer subsystem init failed: %d\n", ret);
49 : 0 : return TEST_FAILED;
50 : : }
51 : :
52 : 0 : tms = rte_malloc(NULL, sizeof(*tms) * MAX_ITERATIONS, 0);
53 : :
54 [ # # ]: 0 : for (i = 0; i < MAX_ITERATIONS; i++)
55 : 0 : rte_timer_init(&tms[i]);
56 : :
57 : : const uint64_t ticks = rte_get_timer_hz() * DELAY_SECONDS;
58 : 0 : const uint64_t ticks_per_ms = rte_get_tsc_hz()/1000;
59 : 0 : const uint64_t ticks_per_us = ticks_per_ms/1000;
60 : :
61 [ # # ]: 0 : while (iterations <= MAX_ITERATIONS) {
62 : :
63 : : printf("Appending %u timers\n", iterations);
64 : : start_tsc = rte_rdtsc();
65 [ # # ]: 0 : for (i = 0; i < iterations; i++)
66 : 0 : rte_timer_reset(&tms[i], ticks, SINGLE, lcore_id,
67 : : timer_cb, NULL);
68 : : end_tsc = rte_rdtsc();
69 : 0 : printf("Time for %u timers: %"PRIu64" (%"PRIu64"ms), ", iterations,
70 : 0 : end_tsc-start_tsc, (end_tsc-start_tsc+ticks_per_ms/2)/(ticks_per_ms));
71 : 0 : printf("Time per timer: %"PRIu64" (%"PRIu64"us)\n",
72 : : (end_tsc-start_tsc)/iterations,
73 : 0 : ((end_tsc-start_tsc)/iterations+ticks_per_us/2)/(ticks_per_us));
74 : 0 : outstanding_count = iterations;
75 : : delay_start = rte_get_timer_cycles();
76 [ # # ]: 0 : while (rte_get_timer_cycles() < delay_start + ticks)
77 : 0 : do_delay();
78 : :
79 : : start_tsc = rte_rdtsc();
80 [ # # ]: 0 : while (outstanding_count)
81 : 0 : rte_timer_manage();
82 : : end_tsc = rte_rdtsc();
83 : 0 : printf("Time for %u callbacks: %"PRIu64" (%"PRIu64"ms), ", iterations,
84 : 0 : end_tsc-start_tsc, (end_tsc-start_tsc+ticks_per_ms/2)/(ticks_per_ms));
85 : 0 : printf("Time per callback: %"PRIu64" (%"PRIu64"us)\n",
86 : : (end_tsc-start_tsc)/iterations,
87 : 0 : ((end_tsc-start_tsc)/iterations+ticks_per_us/2)/(ticks_per_us));
88 : :
89 : : printf("Resetting %u timers\n", iterations);
90 : : start_tsc = rte_rdtsc();
91 [ # # ]: 0 : for (i = 0; i < iterations; i++)
92 : 0 : rte_timer_reset(&tms[i], rte_rand() % ticks, SINGLE, lcore_id,
93 : : timer_cb, NULL);
94 : : end_tsc = rte_rdtsc();
95 : 0 : printf("Time for %u timers: %"PRIu64" (%"PRIu64"ms), ", iterations,
96 : 0 : end_tsc-start_tsc, (end_tsc-start_tsc+ticks_per_ms/2)/(ticks_per_ms));
97 : 0 : printf("Time per timer: %"PRIu64" (%"PRIu64"us)\n",
98 : : (end_tsc-start_tsc)/iterations,
99 : 0 : ((end_tsc-start_tsc)/iterations+ticks_per_us/2)/(ticks_per_us));
100 : 0 : outstanding_count = iterations;
101 : :
102 : : delay_start = rte_get_timer_cycles();
103 [ # # ]: 0 : while (rte_get_timer_cycles() < delay_start + ticks)
104 : 0 : do_delay();
105 : :
106 : 0 : rte_timer_manage();
107 [ # # ]: 0 : if (outstanding_count != 0) {
108 : : printf("Error: outstanding callback count = %d\n", outstanding_count);
109 : 0 : return -1;
110 : : }
111 : :
112 : 0 : iterations *= 10;
113 : : printf("\n");
114 : : }
115 : :
116 : : printf("All timers processed ok\n");
117 : :
118 : : /* measure time to poll an empty timer list */
119 : : start_tsc = rte_rdtsc();
120 [ # # ]: 0 : for (i = 0; i < iterations; i++)
121 : 0 : rte_timer_manage();
122 : : end_tsc = rte_rdtsc();
123 : 0 : printf("\nTime per rte_timer_manage with zero timers: %"PRIu64" cycles\n",
124 : 0 : (end_tsc - start_tsc + iterations/2) / iterations);
125 : :
126 : : /* measure time to poll a timer list with timers, but without
127 : : * calling any callbacks */
128 : 0 : rte_timer_reset(&tms[0], ticks * 100, SINGLE, lcore_id,
129 : : timer_cb, NULL);
130 : : start_tsc = rte_rdtsc();
131 [ # # ]: 0 : for (i = 0; i < iterations; i++)
132 : 0 : rte_timer_manage();
133 : : end_tsc = rte_rdtsc();
134 : 0 : printf("Time per rte_timer_manage with zero callbacks: %"PRIu64" cycles\n",
135 : 0 : (end_tsc - start_tsc + iterations/2) / iterations);
136 : :
137 : 0 : rte_free(tms);
138 : 0 : return 0;
139 : : }
140 : :
141 : 305 : REGISTER_PERF_TEST(timer_perf_autotest, test_timer_perf);
|