xref: /dpdk/app/test/test_memcpy_perf.c (revision e0a8442ccd15bafbb7eb150c35331c8e3b828c53)
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 <time.h>
10 
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>
16 
17 #include <rte_memcpy.h>
18 
19 #include "test.h"
20 
21 /*
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.
24  */
25 #define TEST_VALUE_RANGE        0
26 
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
34 };
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 */
41 
42 
43 /*
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.
47  */
48 #define LARGE_BUFFER_SIZE       (100 * 1024 * 1024)
49 
50 /* How many times to run timing loop for performance tests */
51 #define TEST_ITERATIONS         1000000
52 #define TEST_BATCH_SIZE         100
53 
54 /* Data is aligned on this many bytes (power of 2) */
55 #ifdef __AVX512F__
56 #define ALIGNMENT_UNIT          64
57 #elif defined __AVX2__
58 #define ALIGNMENT_UNIT          32
59 #else
60 #define ALIGNMENT_UNIT          16
61 #endif
62 
63 /*
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.
67  */
68 static uint8_t *large_buf_read, *large_buf_write;
69 static uint8_t *small_buf_read, *small_buf_write;
70 
71 /* Initialise data buffers. */
72 static int
init_buffers(void)73 init_buffers(void)
74 {
75 	unsigned i;
76 
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;
80 
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;
84 
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;
88 
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;
92 
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();
97 
98 	return 0;
99 
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");
108 	return -1;
109 }
110 
111 /* Cleanup data buffers */
112 static void
free_buffers(void)113 free_buffers(void)
114 {
115 	rte_free(large_buf_read);
116 	rte_free(large_buf_write);
117 	rte_free(small_buf_read);
118 	rte_free(small_buf_write);
119 }
120 
121 /*
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.
124  */
125 static inline size_t
get_rand_offset(size_t uoffset)126 get_rand_offset(size_t uoffset)
127 {
128 	return ((rte_rand() % (LARGE_BUFFER_SIZE - SMALL_BUFFER_SIZE)) &
129 			~(ALIGNMENT_UNIT - 1)) + uoffset;
130 }
131 
132 /* Fill in source and destination addresses. */
133 static inline void
fill_addr_arrays(size_t * dst_addr,int is_dst_cached,size_t dst_uoffset,size_t * src_addr,int is_src_cached,size_t src_uoffset)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)
136 {
137 	unsigned int i;
138 
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);
142 	}
143 }
144 
145 /*
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.
149  */
150 static void
do_uncached_write(uint8_t * dst,int is_dst_cached,const uint8_t * src,int is_src_cached,size_t size)151 do_uncached_write(uint8_t *dst, int is_dst_cached,
152 				  const uint8_t *src, int is_src_cached, size_t size)
153 {
154 	unsigned i, j;
155 	size_t dst_addrs[TEST_BATCH_SIZE], src_addrs[TEST_BATCH_SIZE];
156 
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);
162 		}
163 	}
164 }
165 
166 /*
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.
169  */
170 #define SINGLE_PERF_TEST(dst, is_dst_cached, dst_uoffset,                   \
171                          src, is_src_cached, src_uoffset, size)             \
172 do {                                                                        \
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;                             \
184     }                                                                       \
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;                            \
192     }                                                                       \
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); \
196 } while (0)
197 
198 /* Run aligned memcpy tests for each cached/uncached permutation */
199 #define ALL_PERF_TESTS_FOR_SIZE(n)                                       \
200 do {                                                                     \
201     if (__builtin_constant_p(n))                                         \
202         printf("\nC%6u", (unsigned)n);                                   \
203     else                                                                 \
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);    \
209 } while (0)
210 
211 /* Run unaligned memcpy tests for each cached/uncached permutation */
212 #define ALL_PERF_TESTS_FOR_SIZE_UNALIGNED(n)                             \
213 do {                                                                     \
214     if (__builtin_constant_p(n))                                         \
215         printf("\nC%6u", (unsigned)n);                                   \
216     else                                                                 \
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);    \
222 } while (0)
223 
224 /* Run memcpy tests for constant length */
225 #define ALL_PERF_TEST_FOR_CONSTANT                                      \
226 do {                                                                    \
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);    \
230 } while (0)
231 
232 /* Run all memcpy tests for aligned constant cases */
233 static inline void
perf_test_constant_aligned(void)234 perf_test_constant_aligned(void)
235 {
236 #define TEST_CONSTANT ALL_PERF_TESTS_FOR_SIZE
237 	ALL_PERF_TEST_FOR_CONSTANT;
238 #undef TEST_CONSTANT
239 }
240 
241 /* Run all memcpy tests for unaligned constant cases */
242 static inline void
perf_test_constant_unaligned(void)243 perf_test_constant_unaligned(void)
244 {
245 #define TEST_CONSTANT ALL_PERF_TESTS_FOR_SIZE_UNALIGNED
246 	ALL_PERF_TEST_FOR_CONSTANT;
247 #undef TEST_CONSTANT
248 }
249 
250 /* Run all memcpy tests for aligned variable cases */
251 static inline void
perf_test_variable_aligned(void)252 perf_test_variable_aligned(void)
253 {
254 	unsigned i;
255 	for (i = 0; i < RTE_DIM(buf_sizes); i++) {
256 		ALL_PERF_TESTS_FOR_SIZE((size_t)buf_sizes[i]);
257 	}
258 }
259 
260 /* Run all memcpy tests for unaligned variable cases */
261 static inline void
perf_test_variable_unaligned(void)262 perf_test_variable_unaligned(void)
263 {
264 	unsigned i;
265 	for (i = 0; i < RTE_DIM(buf_sizes); i++) {
266 		ALL_PERF_TESTS_FOR_SIZE_UNALIGNED((size_t)buf_sizes[i]);
267 	}
268 }
269 
270 /* Run all memcpy tests */
271 static int
perf_test(void)272 perf_test(void)
273 {
274 	int ret;
275 	struct timespec tv_begin, tv_end;
276 	double time_aligned, time_unaligned;
277 	double time_aligned_const, time_unaligned_const;
278 
279 	ret = init_buffers();
280 	if (ret != 0)
281 		return ret;
282 
283 #if TEST_VALUE_RANGE != 0
284 	/* Set up buf_sizes array, if required */
285 	unsigned i;
286 	for (i = 0; i < TEST_VALUE_RANGE; i++)
287 		buf_sizes[i] = i;
288 #endif
289 
290 	/* See function comment */
291 	do_uncached_write(large_buf_write, 0, small_buf_read, 1, SMALL_BUFFER_SIZE);
292 
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 		   "------- ----------------- ----------------- ----------------- -----------------");
298 
299 	printf("\n================================= %2dB aligned =================================",
300 		ALIGNMENT_UNIT);
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");
329 
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);
335 	free_buffers();
336 
337 	return 0;
338 }
339 
340 static int
test_memcpy_perf(void)341 test_memcpy_perf(void)
342 {
343 	int ret;
344 
345 	ret = perf_test();
346 	if (ret != 0)
347 		return -1;
348 	return 0;
349 }
350 
351 REGISTER_PERF_TEST(memcpy_perf_autotest, test_memcpy_perf);
352