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