app/testeventdev: add perf port setup
[dpdk.git] / app / test-eventdev / test_perf_common.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium 2017.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Cavium networks nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "test_perf_common.h"
34
35 int
36 perf_test_result(struct evt_test *test, struct evt_options *opt)
37 {
38         RTE_SET_USED(opt);
39         struct test_perf *t = evt_test_priv(test);
40
41         return t->result;
42 }
43
44 int
45 perf_event_dev_port_setup(struct evt_test *test, struct evt_options *opt,
46                                 uint8_t stride, uint8_t nb_queues)
47 {
48         struct test_perf *t = evt_test_priv(test);
49         uint8_t port, prod;
50         int ret = -1;
51
52         /* port configuration */
53         const struct rte_event_port_conf wkr_p_conf = {
54                         .dequeue_depth = opt->wkr_deq_dep,
55                         .enqueue_depth = 64,
56                         .new_event_threshold = 4096,
57         };
58
59         /* setup one port per worker, linking to all queues */
60         for (port = 0; port < evt_nr_active_lcores(opt->wlcores);
61                                 port++) {
62                 struct worker_data *w = &t->worker[port];
63
64                 w->dev_id = opt->dev_id;
65                 w->port_id = port;
66                 w->t = t;
67                 w->processed_pkts = 0;
68                 w->latency = 0;
69
70                 ret = rte_event_port_setup(opt->dev_id, port, &wkr_p_conf);
71                 if (ret) {
72                         evt_err("failed to setup port %d", port);
73                         return ret;
74                 }
75
76                 ret = rte_event_port_link(opt->dev_id, port, NULL, NULL, 0);
77                 if (ret != nb_queues) {
78                         evt_err("failed to link all queues to port %d", port);
79                         return -EINVAL;
80                 }
81         }
82
83         /* port for producers, no links */
84         const struct rte_event_port_conf prod_conf = {
85                         .dequeue_depth = 8,
86                         .enqueue_depth = 32,
87                         .new_event_threshold = 1200,
88         };
89         prod = 0;
90         for ( ; port < perf_nb_event_ports(opt); port++) {
91                 struct prod_data *p = &t->prod[port];
92
93                 p->dev_id = opt->dev_id;
94                 p->port_id = port;
95                 p->queue_id = prod * stride;
96                 p->t = t;
97
98                 ret = rte_event_port_setup(opt->dev_id, port, &prod_conf);
99                 if (ret) {
100                         evt_err("failed to setup port %d", port);
101                         return ret;
102                 }
103                 prod++;
104         }
105
106         return ret;
107 }
108
109 int
110 perf_opt_check(struct evt_options *opt, uint64_t nb_queues)
111 {
112         unsigned int lcores;
113         bool need_slcore = !evt_has_distributed_sched(opt->dev_id);
114
115         /* N producer + N worker + 1 scheduler(based on dev capa) + 1 master */
116         lcores = need_slcore ? 4 : 3;
117
118         if (rte_lcore_count() < lcores) {
119                 evt_err("test need minimum %d lcores", lcores);
120                 return -1;
121         }
122
123         /* Validate worker lcores */
124         if (evt_lcores_has_overlap(opt->wlcores, rte_get_master_lcore())) {
125                 evt_err("worker lcores overlaps with master lcore");
126                 return -1;
127         }
128         if (need_slcore && evt_lcores_has_overlap(opt->wlcores, opt->slcore)) {
129                 evt_err("worker lcores overlaps with scheduler lcore");
130                 return -1;
131         }
132         if (evt_lcores_has_overlap_multi(opt->wlcores, opt->plcores)) {
133                 evt_err("worker lcores overlaps producer lcores");
134                 return -1;
135         }
136         if (evt_has_disabled_lcore(opt->wlcores)) {
137                 evt_err("one or more workers lcores are not enabled");
138                 return -1;
139         }
140         if (!evt_has_active_lcore(opt->wlcores)) {
141                 evt_err("minimum one worker is required");
142                 return -1;
143         }
144
145         /* Validate producer lcores */
146         if (evt_lcores_has_overlap(opt->plcores, rte_get_master_lcore())) {
147                 evt_err("producer lcores overlaps with master lcore");
148                 return -1;
149         }
150         if (need_slcore && evt_lcores_has_overlap(opt->plcores, opt->slcore)) {
151                 evt_err("producer lcores overlaps with scheduler lcore");
152                 return -1;
153         }
154         if (evt_has_disabled_lcore(opt->plcores)) {
155                 evt_err("one or more producer lcores are not enabled");
156                 return -1;
157         }
158         if (!evt_has_active_lcore(opt->plcores)) {
159                 evt_err("minimum one producer is required");
160                 return -1;
161         }
162
163         /* Validate scheduler lcore */
164         if (!evt_has_distributed_sched(opt->dev_id) &&
165                         opt->slcore == (int)rte_get_master_lcore()) {
166                 evt_err("scheduler lcore and master lcore should be different");
167                 return -1;
168         }
169         if (need_slcore && !rte_lcore_is_enabled(opt->slcore)) {
170                 evt_err("scheduler lcore is not enabled");
171                 return -1;
172         }
173
174         if (evt_has_invalid_stage(opt))
175                 return -1;
176
177         if (evt_has_invalid_sched_type(opt))
178                 return -1;
179
180         if (nb_queues > EVT_MAX_QUEUES) {
181                 evt_err("number of queues exceeds %d", EVT_MAX_QUEUES);
182                 return -1;
183         }
184         if (perf_nb_event_ports(opt) > EVT_MAX_PORTS) {
185                 evt_err("number of ports exceeds %d", EVT_MAX_PORTS);
186                 return -1;
187         }
188
189         /* Fixups */
190         if (opt->nb_stages == 1 && opt->fwd_latency) {
191                 evt_info("fwd_latency is valid when nb_stages > 1, disabling");
192                 opt->fwd_latency = 0;
193         }
194         if (opt->fwd_latency && !opt->q_priority) {
195                 evt_info("enabled queue priority for latency measurement");
196                 opt->q_priority = 1;
197         }
198
199         return 0;
200 }
201
202 void
203 perf_opt_dump(struct evt_options *opt, uint8_t nb_queues)
204 {
205         evt_dump("nb_prod_lcores", "%d", evt_nr_active_lcores(opt->plcores));
206         evt_dump_producer_lcores(opt);
207         evt_dump("nb_worker_lcores", "%d", evt_nr_active_lcores(opt->wlcores));
208         evt_dump_worker_lcores(opt);
209         if (!evt_has_distributed_sched(opt->dev_id))
210                 evt_dump_scheduler_lcore(opt);
211         evt_dump_nb_stages(opt);
212         evt_dump("nb_evdev_ports", "%d", perf_nb_event_ports(opt));
213         evt_dump("nb_evdev_queues", "%d", nb_queues);
214         evt_dump_queue_priority(opt);
215         evt_dump_sched_type_list(opt);
216 }
217
218 void
219 perf_eventdev_destroy(struct evt_test *test, struct evt_options *opt)
220 {
221         RTE_SET_USED(test);
222
223         rte_event_dev_stop(opt->dev_id);
224         rte_event_dev_close(opt->dev_id);
225 }
226
227 static inline void
228 perf_elt_init(struct rte_mempool *mp, void *arg __rte_unused,
229             void *obj, unsigned i __rte_unused)
230 {
231         memset(obj, 0, mp->elt_size);
232 }
233
234 int
235 perf_mempool_setup(struct evt_test *test, struct evt_options *opt)
236 {
237         struct test_perf *t = evt_test_priv(test);
238
239         t->pool = rte_mempool_create(test->name, /* mempool name */
240                                 opt->pool_sz, /* number of elements*/
241                                 sizeof(struct perf_elt), /* element size*/
242                                 512, /* cache size*/
243                                 0, NULL, NULL,
244                                 perf_elt_init, /* obj constructor */
245                                 NULL, opt->socket_id, 0); /* flags */
246         if (t->pool == NULL) {
247                 evt_err("failed to create mempool");
248                 return -ENOMEM;
249         }
250
251         return 0;
252 }
253
254 void
255 perf_mempool_destroy(struct evt_test *test, struct evt_options *opt)
256 {
257         RTE_SET_USED(opt);
258         struct test_perf *t = evt_test_priv(test);
259
260         rte_mempool_free(t->pool);
261 }
262
263 int
264 perf_test_setup(struct evt_test *test, struct evt_options *opt)
265 {
266         void *test_perf;
267
268         test_perf = rte_zmalloc_socket(test->name, sizeof(struct test_perf),
269                                 RTE_CACHE_LINE_SIZE, opt->socket_id);
270         if (test_perf  == NULL) {
271                 evt_err("failed to allocate test_perf memory");
272                 goto nomem;
273         }
274         test->test_priv = test_perf;
275
276         struct test_perf *t = evt_test_priv(test);
277
278         t->outstand_pkts = opt->nb_pkts * evt_nr_active_lcores(opt->plcores);
279         t->nb_workers = evt_nr_active_lcores(opt->wlcores);
280         t->done = false;
281         t->nb_pkts = opt->nb_pkts;
282         t->nb_flows = opt->nb_flows;
283         t->result = EVT_TEST_FAILED;
284         t->opt = opt;
285         memcpy(t->sched_type_list, opt->sched_type_list,
286                         sizeof(opt->sched_type_list));
287         return 0;
288 nomem:
289         return -ENOMEM;
290 }
291
292 void
293 perf_test_destroy(struct evt_test *test, struct evt_options *opt)
294 {
295         RTE_SET_USED(opt);
296
297         rte_free(test->test_priv);
298 }