xref: /dpdk/app/test/test_memcpy.c (revision edab33b1c01d508fdd934c06ee27f84250d2749a)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #include <stdint.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdlib.h>
38 
39 #include <rte_common.h>
40 #include <rte_cycles.h>
41 #include <rte_random.h>
42 #include <rte_malloc.h>
43 
44 #include <rte_memcpy.h>
45 
46 #include "test.h"
47 
48 /*
49  * Set this to the maximum buffer size you want to test. If it is 0, then the
50  * values in the buf_sizes[] array below will be used.
51  */
52 #define TEST_VALUE_RANGE        0
53 
54 /* List of buffer sizes to test */
55 #if TEST_VALUE_RANGE == 0
56 static size_t buf_sizes[] = {
57 	0, 1, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128, 129, 255,
58 	256, 257, 320, 384, 511, 512, 513, 1023, 1024, 1025, 1518, 1522, 1600,
59 	2048, 3072, 4096, 5120, 6144, 7168, 8192
60 };
61 /* MUST be as large as largest packet size above */
62 #define SMALL_BUFFER_SIZE       8192
63 #else /* TEST_VALUE_RANGE != 0 */
64 static size_t buf_sizes[TEST_VALUE_RANGE];
65 #define SMALL_BUFFER_SIZE       TEST_VALUE_RANGE
66 #endif /* TEST_VALUE_RANGE == 0 */
67 
68 
69 /*
70  * Arrays of this size are used for measuring uncached memory accesses by
71  * picking a random location within the buffer. Make this smaller if there are
72  * memory allocation errors.
73  */
74 #define LARGE_BUFFER_SIZE       (100 * 1024 * 1024)
75 
76 /* How many times to run timing loop for performance tests */
77 #define TEST_ITERATIONS         1000000
78 #define TEST_BATCH_SIZE         100
79 
80 /* Data is aligned on this many bytes (power of 2) */
81 #define ALIGNMENT_UNIT          32
82 
83 
84 /*
85  * Create two buffers, and initialise one with random values. These are copied
86  * to the second buffer and then compared to see if the copy was successful.
87  * The bytes outside the copied area are also checked to make sure they were not
88  * changed.
89  */
90 static int
91 test_single_memcpy(unsigned int off_src, unsigned int off_dst, size_t size)
92 {
93 	unsigned int i;
94 	uint8_t dest[SMALL_BUFFER_SIZE + ALIGNMENT_UNIT];
95 	uint8_t src[SMALL_BUFFER_SIZE + ALIGNMENT_UNIT];
96 	void * ret;
97 
98 	/* Setup buffers */
99 	for (i = 0; i < SMALL_BUFFER_SIZE + ALIGNMENT_UNIT; i++) {
100 		dest[i] = 0;
101 		src[i] = (uint8_t) rte_rand();
102 	}
103 
104 	/* Do the copy */
105 	ret = rte_memcpy(dest + off_dst, src + off_src, size);
106 	if (ret != (dest + off_dst)) {
107 		printf("rte_memcpy() returned %p, not %p\n",
108 		       ret, dest + off_dst);
109 	}
110 
111 	/* Check nothing before offset is affected */
112 	for (i = 0; i < off_dst; i++) {
113 		if (dest[i] != 0) {
114 			printf("rte_memcpy() failed for %u bytes (offsets=%u,%u): "
115 			       "[modified before start of dst].\n",
116 			       (unsigned)size, off_src, off_dst);
117 			return -1;
118 		}
119 	}
120 
121 	/* Check everything was copied */
122 	for (i = 0; i < size; i++) {
123 		if (dest[i + off_dst] != src[i + off_src]) {
124 			printf("rte_memcpy() failed for %u bytes (offsets=%u,%u): "
125 			       "[didn't copy byte %u].\n",
126 			       (unsigned)size, off_src, off_dst, i);
127 			return -1;
128 		}
129 	}
130 
131 	/* Check nothing after copy was affected */
132 	for (i = size; i < SMALL_BUFFER_SIZE; i++) {
133 		if (dest[i + off_dst] != 0) {
134 			printf("rte_memcpy() failed for %u bytes (offsets=%u,%u): "
135 			       "[copied too many].\n",
136 			       (unsigned)size, off_src, off_dst);
137 			return -1;
138 		}
139 	}
140 	return 0;
141 }
142 
143 /*
144  * Check functionality for various buffer sizes and data offsets/alignments.
145  */
146 static int
147 func_test(void)
148 {
149 	unsigned int off_src, off_dst, i;
150 	unsigned int num_buf_sizes = sizeof(buf_sizes) / sizeof(buf_sizes[0]);
151 	int ret;
152 
153 	for (off_src = 0; off_src < ALIGNMENT_UNIT; off_src++) {
154 		for (off_dst = 0; off_dst < ALIGNMENT_UNIT; off_dst++) {
155 			for (i = 0; i < num_buf_sizes; i++) {
156 				ret = test_single_memcpy(off_src, off_dst,
157 				                         buf_sizes[i]);
158 				if (ret != 0)
159 					return -1;
160 			}
161 		}
162 	}
163 	return 0;
164 }
165 
166 static int
167 test_memcpy(void)
168 {
169 	int ret;
170 
171 	ret = func_test();
172 	if (ret != 0)
173 		return -1;
174 	return 0;
175 }
176 
177 static struct test_command memcpy_cmd = {
178 	.command = "memcpy_autotest",
179 	.callback = test_memcpy,
180 };
181 REGISTER_TEST_COMMAND(memcpy_cmd);
182