1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Cavium, Inc
3 * Copyright(c) 2017-2018 Intel Corporation.
8 #include <rte_atomic.h>
9 #include <rte_common.h>
10 #include <rte_cycles.h>
11 #include <rte_debug.h>
13 #include <rte_ethdev.h>
14 #include <rte_eventdev.h>
15 #include <rte_event_timer_adapter.h>
16 #include <rte_mempool.h>
17 #include <rte_launch.h>
18 #include <rte_lcore.h>
19 #include <rte_per_lcore.h>
20 #include <rte_random.h>
21 #include <rte_bus_vdev.h>
22 #include <rte_service.h>
27 /* 4K timers corresponds to sw evdev max inflight events */
28 #define MAX_TIMERS (4 * 1024)
31 #define NSECPERSEC 1E9
33 /* Both the app lcore and adapter ports are linked to this queue */
34 #define TEST_QUEUE_ID 0
35 /* Port the application dequeues from */
36 #define TEST_PORT_ID 0
37 #define TEST_ADAPTER_ID 0
39 /* Handle log statements in same manner as test macros */
40 #define LOG_DBG(...) RTE_LOG(DEBUG, EAL, __VA_ARGS__)
43 static struct rte_event_timer_adapter *timdev;
44 static struct rte_mempool *eventdev_test_mempool;
45 static struct rte_ring *timer_producer_ring;
46 static uint64_t global_bkt_tck_ns;
47 static uint64_t global_info_bkt_tck_ns;
48 static volatile uint8_t arm_done;
50 #define CALC_TICKS(tks) \
51 ceil((double)(tks * global_bkt_tck_ns) / global_info_bkt_tck_ns)
54 static bool using_services;
55 static uint32_t test_lcore1;
56 static uint32_t test_lcore2;
57 static uint32_t test_lcore3;
58 static uint32_t sw_evdev_slcore;
59 static uint32_t sw_adptr_slcore;
62 devconf_set_default_sane_values(struct rte_event_dev_config *dev_conf,
63 struct rte_event_dev_info *info)
65 memset(dev_conf, 0, sizeof(struct rte_event_dev_config));
66 dev_conf->dequeue_timeout_ns = info->min_dequeue_timeout_ns;
67 dev_conf->nb_event_ports = 1;
68 dev_conf->nb_event_queues = 1;
69 dev_conf->nb_event_queue_flows = info->max_event_queue_flows;
70 dev_conf->nb_event_port_dequeue_depth =
71 info->max_event_port_dequeue_depth;
72 dev_conf->nb_event_port_enqueue_depth =
73 info->max_event_port_enqueue_depth;
74 dev_conf->nb_event_port_enqueue_depth =
75 info->max_event_port_enqueue_depth;
76 dev_conf->nb_events_limit =
84 struct rte_event_dev_config dev_conf;
85 struct rte_event_dev_info info;
88 ret = rte_event_dev_info_get(evdev, &info);
89 TEST_ASSERT_SUCCESS(ret, "Failed to get event dev info");
90 TEST_ASSERT(info.max_num_events < 0 ||
91 info.max_num_events >= (int32_t)MAX_TIMERS,
92 "ERROR max_num_events=%d < max_events=%d",
93 info.max_num_events, MAX_TIMERS);
95 devconf_set_default_sane_values(&dev_conf, &info);
96 ret = rte_event_dev_configure(evdev, &dev_conf);
97 TEST_ASSERT_SUCCESS(ret, "Failed to configure eventdev");
99 ret = rte_event_queue_setup(evdev, 0, NULL);
100 TEST_ASSERT_SUCCESS(ret, "Failed to setup queue=%d", 0);
102 /* Configure event port */
103 ret = rte_event_port_setup(evdev, 0, NULL);
104 TEST_ASSERT_SUCCESS(ret, "Failed to setup port=%d", 0);
105 ret = rte_event_port_link(evdev, 0, NULL, NULL, 0);
106 TEST_ASSERT(ret >= 0, "Failed to link all queues port=%d", 0);
108 /* If this is a software event device, map and start its service */
109 if (rte_event_dev_service_id_get(evdev, &service_id) == 0) {
110 TEST_ASSERT_SUCCESS(rte_service_lcore_add(sw_evdev_slcore),
111 "Failed to add service core");
112 TEST_ASSERT_SUCCESS(rte_service_lcore_start(
114 "Failed to start service core");
115 TEST_ASSERT_SUCCESS(rte_service_map_lcore_set(
116 service_id, sw_evdev_slcore, 1),
117 "Failed to map evdev service");
118 TEST_ASSERT_SUCCESS(rte_service_runstate_set(
120 "Failed to start evdev service");
123 ret = rte_event_dev_start(evdev);
124 TEST_ASSERT_SUCCESS(ret, "Failed to start device");
130 testsuite_setup(void)
132 /* Some of the multithreaded tests require 3 other lcores to run */
133 unsigned int required_lcore_count = 4;
136 /* To make it easier to map services later if needed, just reset
137 * service core state.
139 (void) rte_service_lcore_reset_all();
141 if (!rte_event_dev_count()) {
142 /* If there is no hardware eventdev, or no software vdev was
143 * specified on the command line, create an instance of
146 LOG_DBG("Failed to find a valid event device... testing with"
147 " event_sw device\n");
148 TEST_ASSERT_SUCCESS(rte_vdev_init("event_sw0", NULL),
149 "Error creating eventdev");
150 evdev = rte_event_dev_get_dev_id("event_sw0");
153 if (rte_event_dev_service_id_get(evdev, &service_id) == 0) {
154 /* A software event device will use a software event timer
155 * adapter as well. 2 more cores required to convert to
158 required_lcore_count += 2;
159 using_services = true;
162 if (rte_lcore_count() < required_lcore_count) {
163 printf("Not enough cores for event_timer_adapter_test, expecting at least %u\n",
164 required_lcore_count);
168 /* Assign lcores for various tasks */
169 test_lcore1 = rte_get_next_lcore(-1, 1, 0);
170 test_lcore2 = rte_get_next_lcore(test_lcore1, 1, 0);
171 test_lcore3 = rte_get_next_lcore(test_lcore2, 1, 0);
172 if (using_services) {
173 sw_evdev_slcore = rte_get_next_lcore(test_lcore3, 1, 0);
174 sw_adptr_slcore = rte_get_next_lcore(sw_evdev_slcore, 1, 0);
177 return eventdev_setup();
181 testsuite_teardown(void)
183 rte_event_dev_stop(evdev);
184 rte_event_dev_close(evdev);
188 setup_adapter_service(struct rte_event_timer_adapter *adptr)
190 uint32_t adapter_service_id;
193 /* retrieve service ids */
194 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_service_id_get(adptr,
195 &adapter_service_id), "Failed to get event timer "
196 "adapter service id");
197 /* add a service core and start it */
198 ret = rte_service_lcore_add(sw_adptr_slcore);
199 TEST_ASSERT(ret == 0 || ret == -EALREADY,
200 "Failed to add service core");
201 ret = rte_service_lcore_start(sw_adptr_slcore);
202 TEST_ASSERT(ret == 0 || ret == -EALREADY,
203 "Failed to start service core");
205 /* map services to it */
206 TEST_ASSERT_SUCCESS(rte_service_map_lcore_set(adapter_service_id,
208 "Failed to map adapter service");
210 /* set services to running */
211 TEST_ASSERT_SUCCESS(rte_service_runstate_set(adapter_service_id, 1),
212 "Failed to start event timer adapter service");
218 test_port_conf_cb(uint16_t id, uint8_t event_dev_id, uint8_t *event_port_id,
221 struct rte_event_dev_config dev_conf;
222 struct rte_event_dev_info info;
223 struct rte_event_port_conf *port_conf, def_port_conf = {0};
225 static int port_allocated;
226 static uint8_t port_id;
229 if (port_allocated) {
230 *event_port_id = port_id;
236 ret = rte_event_dev_attr_get(event_dev_id, RTE_EVENT_DEV_ATTR_STARTED,
242 rte_event_dev_stop(event_dev_id);
244 ret = rte_event_dev_info_get(evdev, &info);
248 devconf_set_default_sane_values(&dev_conf, &info);
250 port_id = dev_conf.nb_event_ports;
251 dev_conf.nb_event_ports++;
253 ret = rte_event_dev_configure(event_dev_id, &dev_conf);
256 rte_event_dev_start(event_dev_id);
260 if (conf_arg != NULL)
261 port_conf = conf_arg;
263 port_conf = &def_port_conf;
264 ret = rte_event_port_default_conf_get(event_dev_id, port_id,
270 ret = rte_event_port_setup(event_dev_id, port_id, port_conf);
274 *event_port_id = port_id;
277 rte_event_dev_start(event_dev_id);
279 /* Reuse this port number next time this is called */
286 _timdev_setup(uint64_t max_tmo_ns, uint64_t bkt_tck_ns, uint64_t flags)
288 struct rte_event_timer_adapter_info info;
289 struct rte_event_timer_adapter_conf config = {
290 .event_dev_id = evdev,
291 .timer_adapter_id = TEST_ADAPTER_ID,
292 .timer_tick_ns = bkt_tck_ns,
293 .max_tmo_ns = max_tmo_ns,
294 .nb_timers = MAX_TIMERS * 10,
298 const char *pool_name = "timdev_test_pool";
300 global_bkt_tck_ns = bkt_tck_ns;
302 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_caps_get(evdev, &caps),
303 "failed to get adapter capabilities");
305 if (flags & RTE_EVENT_TIMER_ADAPTER_F_PERIODIC &&
306 !(caps & RTE_EVENT_TIMER_ADAPTER_CAP_PERIODIC))
309 if (!(caps & RTE_EVENT_TIMER_ADAPTER_CAP_INTERNAL_PORT)) {
310 timdev = rte_event_timer_adapter_create_ext(&config,
313 setup_adapter_service(timdev);
314 using_services = true;
316 timdev = rte_event_timer_adapter_create(&config);
318 TEST_ASSERT_NOT_NULL(timdev,
319 "failed to create event timer ring");
321 TEST_ASSERT_EQUAL(rte_event_timer_adapter_start(timdev), 0,
322 "failed to Start event timer adapter");
324 /* Create event timer mempool */
325 eventdev_test_mempool = rte_mempool_create(pool_name,
327 sizeof(struct rte_event_timer), /* element size*/
329 0, NULL, NULL, NULL, NULL,
331 if (!eventdev_test_mempool) {
332 printf("ERROR creating mempool\n");
336 rte_event_timer_adapter_get_info(timdev, &info);
338 global_info_bkt_tck_ns = info.min_resolution_ns;
344 timdev_setup_usec(void)
346 uint64_t flags = RTE_EVENT_TIMER_ADAPTER_F_ADJUST_RES;
348 return using_services ?
349 /* Max timeout is 10,000us and bucket interval is 100us */
350 _timdev_setup(1E7, 1E5, flags) :
351 /* Max timeout is 100us and bucket interval is 1us */
352 _timdev_setup(1E5, 1E3, flags);
356 timdev_setup_usec_multicore(void)
358 uint64_t flags = RTE_EVENT_TIMER_ADAPTER_F_ADJUST_RES;
360 return using_services ?
361 /* Max timeout is 10,000us and bucket interval is 100us */
362 _timdev_setup(1E7, 1E5, flags) :
363 /* Max timeout is 100us and bucket interval is 1us */
364 _timdev_setup(1E5, 1E3, flags);
368 timdev_setup_msec(void)
370 uint64_t flags = RTE_EVENT_TIMER_ADAPTER_F_ADJUST_RES;
372 /* Max timeout is 3 mins, and bucket interval is 100 ms */
373 return _timdev_setup(180 * NSECPERSEC, NSECPERSEC / 10, flags);
377 timdev_setup_msec_periodic(void)
379 uint64_t flags = RTE_EVENT_TIMER_ADAPTER_F_ADJUST_RES |
380 RTE_EVENT_TIMER_ADAPTER_F_PERIODIC;
382 /* Periodic mode with 100 ms resolution */
383 return _timdev_setup(0, NSECPERSEC / 10, flags);
387 timdev_setup_sec(void)
389 uint64_t flags = RTE_EVENT_TIMER_ADAPTER_F_ADJUST_RES;
391 /* Max timeout is 100sec and bucket interval is 1sec */
392 return _timdev_setup(1E11, 1E9, flags);
396 timdev_setup_sec_periodic(void)
398 uint64_t flags = RTE_EVENT_TIMER_ADAPTER_F_ADJUST_RES |
399 RTE_EVENT_TIMER_ADAPTER_F_PERIODIC;
401 /* Periodic mode with 1 sec resolution */
402 return _timdev_setup(0, NSECPERSEC, flags);
406 timdev_setup_sec_multicore(void)
408 uint64_t flags = RTE_EVENT_TIMER_ADAPTER_F_ADJUST_RES;
410 /* Max timeout is 100sec and bucket interval is 1sec */
411 return _timdev_setup(1E11, 1E9, flags);
415 timdev_teardown(void)
417 rte_event_timer_adapter_stop(timdev);
418 rte_event_timer_adapter_free(timdev);
420 rte_mempool_free(eventdev_test_mempool);
424 test_timer_state(void)
426 struct rte_event_timer *ev_tim;
428 const struct rte_event_timer tim = {
429 .ev.op = RTE_EVENT_OP_NEW,
431 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
432 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
433 .ev.event_type = RTE_EVENT_TYPE_TIMER,
434 .state = RTE_EVENT_TIMER_NOT_ARMED,
438 rte_mempool_get(eventdev_test_mempool, (void **)&ev_tim);
440 ev_tim->ev.event_ptr = ev_tim;
441 ev_tim->timeout_ticks = CALC_TICKS(120);
443 TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim, 1), 0,
444 "Armed timer exceeding max_timeout.");
445 TEST_ASSERT_EQUAL(ev_tim->state, RTE_EVENT_TIMER_ERROR_TOOLATE,
446 "Improper timer state set expected %d returned %d",
447 RTE_EVENT_TIMER_ERROR_TOOLATE, ev_tim->state);
449 ev_tim->state = RTE_EVENT_TIMER_NOT_ARMED;
450 ev_tim->timeout_ticks = CALC_TICKS(10);
452 TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim, 1), 1,
453 "Failed to arm timer with proper timeout.");
454 TEST_ASSERT_EQUAL(ev_tim->state, RTE_EVENT_TIMER_ARMED,
455 "Improper timer state set expected %d returned %d",
456 RTE_EVENT_TIMER_ARMED, ev_tim->state);
461 rte_delay_us(1000 + 200);
462 TEST_ASSERT_EQUAL(rte_event_dequeue_burst(evdev, 0, &ev, 1, 0), 1,
463 "Armed timer failed to trigger.");
465 ev_tim->state = RTE_EVENT_TIMER_NOT_ARMED;
466 ev_tim->timeout_ticks = CALC_TICKS(90);
467 TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim, 1), 1,
468 "Failed to arm timer with proper timeout.");
469 TEST_ASSERT_EQUAL(rte_event_timer_cancel_burst(timdev, &ev_tim, 1),
470 1, "Failed to cancel armed timer");
471 TEST_ASSERT_EQUAL(ev_tim->state, RTE_EVENT_TIMER_CANCELED,
472 "Improper timer state set expected %d returned %d",
473 RTE_EVENT_TIMER_CANCELED, ev_tim->state);
475 rte_mempool_put(eventdev_test_mempool, (void *)ev_tim);
481 _arm_timers(uint64_t timeout_tcks, uint64_t timers)
484 struct rte_event_timer *ev_tim;
485 const struct rte_event_timer tim = {
486 .ev.op = RTE_EVENT_OP_NEW,
488 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
489 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
490 .ev.event_type = RTE_EVENT_TYPE_TIMER,
491 .state = RTE_EVENT_TIMER_NOT_ARMED,
492 .timeout_ticks = CALC_TICKS(timeout_tcks),
495 for (i = 0; i < timers; i++) {
497 TEST_ASSERT_SUCCESS(rte_mempool_get(eventdev_test_mempool,
499 "mempool alloc failed");
501 ev_tim->ev.event_ptr = ev_tim;
503 TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim,
504 1), 1, "Failed to arm timer %d",
512 _wait_timer_triggers(uint64_t wait_sec, uint64_t arm_count,
513 uint64_t cancel_count)
517 uint64_t wait_start, max_wait;
520 max_wait = rte_get_timer_hz() * wait_sec;
521 wait_start = rte_get_timer_cycles();
523 if (rte_get_timer_cycles() - wait_start > max_wait) {
524 if (events + cancel_count != arm_count)
525 TEST_ASSERT_SUCCESS(max_wait,
526 "Max time limit for timers exceeded.");
530 valid_event = rte_event_dequeue_burst(evdev, 0, &ev, 1, 0);
534 rte_mempool_put(eventdev_test_mempool, ev.event_ptr);
544 TEST_ASSERT_SUCCESS(_arm_timers(20, MAX_TIMERS),
545 "Failed to arm timers");
546 TEST_ASSERT_SUCCESS(_wait_timer_triggers(10, MAX_TIMERS, 0),
547 "Timer triggered count doesn't match arm count");
552 test_timer_arm_periodic(void)
554 TEST_ASSERT_SUCCESS(_arm_timers(1, MAX_TIMERS),
555 "Failed to arm timers");
556 /* With a resolution of 100ms and wait time of 1sec,
557 * there will be 10 * MAX_TIMERS periodic timer triggers.
559 TEST_ASSERT_SUCCESS(_wait_timer_triggers(1, 10 * MAX_TIMERS, 0),
560 "Timer triggered count doesn't match arm count");
565 _arm_wrapper(void *arg)
569 TEST_ASSERT_SUCCESS(_arm_timers(20, MAX_TIMERS),
570 "Failed to arm timers");
576 test_timer_arm_multicore(void)
579 uint32_t lcore_1 = rte_get_next_lcore(-1, 1, 0);
580 uint32_t lcore_2 = rte_get_next_lcore(lcore_1, 1, 0);
582 rte_eal_remote_launch(_arm_wrapper, NULL, lcore_1);
583 rte_eal_remote_launch(_arm_wrapper, NULL, lcore_2);
585 rte_eal_mp_wait_lcore();
586 TEST_ASSERT_SUCCESS(_wait_timer_triggers(10, MAX_TIMERS * 2, 0),
587 "Timer triggered count doesn't match arm count");
594 _arm_timers_burst(uint64_t timeout_tcks, uint64_t timers)
598 struct rte_event_timer *ev_tim[MAX_BURST];
599 const struct rte_event_timer tim = {
600 .ev.op = RTE_EVENT_OP_NEW,
602 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
603 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
604 .ev.event_type = RTE_EVENT_TYPE_TIMER,
605 .state = RTE_EVENT_TIMER_NOT_ARMED,
606 .timeout_ticks = CALC_TICKS(timeout_tcks),
609 for (i = 0; i < timers / MAX_BURST; i++) {
610 TEST_ASSERT_SUCCESS(rte_mempool_get_bulk(
611 eventdev_test_mempool,
612 (void **)ev_tim, MAX_BURST),
613 "mempool alloc failed");
615 for (j = 0; j < MAX_BURST; j++) {
617 ev_tim[j]->ev.event_ptr = ev_tim[j];
620 TEST_ASSERT_EQUAL(rte_event_timer_arm_tmo_tick_burst(timdev,
621 ev_tim, tim.timeout_ticks, MAX_BURST),
622 MAX_BURST, "Failed to arm timer %d", rte_errno);
629 test_timer_arm_burst(void)
631 TEST_ASSERT_SUCCESS(_arm_timers_burst(20, MAX_TIMERS),
632 "Failed to arm timers");
633 TEST_ASSERT_SUCCESS(_wait_timer_triggers(10, MAX_TIMERS, 0),
634 "Timer triggered count doesn't match arm count");
640 test_timer_arm_burst_periodic(void)
642 TEST_ASSERT_SUCCESS(_arm_timers_burst(1, MAX_TIMERS),
643 "Failed to arm timers");
644 /* With a resolution of 100ms and wait time of 1sec,
645 * there will be 10 * MAX_TIMERS periodic timer triggers.
647 TEST_ASSERT_SUCCESS(_wait_timer_triggers(1, 10 * MAX_TIMERS, 0),
648 "Timer triggered count doesn't match arm count");
654 _arm_wrapper_burst(void *arg)
658 TEST_ASSERT_SUCCESS(_arm_timers_burst(20, MAX_TIMERS),
659 "Failed to arm timers");
665 test_timer_arm_burst_multicore(void)
667 rte_eal_remote_launch(_arm_wrapper_burst, NULL, test_lcore1);
668 rte_eal_remote_launch(_arm_wrapper_burst, NULL, test_lcore2);
670 rte_eal_mp_wait_lcore();
671 TEST_ASSERT_SUCCESS(_wait_timer_triggers(10, MAX_TIMERS * 2, 0),
672 "Timer triggered count doesn't match arm count");
678 test_timer_cancel_periodic(void)
681 struct rte_event_timer *ev_tim;
682 const struct rte_event_timer tim = {
683 .ev.op = RTE_EVENT_OP_NEW,
685 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
686 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
687 .ev.event_type = RTE_EVENT_TYPE_TIMER,
688 .state = RTE_EVENT_TIMER_NOT_ARMED,
689 .timeout_ticks = CALC_TICKS(1),
692 for (i = 0; i < MAX_TIMERS; i++) {
693 TEST_ASSERT_SUCCESS(rte_mempool_get(eventdev_test_mempool,
695 "mempool alloc failed");
697 ev_tim->ev.event_ptr = ev_tim;
699 TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim,
700 1), 1, "Failed to arm timer %d",
703 rte_delay_us(100 + (i % 5000));
705 TEST_ASSERT_EQUAL(rte_event_timer_cancel_burst(timdev,
707 "Failed to cancel event timer %d", rte_errno);
708 rte_mempool_put(eventdev_test_mempool, ev_tim);
712 TEST_ASSERT_SUCCESS(_wait_timer_triggers(30, MAX_TIMERS,
714 "Timer triggered count doesn't match arm, cancel count");
720 test_timer_cancel(void)
723 struct rte_event_timer *ev_tim;
724 const struct rte_event_timer tim = {
725 .ev.op = RTE_EVENT_OP_NEW,
727 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
728 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
729 .ev.event_type = RTE_EVENT_TYPE_TIMER,
730 .state = RTE_EVENT_TIMER_NOT_ARMED,
731 .timeout_ticks = CALC_TICKS(20),
734 for (i = 0; i < MAX_TIMERS; i++) {
735 TEST_ASSERT_SUCCESS(rte_mempool_get(eventdev_test_mempool,
737 "mempool alloc failed");
739 ev_tim->ev.event_ptr = ev_tim;
741 TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim,
742 1), 1, "Failed to arm timer %d",
745 rte_delay_us(100 + (i % 5000));
747 TEST_ASSERT_EQUAL(rte_event_timer_cancel_burst(timdev,
749 "Failed to cancel event timer %d", rte_errno);
750 rte_mempool_put(eventdev_test_mempool, ev_tim);
754 TEST_ASSERT_SUCCESS(_wait_timer_triggers(30, MAX_TIMERS,
756 "Timer triggered count doesn't match arm, cancel count");
762 _cancel_producer(uint64_t timeout_tcks, uint64_t timers)
765 struct rte_event_timer *ev_tim;
766 const struct rte_event_timer tim = {
767 .ev.op = RTE_EVENT_OP_NEW,
769 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
770 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
771 .ev.event_type = RTE_EVENT_TYPE_TIMER,
772 .state = RTE_EVENT_TIMER_NOT_ARMED,
773 .timeout_ticks = CALC_TICKS(timeout_tcks),
776 for (i = 0; i < timers; i++) {
777 TEST_ASSERT_SUCCESS(rte_mempool_get(eventdev_test_mempool,
779 "mempool alloc failed");
782 ev_tim->ev.event_ptr = ev_tim;
784 TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim,
785 1), 1, "Failed to arm timer %d",
788 TEST_ASSERT_EQUAL(ev_tim->state, RTE_EVENT_TIMER_ARMED,
789 "Failed to arm event timer");
791 while (rte_ring_enqueue(timer_producer_ring, ev_tim) != 0)
799 _cancel_producer_burst(uint64_t timeout_tcks, uint64_t timers)
804 struct rte_event_timer *ev_tim[MAX_BURST];
805 const struct rte_event_timer tim = {
806 .ev.op = RTE_EVENT_OP_NEW,
808 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
809 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
810 .ev.event_type = RTE_EVENT_TYPE_TIMER,
811 .state = RTE_EVENT_TIMER_NOT_ARMED,
812 .timeout_ticks = CALC_TICKS(timeout_tcks),
816 for (i = 0; i < timers / MAX_BURST; i++) {
817 TEST_ASSERT_SUCCESS(rte_mempool_get_bulk(
818 eventdev_test_mempool,
819 (void **)ev_tim, MAX_BURST),
820 "mempool alloc failed");
822 for (j = 0; j < MAX_BURST; j++) {
824 ev_tim[j]->ev.event_ptr = ev_tim[j];
827 TEST_ASSERT_EQUAL(rte_event_timer_arm_tmo_tick_burst(timdev,
828 ev_tim, tim.timeout_ticks, MAX_BURST),
829 MAX_BURST, "Failed to arm timer %d", rte_errno);
831 for (j = 0; j < MAX_BURST; j++)
832 TEST_ASSERT_EQUAL(ev_tim[j]->state,
833 RTE_EVENT_TIMER_ARMED,
834 "Event timer not armed, state = %d",
837 ret = rte_ring_enqueue_bulk(timer_producer_ring,
838 (void **)ev_tim, MAX_BURST, NULL);
839 TEST_ASSERT_EQUAL(ret, MAX_BURST,
840 "Failed to enqueue event timers to ring");
844 TEST_ASSERT_EQUAL(arm_count, MAX_TIMERS,
845 "Failed to arm expected number of event timers");
851 _cancel_producer_wrapper(void *args)
855 return _cancel_producer(20, MAX_TIMERS);
859 _cancel_producer_burst_wrapper(void *args)
863 return _cancel_producer_burst(100, MAX_TIMERS);
867 _cancel_thread(void *args)
870 struct rte_event_timer *ev_tim = NULL;
871 uint64_t cancel_count = 0;
874 while (!arm_done || rte_ring_count(timer_producer_ring) > 0) {
875 if (rte_ring_dequeue(timer_producer_ring, (void **)&ev_tim))
878 ret = rte_event_timer_cancel_burst(timdev, &ev_tim, 1);
879 TEST_ASSERT_EQUAL(ret, 1, "Failed to cancel timer");
880 rte_mempool_put(eventdev_test_mempool, (void *)ev_tim);
888 _cancel_burst_thread(void *args)
893 struct rte_event_timer *ev_tim[MAX_BURST];
894 uint64_t cancel_count = 0;
895 uint64_t dequeue_count = 0;
897 while (!arm_done || rte_ring_count(timer_producer_ring) > 0) {
898 n = rte_ring_dequeue_burst(timer_producer_ring,
899 (void **)ev_tim, MAX_BURST, NULL);
905 for (i = 0; i < n; i++)
906 TEST_ASSERT_EQUAL(ev_tim[i]->state,
907 RTE_EVENT_TIMER_ARMED,
908 "Event timer not armed, state = %d",
911 ret = rte_event_timer_cancel_burst(timdev, ev_tim, n);
912 TEST_ASSERT_EQUAL(n, ret, "Failed to cancel complete burst of "
914 rte_mempool_put_bulk(eventdev_test_mempool, (void **)ev_tim,
915 RTE_MIN(ret, MAX_BURST));
920 TEST_ASSERT_EQUAL(cancel_count, MAX_TIMERS,
921 "Failed to cancel expected number of timers: "
922 "expected = %d, cancel_count = %"PRIu64", "
923 "dequeue_count = %"PRIu64"\n", MAX_TIMERS,
924 cancel_count, dequeue_count);
930 test_timer_cancel_multicore(void)
933 timer_producer_ring = rte_ring_create("timer_cancel_queue",
934 MAX_TIMERS * 2, rte_socket_id(), 0);
935 TEST_ASSERT_NOT_NULL(timer_producer_ring,
936 "Unable to reserve memory for ring");
938 rte_eal_remote_launch(_cancel_thread, NULL, test_lcore3);
939 rte_eal_remote_launch(_cancel_producer_wrapper, NULL, test_lcore1);
940 rte_eal_remote_launch(_cancel_producer_wrapper, NULL, test_lcore2);
942 rte_eal_wait_lcore(test_lcore1);
943 rte_eal_wait_lcore(test_lcore2);
945 rte_eal_wait_lcore(test_lcore3);
946 rte_ring_free(timer_producer_ring);
948 TEST_ASSERT_SUCCESS(_wait_timer_triggers(30, MAX_TIMERS * 2,
950 "Timer triggered count doesn't match arm count");
956 test_timer_cancel_burst_multicore(void)
959 timer_producer_ring = rte_ring_create("timer_cancel_queue",
960 MAX_TIMERS * 2, rte_socket_id(), 0);
961 TEST_ASSERT_NOT_NULL(timer_producer_ring,
962 "Unable to reserve memory for ring");
964 rte_eal_remote_launch(_cancel_burst_thread, NULL, test_lcore2);
965 rte_eal_remote_launch(_cancel_producer_burst_wrapper, NULL,
968 rte_eal_wait_lcore(test_lcore1);
970 rte_eal_wait_lcore(test_lcore2);
971 rte_ring_free(timer_producer_ring);
973 TEST_ASSERT_SUCCESS(_wait_timer_triggers(30, MAX_TIMERS,
975 "Timer triggered count doesn't match arm count");
981 test_timer_cancel_random(void)
984 uint64_t events_canceled = 0;
985 struct rte_event_timer *ev_tim;
986 const struct rte_event_timer tim = {
987 .ev.op = RTE_EVENT_OP_NEW,
989 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
990 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
991 .ev.event_type = RTE_EVENT_TYPE_TIMER,
992 .state = RTE_EVENT_TIMER_NOT_ARMED,
993 .timeout_ticks = CALC_TICKS(20),
996 for (i = 0; i < MAX_TIMERS; i++) {
998 TEST_ASSERT_SUCCESS(rte_mempool_get(eventdev_test_mempool,
1000 "mempool alloc failed");
1002 ev_tim->ev.event_ptr = ev_tim;
1004 TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim,
1005 1), 1, "Failed to arm timer %d",
1008 if (rte_rand() & 1) {
1009 rte_delay_us(100 + (i % 5000));
1010 TEST_ASSERT_EQUAL(rte_event_timer_cancel_burst(
1013 "Failed to cancel event timer %d", rte_errno);
1014 rte_mempool_put(eventdev_test_mempool, ev_tim);
1019 TEST_ASSERT_SUCCESS(_wait_timer_triggers(30, MAX_TIMERS,
1021 "Timer triggered count doesn't match arm, cancel count");
1023 return TEST_SUCCESS;
1026 /* Check that the adapter can be created correctly */
1028 adapter_create(void)
1031 struct rte_event_timer_adapter *adapter, *adapter2;
1033 struct rte_event_timer_adapter_conf conf = {
1034 .event_dev_id = evdev + 1, // invalid event dev id
1035 .timer_adapter_id = adapter_id,
1036 .clk_src = RTE_EVENT_TIMER_ADAPTER_CPU_CLK,
1037 .timer_tick_ns = NSECPERSEC / 10,
1038 .max_tmo_ns = 180 * NSECPERSEC,
1039 .nb_timers = MAX_TIMERS,
1040 .flags = RTE_EVENT_TIMER_ADAPTER_F_ADJUST_RES,
1044 /* Test invalid conf */
1045 adapter = rte_event_timer_adapter_create(&conf);
1046 TEST_ASSERT_NULL(adapter, "Created adapter with invalid "
1048 TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Incorrect errno value for "
1049 "invalid event device id");
1051 /* Test valid conf */
1052 conf.event_dev_id = evdev;
1053 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_caps_get(evdev, &caps),
1054 "failed to get adapter capabilities");
1055 if (!(caps & RTE_EVENT_TIMER_ADAPTER_CAP_INTERNAL_PORT))
1056 adapter = rte_event_timer_adapter_create_ext(&conf,
1060 adapter = rte_event_timer_adapter_create(&conf);
1061 TEST_ASSERT_NOT_NULL(adapter, "Failed to create adapter with valid "
1064 /* Test existing id */
1065 adapter2 = rte_event_timer_adapter_create(&conf);
1066 TEST_ASSERT_NULL(adapter2, "Created adapter with in-use id");
1067 TEST_ASSERT(rte_errno == EEXIST, "Incorrect errno value for existing "
1070 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_free(adapter),
1071 "Failed to free adapter");
1073 rte_mempool_free(eventdev_test_mempool);
1075 return TEST_SUCCESS;
1079 /* Test that adapter can be freed correctly. */
1083 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_stop(timdev),
1084 "Failed to stop adapter");
1086 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_free(timdev),
1087 "Failed to free valid adapter");
1089 /* Test free of already freed adapter */
1090 TEST_ASSERT_FAIL(rte_event_timer_adapter_free(timdev),
1091 "Freed adapter that was already freed");
1093 /* Test free of null adapter */
1095 TEST_ASSERT_FAIL(rte_event_timer_adapter_free(timdev),
1096 "Freed null adapter");
1098 rte_mempool_free(eventdev_test_mempool);
1100 return TEST_SUCCESS;
1103 /* Test that adapter info can be retrieved and is correct. */
1105 adapter_get_info(void)
1107 struct rte_event_timer_adapter_info info;
1109 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_get_info(timdev, &info),
1110 "Failed to get adapter info");
1113 TEST_ASSERT_EQUAL(info.event_dev_port_id, 1,
1114 "Expected port id = 1, got port id = %d",
1115 info.event_dev_port_id);
1117 return TEST_SUCCESS;
1120 /* Test adapter lookup via adapter ID. */
1122 adapter_lookup(void)
1124 struct rte_event_timer_adapter *adapter;
1126 adapter = rte_event_timer_adapter_lookup(TEST_ADAPTER_ID);
1127 TEST_ASSERT_NOT_NULL(adapter, "Failed to lookup adapter");
1129 return TEST_SUCCESS;
1135 TEST_ASSERT_SUCCESS(_timdev_setup(180 * NSECPERSEC, NSECPERSEC / 10,
1136 RTE_EVENT_TIMER_ADAPTER_F_ADJUST_RES),
1137 "Failed to start adapter");
1138 TEST_ASSERT_EQUAL(rte_event_timer_adapter_start(timdev), -EALREADY,
1139 "Timer adapter started without call to stop.");
1141 return TEST_SUCCESS;
1144 /* Test that adapter stops correctly. */
1148 struct rte_event_timer_adapter *l_adapter = NULL;
1150 /* Test adapter stop */
1151 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_stop(timdev),
1152 "Failed to stop event adapter");
1154 TEST_ASSERT_FAIL(rte_event_timer_adapter_stop(l_adapter),
1155 "Erroneously stopped null event adapter");
1157 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_free(timdev),
1158 "Failed to free adapter");
1160 rte_mempool_free(eventdev_test_mempool);
1162 return TEST_SUCCESS;
1165 /* Test increment and reset of ev_enq_count stat */
1167 stat_inc_reset_ev_enq(void)
1170 int num_evtims = MAX_TIMERS;
1171 struct rte_event_timer *evtims[num_evtims];
1172 struct rte_event evs[BATCH_SIZE];
1173 struct rte_event_timer_adapter_stats stats;
1174 const struct rte_event_timer init_tim = {
1175 .ev.op = RTE_EVENT_OP_NEW,
1176 .ev.queue_id = TEST_QUEUE_ID,
1177 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1178 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1179 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1180 .state = RTE_EVENT_TIMER_NOT_ARMED,
1181 .timeout_ticks = CALC_TICKS(5), // expire in .5 sec
1184 ret = rte_mempool_get_bulk(eventdev_test_mempool, (void **)evtims,
1186 TEST_ASSERT_EQUAL(ret, 0, "Failed to get array of timer objs: ret = %d",
1189 for (i = 0; i < num_evtims; i++) {
1190 *evtims[i] = init_tim;
1191 evtims[i]->ev.event_ptr = evtims[i];
1194 ret = rte_event_timer_adapter_stats_get(timdev, &stats);
1195 TEST_ASSERT_EQUAL(ret, 0, "Failed to get stats");
1196 TEST_ASSERT_EQUAL((int)stats.ev_enq_count, 0, "Stats not clear at "
1199 /* Test with the max value for the adapter */
1200 ret = rte_event_timer_arm_burst(timdev, evtims, num_evtims);
1201 TEST_ASSERT_EQUAL(ret, num_evtims,
1202 "Failed to arm all event timers: attempted = %d, "
1203 "succeeded = %d, rte_errno = %s",
1204 num_evtims, ret, rte_strerror(rte_errno));
1208 #define MAX_TRIES num_evtims
1213 sum += rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs,
1215 if (sum >= num_evtims || ++tries >= MAX_TRIES)
1221 TEST_ASSERT_EQUAL(sum, num_evtims, "Expected %d timer expiry events, "
1222 "got %d", num_evtims, sum);
1224 TEST_ASSERT(tries < MAX_TRIES, "Exceeded max tries");
1228 /* Make sure the eventdev is still empty */
1229 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs),
1232 TEST_ASSERT_EQUAL(n, 0, "Dequeued unexpected number of timer expiry "
1233 "events from event device");
1235 /* Check stats again */
1236 ret = rte_event_timer_adapter_stats_get(timdev, &stats);
1237 TEST_ASSERT_EQUAL(ret, 0, "Failed to get stats");
1238 TEST_ASSERT_EQUAL((int)stats.ev_enq_count, num_evtims,
1239 "Expected enqueue stat = %d; got %d", num_evtims,
1240 (int)stats.ev_enq_count);
1242 /* Reset and check again */
1243 ret = rte_event_timer_adapter_stats_reset(timdev);
1244 TEST_ASSERT_EQUAL(ret, 0, "Failed to reset stats");
1246 ret = rte_event_timer_adapter_stats_get(timdev, &stats);
1247 TEST_ASSERT_EQUAL(ret, 0, "Failed to get stats");
1248 TEST_ASSERT_EQUAL((int)stats.ev_enq_count, 0,
1249 "Expected enqueue stat = %d; got %d", 0,
1250 (int)stats.ev_enq_count);
1252 rte_mempool_put_bulk(eventdev_test_mempool, (void **)evtims,
1255 return TEST_SUCCESS;
1258 /* Test various cases in arming timers */
1260 event_timer_arm(void)
1264 struct rte_event_timer_adapter *adapter = timdev;
1265 struct rte_event_timer *evtim = NULL;
1266 struct rte_event evs[BATCH_SIZE];
1267 const struct rte_event_timer init_tim = {
1268 .ev.op = RTE_EVENT_OP_NEW,
1269 .ev.queue_id = TEST_QUEUE_ID,
1270 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1271 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1272 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1273 .state = RTE_EVENT_TIMER_NOT_ARMED,
1274 .timeout_ticks = CALC_TICKS(5), // expire in .5 sec
1277 rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
1278 if (evtim == NULL) {
1279 /* Failed to get an event timer object */
1283 /* Set up a timer */
1285 evtim->ev.event_ptr = evtim;
1287 /* Test single timer arm succeeds */
1288 ret = rte_event_timer_arm_burst(adapter, &evtim, 1);
1289 TEST_ASSERT_EQUAL(ret, 1, "Failed to arm event timer: %s\n",
1290 rte_strerror(rte_errno));
1291 TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_ARMED, "Event timer "
1292 "in incorrect state");
1294 /* Test arm of armed timer fails */
1295 ret = rte_event_timer_arm_burst(adapter, &evtim, 1);
1296 TEST_ASSERT_EQUAL(ret, 0, "expected return value from "
1297 "rte_event_timer_arm_burst: 0, got: %d", ret);
1298 TEST_ASSERT_EQUAL(rte_errno, EALREADY, "Unexpected rte_errno value "
1299 "after arming already armed timer");
1301 /* Let timer expire */
1304 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
1305 TEST_ASSERT_EQUAL(n, 1, "Failed to dequeue expected number of expiry "
1306 "events from event device");
1308 rte_mempool_put(eventdev_test_mempool, evtim);
1310 return TEST_SUCCESS;
1313 /* This test checks that repeated references to the same event timer in the
1314 * arm request work as expected; only the first one through should succeed.
1317 event_timer_arm_double(void)
1321 struct rte_event_timer_adapter *adapter = timdev;
1322 struct rte_event_timer *evtim = NULL;
1323 struct rte_event evs[BATCH_SIZE];
1324 const struct rte_event_timer init_tim = {
1325 .ev.op = RTE_EVENT_OP_NEW,
1326 .ev.queue_id = TEST_QUEUE_ID,
1327 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1328 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1329 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1330 .state = RTE_EVENT_TIMER_NOT_ARMED,
1331 .timeout_ticks = CALC_TICKS(5), // expire in .5 sec
1334 rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
1335 if (evtim == NULL) {
1336 /* Failed to get an event timer object */
1340 /* Set up a timer */
1342 evtim->ev.event_ptr = evtim;
1344 struct rte_event_timer *evtim_arr[] = {evtim, evtim};
1345 ret = rte_event_timer_arm_burst(adapter, evtim_arr, RTE_DIM(evtim_arr));
1346 TEST_ASSERT_EQUAL(ret, 1, "Unexpected return value from "
1347 "rte_event_timer_arm_burst");
1348 TEST_ASSERT_EQUAL(rte_errno, EALREADY, "Unexpected rte_errno value "
1349 "after double-arm");
1351 /* Let timer expire */
1354 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
1355 TEST_ASSERT_EQUAL(n, 1, "Dequeued incorrect number of expiry events - "
1356 "expected: 1, actual: %d", n);
1358 rte_mempool_put(eventdev_test_mempool, evtim);
1360 return TEST_SUCCESS;
1363 /* Test the timer expiry event is generated at the expected time. */
1365 event_timer_arm_expiry(void)
1369 struct rte_event_timer_adapter *adapter = timdev;
1370 struct rte_event_timer *evtim = NULL;
1371 struct rte_event_timer *evtim2 = NULL;
1372 struct rte_event evs[BATCH_SIZE];
1373 const struct rte_event_timer init_tim = {
1374 .ev.op = RTE_EVENT_OP_NEW,
1375 .ev.queue_id = TEST_QUEUE_ID,
1376 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1377 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1378 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1379 .state = RTE_EVENT_TIMER_NOT_ARMED,
1382 rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
1383 if (evtim == NULL) {
1384 /* Failed to get an event timer object */
1388 /* Set up an event timer */
1390 evtim->timeout_ticks = CALC_TICKS(30), // expire in 3 secs
1391 evtim->ev.event_ptr = evtim;
1393 ret = rte_event_timer_arm_burst(adapter, &evtim, 1);
1394 TEST_ASSERT_EQUAL(ret, 1, "Failed to arm event timer: %s",
1395 rte_strerror(rte_errno));
1396 TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_ARMED, "Event "
1397 "timer in incorrect state");
1401 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
1402 TEST_ASSERT_EQUAL(n, 0, "Dequeued unexpected timer expiry event");
1404 /* Delay 100 ms to account for the adapter tick window - should let us
1409 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
1410 TEST_ASSERT_EQUAL(n, 1, "Dequeued incorrect number (%d) of timer "
1411 "expiry events", n);
1412 TEST_ASSERT_EQUAL(evs[0].event_type, RTE_EVENT_TYPE_TIMER,
1413 "Dequeued unexpected type of event");
1415 /* Check that we recover the original event timer and then free it */
1416 evtim2 = evs[0].event_ptr;
1417 TEST_ASSERT_EQUAL(evtim, evtim2,
1418 "Failed to recover pointer to original event timer");
1419 rte_mempool_put(eventdev_test_mempool, evtim2);
1421 return TEST_SUCCESS;
1424 /* Check that rearming a timer works as expected. */
1426 event_timer_arm_rearm(void)
1430 struct rte_event_timer *evtim = NULL;
1431 struct rte_event_timer *evtim2 = NULL;
1432 struct rte_event evs[BATCH_SIZE];
1433 const struct rte_event_timer init_tim = {
1434 .ev.op = RTE_EVENT_OP_NEW,
1435 .ev.queue_id = TEST_QUEUE_ID,
1436 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1437 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1438 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1439 .state = RTE_EVENT_TIMER_NOT_ARMED,
1442 rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
1443 if (evtim == NULL) {
1444 /* Failed to get an event timer object */
1448 /* Set up a timer */
1450 evtim->timeout_ticks = CALC_TICKS(1); // expire in 0.1 sec
1451 evtim->ev.event_ptr = evtim;
1454 ret = rte_event_timer_arm_burst(timdev, &evtim, 1);
1455 TEST_ASSERT_EQUAL(ret, 1, "Failed to arm event timer: %s\n",
1456 rte_strerror(rte_errno));
1458 /* Add 100ms to account for the adapter tick window */
1459 rte_delay_ms(100 + 100);
1461 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
1462 TEST_ASSERT_EQUAL(n, 1, "Failed to dequeue expected number of expiry "
1463 "events from event device");
1465 /* Recover the timer through the event that was dequeued. */
1466 evtim2 = evs[0].event_ptr;
1467 TEST_ASSERT_EQUAL(evtim, evtim2,
1468 "Failed to recover pointer to original event timer");
1470 /* Need to reset state in case implementation can't do it */
1471 evtim2->state = RTE_EVENT_TIMER_NOT_ARMED;
1474 ret = rte_event_timer_arm_burst(timdev, &evtim2, 1);
1475 TEST_ASSERT_EQUAL(ret, 1, "Failed to arm event timer: %s\n",
1476 rte_strerror(rte_errno));
1478 /* Add 100ms to account for the adapter tick window */
1479 rte_delay_ms(100 + 100);
1481 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
1482 TEST_ASSERT_EQUAL(n, 1, "Failed to dequeue expected number of expiry "
1483 "events from event device");
1486 evtim2 = evs[0].event_ptr;
1487 TEST_ASSERT_EQUAL(evtim, evtim2,
1488 "Failed to recover pointer to original event timer");
1489 rte_mempool_put(eventdev_test_mempool, evtim2);
1491 return TEST_SUCCESS;
1494 /* Check that the adapter handles the max specified number of timers as
1498 event_timer_arm_max(void)
1501 int num_evtims = MAX_TIMERS;
1502 struct rte_event_timer *evtims[num_evtims];
1503 struct rte_event evs[BATCH_SIZE];
1504 const struct rte_event_timer init_tim = {
1505 .ev.op = RTE_EVENT_OP_NEW,
1506 .ev.queue_id = TEST_QUEUE_ID,
1507 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1508 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1509 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1510 .state = RTE_EVENT_TIMER_NOT_ARMED,
1511 .timeout_ticks = CALC_TICKS(5), // expire in .5 sec
1514 ret = rte_mempool_get_bulk(eventdev_test_mempool, (void **)evtims,
1516 TEST_ASSERT_EQUAL(ret, 0, "Failed to get array of timer objs: ret = %d",
1519 for (i = 0; i < num_evtims; i++) {
1520 *evtims[i] = init_tim;
1521 evtims[i]->ev.event_ptr = evtims[i];
1524 /* Test with the max value for the adapter */
1525 ret = rte_event_timer_arm_burst(timdev, evtims, num_evtims);
1526 TEST_ASSERT_EQUAL(ret, num_evtims,
1527 "Failed to arm all event timers: attempted = %d, "
1528 "succeeded = %d, rte_errno = %s",
1529 num_evtims, ret, rte_strerror(rte_errno));
1533 #define MAX_TRIES num_evtims
1538 sum += rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs,
1540 if (sum >= num_evtims || ++tries >= MAX_TRIES)
1546 TEST_ASSERT_EQUAL(sum, num_evtims, "Expected %d timer expiry events, "
1547 "got %d", num_evtims, sum);
1549 TEST_ASSERT(tries < MAX_TRIES, "Exceeded max tries");
1553 /* Make sure the eventdev is still empty */
1554 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs),
1557 TEST_ASSERT_EQUAL(n, 0, "Dequeued unexpected number of timer expiry "
1558 "events from event device");
1560 rte_mempool_put_bulk(eventdev_test_mempool, (void **)evtims,
1563 return TEST_SUCCESS;
1566 /* Check that creating an event timer with incorrect event sched type fails. */
1568 event_timer_arm_invalid_sched_type(void)
1571 struct rte_event_timer *evtim = NULL;
1572 const struct rte_event_timer init_tim = {
1573 .ev.op = RTE_EVENT_OP_NEW,
1574 .ev.queue_id = TEST_QUEUE_ID,
1575 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1576 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1577 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1578 .state = RTE_EVENT_TIMER_NOT_ARMED,
1579 .timeout_ticks = CALC_TICKS(5), // expire in .5 sec
1582 if (!using_services)
1585 rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
1586 if (evtim == NULL) {
1587 /* Failed to get an event timer object */
1592 evtim->ev.event_ptr = evtim;
1593 evtim->ev.sched_type = RTE_SCHED_TYPE_PARALLEL; // bad sched type
1595 ret = rte_event_timer_arm_burst(timdev, &evtim, 1);
1596 TEST_ASSERT_EQUAL(ret, 0, "Expected to fail timer arm with invalid "
1597 "sched type, but didn't");
1598 TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Unexpected rte_errno value after"
1599 " arm fail with invalid queue");
1601 rte_mempool_put(eventdev_test_mempool, &evtim);
1603 return TEST_SUCCESS;
1606 /* Check that creating an event timer with a timeout value that is too small or
1610 event_timer_arm_invalid_timeout(void)
1613 struct rte_event_timer *evtim = NULL;
1614 const struct rte_event_timer init_tim = {
1615 .ev.op = RTE_EVENT_OP_NEW,
1616 .ev.queue_id = TEST_QUEUE_ID,
1617 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1618 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1619 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1620 .state = RTE_EVENT_TIMER_NOT_ARMED,
1621 .timeout_ticks = CALC_TICKS(5), // expire in .5 sec
1624 rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
1625 if (evtim == NULL) {
1626 /* Failed to get an event timer object */
1631 evtim->ev.event_ptr = evtim;
1632 evtim->timeout_ticks = 0; // timeout too small
1634 ret = rte_event_timer_arm_burst(timdev, &evtim, 1);
1635 TEST_ASSERT_EQUAL(ret, 0, "Expected to fail timer arm with invalid "
1636 "timeout, but didn't");
1637 TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Unexpected rte_errno value after"
1638 " arm fail with invalid timeout");
1639 TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_ERROR_TOOEARLY,
1640 "Unexpected event timer state");
1643 evtim->ev.event_ptr = evtim;
1644 evtim->timeout_ticks = CALC_TICKS(1801); // timeout too big
1646 ret = rte_event_timer_arm_burst(timdev, &evtim, 1);
1647 TEST_ASSERT_EQUAL(ret, 0, "Expected to fail timer arm with invalid "
1648 "timeout, but didn't");
1649 TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Unexpected rte_errno value after"
1650 " arm fail with invalid timeout");
1651 TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_ERROR_TOOLATE,
1652 "Unexpected event timer state");
1654 rte_mempool_put(eventdev_test_mempool, evtim);
1656 return TEST_SUCCESS;
1660 event_timer_cancel(void)
1664 struct rte_event_timer_adapter *adapter = timdev;
1665 struct rte_event_timer *evtim = NULL;
1666 struct rte_event evs[BATCH_SIZE];
1667 const struct rte_event_timer init_tim = {
1668 .ev.op = RTE_EVENT_OP_NEW,
1669 .ev.queue_id = TEST_QUEUE_ID,
1670 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1671 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1672 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1673 .state = RTE_EVENT_TIMER_NOT_ARMED,
1676 rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
1677 if (evtim == NULL) {
1678 /* Failed to get an event timer object */
1682 /* Check that cancelling an uninited timer fails */
1683 ret = rte_event_timer_cancel_burst(adapter, &evtim, 1);
1684 TEST_ASSERT_EQUAL(ret, 0, "Succeeded unexpectedly in canceling "
1686 TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Unexpected rte_errno value after "
1687 "cancelling uninited timer");
1689 /* Set up a timer */
1691 evtim->ev.event_ptr = evtim;
1692 evtim->timeout_ticks = CALC_TICKS(30); // expire in 3 sec
1694 /* Check that cancelling an inited but unarmed timer fails */
1695 ret = rte_event_timer_cancel_burst(adapter, &evtim, 1);
1696 TEST_ASSERT_EQUAL(ret, 0, "Succeeded unexpectedly in canceling "
1698 TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Unexpected rte_errno value after "
1699 "cancelling unarmed timer");
1701 ret = rte_event_timer_arm_burst(adapter, &evtim, 1);
1702 TEST_ASSERT_EQUAL(ret, 1, "Failed to arm event timer: %s\n",
1703 rte_strerror(rte_errno));
1704 TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_ARMED,
1705 "evtim in incorrect state");
1710 ret = rte_event_timer_cancel_burst(adapter, &evtim, 1);
1711 TEST_ASSERT_EQUAL(ret, 1, "Failed to cancel event_timer: %s\n",
1712 rte_strerror(rte_errno));
1713 TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_CANCELED,
1714 "evtim in incorrect state");
1718 /* Make sure that no expiry event was generated */
1719 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
1720 TEST_ASSERT_EQUAL(n, 0, "Dequeued unexpected timer expiry event\n");
1722 rte_mempool_put(eventdev_test_mempool, evtim);
1724 return TEST_SUCCESS;
1728 event_timer_cancel_double(void)
1732 struct rte_event_timer_adapter *adapter = timdev;
1733 struct rte_event_timer *evtim = NULL;
1734 struct rte_event evs[BATCH_SIZE];
1735 const struct rte_event_timer init_tim = {
1736 .ev.op = RTE_EVENT_OP_NEW,
1737 .ev.queue_id = TEST_QUEUE_ID,
1738 .ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
1739 .ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
1740 .ev.event_type = RTE_EVENT_TYPE_TIMER,
1741 .state = RTE_EVENT_TIMER_NOT_ARMED,
1742 .timeout_ticks = CALC_TICKS(5), // expire in .5 sec
1745 rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
1746 if (evtim == NULL) {
1747 /* Failed to get an event timer object */
1751 /* Set up a timer */
1753 evtim->ev.event_ptr = evtim;
1754 evtim->timeout_ticks = CALC_TICKS(30); // expire in 3 sec
1756 ret = rte_event_timer_arm_burst(adapter, &evtim, 1);
1757 TEST_ASSERT_EQUAL(ret, 1, "Failed to arm event timer: %s\n",
1758 rte_strerror(rte_errno));
1759 TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_ARMED,
1760 "timer in unexpected state");
1762 /* Now, test that referencing the same timer twice in the same call
1765 struct rte_event_timer *evtim_arr[] = {evtim, evtim};
1766 ret = rte_event_timer_cancel_burst(adapter, evtim_arr,
1767 RTE_DIM(evtim_arr));
1769 /* Two requests to cancel same timer, only one should succeed */
1770 TEST_ASSERT_EQUAL(ret, 1, "Succeeded unexpectedly in canceling timer "
1773 TEST_ASSERT_EQUAL(rte_errno, EALREADY, "Unexpected rte_errno value "
1774 "after double-cancel: rte_errno = %d", rte_errno);
1778 /* Still make sure that no expiry event was generated */
1779 n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
1780 TEST_ASSERT_EQUAL(n, 0, "Dequeued unexpected timer expiry event\n");
1782 rte_mempool_put(eventdev_test_mempool, evtim);
1784 return TEST_SUCCESS;
1787 /* Check that event timer adapter tick resolution works as expected by testing
1788 * the number of adapter ticks that occur within a particular time interval.
1791 adapter_tick_resolution(void)
1793 struct rte_event_timer_adapter_stats stats;
1794 uint64_t adapter_tick_count;
1796 /* Only run this test in the software driver case */
1797 if (!using_services)
1800 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_stats_reset(timdev),
1801 "Failed to reset stats");
1803 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_stats_get(timdev,
1804 &stats), "Failed to get adapter stats");
1805 TEST_ASSERT_EQUAL(stats.adapter_tick_count, 0, "Adapter tick count "
1808 /* Delay 1 second; should let at least 10 ticks occur with the default
1809 * adapter configuration used by this test.
1813 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_stats_get(timdev,
1814 &stats), "Failed to get adapter stats");
1816 adapter_tick_count = stats.adapter_tick_count;
1817 TEST_ASSERT(adapter_tick_count >= 10 && adapter_tick_count <= 12,
1818 "Expected 10-12 adapter ticks, got %"PRIu64"\n",
1819 adapter_tick_count);
1821 return TEST_SUCCESS;
1825 adapter_create_max(void)
1828 uint32_t svc_start_count, svc_end_count;
1829 struct rte_event_timer_adapter *adapters[
1830 RTE_EVENT_TIMER_ADAPTER_NUM_MAX + 1];
1832 struct rte_event_timer_adapter_conf conf = {
1833 .event_dev_id = evdev,
1834 // timer_adapter_id set in loop
1835 .clk_src = RTE_EVENT_TIMER_ADAPTER_CPU_CLK,
1836 .timer_tick_ns = NSECPERSEC / 10,
1837 .max_tmo_ns = 180 * NSECPERSEC,
1838 .nb_timers = MAX_TIMERS,
1839 .flags = RTE_EVENT_TIMER_ADAPTER_F_ADJUST_RES,
1842 if (!using_services)
1845 svc_start_count = rte_service_get_count();
1847 /* This test expects that there are sufficient service IDs available
1848 * to be allocated. I.e., RTE_EVENT_TIMER_ADAPTER_NUM_MAX may need to
1849 * be less than RTE_SERVICE_NUM_MAX if anything else uses a service
1850 * (the SW event device, for example).
1852 for (i = 0; i < RTE_EVENT_TIMER_ADAPTER_NUM_MAX; i++) {
1853 conf.timer_adapter_id = i;
1854 adapters[i] = rte_event_timer_adapter_create_ext(&conf,
1855 test_port_conf_cb, NULL);
1856 TEST_ASSERT_NOT_NULL(adapters[i], "Failed to create adapter "
1860 conf.timer_adapter_id = i;
1861 adapters[i] = rte_event_timer_adapter_create(&conf);
1862 TEST_ASSERT_NULL(adapters[i], "Created too many adapters");
1864 /* Check that at least RTE_EVENT_TIMER_ADAPTER_NUM_MAX services
1867 svc_end_count = rte_service_get_count();
1868 TEST_ASSERT_EQUAL(svc_end_count - svc_start_count,
1869 RTE_EVENT_TIMER_ADAPTER_NUM_MAX,
1870 "Failed to create expected number of services");
1872 for (i = 0; i < RTE_EVENT_TIMER_ADAPTER_NUM_MAX; i++)
1873 TEST_ASSERT_SUCCESS(rte_event_timer_adapter_free(adapters[i]),
1874 "Failed to free adapter %d", i);
1876 /* Check that service count is back to where it was at start */
1877 svc_end_count = rte_service_get_count();
1878 TEST_ASSERT_EQUAL(svc_start_count, svc_end_count, "Failed to release "
1879 "correct number of services");
1881 return TEST_SUCCESS;
1884 static struct unit_test_suite event_timer_adptr_functional_testsuite = {
1885 .suite_name = "event timer functional test suite",
1886 .setup = testsuite_setup,
1887 .teardown = testsuite_teardown,
1888 .unit_test_cases = {
1889 TEST_CASE_ST(timdev_setup_usec, timdev_teardown,
1891 TEST_CASE_ST(timdev_setup_usec, timdev_teardown,
1893 TEST_CASE_ST(timdev_setup_msec_periodic, timdev_teardown,
1894 test_timer_arm_periodic),
1895 TEST_CASE_ST(timdev_setup_usec, timdev_teardown,
1896 test_timer_arm_burst),
1897 TEST_CASE_ST(timdev_setup_msec_periodic, timdev_teardown,
1898 test_timer_arm_burst_periodic),
1899 TEST_CASE_ST(timdev_setup_sec, timdev_teardown,
1901 TEST_CASE_ST(timdev_setup_sec_periodic, timdev_teardown,
1902 test_timer_cancel_periodic),
1903 TEST_CASE_ST(timdev_setup_sec, timdev_teardown,
1904 test_timer_cancel_random),
1905 TEST_CASE_ST(timdev_setup_usec_multicore, timdev_teardown,
1906 test_timer_arm_multicore),
1907 TEST_CASE_ST(timdev_setup_usec_multicore, timdev_teardown,
1908 test_timer_arm_burst_multicore),
1909 TEST_CASE_ST(timdev_setup_sec_multicore, timdev_teardown,
1910 test_timer_cancel_multicore),
1911 TEST_CASE_ST(timdev_setup_sec_multicore, timdev_teardown,
1912 test_timer_cancel_burst_multicore),
1913 TEST_CASE(adapter_create),
1914 TEST_CASE_ST(timdev_setup_msec, NULL, adapter_free),
1915 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1917 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1919 TEST_CASE_ST(NULL, timdev_teardown,
1921 TEST_CASE_ST(timdev_setup_msec, NULL,
1923 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1924 stat_inc_reset_ev_enq),
1925 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1927 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1928 event_timer_arm_double),
1929 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1930 event_timer_arm_expiry),
1931 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1932 event_timer_arm_rearm),
1933 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1934 event_timer_arm_max),
1935 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1936 event_timer_arm_invalid_sched_type),
1937 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1938 event_timer_arm_invalid_timeout),
1939 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1940 event_timer_cancel),
1941 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1942 event_timer_cancel_double),
1943 TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
1944 adapter_tick_resolution),
1945 TEST_CASE(adapter_create_max),
1946 TEST_CASES_END() /**< NULL terminate unit test array */
1951 test_event_timer_adapter_func(void)
1953 return unit_test_suite_runner(&event_timer_adptr_functional_testsuite);
1956 REGISTER_TEST_COMMAND(event_timer_adapter_test, test_event_timer_adapter_func);