DPDK logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2017 Cavium, Inc
 * Copyright(c) 2017-2018 Intel Corporation.
 */

#include <rte_atomic.h>
#include <rte_common.h>
#include <rte_cycles.h>
#include <rte_debug.h>
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_eventdev.h>
#include <rte_event_timer_adapter.h>
#include <rte_mempool.h>
#include <rte_launch.h>
#include <rte_lcore.h>
#include <rte_per_lcore.h>
#include <rte_random.h>
#include <rte_bus_vdev.h>
#include <rte_service.h>
#include <stdbool.h>

#include "test.h"

/* 4K timers corresponds to sw evdev max inflight events */
#define MAX_TIMERS  (4 * 1024)
#define BKT_TCK_NSEC

#define NSECPERSEC 1E9
#define BATCH_SIZE 16
/* Both the app lcore and adapter ports are linked to this queue */
#define TEST_QUEUE_ID 0
/* Port the application dequeues from */
#define TEST_PORT_ID 0
#define TEST_ADAPTER_ID 0

/* Handle log statements in same manner as test macros */
#define LOG_DBG(...)	RTE_LOG(DEBUG, EAL, __VA_ARGS__)

static int evdev;
static struct rte_event_timer_adapter *timdev;
static struct rte_mempool *eventdev_test_mempool;
static struct rte_ring *timer_producer_ring;
static uint64_t global_bkt_tck_ns;
static uint64_t global_info_bkt_tck_ns;
static volatile uint8_t arm_done;

#define CALC_TICKS(tks)					\
	((tks * global_bkt_tck_ns) / global_info_bkt_tck_ns)


static bool using_services;
static uint32_t test_lcore1;
static uint32_t test_lcore2;
static uint32_t test_lcore3;
static uint32_t sw_evdev_slcore;
static uint32_t sw_adptr_slcore;

static inline void
devconf_set_default_sane_values(struct rte_event_dev_config *dev_conf,
		struct rte_event_dev_info *info)
{
	memset(dev_conf, 0, sizeof(struct rte_event_dev_config));
	dev_conf->dequeue_timeout_ns = info->min_dequeue_timeout_ns;
	dev_conf->nb_event_ports = 1;
	dev_conf->nb_event_queues = 1;
	dev_conf->nb_event_queue_flows = info->max_event_queue_flows;
	dev_conf->nb_event_port_dequeue_depth =
		info->max_event_port_dequeue_depth;
	dev_conf->nb_event_port_enqueue_depth =
		info->max_event_port_enqueue_depth;
	dev_conf->nb_event_port_enqueue_depth =
		info->max_event_port_enqueue_depth;
	dev_conf->nb_events_limit =
		info->max_num_events;
}

static inline int
eventdev_setup(void)
{
	int ret;
	struct rte_event_dev_config dev_conf;
	struct rte_event_dev_info info;
	uint32_t service_id;

	ret = rte_event_dev_info_get(evdev, &info);
	TEST_ASSERT_SUCCESS(ret, "Failed to get event dev info");
	TEST_ASSERT(info.max_num_events < 0 ||
			info.max_num_events >= (int32_t)MAX_TIMERS,
			"ERROR max_num_events=%d < max_events=%d",
			info.max_num_events, MAX_TIMERS);

	devconf_set_default_sane_values(&dev_conf, &info);
	ret = rte_event_dev_configure(evdev, &dev_conf);
	TEST_ASSERT_SUCCESS(ret, "Failed to configure eventdev");

	ret = rte_event_queue_setup(evdev, 0, NULL);
	TEST_ASSERT_SUCCESS(ret, "Failed to setup queue=%d", 0);

	/* Configure event port */
	ret = rte_event_port_setup(evdev, 0, NULL);
	TEST_ASSERT_SUCCESS(ret, "Failed to setup port=%d", 0);
	ret = rte_event_port_link(evdev, 0, NULL, NULL, 0);
	TEST_ASSERT(ret >= 0, "Failed to link all queues port=%d", 0);

	/* If this is a software event device, map and start its service */
	if (rte_event_dev_service_id_get(evdev, &service_id) == 0) {
		TEST_ASSERT_SUCCESS(rte_service_lcore_add(sw_evdev_slcore),
				"Failed to add service core");
		TEST_ASSERT_SUCCESS(rte_service_lcore_start(
				sw_evdev_slcore),
				"Failed to start service core");
		TEST_ASSERT_SUCCESS(rte_service_map_lcore_set(
				service_id, sw_evdev_slcore, 1),
				"Failed to map evdev service");
		TEST_ASSERT_SUCCESS(rte_service_runstate_set(
				service_id, 1),
				"Failed to start evdev service");
	}

	ret = rte_event_dev_start(evdev);
	TEST_ASSERT_SUCCESS(ret, "Failed to start device");

	return TEST_SUCCESS;
}

static int
testsuite_setup(void)
{
	/* Some of the multithreaded tests require 3 other lcores to run */
	unsigned int required_lcore_count = 4;
	uint32_t service_id;

	/* To make it easier to map services later if needed, just reset
	 * service core state.
	 */
	(void) rte_service_lcore_reset_all();

	if (!rte_event_dev_count()) {
		/* If there is no hardware eventdev, or no software vdev was
		 * specified on the command line, create an instance of
		 * event_sw.
		 */
		LOG_DBG("Failed to find a valid event device... testing with"
			" event_sw device\n");
		TEST_ASSERT_SUCCESS(rte_vdev_init("event_sw0", NULL),
					"Error creating eventdev");
		evdev = rte_event_dev_get_dev_id("event_sw0");
	}

	if (rte_event_dev_service_id_get(evdev, &service_id) == 0) {
		/* A software event device will use a software event timer
		 * adapter as well. 2 more cores required to convert to
		 * service cores.
		 */
		required_lcore_count += 2;
		using_services = true;
	}

	if (rte_lcore_count() < required_lcore_count) {
		printf("%d lcores needed to run tests", required_lcore_count);
		return TEST_FAILED;
	}

	/* Assign lcores for various tasks */
	test_lcore1 = rte_get_next_lcore(-1, 1, 0);
	test_lcore2 = rte_get_next_lcore(test_lcore1, 1, 0);
	test_lcore3 = rte_get_next_lcore(test_lcore2, 1, 0);
	if (using_services) {
		sw_evdev_slcore = rte_get_next_lcore(test_lcore3, 1, 0);
		sw_adptr_slcore = rte_get_next_lcore(sw_evdev_slcore, 1, 0);
	}

	return eventdev_setup();
}

static void
testsuite_teardown(void)
{
	rte_event_dev_stop(evdev);
	rte_event_dev_close(evdev);
}

static int
setup_adapter_service(struct rte_event_timer_adapter *adptr)
{
	uint32_t adapter_service_id;
	int ret;

	/* retrieve service ids */
	TEST_ASSERT_SUCCESS(rte_event_timer_adapter_service_id_get(adptr,
			&adapter_service_id), "Failed to get event timer "
			"adapter service id");
	/* add a service core and start it */
	ret = rte_service_lcore_add(sw_adptr_slcore);
	TEST_ASSERT(ret == 0 || ret == -EALREADY,
			"Failed to add service core");
	ret = rte_service_lcore_start(sw_adptr_slcore);
	TEST_ASSERT(ret == 0 || ret == -EALREADY,
			"Failed to start service core");

	/* map services to it */
	TEST_ASSERT_SUCCESS(rte_service_map_lcore_set(adapter_service_id,
			sw_adptr_slcore, 1),
			"Failed to map adapter service");

	/* set services to running */
	TEST_ASSERT_SUCCESS(rte_service_runstate_set(adapter_service_id, 1),
			"Failed to start event timer adapter service");

	return TEST_SUCCESS;
}

