f20aff62884f92e4788d8e1756a2c55f7ac5091a
[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  */
34
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39
40 #include <rte_common.h>
41 #include <cmdline_parse.h>
42 #include <rte_cycles.h>
43 #include <rte_random.h>
44 #include <rte_malloc.h>
45
46 #include <rte_memcpy.h>
47
48 #include "test.h"
49
50 /*
51  * Set this to the maximum buffer size you want to test. If it is 0, then the
52  * values in the buf_sizes[] array below will be used.
53  */
54 #define TEST_VALUE_RANGE        0
55
56 /* List of buffer sizes to test */
57 #if TEST_VALUE_RANGE == 0
58 static size_t buf_sizes[] = {
59         0, 1, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128, 129, 255,
60         256, 257, 320, 384, 511, 512, 513, 1023, 1024, 1025, 1518, 1522, 1600,
61         2048, 3072, 4096, 5120, 6144, 7168, 8192
62 };
63 /* MUST be as large as largest packet size above */
64 #define SMALL_BUFFER_SIZE       8192
65 #else /* TEST_VALUE_RANGE != 0 */
66 static size_t buf_sizes[TEST_VALUE_RANGE];
67 #define SMALL_BUFFER_SIZE       TEST_VALUE_RANGE
68 #endif /* TEST_VALUE_RANGE == 0 */
69
70
71 /*
72  * Arrays of this size are used for measuring uncached memory accesses by
73  * picking a random location within the buffer. Make this smaller if there are
74  * memory allocation errors.
75  */
76 #define LARGE_BUFFER_SIZE       (100 * 1024 * 1024)
77
78 /* How many times to run timing loop for performance tests */
79 #define TEST_ITERATIONS         1000000
80 #define TEST_BATCH_SIZE         100
81
82 /* Data is aligned on this many bytes (power of 2) */
83 #define ALIGNMENT_UNIT          16
84
85
86
87 /* Structure with base memcpy func pointer, and number of bytes it copies */
88 struct base_memcpy_func {
89         void (*func)(uint8_t *dst, const uint8_t *src);
90         unsigned size;
91 };
92
93 /* To create base_memcpy_func structure entries */
94 #define BASE_FUNC(n) {rte_mov##n, n}
95
96 /* Max number of bytes that can be copies with a "base" memcpy functions */
97 #define MAX_BASE_FUNC_SIZE 256
98
99 /*
100  * Test the "base" memcpy functions, that a copy fixed number of bytes.
101  */
102 static int
103 base_func_test(void)
104 {
105         const struct base_memcpy_func base_memcpy_funcs[6] = {
106                 BASE_FUNC(16),
107                 BASE_FUNC(32),
108                 BASE_FUNC(48),
109                 BASE_FUNC(64),
110                 BASE_FUNC(128),
111                 BASE_FUNC(256),
112         };
113         unsigned i, j;
114         unsigned num_funcs = sizeof(base_memcpy_funcs) / sizeof(base_memcpy_funcs[0]);
115         uint8_t dst[MAX_BASE_FUNC_SIZE];
116         uint8_t src[MAX_BASE_FUNC_SIZE];
117
118         for (i = 0; i < num_funcs; i++) {
119                 unsigned size = base_memcpy_funcs[i].size;
120                 for (j = 0; j < size; j++) {
121                         dst[j] = 0;
122                         src[j] = (uint8_t) rte_rand();
123                 }
124                 base_memcpy_funcs[i].func(dst, src);
125                 for (j = 0; j < size; j++)
126                         if (dst[j] != src[j])
127                                 return -1;
128         }
129
130         return 0;
131 }
132
133 /*
134  * Create two buffers, and initialise one with random values. These are copied
135  * to the second buffer and then compared to see if the copy was successful.
136  * The bytes outside the copied area are also checked to make sure they were not
137  * changed.
138  */
139 static int
140 test_single_memcpy(unsigned int off_src, unsigned int off_dst, size_t size)
141 {
142         unsigned int i;
143         uint8_t dest[SMALL_BUFFER_SIZE + ALIGNMENT_UNIT];
144         uint8_t src[SMALL_BUFFER_SIZE + ALIGNMENT_UNIT];
145         void * ret;
146
147         /* Setup buffers */
148         for (i = 0; i < SMALL_BUFFER_SIZE + ALIGNMENT_UNIT; i++) {
149                 dest[i] = 0;
150                 src[i] = (uint8_t) rte_rand();
151         }
152
153         /* Do the copy */
154         ret = rte_memcpy(dest + off_dst, src + off_src, size);
155         if (ret != (dest + off_dst)) {
156                 printf("rte_memcpy() returned %p, not %p\n",
157                        ret, dest + off_dst);
158         }
159
160         /* Check nothing before offset is affected */
161         for (i = 0; i < off_dst; i++) {
162                 if (dest[i] != 0) {
163                         printf("rte_memcpy() failed for %u bytes (offsets=%u,%u): "
164                                "[modified before start of dst].\n",
165                                (unsigned)size, off_src, off_dst);
166                         return -1;
167                 }
168         }
169
170         /* Check everything was copied */
171         for (i = 0; i < size; i++) {
172                 if (dest[i + off_dst] != src[i + off_src]) {
173                         printf("rte_memcpy() failed for %u bytes (offsets=%u,%u): "
174                                "[didn't copy byte %u].\n",
175                                (unsigned)size, off_src, off_dst, i);
176                         return -1;
177                 }
178         }
179
180         /* Check nothing after copy was affected */
181         for (i = size; i < SMALL_BUFFER_SIZE; i++) {
182                 if (dest[i + off_dst] != 0) {
183                         printf("rte_memcpy() failed for %u bytes (offsets=%u,%u): "
184                                "[copied too many].\n",
185                                (unsigned)size, off_src, off_dst);
186                         return -1;
187                 }
188         }
189         return 0;
190 }
191
192 /*
193  * Check functionality for various buffer sizes and data offsets/alignments.
194  */
195 static int
196 func_test(void)
197 {
198         unsigned int off_src, off_dst, i;
199         unsigned int num_buf_sizes = sizeof(buf_sizes) / sizeof(buf_sizes[0]);
200         int ret;
201
202         for (off_src = 0; off_src < ALIGNMENT_UNIT; off_src++) {
203                 for (off_dst = 0; off_dst < ALIGNMENT_UNIT; off_dst++) {
204                         for (i = 0; i < num_buf_sizes; i++) {
205                                 ret = test_single_memcpy(off_src, off_dst,
206                                                          buf_sizes[i]);
207                                 if (ret != 0)
208                                         return -1;
209                         }
210                 }
211         }
212         return 0;
213 }
214
215 int
216 test_memcpy(void)
217 {
218         int ret;
219
220         ret = func_test();
221         if (ret != 0)
222                 return -1;
223         ret = base_func_test();
224         if (ret != 0)
225                 return -1;
226         return 0;
227 }