1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation 3 */ 4 5 #include <stdint.h> 6 #include <stdio.h> 7 #include <string.h> 8 #include <stdlib.h> 9 #include <sys/time.h> 10 11 #include <rte_common.h> 12 #include <rte_cycles.h> 13 #include <rte_random.h> 14 #include <rte_malloc.h> 15 16 #include <rte_memcpy.h> 17 18 #include "test.h" 19 20 /* 21 * Set this to the maximum buffer size you want to test. If it is 0, then the 22 * values in the buf_sizes[] array below will be used. 23 */ 24 #define TEST_VALUE_RANGE 0 25 26 /* List of buffer sizes to test */ 27 #if TEST_VALUE_RANGE == 0 28 static size_t buf_sizes[] = { 29 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128, 30 129, 191, 192, 193, 255, 256, 257, 319, 320, 321, 383, 384, 385, 447, 448, 31 449, 511, 512, 513, 767, 768, 769, 1023, 1024, 1025, 1518, 1522, 1536, 1600, 32 2048, 2560, 3072, 3584, 4096, 4608, 5120, 5632, 6144, 6656, 7168, 7680, 8192 33 }; 34 /* MUST be as large as largest packet size above */ 35 #define SMALL_BUFFER_SIZE 8192 36 #else /* TEST_VALUE_RANGE != 0 */ 37 static size_t buf_sizes[TEST_VALUE_RANGE]; 38 #define SMALL_BUFFER_SIZE TEST_VALUE_RANGE 39 #endif /* TEST_VALUE_RANGE == 0 */ 40 41 42 /* 43 * Arrays of this size are used for measuring uncached memory accesses by 44 * picking a random location within the buffer. Make this smaller if there are 45 * memory allocation errors. 46 */ 47 #define LARGE_BUFFER_SIZE (100 * 1024 * 1024) 48 49 /* How many times to run timing loop for performance tests */ 50 #define TEST_ITERATIONS 1000000 51 #define TEST_BATCH_SIZE 100 52 53 /* Data is aligned on this many bytes (power of 2) */ 54 #ifdef __AVX512F__ 55 #define ALIGNMENT_UNIT 64 56 #elif defined __AVX2__ 57 #define ALIGNMENT_UNIT 32 58 #else 59 #define ALIGNMENT_UNIT 16 60 #endif 61 62 /* 63 * Pointers used in performance tests. The two large buffers are for uncached 64 * access where random addresses within the buffer are used for each 65 * memcpy. The two small buffers are for cached access. 66 */ 67 static uint8_t *large_buf_read, *large_buf_write; 68 static uint8_t *small_buf_read, *small_buf_write; 69 70 /* Initialise data buffers. */ 71 static int 72 init_buffers(void) 73 { 74 unsigned i; 75 76 large_buf_read = rte_malloc("memcpy", LARGE_BUFFER_SIZE + ALIGNMENT_UNIT, ALIGNMENT_UNIT); 77 if (large_buf_read == NULL) 78 goto error_large_buf_read; 79 80 large_buf_write = rte_malloc("memcpy", LARGE_BUFFER_SIZE + ALIGNMENT_UNIT, ALIGNMENT_UNIT); 81 if (large_buf_write == NULL) 82 goto error_large_buf_write; 83 84 small_buf_read = rte_malloc("memcpy", SMALL_BUFFER_SIZE + ALIGNMENT_UNIT, ALIGNMENT_UNIT); 85 if (small_buf_read == NULL) 86 goto error_small_buf_read; 87 88 small_buf_write = rte_malloc("memcpy", SMALL_BUFFER_SIZE + ALIGNMENT_UNIT, ALIGNMENT_UNIT); 89 if (small_buf_write == NULL) 90 goto error_small_buf_write; 91 92 for (i = 0; i < LARGE_BUFFER_SIZE; i++) 93 large_buf_read[i] = rte_rand(); 94 for (i = 0; i < SMALL_BUFFER_SIZE; i++) 95 small_buf_read[i] = rte_rand(); 96 97 return 0; 98 99 error_small_buf_write: 100 rte_free(small_buf_read); 101 error_small_buf_read: 102 rte_free(large_buf_write); 103 error_large_buf_write: 104 rte_free(large_buf_read); 105 error_large_buf_read: 106 printf("ERROR: not enough memory\n"); 107 return -1; 108 } 109 110 /* Cleanup data buffers */ 111 static void 112 free_buffers(void) 113 { 114 rte_free(large_buf_read); 115 rte_free(large_buf_write); 116 rte_free(small_buf_read); 117 rte_free(small_buf_write); 118 } 119 120 /* 121 * Get a random offset into large array, with enough space needed to perform 122 * max copy size. Offset is aligned, uoffset is used for unalignment setting. 123 */ 124 static inline size_t 125 get_rand_offset(size_t uoffset) 126 { 127 return ((rte_rand() % (LARGE_BUFFER_SIZE - SMALL_BUFFER_SIZE)) & 128 ~(ALIGNMENT_UNIT - 1)) + uoffset; 129 } 130 131 /* Fill in source and destination addresses. */ 132 static inline void 133 fill_addr_arrays(size_t *dst_addr, int is_dst_cached, size_t dst_uoffset, 134 size_t *src_addr, int is_src_cached, size_t src_uoffset) 135 { 136 unsigned int i; 137 138 for (i = 0; i < TEST_BATCH_SIZE; i++) { 139 dst_addr[i] = (is_dst_cached) ? dst_uoffset : get_rand_offset(dst_uoffset); 140 src_addr[i] = (is_src_cached) ? src_uoffset : get_rand_offset(src_uoffset); 141 } 142 } 143 144 /* 145 * WORKAROUND: For some reason the first test doing an uncached write 146 * takes a very long time (~25 times longer than is expected). So we do 147 * it once without timing. 148 */ 149 static void 150 do_uncached_write(uint8_t *dst, int is_dst_cached, 151 const uint8_t *src, int is_src_cached, size_t size) 152 { 153 unsigned i, j; 154 size_t dst_addrs[TEST_BATCH_SIZE], src_addrs[TEST_BATCH_SIZE]; 155 156 for (i = 0; i < (TEST_ITERATIONS / TEST_BATCH_SIZE); i++) { 157 fill_addr_arrays(dst_addrs, is_dst_cached, 0, 158 src_addrs, is_src_cached, 0); 159 for (j = 0; j < TEST_BATCH_SIZE; j++) { 160 rte_memcpy(dst+dst_addrs[j], src+src_addrs[j], size); 161 } 162 } 163 } 164 165 /* 166 * Run a single memcpy performance test. This is a macro to ensure that if 167 * the "size" parameter is a constant it won't be converted to a variable. 168 */ 169 #define SINGLE_PERF_TEST(dst, is_dst_cached, dst_uoffset, \ 170 src, is_src_cached, src_uoffset, size) \ 171 do { \ 172 unsigned int iter, t; \ 173 size_t dst_addrs[TEST_BATCH_SIZE], src_addrs[TEST_BATCH_SIZE]; \ 174 uint64_t start_time, total_time = 0; \ 175 uint64_t total_time2 = 0; \ 176 for (iter = 0; iter < (TEST_ITERATIONS / TEST_BATCH_SIZE); iter++) { \ 177 fill_addr_arrays(dst_addrs, is_dst_cached, dst_uoffset, \ 178 src_addrs, is_src_cached, src_uoffset); \ 179 start_time = rte_rdtsc(); \ 180 for (t = 0; t < TEST_BATCH_SIZE; t++) \ 181 rte_memcpy(dst+dst_addrs[t], src+src_addrs[t], size); \ 182 total_time += rte_rdtsc() - start_time; \ 183 } \ 184 for (iter = 0; iter < (TEST_ITERATIONS / TEST_BATCH_SIZE); iter++) { \ 185 fill_addr_arrays(dst_addrs, is_dst_cached, dst_uoffset, \ 186 src_addrs, is_src_cached, src_uoffset); \ 187 start_time = rte_rdtsc(); \ 188 for (t = 0; t < TEST_BATCH_SIZE; t++) \ 189 memcpy(dst+dst_addrs[t], src+src_addrs[t], size); \ 190 total_time2 += rte_rdtsc() - start_time; \ 191 } \ 192 printf("%3.0f -", (double)total_time / TEST_ITERATIONS); \ 193 printf("%3.0f", (double)total_time2 / TEST_ITERATIONS); \ 194 printf("(%6.2f%%) ", ((double)total_time - total_time2)*100/total_time2); \ 195 } while (0) 196 197 /* Run aligned memcpy tests for each cached/uncached permutation */ 198 #define ALL_PERF_TESTS_FOR_SIZE(n) \ 199 do { \ 200 if (__builtin_constant_p(n)) \ 201 printf("\nC%6u", (unsigned)n); \ 202 else \ 203 printf("\n%7u", (unsigned)n); \ 204 SINGLE_PERF_TEST(small_buf_write, 1, 0, small_buf_read, 1, 0, n); \ 205 SINGLE_PERF_TEST(large_buf_write, 0, 0, small_buf_read, 1, 0, n); \ 206 SINGLE_PERF_TEST(small_buf_write, 1, 0, large_buf_read, 0, 0, n); \ 207 SINGLE_PERF_TEST(large_buf_write, 0, 0, large_buf_read, 0, 0, n); \ 208 } while (0) 209 210 /* Run unaligned memcpy tests for each cached/uncached permutation */ 211 #define ALL_PERF_TESTS_FOR_SIZE_UNALIGNED(n) \ 212 do { \ 213 if (__builtin_constant_p(n)) \ 214 printf("\nC%6u", (unsigned)n); \ 215 else \ 216 printf("\n%7u", (unsigned)n); \ 217 SINGLE_PERF_TEST(small_buf_write, 1, 1, small_buf_read, 1, 5, n); \ 218 SINGLE_PERF_TEST(large_buf_write, 0, 1, small_buf_read, 1, 5, n); \ 219 SINGLE_PERF_TEST(small_buf_write, 1, 1, large_buf_read, 0, 5, n); \ 220 SINGLE_PERF_TEST(large_buf_write, 0, 1, large_buf_read, 0, 5, n); \ 221 } while (0) 222 223 /* Run memcpy tests for constant length */ 224 #define ALL_PERF_TEST_FOR_CONSTANT \ 225 do { \ 226 TEST_CONSTANT(6U); TEST_CONSTANT(64U); TEST_CONSTANT(128U); \ 227 TEST_CONSTANT(192U); TEST_CONSTANT(256U); TEST_CONSTANT(512U); \ 228 TEST_CONSTANT(768U); TEST_CONSTANT(1024U); TEST_CONSTANT(1536U); \ 229 } while (0) 230 231 /* Run all memcpy tests for aligned constant cases */ 232 static inline void 233 perf_test_constant_aligned(void) 234 { 235 #define TEST_CONSTANT ALL_PERF_TESTS_FOR_SIZE 236 ALL_PERF_TEST_FOR_CONSTANT; 237 #undef TEST_CONSTANT 238 } 239 240 /* Run all memcpy tests for unaligned constant cases */ 241 static inline void 242 perf_test_constant_unaligned(void) 243 { 244 #define TEST_CONSTANT ALL_PERF_TESTS_FOR_SIZE_UNALIGNED 245 ALL_PERF_TEST_FOR_CONSTANT; 246 #undef TEST_CONSTANT 247 } 248 249 /* Run all memcpy tests for aligned variable cases */ 250 static inline void 251 perf_test_variable_aligned(void) 252 { 253 unsigned i; 254 for (i = 0; i < RTE_DIM(buf_sizes); i++) { 255 ALL_PERF_TESTS_FOR_SIZE((size_t)buf_sizes[i]); 256 } 257 } 258 259 /* Run all memcpy tests for unaligned variable cases */ 260 static inline void 261 perf_test_variable_unaligned(void) 262 { 263 unsigned i; 264 for (i = 0; i < RTE_DIM(buf_sizes); i++) { 265 ALL_PERF_TESTS_FOR_SIZE_UNALIGNED((size_t)buf_sizes[i]); 266 } 267 } 268 269 /* Run all memcpy tests */ 270 static int 271 perf_test(void) 272 { 273 int ret; 274 struct timeval tv_begin, tv_end; 275 double time_aligned, time_unaligned; 276 double time_aligned_const, time_unaligned_const; 277 278 ret = init_buffers(); 279 if (ret != 0) 280 return ret; 281 282 #if TEST_VALUE_RANGE != 0 283 /* Set up buf_sizes array, if required */ 284 unsigned i; 285 for (i = 0; i < TEST_VALUE_RANGE; i++) 286 buf_sizes[i] = i; 287 #endif 288 289 /* See function comment */ 290 do_uncached_write(large_buf_write, 0, small_buf_read, 1, SMALL_BUFFER_SIZE); 291 292 printf("\n** rte_memcpy() - memcpy perf. tests (C = compile-time constant) **\n" 293 "======= ================= ================= ================= =================\n" 294 " Size Cache to cache Cache to mem Mem to cache Mem to mem\n" 295 "(bytes) (ticks) (ticks) (ticks) (ticks)\n" 296 "------- ----------------- ----------------- ----------------- -----------------"); 297 298 printf("\n================================= %2dB aligned =================================", 299 ALIGNMENT_UNIT); 300 /* Do aligned tests where size is a variable */ 301 gettimeofday(&tv_begin, NULL); 302 perf_test_variable_aligned(); 303 gettimeofday(&tv_end, NULL); 304 time_aligned = (double)(tv_end.tv_sec - tv_begin.tv_sec) 305 + ((double)tv_end.tv_usec - tv_begin.tv_usec)/1000000; 306 printf("\n------- ----------------- ----------------- ----------------- -----------------"); 307 /* Do aligned tests where size is a compile-time constant */ 308 gettimeofday(&tv_begin, NULL); 309 perf_test_constant_aligned(); 310 gettimeofday(&tv_end, NULL); 311 time_aligned_const = (double)(tv_end.tv_sec - tv_begin.tv_sec) 312 + ((double)tv_end.tv_usec - tv_begin.tv_usec)/1000000; 313 printf("\n================================== Unaligned =================================="); 314 /* Do unaligned tests where size is a variable */ 315 gettimeofday(&tv_begin, NULL); 316 perf_test_variable_unaligned(); 317 gettimeofday(&tv_end, NULL); 318 time_unaligned = (double)(tv_end.tv_sec - tv_begin.tv_sec) 319 + ((double)tv_end.tv_usec - tv_begin.tv_usec)/1000000; 320 printf("\n------- ----------------- ----------------- ----------------- -----------------"); 321 /* Do unaligned tests where size is a compile-time constant */ 322 gettimeofday(&tv_begin, NULL); 323 perf_test_constant_unaligned(); 324 gettimeofday(&tv_end, NULL); 325 time_unaligned_const = (double)(tv_end.tv_sec - tv_begin.tv_sec) 326 + ((double)tv_end.tv_usec - tv_begin.tv_usec)/1000000; 327 printf("\n======= ================= ================= ================= =================\n\n"); 328 329 printf("Test Execution Time (seconds):\n"); 330 printf("Aligned variable copy size = %8.3f\n", time_aligned); 331 printf("Aligned constant copy size = %8.3f\n", time_aligned_const); 332 printf("Unaligned variable copy size = %8.3f\n", time_unaligned); 333 printf("Unaligned constant copy size = %8.3f\n", time_unaligned_const); 334 free_buffers(); 335 336 return 0; 337 } 338 339 static int 340 test_memcpy_perf(void) 341 { 342 int ret; 343 344 ret = perf_test(); 345 if (ret != 0) 346 return -1; 347 return 0; 348 } 349 350 REGISTER_TEST_COMMAND(memcpy_perf_autotest, test_memcpy_perf); 351