static int
test_port_conf_cb(uint16_t id, uint8_t event_dev_id, uint8_t *event_port_id,
		  void *conf_arg)
{
	struct rte_event_dev_config dev_conf;
	struct rte_event_dev_info info;
	struct rte_event_port_conf *port_conf, def_port_conf = {0};
	uint32_t started;
	static int port_allocated;
	static uint8_t port_id;
	int ret;

	if (port_allocated) {
		*event_port_id = port_id;
		return 0;
	}

	RTE_SET_USED(id);

	ret = rte_event_dev_attr_get(event_dev_id, RTE_EVENT_DEV_ATTR_STARTED,
				     &started);
	if (ret < 0)
		return ret;

	if (started)
		rte_event_dev_stop(event_dev_id);

	ret = rte_event_dev_info_get(evdev, &info);
	if (ret < 0)
		return ret;

	devconf_set_default_sane_values(&dev_conf, &info);

	port_id = dev_conf.nb_event_ports;
	dev_conf.nb_event_ports++;

	ret = rte_event_dev_configure(event_dev_id, &dev_conf);
	if (ret < 0) {
		if (started)
			rte_event_dev_start(event_dev_id);
		return ret;
	}

	if (conf_arg != NULL)
		port_conf = conf_arg;
	else {
		port_conf = &def_port_conf;
		ret = rte_event_port_default_conf_get(event_dev_id, port_id,
						      port_conf);
		if (ret < 0)
			return ret;
	}

	ret = rte_event_port_setup(event_dev_id, port_id, port_conf);
	if (ret < 0)
		return ret;

	*event_port_id = port_id;

	if (started)
		rte_event_dev_start(event_dev_id);

	/* Reuse this port number next time this is called */
	port_allocated = 1;

	return 0;
}

static int
_timdev_setup(uint64_t max_tmo_ns, uint64_t bkt_tck_ns)
{
	struct rte_event_timer_adapter_info info;
	struct rte_event_timer_adapter_conf config = {
		.event_dev_id = evdev,
		.timer_adapter_id = TEST_ADAPTER_ID,
		.timer_tick_ns = bkt_tck_ns,
		.max_tmo_ns = max_tmo_ns,
		.nb_timers = MAX_TIMERS * 10,
		.flags = RTE_EVENT_TIMER_ADAPTER_F_ADJUST_RES,
	};
	uint32_t caps = 0;
	const char *pool_name = "timdev_test_pool";

	global_bkt_tck_ns = bkt_tck_ns;

	TEST_ASSERT_SUCCESS(rte_event_timer_adapter_caps_get(evdev, &caps),
				"failed to get adapter capabilities");
	if (!(caps & RTE_EVENT_TIMER_ADAPTER_CAP_INTERNAL_PORT)) {
		timdev = rte_event_timer_adapter_create_ext(&config,
							    test_port_conf_cb,
							    NULL);
		setup_adapter_service(timdev);
		using_services = true;
	} else
		timdev = rte_event_timer_adapter_create(&config);

	TEST_ASSERT_NOT_NULL(timdev,
			"failed to create event timer ring");

	TEST_ASSERT_EQUAL(rte_event_timer_adapter_start(timdev), 0,
			"failed to Start event timer adapter");

	/* Create event timer mempool */
	eventdev_test_mempool = rte_mempool_create(pool_name,
			MAX_TIMERS * 2,
			sizeof(struct rte_event_timer), /* element size*/
			0, /* cache size*/
			0, NULL, NULL, NULL, NULL,
			rte_socket_id(), 0);
	if (!eventdev_test_mempool) {
		printf("ERROR creating mempool\n");
		return TEST_FAILED;
	}

	rte_event_timer_adapter_get_info(timdev, &info);

	global_info_bkt_tck_ns = info.min_resolution_ns;

	return TEST_SUCCESS;
}

static int
timdev_setup_usec(void)
{
	return using_services ?
		/* Max timeout is 10,000us and bucket interval is 100us */
		_timdev_setup(1E7, 1E5) :
		/* Max timeout is 100us and bucket interval is 1us */
		_timdev_setup(1E5, 1E3);
}

static int
timdev_setup_usec_multicore(void)
{
	return using_services ?
		/* Max timeout is 10,000us and bucket interval is 100us */
		_timdev_setup(1E7, 1E5) :
		/* Max timeout is 100us and bucket interval is 1us */
		_timdev_setup(1E5, 1E3);
}

static int
timdev_setup_msec(void)
{
	/* Max timeout is 2 mins, and bucket interval is 100 ms */
	return _timdev_setup(180 * NSECPERSEC, NSECPERSEC / 10);
}

static int
timdev_setup_sec(void)
{
	/* Max timeout is 100sec and bucket interval is 1sec */
	return _timdev_setup(1E11, 1E9);
}

static int
timdev_setup_sec_multicore(void)
{
	/* Max timeout is 100sec and bucket interval is 1sec */
	return _timdev_setup(1E11, 1E9);
}

static void
timdev_teardown(void)
{
	rte_event_timer_adapter_stop(timdev);
	rte_event_timer_adapter_free(timdev);

	rte_mempool_free(eventdev_test_mempool);
}

static inline int
test_timer_state(void)
{
	struct rte_event_timer *ev_tim;
	struct rte_event ev;
	const struct rte_event_timer tim = {
		.ev.op = RTE_EVENT_OP_NEW,
		.ev.queue_id = 0,
		.ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
		.ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
		.ev.event_type =  RTE_EVENT_TYPE_TIMER,
		.state = RTE_EVENT_TIMER_NOT_ARMED,
	};


	rte_mempool_get(eventdev_test_mempool, (void **)&ev_tim);
	*ev_tim = tim;
	ev_tim->ev.event_ptr = ev_tim;
	ev_tim->timeout_ticks = CALC_TICKS(120);

	TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim, 1), 0,
			"Armed timer exceeding max_timeout.");
	TEST_ASSERT_EQUAL(ev_tim->state, RTE_EVENT_TIMER_ERROR_TOOLATE,
			"Improper timer state set expected %d returned %d",
			RTE_EVENT_TIMER_ERROR_TOOLATE, ev_tim->state);

	ev_tim->state = RTE_EVENT_TIMER_NOT_ARMED;
	ev_tim->timeout_ticks = CALC_TICKS(10);

	TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim, 1), 1,
			"Failed to arm timer with proper timeout.");
	TEST_ASSERT_EQUAL(ev_tim->state, RTE_EVENT_TIMER_ARMED,
			"Improper timer state set expected %d returned %d",
			RTE_EVENT_TIMER_ARMED, ev_tim->state);

	if (!using_services)
		rte_delay_us(20);
	else
		rte_delay_us(1000 + 200);
	TEST_ASSERT_EQUAL(rte_event_dequeue_burst(evdev, 0, &ev, 1, 0), 1,
			"Armed timer failed to trigger.");

	ev_tim->state = RTE_EVENT_TIMER_NOT_ARMED;
	ev_tim->timeout_ticks = CALC_TICKS(90);
	TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim, 1), 1,
			"Failed to arm timer with proper timeout.");
	TEST_ASSERT_EQUAL(rte_event_timer_cancel_burst(timdev, &ev_tim, 1),
			1, "Failed to cancel armed timer");
	TEST_ASSERT_EQUAL(ev_tim->state, RTE_EVENT_TIMER_CANCELED,
			"Improper timer state set expected %d returned %d",
			RTE_EVENT_TIMER_CANCELED, ev_tim->state);

	rte_mempool_put(eventdev_test_mempool, (void *)ev_tim);

	return TEST_SUCCESS;
}

static inline int
_arm_timers(uint64_t timeout_tcks, uint64_t timers)
{
	uint64_t i;
	struct rte_event_timer *ev_tim;
	const struct rte_event_timer tim = {
		.ev.op = RTE_EVENT_OP_NEW,
		.ev.queue_id = 0,
		.ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
		.ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
		.ev.event_type =  RTE_EVENT_TYPE_TIMER,
		.state = RTE_EVENT_TIMER_NOT_ARMED,
		.timeout_ticks = CALC_TICKS(timeout_tcks),
	};

	for (i = 0; i < timers; i++) {

		TEST_ASSERT_SUCCESS(rte_mempool_get(eventdev_test_mempool,
					(void **)&ev_tim),
				"mempool alloc failed");
		*ev_tim = tim;
		ev_tim->ev.event_ptr = ev_tim;

		TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim,
					1), 1, "Failed to arm timer %d",
				rte_errno);
	}

	return TEST_SUCCESS;
}

