1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 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 "spdk/stdinc.h" 35 36 #include "spdk/util.h" 37 #include "spdk/queue.h" 38 #include "spdk_cunit.h" 39 40 #include <rte_config.h> 41 #include <rte_version.h> 42 #include <rte_malloc.h> 43 #include <rte_eal_memconfig.h> 44 #include <rte_eal.h> 45 46 struct mem_allocation { 47 uintptr_t vaddr; 48 size_t len; 49 TAILQ_ENTRY(mem_allocation) link; 50 }; 51 52 static TAILQ_HEAD(, mem_allocation) g_mem_allocations = TAILQ_HEAD_INITIALIZER(g_mem_allocations); 53 54 static void 55 memory_hotplug_cb(enum rte_mem_event event_type, const void *addr, size_t len, void *arg) 56 { 57 struct mem_allocation *allocation; 58 59 if (event_type == RTE_MEM_EVENT_ALLOC) { 60 allocation = calloc(1, sizeof(*allocation)); 61 SPDK_CU_ASSERT_FATAL(allocation != NULL); 62 63 printf("register %p %ju\n", addr, len); 64 allocation->vaddr = (uintptr_t)addr; 65 allocation->len = len; 66 TAILQ_INSERT_TAIL(&g_mem_allocations, allocation, link); 67 } else if (event_type == RTE_MEM_EVENT_FREE) { 68 TAILQ_FOREACH(allocation, &g_mem_allocations, link) { 69 if (allocation->vaddr == (uintptr_t)addr && allocation->len == len) { 70 break; 71 } 72 } 73 printf("unregister %p %ju %s\n", addr, len, allocation == NULL ? "FAILED" : "PASSED"); 74 SPDK_CU_ASSERT_FATAL(allocation != NULL); 75 TAILQ_REMOVE(&g_mem_allocations, allocation, link); 76 free(allocation); 77 } 78 } 79 80 static int 81 memory_iter_cb(const struct rte_memseg_list *msl, 82 const struct rte_memseg *ms, size_t len, void *arg) 83 { 84 struct mem_allocation *allocation; 85 86 allocation = calloc(1, sizeof(*allocation)); 87 SPDK_CU_ASSERT_FATAL(allocation != NULL); 88 89 printf("register %p %ju\n", ms->addr, len); 90 allocation->vaddr = (uintptr_t)ms->addr; 91 allocation->len = len; 92 TAILQ_INSERT_TAIL(&g_mem_allocations, allocation, link); 93 94 return 0; 95 } 96 97 static void 98 verify_buffer(void *_buf, size_t len) 99 { 100 uintptr_t buf = (uintptr_t)_buf; 101 struct mem_allocation *allocation; 102 103 SPDK_CU_ASSERT_FATAL(_buf != NULL); 104 printf("buf %p len %ju ", _buf, len); 105 TAILQ_FOREACH(allocation, &g_mem_allocations, link) { 106 if (buf >= allocation->vaddr && 107 buf + len <= allocation->vaddr + allocation->len) { 108 break; 109 } 110 } 111 printf("%s\n", allocation == NULL ? "FAILED" : "PASSED"); 112 CU_ASSERT(allocation != NULL); 113 } 114 115 static void 116 test(void) 117 { 118 void *buf1, *buf2, *buf3, *buf4; 119 size_t len1, len2, len3, len4; 120 121 printf("\n"); 122 123 rte_mem_event_callback_register("test", memory_hotplug_cb, NULL); 124 rte_memseg_contig_walk(memory_iter_cb, NULL); 125 126 /* First allocate a 3MB buffer. This will allocate a 4MB hugepage 127 * region, with the 3MB buffer allocated from the end of it. 128 */ 129 len1 = 3 * 1024 * 1024; 130 printf("malloc %ju\n", len1); 131 buf1 = rte_malloc(NULL, len1, 0); 132 verify_buffer(buf1, len1); 133 134 /* Now allocate a very small buffer. This will get allocated from 135 * the previous 4MB hugepage allocation, just before the 3MB buffer 136 * allocated just above. 137 */ 138 len2 = 64; 139 printf("malloc %ju\n", len2); 140 buf2 = rte_malloc(NULL, len2, 0); 141 verify_buffer(buf2, len2); 142 143 /* Allocate a 4MB buffer. This should trigger a new hugepage allocation 144 * just for thie 4MB buffer. 145 */ 146 len3 = 4 * 1024 * 1024; 147 printf("malloc %ju\n", len3); 148 buf3 = rte_malloc(NULL, len3, 0); 149 verify_buffer(buf3, len3); 150 151 /* Free the three buffers. Specifically free buf1 first. buf2 was 152 * allocated from the same huge page allocation as buf1 - so we want 153 * to make sure that DPDK doesn't try to free part of the first huge 154 * page allocation - it needs to wait until buf2 is also freed so it 155 * can free all of it. 156 */ 157 printf("free %p %ju\n", buf1, len1); 158 rte_free(buf1); 159 printf("free %p %ju\n", buf2, len2); 160 rte_free(buf2); 161 printf("free %p %ju\n", buf3, len3); 162 rte_free(buf3); 163 164 /* Do a single 8MB hugepage allocation and then free it. This covers 165 * the more simple case. 166 */ 167 len4 = 8 * 1024 * 1024; 168 printf("malloc %ju\n", len4); 169 buf4 = rte_malloc(NULL, len4, 0); 170 verify_buffer(buf4, len4); 171 172 printf("free %p %ju\n", buf4, len4); 173 rte_free(buf4); 174 } 175 176 int 177 main(int argc, char **argv) 178 { 179 CU_pSuite suite = NULL; 180 unsigned int num_failures; 181 char *dpdk_arg[] = { 182 "mem_callbacks", "-c 0x1", 183 "--base-virtaddr=0x200000000000", 184 "--match-allocations", 185 }; 186 int rc; 187 188 rc = rte_eal_init(SPDK_COUNTOF(dpdk_arg), dpdk_arg); 189 if (rc < 0) { 190 printf("Err: Unable to initialize DPDK\n"); 191 return 1; 192 } 193 194 if (CU_initialize_registry() != CUE_SUCCESS) { 195 return CU_get_error(); 196 } 197 198 suite = CU_add_suite("memory", NULL, NULL); 199 if (suite == NULL) { 200 CU_cleanup_registry(); 201 return CU_get_error(); 202 } 203 204 if ( 205 CU_add_test(suite, "test", test) == NULL 206 ) { 207 CU_cleanup_registry(); 208 return CU_get_error(); 209 } 210 211 CU_basic_set_mode(CU_BRM_VERBOSE); 212 CU_basic_run_tests(); 213 num_failures = CU_get_number_of_failures(); 214 CU_cleanup_registry(); 215 216 return num_failures; 217 } 218