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_os_shim.h>
14 #include <rte_random.h>
15 #include <rte_malloc.h>
17 #include <rte_memcpy.h>
22 * Set this to the maximum buffer size you want to test. If it is 0, then the
23 * values in the buf_sizes[] array below will be used.
25 #define TEST_VALUE_RANGE 0
27 /* List of buffer sizes to test */
28 #if TEST_VALUE_RANGE == 0
29 static size_t buf_sizes[] = {
30 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128,
31 129, 191, 192, 193, 255, 256, 257, 319, 320, 321, 383, 384, 385, 447, 448,
32 449, 511, 512, 513, 767, 768, 769, 1023, 1024, 1025, 1518, 1522, 1536, 1600,
33 2048, 2560, 3072, 3584, 4096, 4608, 5120, 5632, 6144, 6656, 7168, 7680, 8192
35 /* MUST be as large as largest packet size above */
36 #define SMALL_BUFFER_SIZE 8192
37 #else /* TEST_VALUE_RANGE != 0 */
38 static size_t buf_sizes[TEST_VALUE_RANGE];
39 #define SMALL_BUFFER_SIZE TEST_VALUE_RANGE
40 #endif /* TEST_VALUE_RANGE == 0 */
44 * Arrays of this size are used for measuring uncached memory accesses by
45 * picking a random location within the buffer. Make this smaller if there are
46 * memory allocation errors.
48 #define LARGE_BUFFER_SIZE (100 * 1024 * 1024)
50 /* How many times to run timing loop for performance tests */
51 #define TEST_ITERATIONS 1000000
52 #define TEST_BATCH_SIZE 100
54 /* Data is aligned on this many bytes (power of 2) */
56 #define ALIGNMENT_UNIT 64
57 #elif defined __AVX2__
58 #define ALIGNMENT_UNIT 32
60 #define ALIGNMENT_UNIT 16
64 * Pointers used in performance tests. The two large buffers are for uncached
65 * access where random addresses within the buffer are used for each
66 * memcpy. The two small buffers are for cached access.
68 static uint8_t *large_buf_read, *large_buf_write;
69 static uint8_t *small_buf_read, *small_buf_write;
71 /* Initialise data buffers. */
77 large_buf_read = rte_malloc("memcpy", LARGE_BUFFER_SIZE + ALIGNMENT_UNIT, ALIGNMENT_UNIT);
78 if (large_buf_read == NULL)
79 goto error_large_buf_read;
81 large_buf_write = rte_malloc("memcpy", LARGE_BUFFER_SIZE + ALIGNMENT_UNIT, ALIGNMENT_UNIT);
82 if (large_buf_write == NULL)
83 goto error_large_buf_write;
85 small_buf_read = rte_malloc("memcpy", SMALL_BUFFER_SIZE + ALIGNMENT_UNIT, ALIGNMENT_UNIT);
86 if (small_buf_read == NULL)
87 goto error_small_buf_read;
89 small_buf_write = rte_malloc("memcpy", SMALL_BUFFER_SIZE + ALIGNMENT_UNIT, ALIGNMENT_UNIT);
90 if (small_buf_write == NULL)
91 goto error_small_buf_write;
93 for (i = 0; i < LARGE_BUFFER_SIZE; i++)
94 large_buf_read[i] = rte_rand();
95 for (i = 0; i < SMALL_BUFFER_SIZE; i++)
96 small_buf_read[i] = rte_rand();
100 error_small_buf_write:
101 rte_free(small_buf_read);
102 error_small_buf_read:
103 rte_free(large_buf_write);
104 error_large_buf_write:
105 rte_free(large_buf_read);
106 error_large_buf_read:
107 printf("ERROR: not enough memory\n");
111 /* Cleanup data buffers */
115 rte_free(large_buf_read);
116 rte_free(large_buf_write);
117 rte_free(small_buf_read);
118 rte_free(small_buf_write);
122 * Get a random offset into large array, with enough space needed to perform
123 * max copy size. Offset is aligned, uoffset is used for unalignment setting.
126 get_rand_offset(size_t uoffset)
128 return ((rte_rand() % (LARGE_BUFFER_SIZE - SMALL_BUFFER_SIZE)) &
129 ~(ALIGNMENT_UNIT - 1)) + uoffset;
132 /* Fill in source and destination addresses. */
134 fill_addr_arrays(size_t *dst_addr, int is_dst_cached, size_t dst_uoffset,
135 size_t *src_addr, int is_src_cached, size_t src_uoffset)
139 for (i = 0; i < TEST_BATCH_SIZE; i++) {
140 dst_addr[i] = (is_dst_cached) ? dst_uoffset : get_rand_offset(dst_uoffset);
141 src_addr[i] = (is_src_cached) ? src_uoffset : get_rand_offset(src_uoffset);
146 * WORKAROUND: For some reason the first test doing an uncached write
147 * takes a very long time (~25 times longer than is expected). So we do
148 * it once without timing.
151 do_uncached_write(uint8_t *dst, int is_dst_cached,
152 const uint8_t *src, int is_src_cached, size_t size)
155 size_t dst_addrs[TEST_BATCH_SIZE], src_addrs[TEST_BATCH_SIZE];
157 for (i = 0; i < (TEST_ITERATIONS / TEST_BATCH_SIZE); i++) {
158 fill_addr_arrays(dst_addrs, is_dst_cached, 0,
159 src_addrs, is_src_cached, 0);
160 for (j = 0; j < TEST_BATCH_SIZE; j++) {
161 rte_memcpy(dst+dst_addrs[j], src+src_addrs[j], size);
167 * Run a single memcpy performance test. This is a macro to ensure that if
168 * the "size" parameter is a constant it won't be converted to a variable.
170 #define SINGLE_PERF_TEST(dst, is_dst_cached, dst_uoffset, \
171 src, is_src_cached, src_uoffset, size) \
173 unsigned int iter, t; \
174 size_t dst_addrs[TEST_BATCH_SIZE], src_addrs[TEST_BATCH_SIZE]; \
175 uint64_t start_time, total_time = 0; \
176 uint64_t total_time2 = 0; \
177 for (iter = 0; iter < (TEST_ITERATIONS / TEST_BATCH_SIZE); iter++) { \
178 fill_addr_arrays(dst_addrs, is_dst_cached, dst_uoffset, \
179 src_addrs, is_src_cached, src_uoffset); \
180 start_time = rte_rdtsc(); \
181 for (t = 0; t < TEST_BATCH_SIZE; t++) \
182 rte_memcpy(dst+dst_addrs[t], src+src_addrs[t], size); \
183 total_time += rte_rdtsc() - start_time; \
185 for (iter = 0; iter < (TEST_ITERATIONS / TEST_BATCH_SIZE); iter++) { \
186 fill_addr_arrays(dst_addrs, is_dst_cached, dst_uoffset, \
187 src_addrs, is_src_cached, src_uoffset); \
188 start_time = rte_rdtsc(); \
189 for (t = 0; t < TEST_BATCH_SIZE; t++) \
190 memcpy(dst+dst_addrs[t], src+src_addrs[t], size); \
191 total_time2 += rte_rdtsc() - start_time; \
193 printf("%3.0f -", (double)total_time / TEST_ITERATIONS); \
194 printf("%3.0f", (double)total_time2 / TEST_ITERATIONS); \
195 printf("(%6.2f%%) ", ((double)total_time - total_time2)*100/total_time2); \
198 /* Run aligned memcpy tests for each cached/uncached permutation */
199 #define ALL_PERF_TESTS_FOR_SIZE(n) \
201 if (__builtin_constant_p(n)) \
202 printf("\nC%6u", (unsigned)n); \
204 printf("\n%7u", (unsigned)n); \
205 SINGLE_PERF_TEST(small_buf_write, 1, 0, small_buf_read, 1, 0, n); \
206 SINGLE_PERF_TEST(large_buf_write, 0, 0, small_buf_read, 1, 0, n); \
207 SINGLE_PERF_TEST(small_buf_write, 1, 0, large_buf_read, 0, 0, n); \
208 SINGLE_PERF_TEST(large_buf_write, 0, 0, large_buf_read, 0, 0, n); \
211 /* Run unaligned memcpy tests for each cached/uncached permutation */
212 #define ALL_PERF_TESTS_FOR_SIZE_UNALIGNED(n) \
214 if (__builtin_constant_p(n)) \
215 printf("\nC%6u", (unsigned)n); \
217 printf("\n%7u", (unsigned)n); \
218 SINGLE_PERF_TEST(small_buf_write, 1, 1, small_buf_read, 1, 5, n); \
219 SINGLE_PERF_TEST(large_buf_write, 0, 1, small_buf_read, 1, 5, n); \
220 SINGLE_PERF_TEST(small_buf_write, 1, 1, large_buf_read, 0, 5, n); \
221 SINGLE_PERF_TEST(large_buf_write, 0, 1, large_buf_read, 0, 5, n); \
224 /* Run memcpy tests for constant length */
225 #define ALL_PERF_TEST_FOR_CONSTANT \
227 TEST_CONSTANT(6U); TEST_CONSTANT(64U); TEST_CONSTANT(128U); \
228 TEST_CONSTANT(192U); TEST_CONSTANT(256U); TEST_CONSTANT(512U); \
229 TEST_CONSTANT(768U); TEST_CONSTANT(1024U); TEST_CONSTANT(1536U); \
232 /* Run all memcpy tests for aligned constant cases */
234 perf_test_constant_aligned(void)
236 #define TEST_CONSTANT ALL_PERF_TESTS_FOR_SIZE
237 ALL_PERF_TEST_FOR_CONSTANT;
241 /* Run all memcpy tests for unaligned constant cases */
243 perf_test_constant_unaligned(void)
245 #define TEST_CONSTANT ALL_PERF_TESTS_FOR_SIZE_UNALIGNED
246 ALL_PERF_TEST_FOR_CONSTANT;
250 /* Run all memcpy tests for aligned variable cases */
252 perf_test_variable_aligned(void)
255 for (i = 0; i < RTE_DIM(buf_sizes); i++) {
256 ALL_PERF_TESTS_FOR_SIZE((size_t)buf_sizes[i]);
260 /* Run all memcpy tests for unaligned variable cases */
262 perf_test_variable_unaligned(void)
265 for (i = 0; i < RTE_DIM(buf_sizes); i++) {
266 ALL_PERF_TESTS_FOR_SIZE_UNALIGNED((size_t)buf_sizes[i]);
270 /* Run all memcpy tests */
275 struct timespec tv_begin, tv_end;
276 double time_aligned, time_unaligned;
277 double time_aligned_const, time_unaligned_const;
279 ret = init_buffers();
283 #if TEST_VALUE_RANGE != 0
284 /* Set up buf_sizes array, if required */
286 for (i = 0; i < TEST_VALUE_RANGE; i++)
290 /* See function comment */
291 do_uncached_write(large_buf_write, 0, small_buf_read, 1, SMALL_BUFFER_SIZE);
293 printf("\n** rte_memcpy() - memcpy perf. tests (C = compile-time constant) **\n"
294 "======= ================= ================= ================= =================\n"
295 " Size Cache to cache Cache to mem Mem to cache Mem to mem\n"
296 "(bytes) (ticks) (ticks) (ticks) (ticks)\n"
297 "------- ----------------- ----------------- ----------------- -----------------");
299 printf("\n================================= %2dB aligned =================================",
301 /* Do aligned tests where size is a variable */
302 timespec_get(&tv_begin, TIME_UTC);
303 perf_test_variable_aligned();
304 timespec_get(&tv_end, TIME_UTC);
305 time_aligned = (double)(tv_end.tv_sec - tv_begin.tv_sec)
306 + ((double)tv_end.tv_nsec - tv_begin.tv_nsec) / NS_PER_S;
307 printf("\n------- ----------------- ----------------- ----------------- -----------------");
308 /* Do aligned tests where size is a compile-time constant */
309 timespec_get(&tv_begin, TIME_UTC);
310 perf_test_constant_aligned();
311 timespec_get(&tv_end, TIME_UTC);
312 time_aligned_const = (double)(tv_end.tv_sec - tv_begin.tv_sec)
313 + ((double)tv_end.tv_nsec - tv_begin.tv_nsec) / NS_PER_S;
314 printf("\n================================== Unaligned ==================================");
315 /* Do unaligned tests where size is a variable */
316 timespec_get(&tv_begin, TIME_UTC);
317 perf_test_variable_unaligned();
318 timespec_get(&tv_end, TIME_UTC);
319 time_unaligned = (double)(tv_end.tv_sec - tv_begin.tv_sec)
320 + ((double)tv_end.tv_nsec - tv_begin.tv_nsec) / NS_PER_S;
321 printf("\n------- ----------------- ----------------- ----------------- -----------------");
322 /* Do unaligned tests where size is a compile-time constant */
323 timespec_get(&tv_begin, TIME_UTC);
324 perf_test_constant_unaligned();
325 timespec_get(&tv_end, TIME_UTC);
326 time_unaligned_const = (double)(tv_end.tv_sec - tv_begin.tv_sec)
327 + ((double)tv_end.tv_nsec - tv_begin.tv_nsec) / NS_PER_S;
328 printf("\n======= ================= ================= ================= =================\n\n");
330 printf("Test Execution Time (seconds):\n");
331 printf("Aligned variable copy size = %8.3f\n", time_aligned);
332 printf("Aligned constant copy size = %8.3f\n", time_aligned_const);
333 printf("Unaligned variable copy size = %8.3f\n", time_unaligned);
334 printf("Unaligned constant copy size = %8.3f\n", time_unaligned_const);
341 test_memcpy_perf(void)
351 REGISTER_TEST_COMMAND(memcpy_perf_autotest, test_memcpy_perf);