static inline int
_wait_timer_triggers(uint64_t wait_sec, uint64_t arm_count,
		uint64_t cancel_count)
{
	uint8_t valid_event;
	uint64_t events = 0;
	uint64_t wait_start, max_wait;
	struct rte_event ev;

	max_wait = rte_get_timer_hz() * wait_sec;
	wait_start = rte_get_timer_cycles();
	while (1) {
		if (rte_get_timer_cycles() - wait_start > max_wait) {
			if (events + cancel_count != arm_count)
				TEST_ASSERT_SUCCESS(max_wait,
					"Max time limit for timers exceeded.");
			break;
		}

		valid_event = rte_event_dequeue_burst(evdev, 0, &ev, 1, 0);
		if (!valid_event)
			continue;

		rte_mempool_put(eventdev_test_mempool, ev.event_ptr);
		events++;
	}

	return TEST_SUCCESS;
}

static inline int
test_timer_arm(void)
{
	TEST_ASSERT_SUCCESS(_arm_timers(20, MAX_TIMERS),
			"Failed to arm timers");
	TEST_ASSERT_SUCCESS(_wait_timer_triggers(10, MAX_TIMERS, 0),
			"Timer triggered count doesn't match arm count");
	return TEST_SUCCESS;
}

static int
_arm_wrapper(void *arg)
{
	RTE_SET_USED(arg);

	TEST_ASSERT_SUCCESS(_arm_timers(20, MAX_TIMERS),
			"Failed to arm timers");

	return TEST_SUCCESS;
}

static inline int
test_timer_arm_multicore(void)
{

	uint32_t lcore_1 = rte_get_next_lcore(-1, 1, 0);
	uint32_t lcore_2 = rte_get_next_lcore(lcore_1, 1, 0);

	rte_eal_remote_launch(_arm_wrapper, NULL, lcore_1);
	rte_eal_remote_launch(_arm_wrapper, NULL, lcore_2);

	rte_eal_mp_wait_lcore();
	TEST_ASSERT_SUCCESS(_wait_timer_triggers(10, MAX_TIMERS * 2, 0),
			"Timer triggered count doesn't match arm count");

	return TEST_SUCCESS;
}

#define MAX_BURST 16
static inline int
_arm_timers_burst(uint64_t timeout_tcks, uint64_t timers)
{
	uint64_t i;
	int j;
	struct rte_event_timer *ev_tim[MAX_BURST];
	const struct rte_event_timer tim = {
		.ev.op = RTE_EVENT_OP_NEW,
		.ev.queue_id = 0,
		.ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
		.ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
		.ev.event_type =  RTE_EVENT_TYPE_TIMER,
		.state = RTE_EVENT_TIMER_NOT_ARMED,
		.timeout_ticks = CALC_TICKS(timeout_tcks),
	};

	for (i = 0; i < timers / MAX_BURST; i++) {
		TEST_ASSERT_SUCCESS(rte_mempool_get_bulk(
				eventdev_test_mempool,
				(void **)ev_tim, MAX_BURST),
				"mempool alloc failed");

		for (j = 0; j < MAX_BURST; j++) {
			*ev_tim[j] = tim;
			ev_tim[j]->ev.event_ptr = ev_tim[j];
		}

		TEST_ASSERT_EQUAL(rte_event_timer_arm_tmo_tick_burst(timdev,
				ev_tim, tim.timeout_ticks, MAX_BURST),
				MAX_BURST, "Failed to arm timer %d", rte_errno);
	}

	return TEST_SUCCESS;
}

static inline int
test_timer_arm_burst(void)
{
	TEST_ASSERT_SUCCESS(_arm_timers_burst(20, MAX_TIMERS),
			"Failed to arm timers");
	TEST_ASSERT_SUCCESS(_wait_timer_triggers(10, MAX_TIMERS, 0),
			"Timer triggered count doesn't match arm count");

	return TEST_SUCCESS;
}

static int
_arm_wrapper_burst(void *arg)
{
	RTE_SET_USED(arg);

	TEST_ASSERT_SUCCESS(_arm_timers_burst(20, MAX_TIMERS),
			"Failed to arm timers");

	return TEST_SUCCESS;
}

static inline int
test_timer_arm_burst_multicore(void)
{
	rte_eal_remote_launch(_arm_wrapper_burst, NULL, test_lcore1);
	rte_eal_remote_launch(_arm_wrapper_burst, NULL, test_lcore2);

	rte_eal_mp_wait_lcore();
	TEST_ASSERT_SUCCESS(_wait_timer_triggers(10, MAX_TIMERS * 2, 0),
			"Timer triggered count doesn't match arm count");

	return TEST_SUCCESS;
}

static inline int
test_timer_cancel(void)
{
	uint64_t i;
	struct rte_event_timer *ev_tim;
	const struct rte_event_timer tim = {
		.ev.op = RTE_EVENT_OP_NEW,
		.ev.queue_id = 0,
		.ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
		.ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
		.ev.event_type =  RTE_EVENT_TYPE_TIMER,
		.state = RTE_EVENT_TIMER_NOT_ARMED,
		.timeout_ticks = CALC_TICKS(20),
	};

	for (i = 0; i < MAX_TIMERS; i++) {
		TEST_ASSERT_SUCCESS(rte_mempool_get(eventdev_test_mempool,
					(void **)&ev_tim),
				"mempool alloc failed");
		*ev_tim = tim;
		ev_tim->ev.event_ptr = ev_tim;

		TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim,
					1), 1, "Failed to arm timer %d",
				rte_errno);

		rte_delay_us(100 + (i % 5000));

		TEST_ASSERT_EQUAL(rte_event_timer_cancel_burst(timdev,
					&ev_tim, 1), 1,
				"Failed to cancel event timer %d", rte_errno);
		rte_mempool_put(eventdev_test_mempool, ev_tim);
	}


	TEST_ASSERT_SUCCESS(_wait_timer_triggers(30, MAX_TIMERS,
				MAX_TIMERS),
		"Timer triggered count doesn't match arm, cancel count");

	return TEST_SUCCESS;
}

static int
_cancel_producer(uint64_t timeout_tcks, uint64_t timers)
{
	uint64_t i;
	struct rte_event_timer *ev_tim;
	const struct rte_event_timer tim = {
		.ev.op = RTE_EVENT_OP_NEW,
		.ev.queue_id = 0,
		.ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
		.ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
		.ev.event_type =  RTE_EVENT_TYPE_TIMER,
		.state = RTE_EVENT_TIMER_NOT_ARMED,
		.timeout_ticks = CALC_TICKS(timeout_tcks),
	};

	for (i = 0; i < timers; i++) {
		TEST_ASSERT_SUCCESS(rte_mempool_get(eventdev_test_mempool,
					(void **)&ev_tim),
				"mempool alloc failed");

		*ev_tim = tim;
		ev_tim->ev.event_ptr = ev_tim;

		TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim,
					1), 1, "Failed to arm timer %d",
				rte_errno);

		TEST_ASSERT_EQUAL(ev_tim->state, RTE_EVENT_TIMER_ARMED,
				  "Failed to arm event timer");

		while (rte_ring_enqueue(timer_producer_ring, ev_tim) != 0)
			;
	}

	return TEST_SUCCESS;
}

static int
_cancel_producer_burst(uint64_t timeout_tcks, uint64_t timers)
{

	uint64_t i;
	int j, ret;
	struct rte_event_timer *ev_tim[MAX_BURST];
	const struct rte_event_timer tim = {
		.ev.op = RTE_EVENT_OP_NEW,
		.ev.queue_id = 0,
		.ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
		.ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
		.ev.event_type =  RTE_EVENT_TYPE_TIMER,
		.state = RTE_EVENT_TIMER_NOT_ARMED,
		.timeout_ticks = CALC_TICKS(timeout_tcks),
	};
	int arm_count = 0;

	for (i = 0; i < timers / MAX_BURST; i++) {
		TEST_ASSERT_SUCCESS(rte_mempool_get_bulk(
				eventdev_test_mempool,
				(void **)ev_tim, MAX_BURST),
				"mempool alloc failed");

		for (j = 0; j < MAX_BURST; j++) {
			*ev_tim[j] = tim;
			ev_tim[j]->ev.event_ptr = ev_tim[j];
		}

		TEST_ASSERT_EQUAL(rte_event_timer_arm_tmo_tick_burst(timdev,
				ev_tim, tim.timeout_ticks, MAX_BURST),
				MAX_BURST, "Failed to arm timer %d", rte_errno);

		for (j = 0; j < MAX_BURST; j++)
			TEST_ASSERT_EQUAL(ev_tim[j]->state,
					  RTE_EVENT_TIMER_ARMED,
					  "Event timer not armed, state = %d",
					  ev_tim[j]->state);

		ret = rte_ring_enqueue_bulk(timer_producer_ring,
				(void **)ev_tim, MAX_BURST, NULL);
		TEST_ASSERT_EQUAL(ret, MAX_BURST,
				"Failed to enqueue event timers to ring");
		arm_count += ret;
	}

	TEST_ASSERT_EQUAL(arm_count, MAX_TIMERS,
			  "Failed to arm expected number of event timers");

	return TEST_SUCCESS;
}

