1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2020 Marvell International Ltd.
5 #include <rte_cycles.h>
8 #include <rte_eal_trace.h>
9 #include <rte_malloc.h>
10 #include <rte_lcore.h>
13 #include "test_trace.h"
19 volatile bool started;
20 uint64_t total_cycles;
22 } __rte_cache_aligned;
25 unsigned int nb_workers;
26 struct lcore_data ldata[];
27 } __rte_cache_aligned;
30 #define CENT_OPS(OP) do { \
31 OP; OP; OP; OP; OP; OP; OP; OP; OP; OP; \
32 OP; OP; OP; OP; OP; OP; OP; OP; OP; OP; \
33 OP; OP; OP; OP; OP; OP; OP; OP; OP; OP; \
34 OP; OP; OP; OP; OP; OP; OP; OP; OP; OP; \
35 OP; OP; OP; OP; OP; OP; OP; OP; OP; OP; \
36 OP; OP; OP; OP; OP; OP; OP; OP; OP; OP; \
37 OP; OP; OP; OP; OP; OP; OP; OP; OP; OP; \
38 OP; OP; OP; OP; OP; OP; OP; OP; OP; OP; \
39 OP; OP; OP; OP; OP; OP; OP; OP; OP; OP; \
40 OP; OP; OP; OP; OP; OP; OP; OP; OP; OP; \
44 measure_perf(const char *str, struct test_data *data)
46 uint64_t hz = rte_get_timer_hz();
47 uint64_t total_cycles = 0;
48 uint64_t total_calls = 0;
52 for (workers = 0; workers < data->nb_workers; workers++) {
53 total_cycles += data->ldata[workers].total_cycles;
54 total_calls += data->ldata[workers].total_calls;
57 cycles = total_calls ? (double)total_cycles / (double)total_calls : 0;
59 cycles /= 100; /* CENT_OPS */
61 ns = (cycles / (double)hz) * 1E9;
62 printf("%16s: cycles=%f ns=%f\n", str, cycles, ns);
66 wait_till_workers_are_ready(struct test_data *data)
70 for (workers = 0; workers < data->nb_workers; workers++)
71 while (!data->ldata[workers].started)
76 signal_workers_to_finish(struct test_data *data)
80 for (workers = 0; workers < data->nb_workers; workers++) {
81 data->ldata[workers].done = 1;
85 #define WORKER_DEFINE(func) \
86 static void __rte_noinline \
87 __worker_##func(struct lcore_data *ldata) \
91 while (!ldata->done) { \
92 start = rte_get_timer_cycles(); \
93 for (i = 0; i < STEP; i++) \
95 ldata->total_cycles += rte_get_timer_cycles() - start; \
96 ldata->total_calls++; \
100 worker_fn_##func(void *arg) \
102 struct lcore_data *ldata = arg; \
103 ldata->started = 1; \
104 __worker_##func(ldata); \
109 /* Test to find trace overhead */
110 #define GENERIC_VOID rte_eal_trace_generic_void()
111 #define GENERIC_U64 rte_eal_trace_generic_u64(0x120000)
112 #define GENERIC_INT rte_eal_trace_generic_int(-34)
113 #define GENERIC_FLOAT rte_eal_trace_generic_float(3.3f)
114 #define GENERIC_DOUBLE rte_eal_trace_generic_double(3.66666)
115 #define GENERIC_STR rte_eal_trace_generic_str("hello world")
116 #define VOID_FP app_dpdk_test_fp()
118 WORKER_DEFINE(GENERIC_VOID)
119 WORKER_DEFINE(GENERIC_U64)
120 WORKER_DEFINE(GENERIC_INT)
121 WORKER_DEFINE(GENERIC_FLOAT)
122 WORKER_DEFINE(GENERIC_DOUBLE)
123 WORKER_DEFINE(GENERIC_STR)
124 WORKER_DEFINE(VOID_FP)
127 run_test(const char *str, lcore_function_t f, struct test_data *data, size_t sz)
129 unsigned int id, worker = 0;
132 data->nb_workers = rte_lcore_count() - 1;
133 RTE_LCORE_FOREACH_WORKER(id)
134 rte_eal_remote_launch(f, &data->ldata[worker++], id);
136 wait_till_workers_are_ready(data);
137 rte_delay_ms(100); /* Wait for some time to accumulate the stats */
138 signal_workers_to_finish(data);
140 RTE_LCORE_FOREACH_WORKER(id)
141 rte_eal_wait_lcore(id);
143 measure_perf(str, data);
147 test_trace_perf(void)
149 unsigned int nb_cores, nb_workers;
150 struct test_data *data;
153 nb_cores = rte_lcore_count();
154 nb_workers = nb_cores - 1;
156 printf("Need minimum two cores for testing\n");
160 printf("Timer running at %5.2fMHz\n", rte_get_timer_hz()/1E6);
161 sz = sizeof(struct test_data);
162 sz += nb_workers * sizeof(struct lcore_data);
164 data = rte_zmalloc(NULL, sz, RTE_CACHE_LINE_SIZE);
166 printf("Failed to allocate memory\n");
170 run_test("void", worker_fn_GENERIC_VOID, data, sz);
171 run_test("u64", worker_fn_GENERIC_U64, data, sz);
172 run_test("int", worker_fn_GENERIC_INT, data, sz);
173 run_test("float", worker_fn_GENERIC_FLOAT, data, sz);
174 run_test("double", worker_fn_GENERIC_DOUBLE, data, sz);
175 run_test("string", worker_fn_GENERIC_STR, data, sz);
176 run_test("void_fp", worker_fn_VOID_FP, data, sz);
182 REGISTER_TEST_COMMAND(trace_perf_autotest, test_trace_perf);