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;
86 #define WORKER_DEFINE(func) \
87 static void __rte_noinline \
88 __worker_##func(struct lcore_data *ldata) \
92 while (!ldata->done) { \
93 start = rte_get_timer_cycles(); \
94 for (i = 0; i < STEP; i++) \
96 ldata->total_cycles += rte_get_timer_cycles() - start; \
97 ldata->total_calls++; \
101 worker_fn_##func(void *arg) \
103 struct lcore_data *ldata = arg; \
104 ldata->started = 1; \
106 __worker_##func(ldata); \
111 /* Test to find trace overhead */
112 #define GENERIC_VOID rte_eal_trace_generic_void()
113 #define GENERIC_U64 rte_eal_trace_generic_u64(0x120000)
114 #define GENERIC_INT rte_eal_trace_generic_int(-34)
115 #define GENERIC_FLOAT rte_eal_trace_generic_float(3.3f)
116 #define GENERIC_DOUBLE rte_eal_trace_generic_double(3.66666)
117 #define GENERIC_STR rte_eal_trace_generic_str("hello world")
118 #define VOID_FP app_dpdk_test_fp()
120 WORKER_DEFINE(GENERIC_VOID)
121 WORKER_DEFINE(GENERIC_U64)
122 WORKER_DEFINE(GENERIC_INT)
123 WORKER_DEFINE(GENERIC_FLOAT)
124 WORKER_DEFINE(GENERIC_DOUBLE)
125 WORKER_DEFINE(GENERIC_STR)
126 WORKER_DEFINE(VOID_FP)
129 run_test(const char *str, lcore_function_t f, struct test_data *data, size_t sz)
131 unsigned int id, worker = 0;
134 data->nb_workers = rte_lcore_count() - 1;
135 RTE_LCORE_FOREACH_SLAVE(id)
136 rte_eal_remote_launch(f, &data->ldata[worker++], id);
138 wait_till_workers_are_ready(data);
139 rte_delay_ms(100); /* Wait for some time to accumulate the stats */
140 measure_perf(str, data);
141 signal_workers_to_finish(data);
143 RTE_LCORE_FOREACH_SLAVE(id)
144 rte_eal_wait_lcore(id);
148 test_trace_perf(void)
150 unsigned int nb_cores, nb_workers;
151 struct test_data *data;
154 nb_cores = rte_lcore_count();
155 nb_workers = nb_cores - 1;
157 printf("Need minimum two cores for testing\n");
161 printf("Timer running at %5.2fMHz\n", rte_get_timer_hz()/1E6);
162 sz = sizeof(struct test_data);
163 sz += nb_workers * sizeof(struct lcore_data);
165 data = rte_zmalloc(NULL, sz, RTE_CACHE_LINE_SIZE);
167 printf("Failed to allocate memory\n");
171 run_test("void", worker_fn_GENERIC_VOID, data, sz);
172 run_test("u64", worker_fn_GENERIC_U64, data, sz);
173 run_test("int", worker_fn_GENERIC_INT, data, sz);
174 run_test("float", worker_fn_GENERIC_FLOAT, data, sz);
175 run_test("double", worker_fn_GENERIC_DOUBLE, data, sz);
176 run_test("string", worker_fn_GENERIC_STR, data, sz);
177 run_test("void_fp", worker_fn_VOID_FP, data, sz);
183 REGISTER_TEST_COMMAND(trace_perf_autotest, test_trace_perf);