static int
_cancel_producer_wrapper(void *args)
{
	RTE_SET_USED(args);

	return _cancel_producer(20, MAX_TIMERS);
}

static int
_cancel_producer_burst_wrapper(void *args)
{
	RTE_SET_USED(args);

	return _cancel_producer_burst(100, MAX_TIMERS);
}

static int
_cancel_thread(void *args)
{
	RTE_SET_USED(args);
	struct rte_event_timer *ev_tim = NULL;
	uint64_t cancel_count = 0;
	uint16_t ret;

	while (!arm_done || rte_ring_count(timer_producer_ring) > 0) {
		if (rte_ring_dequeue(timer_producer_ring, (void **)&ev_tim))
			continue;

		ret = rte_event_timer_cancel_burst(timdev, &ev_tim, 1);
		TEST_ASSERT_EQUAL(ret, 1, "Failed to cancel timer");
		rte_mempool_put(eventdev_test_mempool, (void *)ev_tim);
		cancel_count++;
	}

	return TEST_SUCCESS;
}

static int
_cancel_burst_thread(void *args)
{
	RTE_SET_USED(args);

	int ret, i, n;
	struct rte_event_timer *ev_tim[MAX_BURST];
	uint64_t cancel_count = 0;
	uint64_t dequeue_count = 0;

	while (!arm_done || rte_ring_count(timer_producer_ring) > 0) {
		n = rte_ring_dequeue_burst(timer_producer_ring,
				(void **)ev_tim, MAX_BURST, NULL);
		if (!n)
			continue;

		dequeue_count += n;

		for (i = 0; i < n; i++)
			TEST_ASSERT_EQUAL(ev_tim[i]->state,
					  RTE_EVENT_TIMER_ARMED,
					  "Event timer not armed, state = %d",
					  ev_tim[i]->state);

		ret = rte_event_timer_cancel_burst(timdev, ev_tim, n);
		TEST_ASSERT_EQUAL(n, ret, "Failed to cancel complete burst of "
				  "event timers");
		rte_mempool_put_bulk(eventdev_test_mempool, (void **)ev_tim,
				RTE_MIN(ret, MAX_BURST));

		cancel_count += ret;
	}

	TEST_ASSERT_EQUAL(cancel_count, MAX_TIMERS,
			  "Failed to cancel expected number of timers: "
			  "expected = %d, cancel_count = %"PRIu64", "
			  "dequeue_count = %"PRIu64"\n", MAX_TIMERS,
			  cancel_count, dequeue_count);

	return TEST_SUCCESS;
}

static inline int
test_timer_cancel_multicore(void)
{
	arm_done = 0;
	timer_producer_ring = rte_ring_create("timer_cancel_queue",
			MAX_TIMERS * 2, rte_socket_id(), 0);
	TEST_ASSERT_NOT_NULL(timer_producer_ring,
			"Unable to reserve memory for ring");

	rte_eal_remote_launch(_cancel_thread, NULL, test_lcore3);
	rte_eal_remote_launch(_cancel_producer_wrapper, NULL, test_lcore1);
	rte_eal_remote_launch(_cancel_producer_wrapper, NULL, test_lcore2);

	rte_eal_wait_lcore(test_lcore1);
	rte_eal_wait_lcore(test_lcore2);
	arm_done = 1;
	rte_eal_wait_lcore(test_lcore3);
	rte_ring_free(timer_producer_ring);

	TEST_ASSERT_SUCCESS(_wait_timer_triggers(30, MAX_TIMERS * 2,
			MAX_TIMERS * 2),
			"Timer triggered count doesn't match arm count");

	return TEST_SUCCESS;
}

static inline int
test_timer_cancel_burst_multicore(void)
{
	arm_done = 0;
	timer_producer_ring = rte_ring_create("timer_cancel_queue",
			MAX_TIMERS * 2, rte_socket_id(), 0);
	TEST_ASSERT_NOT_NULL(timer_producer_ring,
			"Unable to reserve memory for ring");

	rte_eal_remote_launch(_cancel_burst_thread, NULL, test_lcore2);
	rte_eal_remote_launch(_cancel_producer_burst_wrapper, NULL,
			test_lcore1);

	rte_eal_wait_lcore(test_lcore1);
	arm_done = 1;
	rte_eal_wait_lcore(test_lcore2);
	rte_ring_free(timer_producer_ring);

	TEST_ASSERT_SUCCESS(_wait_timer_triggers(30, MAX_TIMERS,
			MAX_TIMERS),
			"Timer triggered count doesn't match arm count");

	return TEST_SUCCESS;
}

static inline int
test_timer_cancel_random(void)
{
	uint64_t i;
	uint64_t events_canceled = 0;
	struct rte_event_timer *ev_tim;
	const struct rte_event_timer tim = {
		.ev.op = RTE_EVENT_OP_NEW,
		.ev.queue_id = 0,
		.ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
		.ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
		.ev.event_type =  RTE_EVENT_TYPE_TIMER,
		.state = RTE_EVENT_TIMER_NOT_ARMED,
		.timeout_ticks = CALC_TICKS(20),
	};

	for (i = 0; i < MAX_TIMERS; i++) {

		TEST_ASSERT_SUCCESS(rte_mempool_get(eventdev_test_mempool,
					(void **)&ev_tim),
				"mempool alloc failed");
		*ev_tim = tim;
		ev_tim->ev.event_ptr = ev_tim;

		TEST_ASSERT_EQUAL(rte_event_timer_arm_burst(timdev, &ev_tim,
					1), 1, "Failed to arm timer %d",
				rte_errno);

		if (rte_rand() & 1) {
			rte_delay_us(100 + (i % 5000));
			TEST_ASSERT_EQUAL(rte_event_timer_cancel_burst(
						timdev,
						&ev_tim, 1), 1,
				"Failed to cancel event timer %d", rte_errno);
			rte_mempool_put(eventdev_test_mempool, ev_tim);
			events_canceled++;
		}
	}

	TEST_ASSERT_SUCCESS(_wait_timer_triggers(30, MAX_TIMERS,
				events_canceled),
		       "Timer triggered count doesn't match arm, cancel count");

	return TEST_SUCCESS;
}

/* Check that the adapter can be created correctly */
static int
adapter_create(void)
{
	int adapter_id = 0;
	struct rte_event_timer_adapter *adapter, *adapter2;

	struct rte_event_timer_adapter_conf conf = {
		.event_dev_id = evdev + 1,  // invalid event dev id
		.timer_adapter_id = adapter_id,
		.clk_src = RTE_EVENT_TIMER_ADAPTER_CPU_CLK,
		.timer_tick_ns = NSECPERSEC / 10,
		.max_tmo_ns = 180 * NSECPERSEC,
		.nb_timers = MAX_TIMERS,
		.flags = RTE_EVENT_TIMER_ADAPTER_F_ADJUST_RES,
	};
	uint32_t caps = 0;

	/* Test invalid conf */
	adapter = rte_event_timer_adapter_create(&conf);
	TEST_ASSERT_NULL(adapter, "Created adapter with invalid "
			"event device id");
	TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Incorrect errno value for "
			"invalid event device id");

	/* Test valid conf */
	conf.event_dev_id = evdev;
	TEST_ASSERT_SUCCESS(rte_event_timer_adapter_caps_get(evdev, &caps),
			"failed to get adapter capabilities");
	if (!(caps & RTE_EVENT_TIMER_ADAPTER_CAP_INTERNAL_PORT))
		adapter = rte_event_timer_adapter_create_ext(&conf,
				test_port_conf_cb,
				NULL);
	else
		adapter = rte_event_timer_adapter_create(&conf);
	TEST_ASSERT_NOT_NULL(adapter, "Failed to create adapter with valid "
			"configuration");

	/* Test existing id */
	adapter2 = rte_event_timer_adapter_create(&conf);
	TEST_ASSERT_NULL(adapter2, "Created adapter with in-use id");
	TEST_ASSERT(rte_errno == EEXIST, "Incorrect errno value for existing "
			"id");

	TEST_ASSERT_SUCCESS(rte_event_timer_adapter_free(adapter),
			"Failed to free adapter");

	rte_mempool_free(eventdev_test_mempool);

	return TEST_SUCCESS;
}


