1a9de470cSBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2a9de470cSBruce Richardson * Copyright(c) 2015 Intel Corporation
3a9de470cSBruce Richardson */
4a9de470cSBruce Richardson
5a9de470cSBruce Richardson
6a9de470cSBruce Richardson #include <stdio.h>
7a9de470cSBruce Richardson #include <inttypes.h>
8a9de470cSBruce Richardson #include <rte_ring.h>
9a9de470cSBruce Richardson #include <rte_cycles.h>
10a9de470cSBruce Richardson #include <rte_launch.h>
11a9de470cSBruce Richardson #include <rte_ethdev.h>
12a9de470cSBruce Richardson #include <rte_eth_ring.h>
13a9de470cSBruce Richardson #include <rte_bus_vdev.h>
14a9de470cSBruce Richardson
15a9de470cSBruce Richardson #include "test.h"
16a9de470cSBruce Richardson
17a9de470cSBruce Richardson #define RING_NAME "RING_PERF"
18a9de470cSBruce Richardson #define RING_SIZE 4096
19a9de470cSBruce Richardson #define MAX_BURST 32
20a9de470cSBruce Richardson
21a9de470cSBruce Richardson /*
22a9de470cSBruce Richardson * the sizes to enqueue and dequeue in testing
23a9de470cSBruce Richardson * (marked volatile so they won't be seen as compile-time constants)
24a9de470cSBruce Richardson */
25a9de470cSBruce Richardson static const volatile unsigned bulk_sizes[] = { 1, 8, 32 };
26a9de470cSBruce Richardson
27a9de470cSBruce Richardson /* The ring structure used for tests */
28a9de470cSBruce Richardson static struct rte_ring *r;
29a9de470cSBruce Richardson static uint16_t ring_ethdev_port;
30a9de470cSBruce Richardson
31a9de470cSBruce Richardson /* Get cycle counts for dequeuing from an empty ring. Should be 2 or 3 cycles */
32a9de470cSBruce Richardson static void
test_empty_dequeue(void)33a9de470cSBruce Richardson test_empty_dequeue(void)
34a9de470cSBruce Richardson {
35a9de470cSBruce Richardson const unsigned iter_shift = 26;
36a9de470cSBruce Richardson const unsigned iterations = 1 << iter_shift;
37a9de470cSBruce Richardson unsigned i = 0;
38a9de470cSBruce Richardson void *burst[MAX_BURST];
39a9de470cSBruce Richardson
40a9de470cSBruce Richardson const uint64_t sc_start = rte_rdtsc();
41a9de470cSBruce Richardson for (i = 0; i < iterations; i++)
42a9de470cSBruce Richardson rte_ring_sc_dequeue_bulk(r, burst, bulk_sizes[0], NULL);
43a9de470cSBruce Richardson const uint64_t sc_end = rte_rdtsc();
44a9de470cSBruce Richardson
45a9de470cSBruce Richardson const uint64_t eth_start = rte_rdtsc();
46a9de470cSBruce Richardson for (i = 0; i < iterations; i++)
47a9de470cSBruce Richardson rte_eth_rx_burst(ring_ethdev_port, 0, (void *)burst,
48a9de470cSBruce Richardson bulk_sizes[0]);
49a9de470cSBruce Richardson const uint64_t eth_end = rte_rdtsc();
50a9de470cSBruce Richardson
51a9de470cSBruce Richardson printf("Ring empty dequeue : %.1F\n",
52a9de470cSBruce Richardson (double)(sc_end - sc_start) / iterations);
53a9de470cSBruce Richardson printf("Ethdev empty dequeue: %.1F\n",
54a9de470cSBruce Richardson (double)(eth_end - eth_start) / iterations);
55a9de470cSBruce Richardson }
56a9de470cSBruce Richardson
57a9de470cSBruce Richardson /*
58a9de470cSBruce Richardson * Test function that determines how long an enqueue + dequeue of a single item
59a9de470cSBruce Richardson * takes on a single lcore. Result is for comparison with the bulk enq+deq.
60a9de470cSBruce Richardson */
61a9de470cSBruce Richardson static void
test_single_enqueue_dequeue(void)62a9de470cSBruce Richardson test_single_enqueue_dequeue(void)
63a9de470cSBruce Richardson {
64a9de470cSBruce Richardson const unsigned iter_shift = 24;
65a9de470cSBruce Richardson const unsigned iterations = 1 << iter_shift;
66a9de470cSBruce Richardson unsigned i = 0;
67a9de470cSBruce Richardson void *burst = NULL;
68a9de470cSBruce Richardson struct rte_mbuf *mburst[1] = { NULL };
69a9de470cSBruce Richardson
70a9de470cSBruce Richardson const uint64_t sc_start = rte_rdtsc_precise();
71a9de470cSBruce Richardson rte_compiler_barrier();
72a9de470cSBruce Richardson for (i = 0; i < iterations; i++) {
73a9de470cSBruce Richardson rte_ring_enqueue_bulk(r, &burst, 1, NULL);
74a9de470cSBruce Richardson rte_ring_dequeue_bulk(r, &burst, 1, NULL);
75a9de470cSBruce Richardson }
76a9de470cSBruce Richardson const uint64_t sc_end = rte_rdtsc_precise();
77a9de470cSBruce Richardson rte_compiler_barrier();
78a9de470cSBruce Richardson
79a9de470cSBruce Richardson const uint64_t eth_start = rte_rdtsc_precise();
80a9de470cSBruce Richardson rte_compiler_barrier();
81a9de470cSBruce Richardson for (i = 0; i < iterations; i++) {
82a9de470cSBruce Richardson rte_eth_tx_burst(ring_ethdev_port, 0, mburst, 1);
83a9de470cSBruce Richardson rte_eth_rx_burst(ring_ethdev_port, 0, mburst, 1);
84a9de470cSBruce Richardson }
85a9de470cSBruce Richardson const uint64_t eth_end = rte_rdtsc_precise();
86a9de470cSBruce Richardson rte_compiler_barrier();
87a9de470cSBruce Richardson
88a9de470cSBruce Richardson printf("Ring single enq/dequeue : %"PRIu64"\n",
89a9de470cSBruce Richardson (sc_end-sc_start) >> iter_shift);
90a9de470cSBruce Richardson printf("Ethdev single enq/dequeue: %"PRIu64"\n",
91a9de470cSBruce Richardson (eth_end-eth_start) >> iter_shift);
92a9de470cSBruce Richardson }
93a9de470cSBruce Richardson
94a9de470cSBruce Richardson /* Times enqueue and dequeue on a single lcore */
95a9de470cSBruce Richardson static void
test_bulk_enqueue_dequeue(void)96a9de470cSBruce Richardson test_bulk_enqueue_dequeue(void)
97a9de470cSBruce Richardson {
98a9de470cSBruce Richardson const unsigned iter_shift = 23;
99a9de470cSBruce Richardson const unsigned iterations = 1 << iter_shift;
100a9de470cSBruce Richardson unsigned sz, i = 0;
101a9de470cSBruce Richardson struct rte_mbuf *burst[MAX_BURST] = {0};
102a9de470cSBruce Richardson
10371bdd8a1SPavan Nikhilesh for (sz = 0; sz < RTE_DIM(bulk_sizes); sz++) {
104a9de470cSBruce Richardson const uint64_t sc_start = rte_rdtsc();
105a9de470cSBruce Richardson for (i = 0; i < iterations; i++) {
106a9de470cSBruce Richardson rte_ring_sp_enqueue_bulk(r, (void *)burst,
107a9de470cSBruce Richardson bulk_sizes[sz], NULL);
108a9de470cSBruce Richardson rte_ring_sc_dequeue_bulk(r, (void *)burst,
109a9de470cSBruce Richardson bulk_sizes[sz], NULL);
110a9de470cSBruce Richardson }
111a9de470cSBruce Richardson const uint64_t sc_end = rte_rdtsc();
112a9de470cSBruce Richardson
113a9de470cSBruce Richardson const uint64_t eth_start = rte_rdtsc_precise();
114a9de470cSBruce Richardson rte_compiler_barrier();
115a9de470cSBruce Richardson for (i = 0; i < iterations; i++) {
116a9de470cSBruce Richardson rte_eth_tx_burst(ring_ethdev_port, 0, burst, bulk_sizes[sz]);
117a9de470cSBruce Richardson rte_eth_rx_burst(ring_ethdev_port, 0, burst, bulk_sizes[sz]);
118a9de470cSBruce Richardson }
119a9de470cSBruce Richardson const uint64_t eth_end = rte_rdtsc_precise();
120a9de470cSBruce Richardson rte_compiler_barrier();
121a9de470cSBruce Richardson
122a9de470cSBruce Richardson double sc_avg = ((double)(sc_end-sc_start) /
123a9de470cSBruce Richardson (iterations * bulk_sizes[sz]));
124a9de470cSBruce Richardson double eth_avg = ((double)(eth_end-eth_start) /
125a9de470cSBruce Richardson (iterations * bulk_sizes[sz]));
126a9de470cSBruce Richardson
127a9de470cSBruce Richardson printf("ring bulk enq/deq (size: %u) : %.1F\n", bulk_sizes[sz],
128a9de470cSBruce Richardson sc_avg);
129a9de470cSBruce Richardson printf("ethdev bulk enq/deq (size:%u): %.1F\n", bulk_sizes[sz],
130a9de470cSBruce Richardson eth_avg);
131a9de470cSBruce Richardson
132a9de470cSBruce Richardson printf("\n");
133a9de470cSBruce Richardson }
134a9de470cSBruce Richardson }
135a9de470cSBruce Richardson
136a9de470cSBruce Richardson static int
test_ring_pmd_perf(void)137a9de470cSBruce Richardson test_ring_pmd_perf(void)
138a9de470cSBruce Richardson {
139a9de470cSBruce Richardson char name[RTE_ETH_NAME_MAX_LEN];
140a9de470cSBruce Richardson
141a9de470cSBruce Richardson r = rte_ring_create(RING_NAME, RING_SIZE, rte_socket_id(),
142a9de470cSBruce Richardson RING_F_SP_ENQ|RING_F_SC_DEQ);
143a9de470cSBruce Richardson if (r == NULL && (r = rte_ring_lookup(RING_NAME)) == NULL)
144a9de470cSBruce Richardson return -1;
145a9de470cSBruce Richardson
146a9de470cSBruce Richardson ring_ethdev_port = rte_eth_from_ring(r);
147a9de470cSBruce Richardson
148a9de470cSBruce Richardson printf("\n### Testing const single element enq/deq ###\n");
149a9de470cSBruce Richardson test_single_enqueue_dequeue();
150a9de470cSBruce Richardson
151a9de470cSBruce Richardson printf("\n### Testing empty dequeue ###\n");
152a9de470cSBruce Richardson test_empty_dequeue();
153a9de470cSBruce Richardson
154a9de470cSBruce Richardson printf("\n### Testing using a single lcore ###\n");
155a9de470cSBruce Richardson test_bulk_enqueue_dequeue();
156a9de470cSBruce Richardson
157a9de470cSBruce Richardson /* release port and ring resources */
1580ead65afSIvan Ilchenko if (rte_eth_dev_stop(ring_ethdev_port) != 0)
1590ead65afSIvan Ilchenko return -1;
160a9de470cSBruce Richardson rte_eth_dev_get_name_by_port(ring_ethdev_port, name);
161a9de470cSBruce Richardson rte_vdev_uninit(name);
162a9de470cSBruce Richardson rte_ring_free(r);
163a9de470cSBruce Richardson return 0;
164a9de470cSBruce Richardson }
165a9de470cSBruce Richardson
166*d83fb967SDavid Marchand REGISTER_PERF_TEST(ring_pmd_perf_autotest, test_ring_pmd_perf);
167