1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Cavium, Inc
3 * Copyright(c) 2017-2018 Intel Corporation.
6 #include <rte_atomic.h>
7 #include <rte_common.h>
8 #include <rte_cycles.h>
11 #include <rte_ethdev.h>
12 #include <rte_eventdev.h>
13 #include <rte_event_timer_adapter.h>
14 #include <rte_mempool.h>
15 #include <rte_launch.h>
16 #include <rte_lcore.h>
17 #include <rte_per_lcore.h>
18 #include <rte_random.h>
19 #include <rte_bus_vdev.h>
20 #include <rte_service.h>
25 /* 4K timers corresponds to sw evdev max inflight events */
26 #define MAX_TIMERS (4 * 1024)
29 #define NSECPERSEC 1E9
31 /* Both the app lcore and adapter ports are linked to this queue */
32 #define TEST_QUEUE_ID 0
33 /* Port the application dequeues from */
34 #define TEST_PORT_ID 0
35 #define TEST_ADAPTER_ID 0
37 /* Handle log statements in same manner as test macros */
38 #define LOG_DBG(...) RTE_LOG(DEBUG, EAL, __VA_ARGS__)
41 static struct rte_event_timer_adapter *timdev;
42 static struct rte_mempool *eventdev_test_mempool;
43 static struct rte_ring *timer_producer_ring;
44 static uint64_t global_bkt_tck_ns;
45 static volatile uint8_t arm_done;
47 static bool using_services;
48 static uint32_t test_lcore1;
49 static uint32_t test_lcore2;
50 static uint32_t test_lcore3;
51 static uint32_t sw_evdev_slcore;
52 static uint32_t sw_adptr_slcore;
55 devconf_set_default_sane_values(struct rte_event_dev_config *dev_conf,
56 struct rte_event_dev_info *info)
58 memset(dev_conf, 0, sizeof(struct rte_event_dev_config));
59 dev_conf->dequeue_timeout_ns = info->min_dequeue_timeout_ns;
60 dev_conf->nb_event_ports = 1;
61 dev_conf->nb_event_queues = 1;
62 dev_conf->nb_event_queue_flows = info->max_event_queue_flows;
63 dev_conf->nb_event_port_dequeue_depth =
64 info->max_event_port_dequeue_depth;
65 dev_conf->nb_event_port_enqueue_depth =
66 info->max_event_port_enqueue_depth;
67 dev_conf->nb_event_port_enqueue_depth =
68 info->max_event_port_enqueue_depth;
69 dev_conf->nb_events_limit =
77 struct rte_event_dev_config dev_conf;
78 struct rte_event_dev_info info;
81 ret = rte_event_dev_info_get(evdev, &info);
82 TEST_ASSERT_SUCCESS(ret, "Failed to get event dev info");
83 TEST_ASSERT(info.max_num_events >= (int32_t)MAX_TIMERS,
84 "ERROR max_num_events=%d < max_events=%d",
85 info.max_num_events, MAX_TIMERS);
87 devconf_set_default_sane_values(&dev_conf, &info);
88 ret = rte_event_dev_configure(evdev, &dev_conf);
89 TEST_ASSERT_SUCCESS(ret, "Failed to configure eventdev");
91 ret = rte_event_queue_setup(evdev, 0, NULL);
92 TEST_ASSERT_SUCCESS(ret, "Failed to setup queue=%d", 0);
94 /* Configure event port */
95 ret = rte_event_port_setup(evdev, 0, NULL);
96 TEST_ASSERT_SUCCESS(ret, "Failed to setup port=%d", 0);
97 ret = rte_event_port_link(evdev, 0, NULL, NULL, 0);
98 TEST_ASSERT(ret >= 0, "Failed to link all queues port=%d", 0);
100 /* If this is a software event device, map and start its service */
101 if (rte_event_dev_service_id_get(evdev, &service_id) == 0) {
102 TEST_ASSERT_SUCCESS(rte_service_lcore_add(sw_evdev_slcore),
103 "Failed to add service core");
104 TEST_ASSERT_SUCCESS(rte_service_lcore_start(
106 "Failed to start service core");
107 TEST_ASSERT_SUCCESS(rte_service_map_lcore_set(
108 service_id, sw_evdev_slcore, 1),
109 "Failed to map evdev service");
110 TEST_ASSERT_SUCCESS(rte_service_runstate_set(
112 "Failed to start evdev service");
115 ret = rte_event_dev_start(evdev);
116 TEST_ASSERT_SUCCESS(ret, "Failed to start device");
122 testsuite_setup(void)
124 /* Some of the multithreaded tests require 3 other lcores to run */
125 unsigned int required_lcore_count = 4;
128 /* To make it easier to map services later if needed, just reset
129 * service core state.
131 (void) rte_service_lcore_reset_all();
133 if (!rte_event_dev_count()) {
134 /* If there is no hardware eventdev, or no software vdev was
135 * specified on the command line, create an instance of
138 LOG_DBG("Failed to find a valid event device... testing with"
139 " event_sw device\n");
140 TEST_ASSERT_SUCCESS(rte_vdev_init("event_sw0", NULL),
141 "Error creating eventdev");
142 evdev = rte_event_dev_get_dev_id("event_sw0");
145 if (rte_event_dev_service_id_get(evdev, &service_id) == 0) {
146 /* A software event device will use a software event timer
147 * adapter as well. 2 more cores required to convert to
150 required_lcore_count += 2;
151 using_services = true;
154 if (rte_lcore_count() < required_lcore_count) {
155 printf("%d lcores needed to run tests", required_lcore_count);
159 /* Assign lcores for various tasks */
160 test_lcore1 = rte_get_next_lcore(-1, 1, 0);
161 test_lcore2 = rte_get_next_lcore(test_lcore1, 1, 0);
162 test_lcore3 = rte_get_next_lcore(test_lcore2, 1, 0);
163 if (using_services) {
164 sw_evdev_slcore = rte_get_next_lcore(test_lcore3, 1, 0);
165 sw_adptr_slcore = rte_get_next_lcore(sw_evdev_slcore, 1, 0);
168 return eventdev_setup();
172 testsuite_teardown(void)
174 rte_event_dev_stop(evdev);
175 rte_event_dev_close(evdev);
179 setup_adapter_service(struct rte_event_timer_adapter *adptr)
181 uint32_t adapter_service_id;
184 /* retrieve service ids */
185 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_service_id_get(adptr,
186 &adapter_service_id), "Failed to get event timer "
187 "adapter service id");
188 /* add a service core and start it */
189 ret = rte_service_lcore_add(sw_adptr_slcore);
190 TEST_ASSERT(ret == 0 || ret == -EALREADY,
191 "Failed to add service core");
192 ret = rte_service_lcore_start(sw_adptr_slcore);
193 TEST_ASSERT(ret == 0 || ret == -EALREADY,
194 "Failed to start service core");
196 /* map services to it */
197 TEST_ASSERT_SUCCESS(rte_service_map_lcore_set(adapter_service_id,
199 "Failed to map adapter service");
201 /* set services to running */
202 TEST_ASSERT_SUCCESS(rte_service_runstate_set(adapter_service_id, 1),
203 "Failed to start event timer adapter service");
209 test_port_conf_cb(uint16_t id, uint8_t event_dev_id, uint8_t *event_port_id,
212 struct rte_event_dev_config dev_conf;
213 struct rte_event_dev_info info;
214 struct rte_event_port_conf *port_conf, def_port_conf = {0};
216 static int port_allocated;
217 static uint8_t port_id;
220 if (port_allocated) {
221 *event_port_id = port_id;
227 ret = rte_event_dev_attr_get(event_dev_id, RTE_EVENT_DEV_ATTR_STARTED,
233 rte_event_dev_stop(event_dev_id);
235 ret = rte_event_dev_info_get(evdev, &info);
239 devconf_set_default_sane_values(&dev_conf, &info);
241 port_id = dev_conf.nb_event_ports;
242 dev_conf.nb_event_ports++;
244 ret = rte_event_dev_configure(event_dev_id, &dev_conf);
247 rte_event_dev_start(event_dev_id);
251 if (conf_arg != NULL)
252 port_conf = conf_arg;
254 port_conf = &def_port_conf;
255 ret = rte_event_port_default_conf_get(event_dev_id, port_id,
261 ret = rte_event_port_setup(event_dev_id, port_id, port_conf);
265 *event_port_id = port_id;
268 rte_event_dev_start(event_dev_id);
270 /* Reuse this port number next time this is called */
277 _timdev_setup(uint64_t max_tmo_ns, uint64_t bkt_tck_ns)
279 struct rte_event_timer_adapter_conf config = {
280 .event_dev_id = evdev,
281 .timer_adapter_id = TEST_ADAPTER_ID,
282 .timer_tick_ns = bkt_tck_ns,
283 .max_tmo_ns = max_tmo_ns,
284 .nb_timers = MAX_TIMERS * 10,
287 const char *pool_name = "timdev_test_pool";
289 global_bkt_tck_ns = bkt_tck_ns;
291 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_caps_get(evdev, &caps),
292 "failed to get adapter capabilities");
293 if (!(caps & RTE_EVENT_TIMER_ADAPTER_CAP_INTERNAL_PORT)) {
294 timdev = rte_event_timer_adapter_create_ext(&config,
297 setup_adapter_service(timdev);
298 using_services = true;
300 timdev = rte_event_timer_adapter_create(&config);
302 TEST_ASSERT_NOT_NULL(timdev,
303 "failed to create event timer ring");
305 TEST_ASSERT_EQUAL(rte_event_timer_adapter_start(timdev), 0,
306 "failed to Start event timer adapter");
308 /* Create event timer mempool */
309 eventdev_test_mempool = rte_mempool_create(pool_name,
311 sizeof(struct rte_event_timer), /* element size*/
313 0, NULL, NULL, NULL, NULL,
315 if (!eventdev_test_mempool) {
316 printf("ERROR creating mempool\n");
324 timdev_setup_usec(void)
326 return using_services ?
327 /* Max timeout is 10,000us and bucket interval is 100us */
328 _timdev_setup(1E7, 1E5) :
329 /* Max timeout is 100us and bucket interval is 1us */
330 _timdev_setup(1E5, 1E3);
334 timdev_setup_usec_multicore(void)
336 return using_services ?
337 /* Max timeout is 10,000us and bucket interval is 100us */
338 _timdev_setup(1E7, 1E5) :
339 /* Max timeout is 100us and bucket interval is 1us */
340 _timdev_setup(1E5, 1E3);
344 timdev_setup_msec(void)
346 /* Max timeout is 2 mins, and bucket interval is 100 ms */
347 return _timdev_setup(180 * NSECPERSEC, NSECPERSEC / 10);
351 timdev_setup_sec(void)
353 /* Max timeout is 100sec and bucket interval is 1sec */
354 return _timdev_setup(1E11, 1E9);
358 timdev_setup_sec_multicore(void)
360 /* Max timeout is 100sec and bucket interval is 1sec */
361 return _timdev_setup(1E11, 1E9);
365 timdev_teardown(void)
367 rte_event_timer_adapter_stop(timdev);
368 rte_event_timer_adapter_free(timdev);
370 rte_mempool_free(eventdev_test_mempool);
374 test_timer_state(void)
376 struct rte_event_timer *ev_tim;
378 const struct rte_event_timer tim = {
379 .ev.op = RTE_EVENT_OP_NEW,
381 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
382 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
383 .ev.event_type = RTE_EVENT_TYPE_TIMER,
384 .state = RTE_EVENT_TIMER_NOT_ARMED,
387 rte_mempool_get(eventdev_test_mempool, (void **)&ev_tim);
389 ev_tim->ev.event_ptr = ev_tim;
390 ev_tim->timeout_ticks = 120;
392 TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim, 1), 0,
393 "Armed timer exceeding max_timeout.");
394 TEST_ASSERT_EQUAL(ev_tim->state, RTE_EVENT_TIMER_ERROR_TOOLATE,
395 "Improper timer state set expected %d returned %d",
396 RTE_EVENT_TIMER_ERROR_TOOLATE, ev_tim->state);
398 ev_tim->state = RTE_EVENT_TIMER_NOT_ARMED;
399 ev_tim->timeout_ticks = 10;
401 TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim, 1), 1,
402 "Failed to arm timer with proper timeout.");
403 TEST_ASSERT_EQUAL(ev_tim->state, RTE_EVENT_TIMER_ARMED,
404 "Improper timer state set expected %d returned %d",
405 RTE_EVENT_TIMER_ARMED, ev_tim->state);
410 rte_delay_us(1000 + 200);
411 TEST_ASSERT_EQUAL(rte_event_dequeue_burst(evdev, 0, &ev, 1, 0), 1,
412 "Armed timer failed to trigger.");
414 ev_tim->state = RTE_EVENT_TIMER_NOT_ARMED;
415 ev_tim->timeout_ticks = 90;
416 TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim, 1), 1,
417 "Failed to arm timer with proper timeout.");
418 TEST_ASSERT_EQUAL(rte_event_timer_cancel_burst(timdev, &ev_tim, 1),
419 1, "Failed to cancel armed timer");
420 TEST_ASSERT_EQUAL(ev_tim->state, RTE_EVENT_TIMER_CANCELED,
421 "Improper timer state set expected %d returned %d",
422 RTE_EVENT_TIMER_CANCELED, ev_tim->state);
424 rte_mempool_put(eventdev_test_mempool, (void *)ev_tim);
430 _arm_timers(uint64_t timeout_tcks, uint64_t timers)
433 struct rte_event_timer *ev_tim;
434 const struct rte_event_timer tim = {
435 .ev.op = RTE_EVENT_OP_NEW,
437 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
438 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
439 .ev.event_type = RTE_EVENT_TYPE_TIMER,
440 .state = RTE_EVENT_TIMER_NOT_ARMED,
441 .timeout_ticks = timeout_tcks,
444 for (i = 0; i < timers; i++) {
446 TEST_ASSERT_SUCCESS(rte_mempool_get(eventdev_test_mempool,
448 "mempool alloc failed");
450 ev_tim->ev.event_ptr = ev_tim;
452 TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim,
453 1), 1, "Failed to arm timer %d",
461 _wait_timer_triggers(uint64_t wait_sec, uint64_t arm_count,
462 uint64_t cancel_count)
466 uint64_t wait_start, max_wait;
469 max_wait = rte_get_timer_hz() * wait_sec;
470 wait_start = rte_get_timer_cycles();
472 if (rte_get_timer_cycles() - wait_start > max_wait) {
473 if (events + cancel_count != arm_count)
474 TEST_ASSERT_SUCCESS(max_wait,
475 "Max time limit for timers exceeded.");
479 valid_event = rte_event_dequeue_burst(evdev, 0, &ev, 1, 0);
483 rte_mempool_put(eventdev_test_mempool, ev.event_ptr);
493 TEST_ASSERT_SUCCESS(_arm_timers(20, MAX_TIMERS),
494 "Failed to arm timers");
495 TEST_ASSERT_SUCCESS(_wait_timer_triggers(10, MAX_TIMERS, 0),
496 "Timer triggered count doesn't match arm count");
501 _arm_wrapper(void *arg)
505 TEST_ASSERT_SUCCESS(_arm_timers(20, MAX_TIMERS),
506 "Failed to arm timers");
512 test_timer_arm_multicore(void)
515 uint32_t lcore_1 = rte_get_next_lcore(-1, 1, 0);
516 uint32_t lcore_2 = rte_get_next_lcore(lcore_1, 1, 0);
518 rte_eal_remote_launch(_arm_wrapper, NULL, lcore_1);
519 rte_eal_remote_launch(_arm_wrapper, NULL, lcore_2);
521 rte_eal_mp_wait_lcore();
522 TEST_ASSERT_SUCCESS(_wait_timer_triggers(10, MAX_TIMERS * 2, 0),
523 "Timer triggered count doesn't match arm count");
530 _arm_timers_burst(uint64_t timeout_tcks, uint64_t timers)
534 struct rte_event_timer *ev_tim[MAX_BURST];
535 const struct rte_event_timer tim = {
536 .ev.op = RTE_EVENT_OP_NEW,
538 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
539 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
540 .ev.event_type = RTE_EVENT_TYPE_TIMER,
541 .state = RTE_EVENT_TIMER_NOT_ARMED,
542 .timeout_ticks = timeout_tcks,
545 for (i = 0; i < timers / MAX_BURST; i++) {
546 TEST_ASSERT_SUCCESS(rte_mempool_get_bulk(
547 eventdev_test_mempool,
548 (void **)ev_tim, MAX_BURST),
549 "mempool alloc failed");
551 for (j = 0; j < MAX_BURST; j++) {
553 ev_tim[j]->ev.event_ptr = ev_tim[j];
556 TEST_ASSERT_EQUAL(rte_event_timer_arm_tmo_tick_burst(timdev,
557 ev_tim, tim.timeout_ticks, MAX_BURST),
558 MAX_BURST, "Failed to arm timer %d", rte_errno);
565 test_timer_arm_burst(void)
567 TEST_ASSERT_SUCCESS(_arm_timers_burst(20, MAX_TIMERS),
568 "Failed to arm timers");
569 TEST_ASSERT_SUCCESS(_wait_timer_triggers(10, MAX_TIMERS, 0),
570 "Timer triggered count doesn't match arm count");
576 _arm_wrapper_burst(void *arg)
580 TEST_ASSERT_SUCCESS(_arm_timers_burst(20, MAX_TIMERS),
581 "Failed to arm timers");
587 test_timer_arm_burst_multicore(void)
589 rte_eal_remote_launch(_arm_wrapper_burst, NULL, test_lcore1);
590 rte_eal_remote_launch(_arm_wrapper_burst, NULL, test_lcore2);
592 rte_eal_mp_wait_lcore();
593 TEST_ASSERT_SUCCESS(_wait_timer_triggers(10, MAX_TIMERS * 2, 0),
594 "Timer triggered count doesn't match arm count");
600 test_timer_cancel(void)
603 struct rte_event_timer *ev_tim;
604 const struct rte_event_timer tim = {
605 .ev.op = RTE_EVENT_OP_NEW,
607 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
608 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
609 .ev.event_type = RTE_EVENT_TYPE_TIMER,
610 .state = RTE_EVENT_TIMER_NOT_ARMED,
614 for (i = 0; i < MAX_TIMERS; i++) {
615 TEST_ASSERT_SUCCESS(rte_mempool_get(eventdev_test_mempool,
617 "mempool alloc failed");
619 ev_tim->ev.event_ptr = ev_tim;
621 TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim,
622 1), 1, "Failed to arm timer %d",
625 rte_delay_us(100 + (i % 5000));
627 TEST_ASSERT_EQUAL(rte_event_timer_cancel_burst(timdev,
629 "Failed to cancel event timer %d", rte_errno);
630 rte_mempool_put(eventdev_test_mempool, ev_tim);
634 TEST_ASSERT_SUCCESS(_wait_timer_triggers(30, MAX_TIMERS,
636 "Timer triggered count doesn't match arm, cancel count");
642 _cancel_producer(uint64_t timeout_tcks, uint64_t timers)
645 struct rte_event_timer *ev_tim;
646 const struct rte_event_timer tim = {
647 .ev.op = RTE_EVENT_OP_NEW,
649 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
650 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
651 .ev.event_type = RTE_EVENT_TYPE_TIMER,
652 .state = RTE_EVENT_TIMER_NOT_ARMED,
653 .timeout_ticks = timeout_tcks,
656 for (i = 0; i < timers; i++) {
657 TEST_ASSERT_SUCCESS(rte_mempool_get(eventdev_test_mempool,
659 "mempool alloc failed");
662 ev_tim->ev.event_ptr = ev_tim;
664 TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim,
665 1), 1, "Failed to arm timer %d",
668 TEST_ASSERT_EQUAL(ev_tim->state, RTE_EVENT_TIMER_ARMED,
669 "Failed to arm event timer");
671 while (rte_ring_enqueue(timer_producer_ring, ev_tim) != 0)
679 _cancel_producer_burst(uint64_t timeout_tcks, uint64_t timers)
684 struct rte_event_timer *ev_tim[MAX_BURST];
685 const struct rte_event_timer tim = {
686 .ev.op = RTE_EVENT_OP_NEW,
688 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
689 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
690 .ev.event_type = RTE_EVENT_TYPE_TIMER,
691 .state = RTE_EVENT_TIMER_NOT_ARMED,
692 .timeout_ticks = timeout_tcks,
696 for (i = 0; i < timers / MAX_BURST; i++) {
697 TEST_ASSERT_SUCCESS(rte_mempool_get_bulk(
698 eventdev_test_mempool,
699 (void **)ev_tim, MAX_BURST),
700 "mempool alloc failed");
702 for (j = 0; j < MAX_BURST; j++) {
704 ev_tim[j]->ev.event_ptr = ev_tim[j];
707 TEST_ASSERT_EQUAL(rte_event_timer_arm_tmo_tick_burst(timdev,
708 ev_tim, tim.timeout_ticks, MAX_BURST),
709 MAX_BURST, "Failed to arm timer %d", rte_errno);
711 for (j = 0; j < MAX_BURST; j++)
712 TEST_ASSERT_EQUAL(ev_tim[j]->state,
713 RTE_EVENT_TIMER_ARMED,
714 "Event timer not armed, state = %d",
717 ret = rte_ring_enqueue_bulk(timer_producer_ring,
718 (void **)ev_tim, MAX_BURST, NULL);
719 TEST_ASSERT_EQUAL(ret, MAX_BURST,
720 "Failed to enqueue event timers to ring");
724 TEST_ASSERT_EQUAL(arm_count, MAX_TIMERS,
725 "Failed to arm expected number of event timers");
731 _cancel_producer_wrapper(void *args)
735 return _cancel_producer(20, MAX_TIMERS);
739 _cancel_producer_burst_wrapper(void *args)
743 return _cancel_producer_burst(100, MAX_TIMERS);
747 _cancel_thread(void *args)
750 struct rte_event_timer *ev_tim = NULL;
751 uint64_t cancel_count = 0;
754 while (!arm_done || rte_ring_count(timer_producer_ring) > 0) {
755 if (rte_ring_dequeue(timer_producer_ring, (void **)&ev_tim))
758 ret = rte_event_timer_cancel_burst(timdev, &ev_tim, 1);
759 TEST_ASSERT_EQUAL(ret, 1, "Failed to cancel timer");
760 rte_mempool_put(eventdev_test_mempool, (void *)ev_tim);
768 _cancel_burst_thread(void *args)
773 struct rte_event_timer *ev_tim[MAX_BURST];
774 uint64_t cancel_count = 0;
775 uint64_t dequeue_count = 0;
777 while (!arm_done || rte_ring_count(timer_producer_ring) > 0) {
778 n = rte_ring_dequeue_burst(timer_producer_ring,
779 (void **)ev_tim, MAX_BURST, NULL);
785 for (i = 0; i < n; i++)
786 TEST_ASSERT_EQUAL(ev_tim[i]->state,
787 RTE_EVENT_TIMER_ARMED,
788 "Event timer not armed, state = %d",
791 ret = rte_event_timer_cancel_burst(timdev, ev_tim, n);
792 TEST_ASSERT_EQUAL(n, ret, "Failed to cancel complete burst of "
794 rte_mempool_put_bulk(eventdev_test_mempool, (void **)ev_tim,
795 RTE_MIN(ret, MAX_BURST));
800 TEST_ASSERT_EQUAL(cancel_count, MAX_TIMERS,
801 "Failed to cancel expected number of timers: "
802 "expected = %d, cancel_count = %"PRIu64", "
803 "dequeue_count = %"PRIu64"\n", MAX_TIMERS,
804 cancel_count, dequeue_count);
810 test_timer_cancel_multicore(void)
813 timer_producer_ring = rte_ring_create("timer_cancel_queue",
814 MAX_TIMERS * 2, rte_socket_id(), 0);
815 TEST_ASSERT_NOT_NULL(timer_producer_ring,
816 "Unable to reserve memory for ring");
818 rte_eal_remote_launch(_cancel_thread, NULL, test_lcore3);
819 rte_eal_remote_launch(_cancel_producer_wrapper, NULL, test_lcore1);
820 rte_eal_remote_launch(_cancel_producer_wrapper, NULL, test_lcore2);
822 rte_eal_wait_lcore(test_lcore1);
823 rte_eal_wait_lcore(test_lcore2);
825 rte_eal_wait_lcore(test_lcore3);
826 rte_ring_free(timer_producer_ring);
828 TEST_ASSERT_SUCCESS(_wait_timer_triggers(30, MAX_TIMERS * 2,
830 "Timer triggered count doesn't match arm count");
836 test_timer_cancel_burst_multicore(void)
839 timer_producer_ring = rte_ring_create("timer_cancel_queue",
840 MAX_TIMERS * 2, rte_socket_id(), 0);
841 TEST_ASSERT_NOT_NULL(timer_producer_ring,
842 "Unable to reserve memory for ring");
844 rte_eal_remote_launch(_cancel_burst_thread, NULL, test_lcore2);
845 rte_eal_remote_launch(_cancel_producer_burst_wrapper, NULL,
848 rte_eal_wait_lcore(test_lcore1);
850 rte_eal_wait_lcore(test_lcore2);
851 rte_ring_free(timer_producer_ring);
853 TEST_ASSERT_SUCCESS(_wait_timer_triggers(30, MAX_TIMERS,
855 "Timer triggered count doesn't match arm count");
861 test_timer_cancel_random(void)
864 uint64_t events_canceled = 0;
865 struct rte_event_timer *ev_tim;
866 const struct rte_event_timer tim = {
867 .ev.op = RTE_EVENT_OP_NEW,
869 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
870 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
871 .ev.event_type = RTE_EVENT_TYPE_TIMER,
872 .state = RTE_EVENT_TIMER_NOT_ARMED,
876 for (i = 0; i < MAX_TIMERS; i++) {
878 TEST_ASSERT_SUCCESS(rte_mempool_get(eventdev_test_mempool,
880 "mempool alloc failed");
882 ev_tim->ev.event_ptr = ev_tim;
884 TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim,
885 1), 1, "Failed to arm timer %d",
888 if (rte_rand() & 1) {
889 rte_delay_us(100 + (i % 5000));
890 TEST_ASSERT_EQUAL(rte_event_timer_cancel_burst(
893 "Failed to cancel event timer %d", rte_errno);
894 rte_mempool_put(eventdev_test_mempool, ev_tim);
899 TEST_ASSERT_SUCCESS(_wait_timer_triggers(30, MAX_TIMERS,
901 "Timer triggered count doesn't match arm, cancel count");
906 /* Check that the adapter can be created correctly */
911 struct rte_event_timer_adapter *adapter, *adapter2;
913 struct rte_event_timer_adapter_conf conf = {
914 .event_dev_id = evdev + 1, // invalid event dev id
915 .timer_adapter_id = adapter_id,
916 .clk_src = RTE_EVENT_TIMER_ADAPTER_CPU_CLK,
917 .timer_tick_ns = NSECPERSEC / 10,
918 .max_tmo_ns = 180 * NSECPERSEC,
919 .nb_timers = MAX_TIMERS,
924 /* Test invalid conf */
925 adapter = rte_event_timer_adapter_create(&conf);
926 TEST_ASSERT_NULL(adapter, "Created adapter with invalid "
928 TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Incorrect errno value for "
929 "invalid event device id");
931 /* Test valid conf */
932 conf.event_dev_id = evdev;
933 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_caps_get(evdev, &caps),
934 "failed to get adapter capabilities");
935 if (!(caps & RTE_EVENT_TIMER_ADAPTER_CAP_INTERNAL_PORT))
936 adapter = rte_event_timer_adapter_create_ext(&conf,
940 adapter = rte_event_timer_adapter_create(&conf);
941 TEST_ASSERT_NOT_NULL(adapter, "Failed to create adapter with valid "
944 /* Test existing id */
945 adapter2 = rte_event_timer_adapter_create(&conf);
946 TEST_ASSERT_NULL(adapter2, "Created adapter with in-use id");
947 TEST_ASSERT(rte_errno == EEXIST, "Incorrect errno value for existing "
950 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_free(adapter),
951 "Failed to free adapter");
953 rte_mempool_free(eventdev_test_mempool);
959 /* Test that adapter can be freed correctly. */
963 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_stop(timdev),
964 "Failed to stop adapter");
966 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_free(timdev),
967 "Failed to free valid adapter");
969 /* Test free of already freed adapter */
970 TEST_ASSERT_FAIL(rte_event_timer_adapter_free(timdev),
971 "Freed adapter that was already freed");
973 /* Test free of null adapter */
975 TEST_ASSERT_FAIL(rte_event_timer_adapter_free(timdev),
976 "Freed null adapter");
978 rte_mempool_free(eventdev_test_mempool);
983 /* Test that adapter info can be retrieved and is correct. */
985 adapter_get_info(void)
987 struct rte_event_timer_adapter_info info;
989 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_get_info(timdev, &info),
990 "Failed to get adapter info");
993 TEST_ASSERT_EQUAL(info.event_dev_port_id, 1,
994 "Expected port id = 1, got port id = %d",
995 info.event_dev_port_id);
1000 /* Test adapter lookup via adapter ID. */
1002 adapter_lookup(void)
1004 struct rte_event_timer_adapter *adapter;
1006 adapter = rte_event_timer_adapter_lookup(TEST_ADAPTER_ID);
1007 TEST_ASSERT_NOT_NULL(adapter, "Failed to lookup adapter");
1009 return TEST_SUCCESS;
1015 TEST_ASSERT_SUCCESS(_timdev_setup(180 * NSECPERSEC,
1017 "Failed to start adapter");
1018 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_start(timdev),
1019 "Failed to repeatedly start adapter");
1021 return TEST_SUCCESS;
1024 /* Test that adapter stops correctly. */
1028 struct rte_event_timer_adapter *l_adapter = NULL;
1030 /* Test adapter stop */
1031 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_stop(timdev),
1032 "Failed to stop event adapter");
1034 TEST_ASSERT_FAIL(rte_event_timer_adapter_stop(l_adapter),
1035 "Erroneously stopped null event adapter");
1037 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_free(timdev),
1038 "Failed to free adapter");
1040 rte_mempool_free(eventdev_test_mempool);
1042 return TEST_SUCCESS;
1045 /* Test increment and reset of ev_enq_count stat */
1047 stat_inc_reset_ev_enq(void)
1050 int num_evtims = MAX_TIMERS;
1051 struct rte_event_timer *evtims[num_evtims];
1052 struct rte_event evs[BATCH_SIZE];
1053 struct rte_event_timer_adapter_stats stats;
1054 const struct rte_event_timer init_tim = {
1055 .ev.op = RTE_EVENT_OP_NEW,
1056 .ev.queue_id = TEST_QUEUE_ID,
1057 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1058 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1059 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1060 .state = RTE_EVENT_TIMER_NOT_ARMED,
1061 .timeout_ticks = 5, // expire in .5 sec
1064 ret = rte_mempool_get_bulk(eventdev_test_mempool, (void **)evtims,
1066 TEST_ASSERT_EQUAL(ret, 0, "Failed to get array of timer objs: ret = %d",
1069 for (i = 0; i < num_evtims; i++) {
1070 *evtims[i] = init_tim;
1071 evtims[i]->ev.event_ptr = evtims[i];
1074 ret = rte_event_timer_adapter_stats_get(timdev, &stats);
1075 TEST_ASSERT_EQUAL(ret, 0, "Failed to get stats");
1076 TEST_ASSERT_EQUAL((int)stats.ev_enq_count, 0, "Stats not clear at "
1079 /* Test with the max value for the adapter */
1080 ret = rte_event_timer_arm_burst(timdev, evtims, num_evtims);
1081 TEST_ASSERT_EQUAL(ret, num_evtims,
1082 "Failed to arm all event timers: attempted = %d, "
1083 "succeeded = %d, rte_errno = %s",
1084 num_evtims, ret, rte_strerror(rte_errno));
1088 #define MAX_TRIES num_evtims
1093 sum += rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs,
1095 if (sum >= num_evtims || ++tries >= MAX_TRIES)
1101 TEST_ASSERT_EQUAL(sum, num_evtims, "Expected %d timer expiry events, "
1102 "got %d", num_evtims, sum);
1104 TEST_ASSERT(tries < MAX_TRIES, "Exceeded max tries");
1108 /* Make sure the eventdev is still empty */
1109 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs),
1112 TEST_ASSERT_EQUAL(n, 0, "Dequeued unexpected number of timer expiry "
1113 "events from event device");
1115 /* Check stats again */
1116 ret = rte_event_timer_adapter_stats_get(timdev, &stats);
1117 TEST_ASSERT_EQUAL(ret, 0, "Failed to get stats");
1118 TEST_ASSERT_EQUAL((int)stats.ev_enq_count, num_evtims,
1119 "Expected enqueue stat = %d; got %d", num_evtims,
1120 (int)stats.ev_enq_count);
1122 /* Reset and check again */
1123 ret = rte_event_timer_adapter_stats_reset(timdev);
1124 TEST_ASSERT_EQUAL(ret, 0, "Failed to reset stats");
1126 ret = rte_event_timer_adapter_stats_get(timdev, &stats);
1127 TEST_ASSERT_EQUAL(ret, 0, "Failed to get stats");
1128 TEST_ASSERT_EQUAL((int)stats.ev_enq_count, 0,
1129 "Expected enqueue stat = %d; got %d", 0,
1130 (int)stats.ev_enq_count);
1132 rte_mempool_put_bulk(eventdev_test_mempool, (void **)evtims,
1135 return TEST_SUCCESS;
1138 /* Test various cases in arming timers */
1140 event_timer_arm(void)
1144 struct rte_event_timer_adapter *adapter = timdev;
1145 struct rte_event_timer *evtim = NULL;
1146 struct rte_event evs[BATCH_SIZE];
1147 const struct rte_event_timer init_tim = {
1148 .ev.op = RTE_EVENT_OP_NEW,
1149 .ev.queue_id = TEST_QUEUE_ID,
1150 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1151 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1152 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1153 .state = RTE_EVENT_TIMER_NOT_ARMED,
1154 .timeout_ticks = 5, // expire in .5 sec
1157 rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
1158 if (evtim == NULL) {
1159 /* Failed to get an event timer object */
1163 /* Set up a timer */
1165 evtim->ev.event_ptr = evtim;
1167 /* Test single timer arm succeeds */
1168 ret = rte_event_timer_arm_burst(adapter, &evtim, 1);
1169 TEST_ASSERT_EQUAL(ret, 1, "Failed to arm event timer: %s\n",
1170 rte_strerror(rte_errno));
1171 TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_ARMED, "Event timer "
1172 "in incorrect state");
1174 /* Test arm of armed timer fails */
1175 ret = rte_event_timer_arm_burst(adapter, &evtim, 1);
1176 TEST_ASSERT_EQUAL(ret, 0, "expected return value from "
1177 "rte_event_timer_arm_burst: 0, got: %d", ret);
1178 TEST_ASSERT_EQUAL(rte_errno, EALREADY, "Unexpected rte_errno value "
1179 "after arming already armed timer");
1181 /* Let timer expire */
1184 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
1185 TEST_ASSERT_EQUAL(n, 1, "Failed to dequeue expected number of expiry "
1186 "events from event device");
1188 rte_mempool_put(eventdev_test_mempool, evtim);
1190 return TEST_SUCCESS;
1193 /* This test checks that repeated references to the same event timer in the
1194 * arm request work as expected; only the first one through should succeed.
1197 event_timer_arm_double(void)
1201 struct rte_event_timer_adapter *adapter = timdev;
1202 struct rte_event_timer *evtim = NULL;
1203 struct rte_event evs[BATCH_SIZE];
1204 const struct rte_event_timer init_tim = {
1205 .ev.op = RTE_EVENT_OP_NEW,
1206 .ev.queue_id = TEST_QUEUE_ID,
1207 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1208 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1209 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1210 .state = RTE_EVENT_TIMER_NOT_ARMED,
1211 .timeout_ticks = 5, // expire in .5 sec
1214 rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
1215 if (evtim == NULL) {
1216 /* Failed to get an event timer object */
1220 /* Set up a timer */
1222 evtim->ev.event_ptr = evtim;
1224 struct rte_event_timer *evtim_arr[] = {evtim, evtim};
1225 ret = rte_event_timer_arm_burst(adapter, evtim_arr, RTE_DIM(evtim_arr));
1226 TEST_ASSERT_EQUAL(ret, 1, "Unexpected return value from "
1227 "rte_event_timer_arm_burst");
1228 TEST_ASSERT_EQUAL(rte_errno, EALREADY, "Unexpected rte_errno value "
1229 "after double-arm");
1231 /* Let timer expire */
1234 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
1235 TEST_ASSERT_EQUAL(n, 1, "Dequeued incorrect number of expiry events - "
1236 "expected: 1, actual: %d", n);
1238 rte_mempool_put(eventdev_test_mempool, evtim);
1240 return TEST_SUCCESS;
1243 /* Test the timer expiry event is generated at the expected time. */
1245 event_timer_arm_expiry(void)
1249 struct rte_event_timer_adapter *adapter = timdev;
1250 struct rte_event_timer *evtim = NULL;
1251 struct rte_event_timer *evtim2 = NULL;
1252 struct rte_event evs[BATCH_SIZE];
1253 const struct rte_event_timer init_tim = {
1254 .ev.op = RTE_EVENT_OP_NEW,
1255 .ev.queue_id = TEST_QUEUE_ID,
1256 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1257 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1258 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1259 .state = RTE_EVENT_TIMER_NOT_ARMED,
1262 rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
1263 if (evtim == NULL) {
1264 /* Failed to get an event timer object */
1268 /* Set up an event timer */
1270 evtim->timeout_ticks = 30, // expire in 3 secs
1271 evtim->ev.event_ptr = evtim;
1273 ret = rte_event_timer_arm_burst(adapter, &evtim, 1);
1274 TEST_ASSERT_EQUAL(ret, 1, "Failed to arm event timer: %s",
1275 rte_strerror(rte_errno));
1276 TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_ARMED, "Event "
1277 "timer in incorrect state");
1281 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
1282 TEST_ASSERT_EQUAL(n, 0, "Dequeued unexpected timer expiry event");
1284 /* Delay 100 ms to account for the adapter tick window - should let us
1289 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
1290 TEST_ASSERT_EQUAL(n, 1, "Dequeued incorrect number (%d) of timer "
1291 "expiry events", n);
1292 TEST_ASSERT_EQUAL(evs[0].event_type, RTE_EVENT_TYPE_TIMER,
1293 "Dequeued unexpected type of event");
1295 /* Check that we recover the original event timer and then free it */
1296 evtim2 = evs[0].event_ptr;
1297 TEST_ASSERT_EQUAL(evtim, evtim2,
1298 "Failed to recover pointer to original event timer");
1299 rte_mempool_put(eventdev_test_mempool, evtim2);
1301 return TEST_SUCCESS;
1304 /* Check that rearming a timer works as expected. */
1306 event_timer_arm_rearm(void)
1310 struct rte_event_timer *evtim = NULL;
1311 struct rte_event_timer *evtim2 = NULL;
1312 struct rte_event evs[BATCH_SIZE];
1313 const struct rte_event_timer init_tim = {
1314 .ev.op = RTE_EVENT_OP_NEW,
1315 .ev.queue_id = TEST_QUEUE_ID,
1316 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1317 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1318 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1319 .state = RTE_EVENT_TIMER_NOT_ARMED,
1322 rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
1323 if (evtim == NULL) {
1324 /* Failed to get an event timer object */
1328 /* Set up a timer */
1330 evtim->timeout_ticks = 1; // expire in 0.1 sec
1331 evtim->ev.event_ptr = evtim;
1334 ret = rte_event_timer_arm_burst(timdev, &evtim, 1);
1335 TEST_ASSERT_EQUAL(ret, 1, "Failed to arm event timer: %s\n",
1336 rte_strerror(rte_errno));
1338 /* Add 100ms to account for the adapter tick window */
1339 rte_delay_ms(100 + 100);
1341 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
1342 TEST_ASSERT_EQUAL(n, 1, "Failed to dequeue expected number of expiry "
1343 "events from event device");
1345 /* Recover the timer through the event that was dequeued. */
1346 evtim2 = evs[0].event_ptr;
1347 TEST_ASSERT_EQUAL(evtim, evtim2,
1348 "Failed to recover pointer to original event timer");
1350 /* Need to reset state in case implementation can't do it */
1351 evtim2->state = RTE_EVENT_TIMER_NOT_ARMED;
1354 ret = rte_event_timer_arm_burst(timdev, &evtim2, 1);
1355 TEST_ASSERT_EQUAL(ret, 1, "Failed to arm event timer: %s\n",
1356 rte_strerror(rte_errno));
1358 /* Add 100ms to account for the adapter tick window */
1359 rte_delay_ms(100 + 100);
1361 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
1362 TEST_ASSERT_EQUAL(n, 1, "Failed to dequeue expected number of expiry "
1363 "events from event device");
1366 evtim2 = evs[0].event_ptr;
1367 TEST_ASSERT_EQUAL(evtim, evtim2,
1368 "Failed to recover pointer to original event timer");
1369 rte_mempool_put(eventdev_test_mempool, evtim2);
1371 return TEST_SUCCESS;
1374 /* Check that the adapter handles the max specified number of timers as
1378 event_timer_arm_max(void)
1381 int num_evtims = MAX_TIMERS;
1382 struct rte_event_timer *evtims[num_evtims];
1383 struct rte_event evs[BATCH_SIZE];
1384 const struct rte_event_timer init_tim = {
1385 .ev.op = RTE_EVENT_OP_NEW,
1386 .ev.queue_id = TEST_QUEUE_ID,
1387 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1388 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1389 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1390 .state = RTE_EVENT_TIMER_NOT_ARMED,
1391 .timeout_ticks = 5, // expire in .5 sec
1394 ret = rte_mempool_get_bulk(eventdev_test_mempool, (void **)evtims,
1396 TEST_ASSERT_EQUAL(ret, 0, "Failed to get array of timer objs: ret = %d",
1399 for (i = 0; i < num_evtims; i++) {
1400 *evtims[i] = init_tim;
1401 evtims[i]->ev.event_ptr = evtims[i];
1404 /* Test with the max value for the adapter */
1405 ret = rte_event_timer_arm_burst(timdev, evtims, num_evtims);
1406 TEST_ASSERT_EQUAL(ret, num_evtims,
1407 "Failed to arm all event timers: attempted = %d, "
1408 "succeeded = %d, rte_errno = %s",
1409 num_evtims, ret, rte_strerror(rte_errno));
1413 #define MAX_TRIES num_evtims
1418 sum += rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs,
1420 if (sum >= num_evtims || ++tries >= MAX_TRIES)
1426 TEST_ASSERT_EQUAL(sum, num_evtims, "Expected %d timer expiry events, "
1427 "got %d", num_evtims, sum);
1429 TEST_ASSERT(tries < MAX_TRIES, "Exceeded max tries");
1433 /* Make sure the eventdev is still empty */
1434 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs),
1437 TEST_ASSERT_EQUAL(n, 0, "Dequeued unexpected number of timer expiry "
1438 "events from event device");
1440 rte_mempool_put_bulk(eventdev_test_mempool, (void **)evtims,
1443 return TEST_SUCCESS;
1446 /* Check that creating an event timer with incorrect event sched type fails. */
1448 event_timer_arm_invalid_sched_type(void)
1451 struct rte_event_timer *evtim = NULL;
1452 const struct rte_event_timer init_tim = {
1453 .ev.op = RTE_EVENT_OP_NEW,
1454 .ev.queue_id = TEST_QUEUE_ID,
1455 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1456 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1457 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1458 .state = RTE_EVENT_TIMER_NOT_ARMED,
1459 .timeout_ticks = 5, // expire in .5 sec
1462 if (!using_services)
1465 rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
1466 if (evtim == NULL) {
1467 /* Failed to get an event timer object */
1472 evtim->ev.event_ptr = evtim;
1473 evtim->ev.sched_type = RTE_SCHED_TYPE_PARALLEL; // bad sched type
1475 ret = rte_event_timer_arm_burst(timdev, &evtim, 1);
1476 TEST_ASSERT_EQUAL(ret, 0, "Expected to fail timer arm with invalid "
1477 "sched type, but didn't");
1478 TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Unexpected rte_errno value after"
1479 " arm fail with invalid queue");
1481 rte_mempool_put(eventdev_test_mempool, &evtim);
1483 return TEST_SUCCESS;
1486 /* Check that creating an event timer with a timeout value that is too small or
1490 event_timer_arm_invalid_timeout(void)
1493 struct rte_event_timer *evtim = NULL;
1494 const struct rte_event_timer init_tim = {
1495 .ev.op = RTE_EVENT_OP_NEW,
1496 .ev.queue_id = TEST_QUEUE_ID,
1497 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1498 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1499 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1500 .state = RTE_EVENT_TIMER_NOT_ARMED,
1501 .timeout_ticks = 5, // expire in .5 sec
1504 rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
1505 if (evtim == NULL) {
1506 /* Failed to get an event timer object */
1511 evtim->ev.event_ptr = evtim;
1512 evtim->timeout_ticks = 0; // timeout too small
1514 ret = rte_event_timer_arm_burst(timdev, &evtim, 1);
1515 TEST_ASSERT_EQUAL(ret, 0, "Expected to fail timer arm with invalid "
1516 "timeout, but didn't");
1517 TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Unexpected rte_errno value after"
1518 " arm fail with invalid timeout");
1519 TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_ERROR_TOOEARLY,
1520 "Unexpected event timer state");
1523 evtim->ev.event_ptr = evtim;
1524 evtim->timeout_ticks = 1801; // timeout too big
1526 ret = rte_event_timer_arm_burst(timdev, &evtim, 1);
1527 TEST_ASSERT_EQUAL(ret, 0, "Expected to fail timer arm with invalid "
1528 "timeout, but didn't");
1529 TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Unexpected rte_errno value after"
1530 " arm fail with invalid timeout");
1531 TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_ERROR_TOOLATE,
1532 "Unexpected event timer state");
1534 rte_mempool_put(eventdev_test_mempool, evtim);
1536 return TEST_SUCCESS;
1540 event_timer_cancel(void)
1544 struct rte_event_timer_adapter *adapter = timdev;
1545 struct rte_event_timer *evtim = NULL;
1546 struct rte_event evs[BATCH_SIZE];
1547 const struct rte_event_timer init_tim = {
1548 .ev.op = RTE_EVENT_OP_NEW,
1549 .ev.queue_id = TEST_QUEUE_ID,
1550 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1551 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1552 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1553 .state = RTE_EVENT_TIMER_NOT_ARMED,
1556 rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
1557 if (evtim == NULL) {
1558 /* Failed to get an event timer object */
1562 /* Check that cancelling an uninited timer fails */
1563 ret = rte_event_timer_cancel_burst(adapter, &evtim, 1);
1564 TEST_ASSERT_EQUAL(ret, 0, "Succeeded unexpectedly in canceling "
1566 TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Unexpected rte_errno value after "
1567 "cancelling uninited timer");
1569 /* Set up a timer */
1571 evtim->ev.event_ptr = evtim;
1572 evtim->timeout_ticks = 30; // expire in 3 sec
1574 /* Check that cancelling an inited but unarmed timer fails */
1575 ret = rte_event_timer_cancel_burst(adapter, &evtim, 1);
1576 TEST_ASSERT_EQUAL(ret, 0, "Succeeded unexpectedly in canceling "
1578 TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Unexpected rte_errno value after "
1579 "cancelling unarmed timer");
1581 ret = rte_event_timer_arm_burst(adapter, &evtim, 1);
1582 TEST_ASSERT_EQUAL(ret, 1, "Failed to arm event timer: %s\n",
1583 rte_strerror(rte_errno));
1584 TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_ARMED,
1585 "evtim in incorrect state");
1590 ret = rte_event_timer_cancel_burst(adapter, &evtim, 1);
1591 TEST_ASSERT_EQUAL(ret, 1, "Failed to cancel event_timer: %s\n",
1592 rte_strerror(rte_errno));
1593 TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_CANCELED,
1594 "evtim in incorrect state");
1598 /* Make sure that no expiry event was generated */
1599 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
1600 TEST_ASSERT_EQUAL(n, 0, "Dequeued unexpected timer expiry event\n");
1602 rte_mempool_put(eventdev_test_mempool, evtim);
1604 return TEST_SUCCESS;
1608 event_timer_cancel_double(void)
1612 struct rte_event_timer_adapter *adapter = timdev;
1613 struct rte_event_timer *evtim = NULL;
1614 struct rte_event evs[BATCH_SIZE];
1615 const struct rte_event_timer init_tim = {
1616 .ev.op = RTE_EVENT_OP_NEW,
1617 .ev.queue_id = TEST_QUEUE_ID,
1618 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1619 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1620 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1621 .state = RTE_EVENT_TIMER_NOT_ARMED,
1622 .timeout_ticks = 5, // expire in .5 sec
1625 rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
1626 if (evtim == NULL) {
1627 /* Failed to get an event timer object */
1631 /* Set up a timer */
1633 evtim->ev.event_ptr = evtim;
1634 evtim->timeout_ticks = 30; // expire in 3 sec
1636 ret = rte_event_timer_arm_burst(adapter, &evtim, 1);
1637 TEST_ASSERT_EQUAL(ret, 1, "Failed to arm event timer: %s\n",
1638 rte_strerror(rte_errno));
1639 TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_ARMED,
1640 "timer in unexpected state");
1642 /* Now, test that referencing the same timer twice in the same call
1645 struct rte_event_timer *evtim_arr[] = {evtim, evtim};
1646 ret = rte_event_timer_cancel_burst(adapter, evtim_arr,
1647 RTE_DIM(evtim_arr));
1649 /* Two requests to cancel same timer, only one should succeed */
1650 TEST_ASSERT_EQUAL(ret, 1, "Succeeded unexpectedly in canceling timer "
1653 TEST_ASSERT_EQUAL(rte_errno, EALREADY, "Unexpected rte_errno value "
1654 "after double-cancel: rte_errno = %d", rte_errno);
1658 /* Still make sure that no expiry event was generated */
1659 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
1660 TEST_ASSERT_EQUAL(n, 0, "Dequeued unexpected timer expiry event\n");
1662 rte_mempool_put(eventdev_test_mempool, evtim);
1664 return TEST_SUCCESS;
1667 /* Check that event timer adapter tick resolution works as expected by testing
1668 * the number of adapter ticks that occur within a particular time interval.
1671 adapter_tick_resolution(void)
1673 struct rte_event_timer_adapter_stats stats;
1674 uint64_t adapter_tick_count;
1676 /* Only run this test in the software driver case */
1677 if (!using_services)
1680 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_stats_reset(timdev),
1681 "Failed to reset stats");
1683 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_stats_get(timdev,
1684 &stats), "Failed to get adapter stats");
1685 TEST_ASSERT_EQUAL(stats.adapter_tick_count, 0, "Adapter tick count "
1688 /* Delay 1 second; should let at least 10 ticks occur with the default
1689 * adapter configuration used by this test.
1693 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_stats_get(timdev,
1694 &stats), "Failed to get adapter stats");
1696 adapter_tick_count = stats.adapter_tick_count;
1697 TEST_ASSERT(adapter_tick_count >= 10 && adapter_tick_count <= 12,
1698 "Expected 10-12 adapter ticks, got %"PRIu64"\n",
1699 adapter_tick_count);
1701 return TEST_SUCCESS;
1705 adapter_create_max(void)
1708 uint32_t svc_start_count, svc_end_count;
1709 struct rte_event_timer_adapter *adapters[
1710 RTE_EVENT_TIMER_ADAPTER_NUM_MAX + 1];
1712 struct rte_event_timer_adapter_conf conf = {
1713 .event_dev_id = evdev,
1714 // timer_adapter_id set in loop
1715 .clk_src = RTE_EVENT_TIMER_ADAPTER_CPU_CLK,
1716 .timer_tick_ns = NSECPERSEC / 10,
1717 .max_tmo_ns = 180 * NSECPERSEC,
1718 .nb_timers = MAX_TIMERS,
1722 if (!using_services)
1725 svc_start_count = rte_service_get_count();
1727 /* This test expects that there are sufficient service IDs available
1728 * to be allocated. I.e., RTE_EVENT_TIMER_ADAPTER_NUM_MAX may need to
1729 * be less than RTE_SERVICE_NUM_MAX if anything else uses a service
1730 * (the SW event device, for example).
1732 for (i = 0; i < RTE_EVENT_TIMER_ADAPTER_NUM_MAX; i++) {
1733 conf.timer_adapter_id = i;
1734 adapters[i] = rte_event_timer_adapter_create_ext(&conf,
1735 test_port_conf_cb, NULL);
1736 TEST_ASSERT_NOT_NULL(adapters[i], "Failed to create adapter "
1740 conf.timer_adapter_id = i;
1741 adapters[i] = rte_event_timer_adapter_create(&conf);
1742 TEST_ASSERT_NULL(adapters[i], "Created too many adapters");
1744 /* Check that at least RTE_EVENT_TIMER_ADAPTER_NUM_MAX services
1747 svc_end_count = rte_service_get_count();
1748 TEST_ASSERT_EQUAL(svc_end_count - svc_start_count,
1749 RTE_EVENT_TIMER_ADAPTER_NUM_MAX,
1750 "Failed to create expected number of services");
1752 for (i = 0; i < RTE_EVENT_TIMER_ADAPTER_NUM_MAX; i++)
1753 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_free(adapters[i]),
1754 "Failed to free adapter %d", i);
1756 /* Check that service count is back to where it was at start */
1757 svc_end_count = rte_service_get_count();
1758 TEST_ASSERT_EQUAL(svc_start_count, svc_end_count, "Failed to release "
1759 "correct number of services");
1761 return TEST_SUCCESS;
1764 static struct unit_test_suite event_timer_adptr_functional_testsuite = {
1765 .suite_name = "event timer functional test suite",
1766 .setup = testsuite_setup,
1767 .teardown = testsuite_teardown,
1768 .unit_test_cases = {
1769 TEST_CASE_ST(timdev_setup_usec, timdev_teardown,
1771 TEST_CASE_ST(timdev_setup_usec, timdev_teardown,
1773 TEST_CASE_ST(timdev_setup_usec, timdev_teardown,
1774 test_timer_arm_burst),
1775 TEST_CASE_ST(timdev_setup_sec, timdev_teardown,
1777 TEST_CASE_ST(timdev_setup_sec, timdev_teardown,
1778 test_timer_cancel_random),
1779 TEST_CASE_ST(timdev_setup_usec_multicore, timdev_teardown,
1780 test_timer_arm_multicore),
1781 TEST_CASE_ST(timdev_setup_usec_multicore, timdev_teardown,
1782 test_timer_arm_burst_multicore),
1783 TEST_CASE_ST(timdev_setup_sec_multicore, timdev_teardown,
1784 test_timer_cancel_multicore),
1785 TEST_CASE_ST(timdev_setup_sec_multicore, timdev_teardown,
1786 test_timer_cancel_burst_multicore),
1787 TEST_CASE(adapter_create),
1788 TEST_CASE_ST(timdev_setup_msec, NULL, adapter_free),
1789 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1791 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1793 TEST_CASE_ST(NULL, timdev_teardown,
1795 TEST_CASE_ST(timdev_setup_msec, NULL,
1797 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1798 stat_inc_reset_ev_enq),
1799 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1801 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1802 event_timer_arm_double),
1803 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1804 event_timer_arm_expiry),
1805 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1806 event_timer_arm_rearm),
1807 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1808 event_timer_arm_max),
1809 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1810 event_timer_arm_invalid_sched_type),
1811 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1812 event_timer_arm_invalid_timeout),
1813 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1814 event_timer_cancel),
1815 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1816 event_timer_cancel_double),
1817 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1818 adapter_tick_resolution),
1819 TEST_CASE(adapter_create_max),
1820 TEST_CASES_END() /**< NULL terminate unit test array */
1825 test_event_timer_adapter_func(void)
1827 return unit_test_suite_runner(&event_timer_adptr_functional_testsuite);
1830 REGISTER_TEST_COMMAND(event_timer_adapter_test, test_event_timer_adapter_func);