/* Test that adapter can be freed correctly. */
static int
adapter_free(void)
{
	TEST_ASSERT_SUCCESS(rte_event_timer_adapter_stop(timdev),
			"Failed to stop adapter");

	TEST_ASSERT_SUCCESS(rte_event_timer_adapter_free(timdev),
			"Failed to free valid adapter");

	/* Test free of already freed adapter */
	TEST_ASSERT_FAIL(rte_event_timer_adapter_free(timdev),
			"Freed adapter that was already freed");

	/* Test free of null adapter */
	timdev = NULL;
	TEST_ASSERT_FAIL(rte_event_timer_adapter_free(timdev),
			"Freed null adapter");

	rte_mempool_free(eventdev_test_mempool);

	return TEST_SUCCESS;
}

/* Test that adapter info can be retrieved and is correct. */
static int
adapter_get_info(void)
{
	struct rte_event_timer_adapter_info info;

	TEST_ASSERT_SUCCESS(rte_event_timer_adapter_get_info(timdev, &info),
			"Failed to get adapter info");

	if (using_services)
		TEST_ASSERT_EQUAL(info.event_dev_port_id, 1,
				"Expected port id = 1, got port id = %d",
				info.event_dev_port_id);

	return TEST_SUCCESS;
}

/* Test adapter lookup via adapter ID. */
static int
adapter_lookup(void)
{
	struct rte_event_timer_adapter *adapter;

	adapter = rte_event_timer_adapter_lookup(TEST_ADAPTER_ID);
	TEST_ASSERT_NOT_NULL(adapter, "Failed to lookup adapter");

	return TEST_SUCCESS;
}

static int
adapter_start(void)
{
	TEST_ASSERT_SUCCESS(_timdev_setup(180 * NSECPERSEC,
			NSECPERSEC / 10),
			"Failed to start adapter");
	TEST_ASSERT_EQUAL(rte_event_timer_adapter_start(timdev), -EALREADY,
			"Timer adapter started without call to stop.");

	return TEST_SUCCESS;
}

/* Test that adapter stops correctly. */
static int
adapter_stop(void)
{
	struct rte_event_timer_adapter *l_adapter = NULL;

	/* Test adapter stop */
	TEST_ASSERT_SUCCESS(rte_event_timer_adapter_stop(timdev),
			"Failed to stop event adapter");

	TEST_ASSERT_FAIL(rte_event_timer_adapter_stop(l_adapter),
			"Erroneously stopped null event adapter");

	TEST_ASSERT_SUCCESS(rte_event_timer_adapter_free(timdev),
			"Failed to free adapter");

	rte_mempool_free(eventdev_test_mempool);

	return TEST_SUCCESS;
}

/* Test increment and reset of ev_enq_count stat */
static int
stat_inc_reset_ev_enq(void)
{
	int ret, i, n;
	int num_evtims = MAX_TIMERS;
	struct rte_event_timer *evtims[num_evtims];
	struct rte_event evs[BATCH_SIZE];
	struct rte_event_timer_adapter_stats stats;
	const struct rte_event_timer init_tim = {
		.ev.op = RTE_EVENT_OP_NEW,
		.ev.queue_id = TEST_QUEUE_ID,
		.ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
		.ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
		.ev.event_type =  RTE_EVENT_TYPE_TIMER,
		.state = RTE_EVENT_TIMER_NOT_ARMED,
		.timeout_ticks = CALC_TICKS(5), // expire in .5 sec
	};

	ret = rte_mempool_get_bulk(eventdev_test_mempool, (void **)evtims,
				   num_evtims);
	TEST_ASSERT_EQUAL(ret, 0, "Failed to get array of timer objs: ret = %d",
			  ret);

	for (i = 0; i < num_evtims; i++) {
		*evtims[i] = init_tim;
		evtims[i]->ev.event_ptr = evtims[i];
	}

	ret = rte_event_timer_adapter_stats_get(timdev, &stats);
	TEST_ASSERT_EQUAL(ret, 0, "Failed to get stats");
	TEST_ASSERT_EQUAL((int)stats.ev_enq_count, 0, "Stats not clear at "
			  "startup");

	/* Test with the max value for the adapter */
	ret = rte_event_timer_arm_burst(timdev, evtims, num_evtims);
	TEST_ASSERT_EQUAL(ret, num_evtims,
			  "Failed to arm all event timers: attempted = %d, "
			  "succeeded = %d, rte_errno = %s",
			  num_evtims, ret, rte_strerror(rte_errno));

	rte_delay_ms(1000);

#define MAX_TRIES num_evtims
	int sum = 0;
	int tries = 0;
	bool done = false;
	while (!done) {
		sum += rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs,
					       RTE_DIM(evs), 10);
		if (sum >= num_evtims || ++tries >= MAX_TRIES)
			done = true;

		rte_delay_ms(10);
	}

	TEST_ASSERT_EQUAL(sum, num_evtims, "Expected %d timer expiry events, "
			  "got %d", num_evtims, sum);

	TEST_ASSERT(tries < MAX_TRIES, "Exceeded max tries");

	rte_delay_ms(100);

	/* Make sure the eventdev is still empty */
	n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs),
				      10);

	TEST_ASSERT_EQUAL(n, 0, "Dequeued unexpected number of timer expiry "
			  "events from event device");

	/* Check stats again */
	ret = rte_event_timer_adapter_stats_get(timdev, &stats);
	TEST_ASSERT_EQUAL(ret, 0, "Failed to get stats");
	TEST_ASSERT_EQUAL((int)stats.ev_enq_count, num_evtims,
			  "Expected enqueue stat = %d; got %d", num_evtims,
			  (int)stats.ev_enq_count);

	/* Reset and check again */
	ret = rte_event_timer_adapter_stats_reset(timdev);
	TEST_ASSERT_EQUAL(ret, 0, "Failed to reset stats");

	ret = rte_event_timer_adapter_stats_get(timdev, &stats);
	TEST_ASSERT_EQUAL(ret, 0, "Failed to get stats");
	TEST_ASSERT_EQUAL((int)stats.ev_enq_count, 0,
			  "Expected enqueue stat = %d; got %d", 0,
			  (int)stats.ev_enq_count);

	rte_mempool_put_bulk(eventdev_test_mempool, (void **)evtims,
			     num_evtims);

	return TEST_SUCCESS;
}

