1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <rte_common.h>
12 #include <rte_cycles.h>
13 #include <rte_random.h>
14 #include <rte_malloc.h>
16 #include <rte_memcpy.h>
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.
24 #define TEST_VALUE_RANGE 0
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
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 */
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.
47 #define LARGE_BUFFER_SIZE (100 * 1024 * 1024)
49 /* How many times to run timing loop for performance tests */
50 #define TEST_ITERATIONS 1000000
51 #define TEST_BATCH_SIZE 100
53 /* Data is aligned on this many bytes (power of 2) */
54 #ifdef RTE_MACHINE_CPUFLAG_AVX512F
55 #define ALIGNMENT_UNIT 64
56 #elif defined RTE_MACHINE_CPUFLAG_AVX2
57 #define ALIGNMENT_UNIT 32
58 #else /* RTE_MACHINE_CPUFLAG */
59 #define ALIGNMENT_UNIT 16
60 #endif /* RTE_MACHINE_CPUFLAG */
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.
67 static uint8_t *large_buf_read, *large_buf_write;
68 static uint8_t *small_buf_read, *small_buf_write;
70 /* Initialise data buffers. */
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;
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;
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;
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;
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();
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");
110 /* Cleanup data buffers */
114 rte_free(large_buf_read);
115 rte_free(large_buf_write);
116 rte_free(small_buf_read);
117 rte_free(small_buf_write);
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.
125 get_rand_offset(size_t uoffset)
127 return ((rte_rand() % (LARGE_BUFFER_SIZE - SMALL_BUFFER_SIZE)) &
128 ~(ALIGNMENT_UNIT - 1)) + uoffset;
131 /* Fill in source and destination addresses. */
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)
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);
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.
150 do_uncached_write(uint8_t *dst, int is_dst_cached,
151 const uint8_t *src, int is_src_cached, size_t size)
154 size_t dst_addrs[TEST_BATCH_SIZE], src_addrs[TEST_BATCH_SIZE];
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);
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.
169 #define SINGLE_PERF_TEST(dst, is_dst_cached, dst_uoffset, \
170 src, is_src_cached, src_uoffset, size) \
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; \
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; \
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); \
197 /* Run aligned memcpy tests for each cached/uncached permutation */
198 #define ALL_PERF_TESTS_FOR_SIZE(n) \
200 if (__builtin_constant_p(n)) \
201 printf("\nC%6u", (unsigned)n); \
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); \
210 /* Run unaligned memcpy tests for each cached/uncached permutation */
211 #define ALL_PERF_TESTS_FOR_SIZE_UNALIGNED(n) \
213 if (__builtin_constant_p(n)) \
214 printf("\nC%6u", (unsigned)n); \
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); \
223 /* Run memcpy tests for constant length */
224 #define ALL_PERF_TEST_FOR_CONSTANT \
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); \
231 /* Run all memcpy tests for aligned constant cases */
233 perf_test_constant_aligned(void)
235 #define TEST_CONSTANT ALL_PERF_TESTS_FOR_SIZE
236 ALL_PERF_TEST_FOR_CONSTANT;
240 /* Run all memcpy tests for unaligned constant cases */
242 perf_test_constant_unaligned(void)
244 #define TEST_CONSTANT ALL_PERF_TESTS_FOR_SIZE_UNALIGNED
245 ALL_PERF_TEST_FOR_CONSTANT;
249 /* Run all memcpy tests for aligned variable cases */
251 perf_test_variable_aligned(void)
254 for (i = 0; i < RTE_DIM(buf_sizes); i++) {
255 ALL_PERF_TESTS_FOR_SIZE((size_t)buf_sizes[i]);
259 /* Run all memcpy tests for unaligned variable cases */
261 perf_test_variable_unaligned(void)
264 for (i = 0; i < RTE_DIM(buf_sizes); i++) {
265 ALL_PERF_TESTS_FOR_SIZE_UNALIGNED((size_t)buf_sizes[i]);
269 /* Run all memcpy tests */
274 struct timeval tv_begin, tv_end;
275 double time_aligned, time_unaligned;
276 double time_aligned_const, time_unaligned_const;
278 ret = init_buffers();
282 #if TEST_VALUE_RANGE != 0
283 /* Set up buf_sizes array, if required */
285 for (i = 0; i < TEST_VALUE_RANGE; i++)
289 /* See function comment */
290 do_uncached_write(large_buf_write, 0, small_buf_read, 1, SMALL_BUFFER_SIZE);
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 "------- ----------------- ----------------- ----------------- -----------------");
298 printf("\n================================= %2dB aligned =================================",
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");
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);
340 test_memcpy_perf(void)
350 REGISTER_TEST_COMMAND(memcpy_perf_autotest, test_memcpy_perf);