1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2017 Intel Corporation 3 */ 4 5 #include "test.h" 6 7 #include <unistd.h> 8 #include <string.h> 9 #include <rte_mempool.h> 10 #include <rte_cycles.h> 11 #include <rte_common.h> 12 #include <rte_mbuf.h> 13 #include <rte_distributor.h> 14 #include <rte_pause.h> 15 16 #define ITER_POWER_CL 25 /* log 2 of how many iterations for Cache Line test */ 17 #define ITER_POWER 21 /* log 2 of how many iterations we do when timing. */ 18 #define BURST 64 19 #define BIG_BATCH 1024 20 21 /* static vars - zero initialized by default */ 22 static volatile int quit; 23 static volatile unsigned worker_idx; 24 25 struct worker_stats { 26 volatile unsigned handled_packets; 27 } __rte_cache_aligned; 28 struct worker_stats worker_stats[RTE_MAX_LCORE]; 29 30 /* 31 * worker thread used for testing the time to do a round-trip of a cache 32 * line between two cores and back again 33 */ 34 static int 35 flip_bit(volatile uint64_t *arg) 36 { 37 uint64_t old_val = 0; 38 while (old_val != 2) { 39 while (!*arg) 40 rte_pause(); 41 old_val = *arg; 42 *arg = 0; 43 } 44 return 0; 45 } 46 47 /* 48 * test case to time the number of cycles to round-trip a cache line between 49 * two cores and back again. 50 */ 51 static void 52 time_cache_line_switch(void) 53 { 54 /* allocate a full cache line for data, we use only first byte of it */ 55 uint64_t data[RTE_CACHE_LINE_SIZE*3 / sizeof(uint64_t)]; 56 57 unsigned i, slaveid = rte_get_next_lcore(rte_lcore_id(), 0, 0); 58 volatile uint64_t *pdata = &data[0]; 59 *pdata = 1; 60 rte_eal_remote_launch((lcore_function_t *)flip_bit, &data[0], slaveid); 61 while (*pdata) 62 rte_pause(); 63 64 const uint64_t start_time = rte_rdtsc(); 65 for (i = 0; i < (1 << ITER_POWER_CL); i++) { 66 while (*pdata) 67 rte_pause(); 68 *pdata = 1; 69 } 70 const uint64_t end_time = rte_rdtsc(); 71 72 while (*pdata) 73 rte_pause(); 74 *pdata = 2; 75 rte_eal_wait_lcore(slaveid); 76 printf("==== Cache line switch test ===\n"); 77 printf("Time for %u iterations = %"PRIu64" ticks\n", (1<<ITER_POWER_CL), 78 end_time-start_time); 79 printf("Ticks per iteration = %"PRIu64"\n\n", 80 (end_time-start_time) >> ITER_POWER_CL); 81 } 82 83 /* 84 * returns the total count of the number of packets handled by the worker 85 * functions given below. 86 */ 87 static unsigned 88 total_packet_count(void) 89 { 90 unsigned i, count = 0; 91 for (i = 0; i < worker_idx; i++) 92 count += worker_stats[i].handled_packets; 93 return count; 94 } 95 96 /* resets the packet counts for a new test */ 97 static void 98 clear_packet_count(void) 99 { 100 memset(&worker_stats, 0, sizeof(worker_stats)); 101 } 102 103 /* 104 * This is the basic worker function for performance tests. 105 * it does nothing but return packets and count them. 106 */ 107 static int 108 handle_work(void *arg) 109 { 110 struct rte_distributor *d = arg; 111 unsigned int count = 0; 112 unsigned int num = 0; 113 int i; 114 unsigned int id = __atomic_fetch_add(&worker_idx, 1, __ATOMIC_RELAXED); 115 struct rte_mbuf *buf[8] __rte_cache_aligned; 116 117 for (i = 0; i < 8; i++) 118 buf[i] = NULL; 119 120 num = rte_distributor_get_pkt(d, id, buf, buf, num); 121 while (!quit) { 122 worker_stats[id].handled_packets += num; 123 count += num; 124 num = rte_distributor_get_pkt(d, id, buf, buf, num); 125 } 126 worker_stats[id].handled_packets += num; 127 count += num; 128 rte_distributor_return_pkt(d, id, buf, num); 129 return 0; 130 } 131 132 /* 133 * This basic performance test just repeatedly sends in 32 packets at a time 134 * to the distributor and verifies at the end that we got them all in the worker 135 * threads and finally how long per packet the processing took. 136 */ 137 static inline int 138 perf_test(struct rte_distributor *d, struct rte_mempool *p) 139 { 140 unsigned int i; 141 uint64_t start, end; 142 struct rte_mbuf *bufs[BURST]; 143 144 clear_packet_count(); 145 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) { 146 printf("Error getting mbufs from pool\n"); 147 return -1; 148 } 149 /* ensure we have different hash value for each pkt */ 150 for (i = 0; i < BURST; i++) 151 bufs[i]->hash.usr = i; 152 153 start = rte_rdtsc(); 154 for (i = 0; i < (1<<ITER_POWER); i++) 155 rte_distributor_process(d, bufs, BURST); 156 end = rte_rdtsc(); 157 158 do { 159 usleep(100); 160 rte_distributor_process(d, NULL, 0); 161 } while (total_packet_count() < (BURST << ITER_POWER)); 162 163 rte_distributor_clear_returns(d); 164 165 printf("Time per burst: %"PRIu64"\n", (end - start) >> ITER_POWER); 166 printf("Time per packet: %"PRIu64"\n\n", 167 ((end - start) >> ITER_POWER)/BURST); 168 rte_mempool_put_bulk(p, (void *)bufs, BURST); 169 170 for (i = 0; i < rte_lcore_count() - 1; i++) 171 printf("Worker %u handled %u packets\n", i, 172 worker_stats[i].handled_packets); 173 printf("Total packets: %u (%x)\n", total_packet_count(), 174 total_packet_count()); 175 printf("=== Perf test done ===\n\n"); 176 177 return 0; 178 } 179 180 /* Useful function which ensures that all worker functions terminate */ 181 static void 182 quit_workers(struct rte_distributor *d, struct rte_mempool *p) 183 { 184 const unsigned int num_workers = rte_lcore_count() - 1; 185 unsigned int i; 186 struct rte_mbuf *bufs[RTE_MAX_LCORE]; 187 188 rte_mempool_get_bulk(p, (void *)bufs, num_workers); 189 190 quit = 1; 191 for (i = 0; i < num_workers; i++) 192 bufs[i]->hash.usr = i << 1; 193 rte_distributor_process(d, bufs, num_workers); 194 195 rte_mempool_put_bulk(p, (void *)bufs, num_workers); 196 197 rte_distributor_process(d, NULL, 0); 198 rte_eal_mp_wait_lcore(); 199 quit = 0; 200 worker_idx = 0; 201 } 202 203 static int 204 test_distributor_perf(void) 205 { 206 static struct rte_distributor *ds; 207 static struct rte_distributor *db; 208 static struct rte_mempool *p; 209 210 if (rte_lcore_count() < 2) { 211 printf("Not enough cores for distributor_perf_autotest, expecting at least 2\n"); 212 return TEST_SKIPPED; 213 } 214 215 /* first time how long it takes to round-trip a cache line */ 216 time_cache_line_switch(); 217 218 if (ds == NULL) { 219 ds = rte_distributor_create("Test_perf", rte_socket_id(), 220 rte_lcore_count() - 1, 221 RTE_DIST_ALG_SINGLE); 222 if (ds == NULL) { 223 printf("Error creating distributor\n"); 224 return -1; 225 } 226 } else { 227 rte_distributor_clear_returns(ds); 228 } 229 230 if (db == NULL) { 231 db = rte_distributor_create("Test_burst", rte_socket_id(), 232 rte_lcore_count() - 1, 233 RTE_DIST_ALG_BURST); 234 if (db == NULL) { 235 printf("Error creating burst distributor\n"); 236 return -1; 237 } 238 } else { 239 rte_distributor_clear_returns(db); 240 } 241 242 const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ? 243 (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count()); 244 if (p == NULL) { 245 p = rte_pktmbuf_pool_create("DPT_MBUF_POOL", nb_bufs, BURST, 246 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); 247 if (p == NULL) { 248 printf("Error creating mempool\n"); 249 return -1; 250 } 251 } 252 253 printf("=== Performance test of distributor (single mode) ===\n"); 254 rte_eal_mp_remote_launch(handle_work, ds, SKIP_MASTER); 255 if (perf_test(ds, p) < 0) 256 return -1; 257 quit_workers(ds, p); 258 259 printf("=== Performance test of distributor (burst mode) ===\n"); 260 rte_eal_mp_remote_launch(handle_work, db, SKIP_MASTER); 261 if (perf_test(db, p) < 0) 262 return -1; 263 quit_workers(db, p); 264 265 return 0; 266 } 267 268 REGISTER_TEST_COMMAND(distributor_perf_autotest, test_distributor_perf); 269