/* Test various cases in arming timers */
static int
event_timer_arm(void)
{
	uint16_t n;
	int ret;
	struct rte_event_timer_adapter *adapter = timdev;
	struct rte_event_timer *evtim = NULL;
	struct rte_event evs[BATCH_SIZE];
	const struct rte_event_timer init_tim = {
		.ev.op = RTE_EVENT_OP_NEW,
		.ev.queue_id = TEST_QUEUE_ID,
		.ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
		.ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
		.ev.event_type =  RTE_EVENT_TYPE_TIMER,
		.state = RTE_EVENT_TIMER_NOT_ARMED,
		.timeout_ticks = CALC_TICKS(5), // expire in .5 sec
	};

	rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
	if (evtim == NULL) {
		/* Failed to get an event timer object */
		return TEST_FAILED;
	}

	/* Set up a timer */
	*evtim = init_tim;
	evtim->ev.event_ptr = evtim;

	/* Test single timer arm succeeds */
	ret = rte_event_timer_arm_burst(adapter, &evtim, 1);
	TEST_ASSERT_EQUAL(ret, 1, "Failed to arm event timer: %s\n",
			  rte_strerror(rte_errno));
	TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_ARMED, "Event timer "
			  "in incorrect state");

	/* Test arm of armed timer fails */
	ret = rte_event_timer_arm_burst(adapter, &evtim, 1);
	TEST_ASSERT_EQUAL(ret, 0, "expected return value from "
			  "rte_event_timer_arm_burst: 0, got: %d", ret);
	TEST_ASSERT_EQUAL(rte_errno, EALREADY, "Unexpected rte_errno value "
			  "after arming already armed timer");

	/* Let timer expire */
	rte_delay_ms(1000);

	n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
	TEST_ASSERT_EQUAL(n, 1, "Failed to dequeue expected number of expiry "
			  "events from event device");

	rte_mempool_put(eventdev_test_mempool, evtim);

	return TEST_SUCCESS;
}

/* This test checks that repeated references to the same event timer in the
 * arm request work as expected; only the first one through should succeed.
 */
static int
event_timer_arm_double(void)
{
	uint16_t n;
	int ret;
	struct rte_event_timer_adapter *adapter = timdev;
	struct rte_event_timer *evtim = NULL;
	struct rte_event evs[BATCH_SIZE];
	const struct rte_event_timer init_tim = {
		.ev.op = RTE_EVENT_OP_NEW,
		.ev.queue_id = TEST_QUEUE_ID,
		.ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
		.ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
		.ev.event_type =  RTE_EVENT_TYPE_TIMER,
		.state = RTE_EVENT_TIMER_NOT_ARMED,
		.timeout_ticks = CALC_TICKS(5), // expire in .5 sec
	};

	rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
	if (evtim == NULL) {
		/* Failed to get an event timer object */
		return TEST_FAILED;
	}

	/* Set up a timer */
	*evtim = init_tim;
	evtim->ev.event_ptr = evtim;

	struct rte_event_timer *evtim_arr[] = {evtim, evtim};
	ret = rte_event_timer_arm_burst(adapter, evtim_arr, RTE_DIM(evtim_arr));
	TEST_ASSERT_EQUAL(ret, 1, "Unexpected return value from "
			  "rte_event_timer_arm_burst");
	TEST_ASSERT_EQUAL(rte_errno, EALREADY, "Unexpected rte_errno value "
			  "after double-arm");

	/* Let timer expire */
	rte_delay_ms(600);

	n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
	TEST_ASSERT_EQUAL(n, 1, "Dequeued incorrect number of expiry events - "
			  "expected: 1, actual: %d", n);

	rte_mempool_put(eventdev_test_mempool, evtim);

	return TEST_SUCCESS;
}

/* Test the timer expiry event is generated at the expected time.  */
static int
event_timer_arm_expiry(void)
{
	uint16_t n;
	int ret;
	struct rte_event_timer_adapter *adapter = timdev;
	struct rte_event_timer *evtim = NULL;
	struct rte_event_timer *evtim2 = NULL;
	struct rte_event evs[BATCH_SIZE];
	const struct rte_event_timer init_tim = {
		.ev.op = RTE_EVENT_OP_NEW,
		.ev.queue_id = TEST_QUEUE_ID,
		.ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
		.ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
		.ev.event_type =  RTE_EVENT_TYPE_TIMER,
		.state = RTE_EVENT_TIMER_NOT_ARMED,
	};

	rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
	if (evtim == NULL) {
		/* Failed to get an event timer object */
		return TEST_FAILED;
	}

	/* Set up an event timer */
	*evtim = init_tim;
	evtim->timeout_ticks = CALC_TICKS(30),	// expire in 3 secs
	evtim->ev.event_ptr = evtim;

	ret = rte_event_timer_arm_burst(adapter, &evtim, 1);
	TEST_ASSERT_EQUAL(ret, 1, "Failed to arm event timer: %s",
			  rte_strerror(rte_errno));
	TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_ARMED, "Event "
			  "timer in incorrect state");

	rte_delay_ms(2999);

	n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
	TEST_ASSERT_EQUAL(n, 0, "Dequeued unexpected timer expiry event");

	/* Delay 100 ms to account for the adapter tick window - should let us
	 * dequeue one event
	 */
	rte_delay_ms(100);

	n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
	TEST_ASSERT_EQUAL(n, 1, "Dequeued incorrect number (%d) of timer "
			  "expiry events", n);
	TEST_ASSERT_EQUAL(evs[0].event_type, RTE_EVENT_TYPE_TIMER,
			  "Dequeued unexpected type of event");

	/* Check that we recover the original event timer and then free it */
	evtim2 = evs[0].event_ptr;
	TEST_ASSERT_EQUAL(evtim, evtim2,
			  "Failed to recover pointer to original event timer");
	rte_mempool_put(eventdev_test_mempool, evtim2);

	return TEST_SUCCESS;
}

/* Check that rearming a timer works as expected. */
static int
event_timer_arm_rearm(void)
{
	uint16_t n;
	int ret;
	struct rte_event_timer *evtim = NULL;
	struct rte_event_timer *evtim2 = NULL;
	struct rte_event evs[BATCH_SIZE];
	const struct rte_event_timer init_tim = {
		.ev.op = RTE_EVENT_OP_NEW,
		.ev.queue_id = TEST_QUEUE_ID,
		.ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
		.ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
		.ev.event_type = RTE_EVENT_TYPE_TIMER,
		.state = RTE_EVENT_TIMER_NOT_ARMED,
	};

	rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
	if (evtim == NULL) {
		/* Failed to get an event timer object */
		return TEST_FAILED;
	}

	/* Set up a timer */
	*evtim = init_tim;
	evtim->timeout_ticks = CALC_TICKS(1);  // expire in 0.1 sec
	evtim->ev.event_ptr = evtim;

	/* Arm it */
	ret = rte_event_timer_arm_burst(timdev, &evtim, 1);
	TEST_ASSERT_EQUAL(ret, 1, "Failed to arm event timer: %s\n",
			  rte_strerror(rte_errno));

	/* Add 100ms to account for the adapter tick window */
	rte_delay_ms(100 + 100);

	n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
	TEST_ASSERT_EQUAL(n, 1, "Failed to dequeue expected number of expiry "
			  "events from event device");

	/* Recover the timer through the event that was dequeued. */
	evtim2 = evs[0].event_ptr;
	TEST_ASSERT_EQUAL(evtim, evtim2,
			  "Failed to recover pointer to original event timer");

	/* Need to reset state in case implementation can't do it */
	evtim2->state = RTE_EVENT_TIMER_NOT_ARMED;

	/* Rearm it */
	ret = rte_event_timer_arm_burst(timdev, &evtim2, 1);
	TEST_ASSERT_EQUAL(ret, 1, "Failed to arm event timer: %s\n",
			  rte_strerror(rte_errno));

	/* Add 100ms to account for the adapter tick window */
	rte_delay_ms(100 + 100);

	n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
	TEST_ASSERT_EQUAL(n, 1, "Failed to dequeue expected number of expiry "
			  "events from event device");

	/* Free it */
	evtim2 = evs[0].event_ptr;
	TEST_ASSERT_EQUAL(evtim, evtim2,
			  "Failed to recover pointer to original event timer");
	rte_mempool_put(eventdev_test_mempool, evtim2);

	return TEST_SUCCESS;
}

/* Check that the adapter handles the max specified number of timers as
 * expected.
 */
