26122a5e8a14867522ad0ec8b779e3c8f86b3265
[dpdk.git] / app / test-eventdev / test_perf_common.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Cavium, Inc
3  */
4
5 #include "test_perf_common.h"
6
7 int
8 perf_test_result(struct evt_test *test, struct evt_options *opt)
9 {
10         RTE_SET_USED(opt);
11         struct test_perf *t = evt_test_priv(test);
12
13         return t->result;
14 }
15
16 static inline int
17 perf_producer(void *arg)
18 {
19         struct prod_data *p  = arg;
20         struct test_perf *t = p->t;
21         struct evt_options *opt = t->opt;
22         const uint8_t dev_id = p->dev_id;
23         const uint8_t port = p->port_id;
24         struct rte_mempool *pool = t->pool;
25         const uint64_t nb_pkts = t->nb_pkts;
26         const uint32_t nb_flows = t->nb_flows;
27         uint32_t flow_counter = 0;
28         uint64_t count = 0;
29         struct perf_elt *m;
30         struct rte_event ev;
31
32         if (opt->verbose_level > 1)
33                 printf("%s(): lcore %d dev_id %d port=%d queue %d\n", __func__,
34                                 rte_lcore_id(), dev_id, port, p->queue_id);
35
36         ev.event = 0;
37         ev.op = RTE_EVENT_OP_NEW;
38         ev.queue_id = p->queue_id;
39         ev.sched_type = t->opt->sched_type_list[0];
40         ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL;
41         ev.event_type =  RTE_EVENT_TYPE_CPU;
42         ev.sub_event_type = 0; /* stage 0 */
43
44         while (count < nb_pkts && t->done == false) {
45                 if (rte_mempool_get(pool, (void **)&m) < 0)
46                         continue;
47
48                 ev.flow_id = flow_counter++ % nb_flows;
49                 ev.event_ptr = m;
50                 m->timestamp = rte_get_timer_cycles();
51                 while (rte_event_enqueue_burst(dev_id, port, &ev, 1) != 1) {
52                         if (t->done)
53                                 break;
54                         rte_pause();
55                         m->timestamp = rte_get_timer_cycles();
56                 }
57                 count++;
58         }
59
60         return 0;
61 }
62
63 static int
64 perf_producer_wrapper(void *arg)
65 {
66         struct prod_data *p  = arg;
67         struct test_perf *t = p->t;
68         /* Launch the producer function only in case of synthetic producer. */
69         if (t->opt->prod_type == EVT_PROD_TYPE_SYNT)
70                 return perf_producer(arg);
71         return 0;
72 }
73
74 static inline uint64_t
75 processed_pkts(struct test_perf *t)
76 {
77         uint8_t i;
78         uint64_t total = 0;
79
80         rte_smp_rmb();
81         for (i = 0; i < t->nb_workers; i++)
82                 total += t->worker[i].processed_pkts;
83
84         return total;
85 }
86
87 static inline uint64_t
88 total_latency(struct test_perf *t)
89 {
90         uint8_t i;
91         uint64_t total = 0;
92
93         rte_smp_rmb();
94         for (i = 0; i < t->nb_workers; i++)
95                 total += t->worker[i].latency;
96
97         return total;
98 }
99
100
101 int
102 perf_launch_lcores(struct evt_test *test, struct evt_options *opt,
103                 int (*worker)(void *))
104 {
105         int ret, lcore_id;
106         struct test_perf *t = evt_test_priv(test);
107
108         int port_idx = 0;
109         /* launch workers */
110         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
111                 if (!(opt->wlcores[lcore_id]))
112                         continue;
113
114                 ret = rte_eal_remote_launch(worker,
115                                  &t->worker[port_idx], lcore_id);
116                 if (ret) {
117                         evt_err("failed to launch worker %d", lcore_id);
118                         return ret;
119                 }
120                 port_idx++;
121         }
122
123         /* launch producers */
124         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
125                 if (!(opt->plcores[lcore_id]))
126                         continue;
127
128                 ret = rte_eal_remote_launch(perf_producer_wrapper,
129                                 &t->prod[port_idx], lcore_id);
130                 if (ret) {
131                         evt_err("failed to launch perf_producer %d", lcore_id);
132                         return ret;
133                 }
134                 port_idx++;
135         }
136
137         const uint64_t total_pkts = opt->nb_pkts *
138                         evt_nr_active_lcores(opt->plcores);
139
140         uint64_t dead_lock_cycles = rte_get_timer_cycles();
141         int64_t dead_lock_remaining  =  total_pkts;
142         const uint64_t dead_lock_sample = rte_get_timer_hz() * 5;
143
144         uint64_t perf_cycles = rte_get_timer_cycles();
145         int64_t perf_remaining  = total_pkts;
146         const uint64_t perf_sample = rte_get_timer_hz();
147
148         static float total_mpps;
149         static uint64_t samples;
150
151         const uint64_t freq_mhz = rte_get_timer_hz() / 1000000;
152         int64_t remaining = t->outstand_pkts - processed_pkts(t);
153
154         while (t->done == false) {
155                 const uint64_t new_cycles = rte_get_timer_cycles();
156
157                 if ((new_cycles - perf_cycles) > perf_sample) {
158                         const uint64_t latency = total_latency(t);
159                         const uint64_t pkts = processed_pkts(t);
160
161                         remaining = t->outstand_pkts - pkts;
162                         float mpps = (float)(perf_remaining-remaining)/1000000;
163
164                         perf_remaining = remaining;
165                         perf_cycles = new_cycles;
166                         total_mpps += mpps;
167                         ++samples;
168                         if (opt->fwd_latency && pkts > 0) {
169                                 printf(CLGRN"\r%.3f mpps avg %.3f mpps [avg fwd latency %.3f us] "CLNRM,
170                                         mpps, total_mpps/samples,
171                                         (float)(latency/pkts)/freq_mhz);
172                         } else {
173                                 printf(CLGRN"\r%.3f mpps avg %.3f mpps"CLNRM,
174                                         mpps, total_mpps/samples);
175                         }
176                         fflush(stdout);
177
178                         if (remaining <= 0) {
179                                 t->result = EVT_TEST_SUCCESS;
180                                 if (opt->prod_type == EVT_PROD_TYPE_SYNT) {
181                                         t->done = true;
182                                         rte_smp_wmb();
183                                         break;
184                                 }
185                         }
186                 }
187
188                 if (new_cycles - dead_lock_cycles > dead_lock_sample &&
189                                 opt->prod_type == EVT_PROD_TYPE_SYNT) {
190                         remaining = t->outstand_pkts - processed_pkts(t);
191                         if (dead_lock_remaining == remaining) {
192                                 rte_event_dev_dump(opt->dev_id, stdout);
193                                 evt_err("No schedules for seconds, deadlock");
194                                 t->done = true;
195                                 rte_smp_wmb();
196                                 break;
197                         }
198                         dead_lock_remaining = remaining;
199                         dead_lock_cycles = new_cycles;
200                 }
201         }
202         printf("\n");
203         return 0;
204 }
205
206 static int
207 perf_event_rx_adapter_setup(struct evt_options *opt, uint8_t stride,
208                 struct rte_event_port_conf prod_conf)
209 {
210         int ret = 0;
211         uint16_t prod;
212         struct rte_event_eth_rx_adapter_queue_conf queue_conf;
213
214         memset(&queue_conf, 0,
215                         sizeof(struct rte_event_eth_rx_adapter_queue_conf));
216         queue_conf.ev.sched_type = opt->sched_type_list[0];
217         for (prod = 0; prod < rte_eth_dev_count(); prod++) {
218                 uint32_t cap;
219
220                 ret = rte_event_eth_rx_adapter_caps_get(opt->dev_id,
221                                 prod, &cap);
222                 if (ret) {
223                         evt_err("failed to get event rx adapter[%d]"
224                                         " capabilities",
225                                         opt->dev_id);
226                         return ret;
227                 }
228                 queue_conf.ev.queue_id = prod * stride;
229                 ret = rte_event_eth_rx_adapter_create(prod, opt->dev_id,
230                                 &prod_conf);
231                 if (ret) {
232                         evt_err("failed to create rx adapter[%d]", prod);
233                         return ret;
234                 }
235                 ret = rte_event_eth_rx_adapter_queue_add(prod, prod, -1,
236                                 &queue_conf);
237                 if (ret) {
238                         evt_err("failed to add rx queues to adapter[%d]", prod);
239                         return ret;
240                 }
241
242                 ret = rte_eth_dev_start(prod);
243                 if (ret) {
244                         evt_err("Ethernet dev [%d] failed to start."
245                                         " Using synthetic producer", prod);
246                         return ret;
247                 }
248
249                 ret = rte_event_eth_rx_adapter_start(prod);
250                 if (ret) {
251                         evt_err("Rx adapter[%d] start failed", prod);
252                         return ret;
253                 }
254                 printf("%s: Port[%d] using Rx adapter[%d] started\n", __func__,
255                                 prod, prod);
256         }
257
258         return ret;
259 }
260
261 int
262 perf_event_dev_port_setup(struct evt_test *test, struct evt_options *opt,
263                                 uint8_t stride, uint8_t nb_queues)
264 {
265         struct test_perf *t = evt_test_priv(test);
266         uint16_t port, prod;
267         int ret = -1;
268         struct rte_event_port_conf port_conf;
269
270         memset(&port_conf, 0, sizeof(struct rte_event_port_conf));
271         rte_event_port_default_conf_get(opt->dev_id, 0, &port_conf);
272
273         /* port configuration */
274         const struct rte_event_port_conf wkr_p_conf = {
275                         .dequeue_depth = opt->wkr_deq_dep,
276                         .enqueue_depth = port_conf.enqueue_depth,
277                         .new_event_threshold = port_conf.new_event_threshold,
278         };
279
280         /* setup one port per worker, linking to all queues */
281         for (port = 0; port < evt_nr_active_lcores(opt->wlcores);
282                                 port++) {
283                 struct worker_data *w = &t->worker[port];
284
285                 w->dev_id = opt->dev_id;
286                 w->port_id = port;
287                 w->t = t;
288                 w->processed_pkts = 0;
289                 w->latency = 0;
290
291                 ret = rte_event_port_setup(opt->dev_id, port, &wkr_p_conf);
292                 if (ret) {
293                         evt_err("failed to setup port %d", port);
294                         return ret;
295                 }
296
297                 ret = rte_event_port_link(opt->dev_id, port, NULL, NULL, 0);
298                 if (ret != nb_queues) {
299                         evt_err("failed to link all queues to port %d", port);
300                         return -EINVAL;
301                 }
302         }
303
304         /* port for producers, no links */
305         struct rte_event_port_conf prod_conf = {
306                         .dequeue_depth = port_conf.dequeue_depth,
307                         .enqueue_depth = port_conf.enqueue_depth,
308                         .new_event_threshold = port_conf.new_event_threshold,
309         };
310         if (opt->prod_type == EVT_PROD_TYPE_ETH_RX_ADPTR) {
311                 for ( ; port < perf_nb_event_ports(opt); port++) {
312                         struct prod_data *p = &t->prod[port];
313                         p->t = t;
314                 }
315
316                 ret = perf_event_rx_adapter_setup(opt, stride, prod_conf);
317                 if (ret)
318                         return ret;
319         } else {
320                 prod = 0;
321                 for ( ; port < perf_nb_event_ports(opt); port++) {
322                         struct prod_data *p = &t->prod[port];
323
324                         p->dev_id = opt->dev_id;
325                         p->port_id = port;
326                         p->queue_id = prod * stride;
327                         p->t = t;
328
329                         ret = rte_event_port_setup(opt->dev_id, port,
330                                         &prod_conf);
331                         if (ret) {
332                                 evt_err("failed to setup port %d", port);
333                                 return ret;
334                         }
335                         prod++;
336                 }
337         }
338
339         return ret;
340 }
341
342 int
343 perf_opt_check(struct evt_options *opt, uint64_t nb_queues)
344 {
345         unsigned int lcores;
346
347         /* N producer + N worker + 1 master when producer cores are used
348          * Else N worker + 1 master when Rx adapter is used
349          */
350         lcores = opt->prod_type == EVT_PROD_TYPE_SYNT ? 3 : 2;
351
352         if (rte_lcore_count() < lcores) {
353                 evt_err("test need minimum %d lcores", lcores);
354                 return -1;
355         }
356
357         /* Validate worker lcores */
358         if (evt_lcores_has_overlap(opt->wlcores, rte_get_master_lcore())) {
359                 evt_err("worker lcores overlaps with master lcore");
360                 return -1;
361         }
362         if (evt_lcores_has_overlap_multi(opt->wlcores, opt->plcores)) {
363                 evt_err("worker lcores overlaps producer lcores");
364                 return -1;
365         }
366         if (evt_has_disabled_lcore(opt->wlcores)) {
367                 evt_err("one or more workers lcores are not enabled");
368                 return -1;
369         }
370         if (!evt_has_active_lcore(opt->wlcores)) {
371                 evt_err("minimum one worker is required");
372                 return -1;
373         }
374
375         if (opt->prod_type == EVT_PROD_TYPE_SYNT) {
376                 /* Validate producer lcores */
377                 if (evt_lcores_has_overlap(opt->plcores,
378                                         rte_get_master_lcore())) {
379                         evt_err("producer lcores overlaps with master lcore");
380                         return -1;
381                 }
382                 if (evt_has_disabled_lcore(opt->plcores)) {
383                         evt_err("one or more producer lcores are not enabled");
384                         return -1;
385                 }
386                 if (!evt_has_active_lcore(opt->plcores)) {
387                         evt_err("minimum one producer is required");
388                         return -1;
389                 }
390         }
391
392         if (evt_has_invalid_stage(opt))
393                 return -1;
394
395         if (evt_has_invalid_sched_type(opt))
396                 return -1;
397
398         if (nb_queues > EVT_MAX_QUEUES) {
399                 evt_err("number of queues exceeds %d", EVT_MAX_QUEUES);
400                 return -1;
401         }
402         if (perf_nb_event_ports(opt) > EVT_MAX_PORTS) {
403                 evt_err("number of ports exceeds %d", EVT_MAX_PORTS);
404                 return -1;
405         }
406
407         /* Fixups */
408         if (opt->nb_stages == 1 && opt->fwd_latency) {
409                 evt_info("fwd_latency is valid when nb_stages > 1, disabling");
410                 opt->fwd_latency = 0;
411         }
412         if (opt->fwd_latency && !opt->q_priority) {
413                 evt_info("enabled queue priority for latency measurement");
414                 opt->q_priority = 1;
415         }
416         if (opt->nb_pkts == 0)
417                 opt->nb_pkts = INT64_MAX/evt_nr_active_lcores(opt->plcores);
418
419         return 0;
420 }
421
422 void
423 perf_opt_dump(struct evt_options *opt, uint8_t nb_queues)
424 {
425         evt_dump("nb_prod_lcores", "%d", evt_nr_active_lcores(opt->plcores));
426         evt_dump_producer_lcores(opt);
427         evt_dump("nb_worker_lcores", "%d", evt_nr_active_lcores(opt->wlcores));
428         evt_dump_worker_lcores(opt);
429         evt_dump_nb_stages(opt);
430         evt_dump("nb_evdev_ports", "%d", perf_nb_event_ports(opt));
431         evt_dump("nb_evdev_queues", "%d", nb_queues);
432         evt_dump_queue_priority(opt);
433         evt_dump_sched_type_list(opt);
434         evt_dump_producer_type(opt);
435 }
436
437 void
438 perf_eventdev_destroy(struct evt_test *test, struct evt_options *opt)
439 {
440         RTE_SET_USED(test);
441
442         rte_event_dev_stop(opt->dev_id);
443         rte_event_dev_close(opt->dev_id);
444 }
445
446 static inline void
447 perf_elt_init(struct rte_mempool *mp, void *arg __rte_unused,
448             void *obj, unsigned i __rte_unused)
449 {
450         memset(obj, 0, mp->elt_size);
451 }
452
453 #define NB_RX_DESC                      128
454 #define NB_TX_DESC                      512
455 int
456 perf_ethdev_setup(struct evt_test *test, struct evt_options *opt)
457 {
458         int i;
459         struct test_perf *t = evt_test_priv(test);
460         struct rte_eth_conf port_conf = {
461                 .rxmode = {
462                         .mq_mode = ETH_MQ_RX_RSS,
463                         .max_rx_pkt_len = ETHER_MAX_LEN,
464                         .split_hdr_size = 0,
465                         .header_split   = 0,
466                         .hw_ip_checksum = 0,
467                         .hw_vlan_filter = 0,
468                         .hw_vlan_strip  = 0,
469                         .hw_vlan_extend = 0,
470                         .jumbo_frame    = 0,
471                         .hw_strip_crc   = 1,
472                 },
473                 .rx_adv_conf = {
474                         .rss_conf = {
475                                 .rss_key = NULL,
476                                 .rss_hf = ETH_RSS_IP,
477                         },
478                 },
479         };
480
481         if (opt->prod_type == EVT_PROD_TYPE_SYNT)
482                 return 0;
483
484         if (!rte_eth_dev_count()) {
485                 evt_err("No ethernet ports found.");
486                 return -ENODEV;
487         }
488
489         for (i = 0; i < rte_eth_dev_count(); i++) {
490
491                 if (rte_eth_dev_configure(i, 1, 1,
492                                         &port_conf)
493                                 < 0) {
494                         evt_err("Failed to configure eth port [%d]", i);
495                         return -EINVAL;
496                 }
497
498                 if (rte_eth_rx_queue_setup(i, 0, NB_RX_DESC,
499                                 rte_socket_id(), NULL, t->pool) < 0) {
500                         evt_err("Failed to setup eth port [%d] rx_queue: %d.",
501                                         i, 0);
502                         return -EINVAL;
503                 }
504
505                 if (rte_eth_tx_queue_setup(i, 0, NB_TX_DESC,
506                                         rte_socket_id(), NULL) < 0) {
507                         evt_err("Failed to setup eth port [%d] tx_queue: %d.",
508                                         i, 0);
509                         return -EINVAL;
510                 }
511
512                 rte_eth_promiscuous_enable(i);
513         }
514
515         return 0;
516 }
517
518 void perf_ethdev_destroy(struct evt_test *test, struct evt_options *opt)
519 {
520         int i;
521         RTE_SET_USED(test);
522
523         if (opt->prod_type == EVT_PROD_TYPE_ETH_RX_ADPTR) {
524                 for (i = 0; i < rte_eth_dev_count(); i++) {
525                         rte_event_eth_rx_adapter_stop(i);
526                         rte_eth_dev_stop(i);
527                         rte_eth_dev_close(i);
528                 }
529         }
530 }
531
532 int
533 perf_mempool_setup(struct evt_test *test, struct evt_options *opt)
534 {
535         struct test_perf *t = evt_test_priv(test);
536
537         if (opt->prod_type == EVT_PROD_TYPE_SYNT) {
538                 t->pool = rte_mempool_create(test->name, /* mempool name */
539                                 opt->pool_sz, /* number of elements*/
540                                 sizeof(struct perf_elt), /* element size*/
541                                 512, /* cache size*/
542                                 0, NULL, NULL,
543                                 perf_elt_init, /* obj constructor */
544                                 NULL, opt->socket_id, 0); /* flags */
545         } else {
546                 t->pool = rte_pktmbuf_pool_create(test->name, /* mempool name */
547                                 opt->pool_sz, /* number of elements*/
548                                 512, /* cache size*/
549                                 0,
550                                 RTE_MBUF_DEFAULT_BUF_SIZE,
551                                 opt->socket_id); /* flags */
552
553         }
554
555         if (t->pool == NULL) {
556                 evt_err("failed to create mempool");
557                 return -ENOMEM;
558         }
559
560         return 0;
561 }
562
563 void
564 perf_mempool_destroy(struct evt_test *test, struct evt_options *opt)
565 {
566         RTE_SET_USED(opt);
567         struct test_perf *t = evt_test_priv(test);
568
569         rte_mempool_free(t->pool);
570 }
571
572 int
573 perf_test_setup(struct evt_test *test, struct evt_options *opt)
574 {
575         void *test_perf;
576
577         test_perf = rte_zmalloc_socket(test->name, sizeof(struct test_perf),
578                                 RTE_CACHE_LINE_SIZE, opt->socket_id);
579         if (test_perf  == NULL) {
580                 evt_err("failed to allocate test_perf memory");
581                 goto nomem;
582         }
583         test->test_priv = test_perf;
584
585         struct test_perf *t = evt_test_priv(test);
586
587         t->outstand_pkts = opt->nb_pkts * evt_nr_active_lcores(opt->plcores);
588         t->nb_workers = evt_nr_active_lcores(opt->wlcores);
589         t->done = false;
590         t->nb_pkts = opt->nb_pkts;
591         t->nb_flows = opt->nb_flows;
592         t->result = EVT_TEST_FAILED;
593         t->opt = opt;
594         memcpy(t->sched_type_list, opt->sched_type_list,
595                         sizeof(opt->sched_type_list));
596         return 0;
597 nomem:
598         return -ENOMEM;
599 }
600
601 void
602 perf_test_destroy(struct evt_test *test, struct evt_options *opt)
603 {
604         RTE_SET_USED(opt);
605
606         rte_free(test->test_priv);
607 }