first public release
[dpdk.git] / app / test / test_memcpy.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  *  version: DPDK.L.1.2.3-3
34  */
35
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40
41 #include <rte_common.h>
42 #include <cmdline_parse.h>
43 #include <rte_cycles.h>
44 #include <rte_random.h>
45 #include <rte_malloc.h>
46
47 #include <rte_memcpy.h>
48
49 #include "test.h"
50
51 /*
52  * Set this to the maximum buffer size you want to test. If it is 0, then the
53  * values in the buf_sizes[] array below will be used.
54  */
55 #define TEST_VALUE_RANGE        0
56
57 /* List of buffer sizes to test */
58 #if TEST_VALUE_RANGE == 0
59 static size_t buf_sizes[] = {
60         0, 1, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128, 129, 255,
61         256, 257, 320, 384, 511, 512, 513, 1023, 1024, 1025, 1518, 1522, 1600,
62         2048, 3072, 4096, 5120, 6144, 7168, 8192
63 };
64 /* MUST be as large as largest packet size above */
65 #define SMALL_BUFFER_SIZE       8192
66 #else /* TEST_VALUE_RANGE != 0 */
67 static size_t buf_sizes[TEST_VALUE_RANGE];
68 #define SMALL_BUFFER_SIZE       TEST_VALUE_RANGE
69 #endif /* TEST_VALUE_RANGE == 0 */
70
71
72 /*
73  * Arrays of this size are used for measuring uncached memory accesses by
74  * picking a random location within the buffer. Make this smaller if there are
75  * memory allocation errors.
76  */
77 #define LARGE_BUFFER_SIZE       (100 * 1024 * 1024)
78
79 /* How many times to run timing loop for performance tests */
80 #define TEST_ITERATIONS         1000000
81 #define TEST_BATCH_SIZE         100
82
83 /* Data is aligned on this many bytes (power of 2) */
84 #define ALIGNMENT_UNIT          16
85
86 /*
87  * Pointers used in performance tests. The two large buffers are for uncached
88  * access where random addresses within the buffer are used for each
89  * memcpy. The two small buffers are for cached access.
90  */
91 static uint8_t *large_buf_read, *large_buf_write,
92                *small_buf_read, *small_buf_write;
93
94 /* Initialise data buffers. */
95 static int
96 init_buffers(void)
97 {
98         unsigned i;
99
100         large_buf_read = rte_malloc("memcpy", LARGE_BUFFER_SIZE, ALIGNMENT_UNIT);
101         if (large_buf_read == NULL)
102                 goto error_large_buf_read;
103
104         large_buf_write = rte_malloc("memcpy", LARGE_BUFFER_SIZE, ALIGNMENT_UNIT);
105         if (large_buf_write == NULL)
106                 goto error_large_buf_write;
107
108         small_buf_read = rte_malloc("memcpy", SMALL_BUFFER_SIZE, ALIGNMENT_UNIT);
109         if (small_buf_read == NULL)
110                 goto error_small_buf_read;
111
112         small_buf_write = rte_malloc("memcpy", SMALL_BUFFER_SIZE, ALIGNMENT_UNIT);
113         if (small_buf_write == NULL)
114                 goto error_small_buf_write;
115
116         for (i = 0; i < LARGE_BUFFER_SIZE; i++)
117                 large_buf_read[i] = rte_rand();
118         for (i = 0; i < SMALL_BUFFER_SIZE; i++)
119                 small_buf_read[i] = rte_rand();
120
121         return 0;
122
123 error_small_buf_write:
124         rte_free(small_buf_read);
125 error_small_buf_read:
126         rte_free(large_buf_write);
127 error_large_buf_write:
128         rte_free(large_buf_read);
129 error_large_buf_read:
130         printf("ERROR: not enough memory");
131         return -1;
132 }
133
134 /* Cleanup data buffers */
135 static void
136 free_buffers(void)
137 {
138         rte_free(large_buf_read);
139         rte_free(large_buf_write);
140         rte_free(small_buf_read);
141         rte_free(small_buf_write);
142 }
143
144 /*
145  * Get a random offset into large array, with enough space needed to perform
146  * max copy size. Offset is aligned.
147  */
148 static inline size_t
149 get_rand_offset(void)
150 {
151         return ((rte_rand() % (LARGE_BUFFER_SIZE - SMALL_BUFFER_SIZE)) &
152                         ~(ALIGNMENT_UNIT - 1));
153 }
154
155 /* Fill in source and destination addresses. */
156 static inline void
157 fill_addr_arrays(size_t *dst_addr, int is_dst_cached,
158                  size_t *src_addr, int is_src_cached)
159 {
160         unsigned int i;
161
162         for (i = 0; i < TEST_BATCH_SIZE; i++) {
163                 dst_addr[i] = (is_dst_cached) ? 0 : get_rand_offset();
164                 src_addr[i] = (is_src_cached) ? 0 : get_rand_offset();
165         }
166 }
167
168 /* Integer division with round to nearest */
169 static inline uint64_t
170 div_round(uint64_t dividend, uint64_t divisor)
171 {
172         return ((2 * dividend) + divisor) / (2 * divisor);
173 }
174
175 /*
176  * WORKAROUND: For some reason the first test doing an uncached write
177  * takes a very long time (~25 times longer than is expected). So we do
178  * it once without timing.
179  */
180 static void
181 do_uncached_write(uint8_t *dst, int is_dst_cached,
182                   const uint8_t *src, int is_src_cached, size_t size)
183 {
184         unsigned i, j;
185         size_t dst_addrs[TEST_BATCH_SIZE], src_addrs[TEST_BATCH_SIZE];
186
187         for (i = 0; i < (TEST_ITERATIONS / TEST_BATCH_SIZE); i++) {
188                 fill_addr_arrays(dst_addrs, is_dst_cached,
189                          src_addrs, is_src_cached);
190                 for (j = 0; j < TEST_BATCH_SIZE; j++)
191                         rte_memcpy(dst+dst_addrs[j], src+src_addrs[j], size);
192         }
193 }
194
195 /*
196  * Run a single memcpy performance test. This is a macro to ensure that if
197  * the "size" parameter is a constant it won't be converted to a variable.
198  */
199 #define SINGLE_PERF_TEST(dst, is_dst_cached, src, is_src_cached, size) do {   \
200         unsigned int iter, t;                                                 \
201         size_t dst_addrs[TEST_BATCH_SIZE], src_addrs[TEST_BATCH_SIZE];        \
202         uint64_t start_time, total_time = 0;                                  \
203         uint64_t total_time2 = 0;                                             \
204         for (iter = 0; iter < (TEST_ITERATIONS / TEST_BATCH_SIZE); iter++) {  \
205                 fill_addr_arrays(dst_addrs, is_dst_cached,                    \
206                                  src_addrs, is_src_cached);                   \
207                 start_time = rte_rdtsc();                                     \
208                 for (t = 0; t < TEST_BATCH_SIZE; t++)                         \
209                         rte_memcpy(dst+dst_addrs[t], src+src_addrs[t], size); \
210                 total_time += rte_rdtsc() - start_time;                       \
211         }                                                                     \
212         for (iter = 0; iter < (TEST_ITERATIONS / TEST_BATCH_SIZE); iter++) {  \
213                 fill_addr_arrays(dst_addrs, is_dst_cached,                    \
214                                  src_addrs, is_src_cached);                   \
215                 start_time = rte_rdtsc();                                     \
216                 for (t = 0; t < TEST_BATCH_SIZE; t++)                         \
217                         memcpy(dst+dst_addrs[t], src+src_addrs[t], size);     \
218                 total_time2 += rte_rdtsc() - start_time;                      \
219         }                                                                     \
220         printf("%9u/",  (unsigned)div_round(total_time, TEST_ITERATIONS));    \
221         printf("%4u",   (unsigned)div_round(total_time2, TEST_ITERATIONS));   \
222 } while (0)
223
224 /* Run memcpy() tests for each cached/uncached permutation. */
225 #define ALL_PERF_TESTS_FOR_SIZE(n) do {                             \
226         if (__builtin_constant_p(n))                                \
227                 printf("\nC%6u ", (unsigned)n);                     \
228         else                                                        \
229                 printf("\n%7u ", (unsigned)n);                      \
230         SINGLE_PERF_TEST(small_buf_write, 1, small_buf_read, 1, n); \
231         SINGLE_PERF_TEST(large_buf_write, 0, small_buf_read, 1, n); \
232         SINGLE_PERF_TEST(small_buf_write, 1, large_buf_read, 0, n); \
233         SINGLE_PERF_TEST(large_buf_write, 0, large_buf_read, 0, n); \
234 } while (0)
235
236 /*
237  * Run performance tests for a number of different sizes and cached/uncached
238  * permutations.
239  */
240 static int
241 perf_test(void)
242 {
243         const unsigned num_buf_sizes = sizeof(buf_sizes) / sizeof(buf_sizes[0]);
244         unsigned i;
245         int ret;
246
247         ret = init_buffers();
248         if (ret != 0)
249                 return ret;
250
251 #if TEST_VALUE_RANGE != 0
252         /* Setup buf_sizes array, if required */
253         for (i = 0; i < TEST_VALUE_RANGE; i++)
254                 buf_sizes[i] = i;
255 #endif
256
257         /* See function comment */
258         do_uncached_write(large_buf_write, 0, small_buf_read, 1, SMALL_BUFFER_SIZE);
259
260         printf("\n** rte_memcpy()/memcpy performance tests **\n"
261                "======= ============== ============== ============== ==============\n"
262                "   Size Cache to cache   Cache to mem   Mem to cache     Mem to mem\n"
263                "(bytes)        (ticks)        (ticks)        (ticks)        (ticks)\n"
264                "------- -------------- -------------- -------------- --------------");
265
266         /* Do tests where size is a variable */
267         for (i = 0; i < num_buf_sizes; i++) {
268                 ALL_PERF_TESTS_FOR_SIZE((size_t)buf_sizes[i]);
269         }
270
271 #ifdef RTE_MEMCPY_BUILTIN_CONSTANT_P
272         /* Do tests where size is a compile-time constant */
273         ALL_PERF_TESTS_FOR_SIZE(63U);
274         ALL_PERF_TESTS_FOR_SIZE(64U);
275         ALL_PERF_TESTS_FOR_SIZE(65U);
276         ALL_PERF_TESTS_FOR_SIZE(255U);
277         ALL_PERF_TESTS_FOR_SIZE(256U);
278         ALL_PERF_TESTS_FOR_SIZE(257U);
279         ALL_PERF_TESTS_FOR_SIZE(1023U);
280         ALL_PERF_TESTS_FOR_SIZE(1024U);
281         ALL_PERF_TESTS_FOR_SIZE(1025U);
282         ALL_PERF_TESTS_FOR_SIZE(1518U);
283 #endif
284         printf("\n======= ============== ============== ============== ==============\n\n");
285
286         free_buffers();
287
288         return 0;
289 }
290
291 /* Structure with base memcpy func pointer, and number of bytes it copies */
292 struct base_memcpy_func {
293         void (*func)(uint8_t *dst, const uint8_t *src);
294         unsigned size;
295 };
296
297 /* To create base_memcpy_func structure entries */
298 #define BASE_FUNC(n) {rte_mov##n, n}
299
300 /* Max number of bytes that can be copies with a "base" memcpy functions */
301 #define MAX_BASE_FUNC_SIZE 256
302
303 /*
304  * Test the "base" memcpy functions, that a copy fixed number of bytes.
305  */
306 static int
307 base_func_test(void)
308 {
309         const struct base_memcpy_func base_memcpy_funcs[6] = {
310                 BASE_FUNC(16),
311                 BASE_FUNC(32),
312                 BASE_FUNC(48),
313                 BASE_FUNC(64),
314                 BASE_FUNC(128),
315                 BASE_FUNC(256),
316         };
317         unsigned i, j;
318         unsigned num_funcs = sizeof(base_memcpy_funcs) / sizeof(base_memcpy_funcs[0]);
319         uint8_t dst[MAX_BASE_FUNC_SIZE];
320         uint8_t src[MAX_BASE_FUNC_SIZE];
321
322         for (i = 0; i < num_funcs; i++) {
323                 unsigned size = base_memcpy_funcs[i].size;
324                 for (j = 0; j < size; j++) {
325                         dst[j] = 0;
326                         src[j] = (uint8_t) rte_rand();
327                 }
328                 base_memcpy_funcs[i].func(dst, src);
329                 for (j = 0; j < size; j++)
330                         if (dst[j] != src[j])
331                                 return -1;
332         }
333
334         return 0;
335 }
336
337 /*
338  * Create two buffers, and initialise one with random values. These are copied
339  * to the second buffer and then compared to see if the copy was successful.
340  * The bytes outside the copied area are also checked to make sure they were not
341  * changed.
342  */
343 static int
344 test_single_memcpy(unsigned int off_src, unsigned int off_dst, size_t size)
345 {
346         unsigned int i;
347         uint8_t dest[SMALL_BUFFER_SIZE + ALIGNMENT_UNIT];
348         uint8_t src[SMALL_BUFFER_SIZE + ALIGNMENT_UNIT];
349
350         /* Setup buffers */
351         for (i = 0; i < SMALL_BUFFER_SIZE + ALIGNMENT_UNIT; i++) {
352                 dest[i] = 0;
353                 src[i] = (uint8_t) rte_rand();
354         }
355
356         /* Do the copy */
357         rte_memcpy(dest + off_dst, src + off_src, size);
358
359         /* Check nothing before offset is affected */
360         for (i = 0; i < off_dst; i++) {
361                 if (dest[i] != 0) {
362                         printf("rte_memcpy() failed for %u bytes (offsets=%u,%u): "
363                                "[modified before start of dst].\n",
364                                (unsigned)size, off_src, off_dst);
365                         return -1;
366                 }
367         }
368
369         /* Check everything was copied */
370         for (i = 0; i < size; i++) {
371                 if (dest[i + off_dst] != src[i + off_src]) {
372                         printf("rte_memcpy() failed for %u bytes (offsets=%u,%u): "
373                                "[didn't copy byte %u].\n",
374                                (unsigned)size, off_src, off_dst, i);
375                         return -1;
376                 }
377         }
378
379         /* Check nothing after copy was affected */
380         for (i = size; i < SMALL_BUFFER_SIZE; i++) {
381                 if (dest[i + off_dst] != 0) {
382                         printf("rte_memcpy() failed for %u bytes (offsets=%u,%u): "
383                                "[copied too many].\n",
384                                (unsigned)size, off_src, off_dst);
385                         return -1;
386                 }
387         }
388         return 0;
389 }
390
391 /*
392  * Check functionality for various buffer sizes and data offsets/alignments.
393  */
394 static int
395 func_test(void)
396 {
397         unsigned int off_src, off_dst, i;
398         unsigned int num_buf_sizes = sizeof(buf_sizes) / sizeof(buf_sizes[0]);
399         int ret;
400
401         for (off_src = 0; off_src < ALIGNMENT_UNIT; off_src++) {
402                 for (off_dst = 0; off_dst < ALIGNMENT_UNIT; off_dst++) {
403                         for (i = 0; i < num_buf_sizes; i++) {
404                                 ret = test_single_memcpy(off_src, off_dst,
405                                                          buf_sizes[i]);
406                                 if (ret != 0)
407                                         return -1;
408                         }
409                 }
410         }
411         return 0;
412 }
413
414 int
415 test_memcpy(void)
416 {
417         int ret;
418
419         ret = func_test();
420         if (ret != 0)
421                 return -1;
422         ret = base_func_test();
423         if (ret != 0)
424                 return -1;
425         ret = perf_test();
426         if (ret != 0)
427                 return -1;
428         return 0;
429 }