static int
event_timer_arm_max(void)
{
	int ret, i, n;
	int num_evtims = MAX_TIMERS;
	struct rte_event_timer *evtims[num_evtims];
	struct rte_event evs[BATCH_SIZE];
	const struct rte_event_timer init_tim = {
		.ev.op = RTE_EVENT_OP_NEW,
		.ev.queue_id = TEST_QUEUE_ID,
		.ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
		.ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
		.ev.event_type =  RTE_EVENT_TYPE_TIMER,
		.state = RTE_EVENT_TIMER_NOT_ARMED,
		.timeout_ticks = CALC_TICKS(5), // expire in .5 sec
	};

	ret = rte_mempool_get_bulk(eventdev_test_mempool, (void **)evtims,
				   num_evtims);
	TEST_ASSERT_EQUAL(ret, 0, "Failed to get array of timer objs: ret = %d",
			  ret);

	for (i = 0; i < num_evtims; i++) {
		*evtims[i] = init_tim;
		evtims[i]->ev.event_ptr = evtims[i];
	}

	/* Test with the max value for the adapter */
	ret = rte_event_timer_arm_burst(timdev, evtims, num_evtims);
	TEST_ASSERT_EQUAL(ret, num_evtims,
			  "Failed to arm all event timers: attempted = %d, "
			  "succeeded = %d, rte_errno = %s",
			  num_evtims, ret, rte_strerror(rte_errno));

	rte_delay_ms(1000);

#define MAX_TRIES num_evtims
	int sum = 0;
	int tries = 0;
	bool done = false;
	while (!done) {
		sum += rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs,
					       RTE_DIM(evs), 10);
		if (sum >= num_evtims || ++tries >= MAX_TRIES)
			done = true;

		rte_delay_ms(10);
	}

	TEST_ASSERT_EQUAL(sum, num_evtims, "Expected %d timer expiry events, "
			  "got %d", num_evtims, sum);

	TEST_ASSERT(tries < MAX_TRIES, "Exceeded max tries");

	rte_delay_ms(100);

	/* Make sure the eventdev is still empty */
	n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs),
				    10);

	TEST_ASSERT_EQUAL(n, 0, "Dequeued unexpected number of timer expiry "
			  "events from event device");

	rte_mempool_put_bulk(eventdev_test_mempool, (void **)evtims,
			     num_evtims);

	return TEST_SUCCESS;
}

/* Check that creating an event timer with incorrect event sched type fails. */
static int
event_timer_arm_invalid_sched_type(void)
{
	int ret;
	struct rte_event_timer *evtim = NULL;
	const struct rte_event_timer init_tim = {
		.ev.op = RTE_EVENT_OP_NEW,
		.ev.queue_id = TEST_QUEUE_ID,
		.ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
		.ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
		.ev.event_type =  RTE_EVENT_TYPE_TIMER,
		.state = RTE_EVENT_TIMER_NOT_ARMED,
		.timeout_ticks = CALC_TICKS(5), // expire in .5 sec
	};

	if (!using_services)
		return -ENOTSUP;

	rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
	if (evtim == NULL) {
		/* Failed to get an event timer object */
		return TEST_FAILED;
	}

	*evtim = init_tim;
	evtim->ev.event_ptr = evtim;
	evtim->ev.sched_type = RTE_SCHED_TYPE_PARALLEL; // bad sched type

	ret = rte_event_timer_arm_burst(timdev, &evtim, 1);
	TEST_ASSERT_EQUAL(ret, 0, "Expected to fail timer arm with invalid "
			  "sched type, but didn't");
	TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Unexpected rte_errno value after"
			  " arm fail with invalid queue");

	rte_mempool_put(eventdev_test_mempool, &evtim);

	return TEST_SUCCESS;
}

/* Check that creating an event timer with a timeout value that is too small or
 * too big fails.
 */
static int
event_timer_arm_invalid_timeout(void)
{
	int ret;
	struct rte_event_timer *evtim = NULL;
	const struct rte_event_timer init_tim = {
		.ev.op = RTE_EVENT_OP_NEW,
		.ev.queue_id = TEST_QUEUE_ID,
		.ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
		.ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
		.ev.event_type =  RTE_EVENT_TYPE_TIMER,
		.state = RTE_EVENT_TIMER_NOT_ARMED,
		.timeout_ticks = CALC_TICKS(5), // expire in .5 sec
	};

	rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
	if (evtim == NULL) {
		/* Failed to get an event timer object */
		return TEST_FAILED;
	}

	*evtim = init_tim;
	evtim->ev.event_ptr = evtim;
	evtim->timeout_ticks = 0;  // timeout too small

	ret = rte_event_timer_arm_burst(timdev, &evtim, 1);
	TEST_ASSERT_EQUAL(ret, 0, "Expected to fail timer arm with invalid "
			  "timeout, but didn't");
	TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Unexpected rte_errno value after"
			  " arm fail with invalid timeout");
	TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_ERROR_TOOEARLY,
			  "Unexpected event timer state");

	*evtim = init_tim;
	evtim->ev.event_ptr = evtim;
	evtim->timeout_ticks = CALC_TICKS(1801);  // timeout too big

	ret = rte_event_timer_arm_burst(timdev, &evtim, 1);
	TEST_ASSERT_EQUAL(ret, 0, "Expected to fail timer arm with invalid "
			  "timeout, but didn't");
	TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Unexpected rte_errno value after"
			  " arm fail with invalid timeout");
	TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_ERROR_TOOLATE,
			  "Unexpected event timer state");

	rte_mempool_put(eventdev_test_mempool, evtim);

	return TEST_SUCCESS;
}

static int
event_timer_cancel(void)
{
	uint16_t n;
	int ret;
	struct rte_event_timer_adapter *adapter = timdev;
	struct rte_event_timer *evtim = NULL;
	struct rte_event evs[BATCH_SIZE];
	const struct rte_event_timer init_tim = {
		.ev.op = RTE_EVENT_OP_NEW,
		.ev.queue_id = TEST_QUEUE_ID,
		.ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
		.ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
		.ev.event_type =  RTE_EVENT_TYPE_TIMER,
		.state = RTE_EVENT_TIMER_NOT_ARMED,
	};

	rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
	if (evtim == NULL) {
		/* Failed to get an event timer object */
		return TEST_FAILED;
	}

	/* Check that cancelling an uninited timer fails */
	ret = rte_event_timer_cancel_burst(adapter, &evtim, 1);
	TEST_ASSERT_EQUAL(ret, 0, "Succeeded unexpectedly in canceling "
			  "uninited timer");
	TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Unexpected rte_errno value after "
			  "cancelling uninited timer");

	/* Set up a timer */
	*evtim = init_tim;
	evtim->ev.event_ptr = evtim;
	evtim->timeout_ticks = CALC_TICKS(30);  // expire in 3 sec

	/* Check that cancelling an inited but unarmed timer fails */
	ret = rte_event_timer_cancel_burst(adapter, &evtim, 1);
	TEST_ASSERT_EQUAL(ret, 0, "Succeeded unexpectedly in canceling "
			  "unarmed timer");
	TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Unexpected rte_errno value after "
			  "cancelling unarmed timer");

	ret = rte_event_timer_arm_burst(adapter, &evtim, 1);
	TEST_ASSERT_EQUAL(ret, 1, "Failed to arm event timer: %s\n",
			  rte_strerror(rte_errno));
	TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_ARMED,
			  "evtim in incorrect state");

	/* Delay 1 sec */
	rte_delay_ms(1000);

	ret = rte_event_timer_cancel_burst(adapter, &evtim, 1);
	TEST_ASSERT_EQUAL(ret, 1, "Failed to cancel event_timer: %s\n",
			  rte_strerror(rte_errno));
	TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_CANCELED,
			  "evtim in incorrect state");

	rte_delay_ms(3000);

	/* Make sure that no expiry event was generated */
	n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
	TEST_ASSERT_EQUAL(n, 0, "Dequeued unexpected timer expiry event\n");

	rte_mempool_put(eventdev_test_mempool, evtim);

	return TEST_SUCCESS;
}

static int
event_timer_cancel_double(void)
{
	uint16_t n;
	int ret;
	struct rte_event_timer_adapter *adapter = timdev;
	struct rte_event_timer *evtim = NULL;
	struct rte_event evs[BATCH_SIZE];
	const struct rte_event_timer init_tim = {
		.ev.op = RTE_EVENT_OP_NEW,
		.ev.queue_id = TEST_QUEUE_ID,
		.ev.sched_type = RTE_SCHED_TYPE_ATOMIC,
		.ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
		.ev.event_type =  RTE_EVENT_TYPE_TIMER,
		.state = RTE_EVENT_TIMER_NOT_ARMED,
		.timeout_ticks = CALC_TICKS(5), // expire in .5 sec
	};

	rte_mempool_get(eventdev_test_mempool, (void **)&evtim);
	if (evtim == NULL) {
		/* Failed to get an event timer object */
		return TEST_FAILED;
	}

	/* Set up a timer */
	*evtim = init_tim;
	evtim->ev.event_ptr = evtim;
	evtim->timeout_ticks = CALC_TICKS(30);  // expire in 3 sec

	ret = rte_event_timer_arm_burst(adapter, &evtim, 1);
	TEST_ASSERT_EQUAL(ret, 1, "Failed to arm event timer: %s\n",
			  rte_strerror(rte_errno));
	TEST_ASSERT_EQUAL(evtim->state, RTE_EVENT_TIMER_ARMED,
			  "timer in unexpected state");

	/* Now, test that referencing the same timer twice in the same call
	 * fails
	 */
	struct rte_event_timer *evtim_arr[] = {evtim, evtim};
	ret = rte_event_timer_cancel_burst(adapter, evtim_arr,
					   RTE_DIM(evtim_arr));

	/* Two requests to cancel same timer, only one should succeed */
	TEST_ASSERT_EQUAL(ret, 1, "Succeeded unexpectedly in canceling timer "
			  "twice");

	TEST_ASSERT_EQUAL(rte_errno, EALREADY, "Unexpected rte_errno value "
			  "after double-cancel: rte_errno = %d", rte_errno);

	rte_delay_ms(3000);

	/* Still make sure that no expiry event was generated */
	n = rte_event_dequeue_burst(evdev, TEST_PORT_ID, evs, RTE_DIM(evs), 0);
	TEST_ASSERT_EQUAL(n, 0, "Dequeued unexpected timer expiry event\n");

	rte_mempool_put(eventdev_test_mempool, evtim);

	return TEST_SUCCESS;
}

/* Check that event timer adapter tick resolution works as expected by testing
 * the number of adapter ticks that occur within a particular time interval.
 */
static int
adapter_tick_resolution(void)
{
	struct rte_event_timer_adapter_stats stats;
	uint64_t adapter_tick_count;

	/* Only run this test in the software driver case */
	if (!using_services)
		return -ENOTSUP;

	TEST_ASSERT_SUCCESS(rte_event_timer_adapter_stats_reset(timdev),
				"Failed to reset stats");

	TEST_ASSERT_SUCCESS(rte_event_timer_adapter_stats_get(timdev,
			&stats), "Failed to get adapter stats");
	TEST_ASSERT_EQUAL(stats.adapter_tick_count, 0, "Adapter tick count "
			"not zeroed out");

	/* Delay 1 second; should let at least 10 ticks occur with the default
	 * adapter configuration used by this test.
	 */
	rte_delay_ms(1000);

	TEST_ASSERT_SUCCESS(rte_event_timer_adapter_stats_get(timdev,
			&stats), "Failed to get adapter stats");

	adapter_tick_count = stats.adapter_tick_count;
	TEST_ASSERT(adapter_tick_count >= 10 && adapter_tick_count <= 12,
			"Expected 10-12 adapter ticks, got %"PRIu64"\n",
			adapter_tick_count);

	return TEST_SUCCESS;
}

static int
adapter_create_max(void)
{
	int i;
	uint32_t svc_start_count, svc_end_count;
	struct rte_event_timer_adapter *adapters[
					RTE_EVENT_TIMER_ADAPTER_NUM_MAX + 1];

	struct rte_event_timer_adapter_conf conf = {
		.event_dev_id = evdev,
		// timer_adapter_id set in loop
		.clk_src = RTE_EVENT_TIMER_ADAPTER_CPU_CLK,
		.timer_tick_ns = NSECPERSEC / 10,
		.max_tmo_ns = 180 * NSECPERSEC,
		.nb_timers = MAX_TIMERS,
		.flags = RTE_EVENT_TIMER_ADAPTER_F_ADJUST_RES,
	};

	if (!using_services)
		return -ENOTSUP;

	svc_start_count = rte_service_get_count();

	/* This test expects that there are sufficient service IDs available
	 * to be allocated. I.e., RTE_EVENT_TIMER_ADAPTER_NUM_MAX may need to
	 * be less than RTE_SERVICE_NUM_MAX if anything else uses a service
	 * (the SW event device, for example).
	 */
	for (i = 0; i < RTE_EVENT_TIMER_ADAPTER_NUM_MAX; i++) {
		conf.timer_adapter_id = i;
		adapters[i] = rte_event_timer_adapter_create_ext(&conf,
				test_port_conf_cb, NULL);
		TEST_ASSERT_NOT_NULL(adapters[i], "Failed to create adapter "
				"%d", i);
	}

	conf.timer_adapter_id = i;
	adapters[i] = rte_event_timer_adapter_create(&conf);
	TEST_ASSERT_NULL(adapters[i], "Created too many adapters");

	/* Check that at least RTE_EVENT_TIMER_ADAPTER_NUM_MAX services
	 * have been created
	 */
	svc_end_count = rte_service_get_count();
	TEST_ASSERT_EQUAL(svc_end_count - svc_start_count,
			RTE_EVENT_TIMER_ADAPTER_NUM_MAX,
			"Failed to create expected number of services");

	for (i = 0; i < RTE_EVENT_TIMER_ADAPTER_NUM_MAX; i++)
		TEST_ASSERT_SUCCESS(rte_event_timer_adapter_free(adapters[i]),
				"Failed to free adapter %d", i);

	/* Check that service count is back to where it was at start */
	svc_end_count = rte_service_get_count();
	TEST_ASSERT_EQUAL(svc_start_count, svc_end_count, "Failed to release "
			  "correct number of services");

	return TEST_SUCCESS;
}

static struct unit_test_suite event_timer_adptr_functional_testsuite  = {
	.suite_name = "event timer functional test suite",
	.setup = testsuite_setup,
	.teardown = testsuite_teardown,
	.unit_test_cases = {
		TEST_CASE_ST(timdev_setup_usec, timdev_teardown,
				test_timer_state),
		TEST_CASE_ST(timdev_setup_usec, timdev_teardown,
				test_timer_arm),
		TEST_CASE_ST(timdev_setup_usec, timdev_teardown,
				test_timer_arm_burst),
		TEST_CASE_ST(timdev_setup_sec, timdev_teardown,
				test_timer_cancel),
		TEST_CASE_ST(timdev_setup_sec, timdev_teardown,
				test_timer_cancel_random),
		TEST_CASE_ST(timdev_setup_usec_multicore, timdev_teardown,
				test_timer_arm_multicore),
		TEST_CASE_ST(timdev_setup_usec_multicore, timdev_teardown,
				test_timer_arm_burst_multicore),
		TEST_CASE_ST(timdev_setup_sec_multicore, timdev_teardown,
				test_timer_cancel_multicore),
		TEST_CASE_ST(timdev_setup_sec_multicore, timdev_teardown,
				test_timer_cancel_burst_multicore),
		TEST_CASE(adapter_create),
		TEST_CASE_ST(timdev_setup_msec, NULL, adapter_free),
		TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
				adapter_get_info),
		TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
				adapter_lookup),
		TEST_CASE_ST(NULL, timdev_teardown,
				adapter_start),
		TEST_CASE_ST(timdev_setup_msec, NULL,
				adapter_stop),
		TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
				stat_inc_reset_ev_enq),
		TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
			     event_timer_arm),
		TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
			     event_timer_arm_double),
		TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
			     event_timer_arm_expiry),
		TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
				event_timer_arm_rearm),
		TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
				event_timer_arm_max),
		TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
				event_timer_arm_invalid_sched_type),
		TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
				event_timer_arm_invalid_timeout),
		TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
				event_timer_cancel),
		TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
				event_timer_cancel_double),
		TEST_CASE_ST(timdev_setup_msec, timdev_teardown,
				adapter_tick_resolution),
		TEST_CASE(adapter_create_max),
		TEST_CASES_END() /**< NULL terminate unit test array */
	}
};

static int
test_event_timer_adapter_func(void)
{
	return unit_test_suite_runner(&event_timer_adptr_functional_testsuite);
}

REGISTER_TEST_COMMAND(event_timer_adapter_test, test_event_timer_adapter_func);