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_cunit.h" 37 38 #include "thread/thread.c" 39 #include "common/lib/test_env.c" 40 #include "common/lib/ut_multithread.c" 41 42 static void 43 _send_msg(spdk_thread_fn fn, void *ctx, void *thread_ctx) 44 { 45 fn(ctx); 46 } 47 48 static void 49 thread_alloc(void) 50 { 51 CU_ASSERT(TAILQ_EMPTY(&g_threads)); 52 allocate_threads(1); 53 CU_ASSERT(!TAILQ_EMPTY(&g_threads)); 54 free_threads(); 55 CU_ASSERT(TAILQ_EMPTY(&g_threads)); 56 } 57 58 static void 59 send_msg_cb(void *ctx) 60 { 61 bool *done = ctx; 62 63 *done = true; 64 } 65 66 static void 67 thread_send_msg(void) 68 { 69 struct spdk_thread *thread0; 70 bool done = false; 71 72 allocate_threads(2); 73 set_thread(0); 74 thread0 = spdk_get_thread(); 75 76 set_thread(1); 77 /* Simulate thread 1 sending a message to thread 0. */ 78 spdk_thread_send_msg(thread0, send_msg_cb, &done); 79 80 /* We have not polled thread 0 yet, so done should be false. */ 81 CU_ASSERT(!done); 82 83 /* 84 * Poll thread 1. The message was sent to thread 0, so this should be 85 * a nop and done should still be false. 86 */ 87 poll_thread(1); 88 CU_ASSERT(!done); 89 90 /* 91 * Poll thread 0. This should execute the message and done should then 92 * be true. 93 */ 94 poll_thread(0); 95 CU_ASSERT(done); 96 97 free_threads(); 98 } 99 100 static int 101 poller_run_done(void *ctx) 102 { 103 bool *poller_run = ctx; 104 105 *poller_run = true; 106 107 return -1; 108 } 109 110 static void 111 thread_poller(void) 112 { 113 struct spdk_poller *poller = NULL; 114 bool poller_run = false; 115 116 allocate_threads(1); 117 118 set_thread(0); 119 reset_time(); 120 /* Register a poller with no-wait time and test execution */ 121 poller = spdk_poller_register(poller_run_done, &poller_run, 0); 122 CU_ASSERT(poller != NULL); 123 124 poll_threads(); 125 CU_ASSERT(poller_run == true); 126 127 spdk_poller_unregister(&poller); 128 CU_ASSERT(poller == NULL); 129 130 /* Register a poller with 1000us wait time and test single execution */ 131 poller_run = false; 132 poller = spdk_poller_register(poller_run_done, &poller_run, 1000); 133 CU_ASSERT(poller != NULL); 134 135 poll_threads(); 136 CU_ASSERT(poller_run == false); 137 138 increment_time(1000); 139 poll_threads(); 140 CU_ASSERT(poller_run == true); 141 142 reset_time(); 143 poller_run = false; 144 poll_threads(); 145 CU_ASSERT(poller_run == false); 146 147 increment_time(1000); 148 poll_threads(); 149 CU_ASSERT(poller_run == true); 150 151 spdk_poller_unregister(&poller); 152 CU_ASSERT(poller == NULL); 153 154 free_threads(); 155 } 156 157 static void 158 for_each_cb(void *ctx) 159 { 160 int *count = ctx; 161 162 (*count)++; 163 } 164 165 static void 166 thread_for_each(void) 167 { 168 int count = 0; 169 int i; 170 171 allocate_threads(3); 172 set_thread(0); 173 174 spdk_for_each_thread(for_each_cb, &count, for_each_cb); 175 176 /* We have not polled thread 0 yet, so count should be 0 */ 177 CU_ASSERT(count == 0); 178 179 /* Poll each thread to verify the message is passed to each */ 180 for (i = 0; i < 3; i++) { 181 poll_thread(i); 182 CU_ASSERT(count == (i + 1)); 183 } 184 185 /* 186 * After each thread is called, the completion calls it 187 * one more time. 188 */ 189 poll_thread(0); 190 CU_ASSERT(count == 4); 191 192 free_threads(); 193 } 194 195 static int 196 channel_create(void *io_device, void *ctx_buf) 197 { 198 return 0; 199 } 200 201 static void 202 channel_destroy(void *io_device, void *ctx_buf) 203 { 204 } 205 206 static void 207 channel_msg(struct spdk_io_channel_iter *i) 208 { 209 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 210 int *count = spdk_io_channel_get_ctx(ch); 211 212 (*count)++; 213 214 spdk_for_each_channel_continue(i, 0); 215 } 216 217 static void 218 channel_cpl(struct spdk_io_channel_iter *i, int status) 219 { 220 } 221 222 static void 223 for_each_channel_remove(void) 224 { 225 struct spdk_io_channel *ch0, *ch1, *ch2; 226 int io_target; 227 int count = 0; 228 229 allocate_threads(3); 230 spdk_io_device_register(&io_target, channel_create, channel_destroy, sizeof(int)); 231 set_thread(0); 232 ch0 = spdk_get_io_channel(&io_target); 233 set_thread(1); 234 ch1 = spdk_get_io_channel(&io_target); 235 set_thread(2); 236 ch2 = spdk_get_io_channel(&io_target); 237 238 /* 239 * Test that io_channel handles the case where we start to iterate through 240 * the channels, and during the iteration, one of the channels is deleted. 241 * This is done in some different and sometimes non-intuitive orders, because 242 * some operations are deferred and won't execute until their threads are 243 * polled. 244 * 245 * Case #1: Put the I/O channel before spdk_for_each_channel. 246 */ 247 set_thread(0); 248 spdk_put_io_channel(ch0); 249 spdk_for_each_channel(&io_target, channel_msg, &count, channel_cpl); 250 poll_threads(); 251 252 /* 253 * Case #2: Put the I/O channel after spdk_for_each_channel, but before 254 * thread 0 is polled. 255 */ 256 ch0 = spdk_get_io_channel(&io_target); 257 spdk_for_each_channel(&io_target, channel_msg, &count, channel_cpl); 258 spdk_put_io_channel(ch0); 259 poll_threads(); 260 261 set_thread(1); 262 spdk_put_io_channel(ch1); 263 set_thread(2); 264 spdk_put_io_channel(ch2); 265 spdk_io_device_unregister(&io_target, NULL); 266 poll_threads(); 267 268 free_threads(); 269 } 270 271 struct unreg_ctx { 272 bool ch_done; 273 bool foreach_done; 274 }; 275 276 static void 277 unreg_ch_done(struct spdk_io_channel_iter *i) 278 { 279 struct unreg_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 280 281 ctx->ch_done = true; 282 283 spdk_for_each_channel_continue(i, 0); 284 } 285 286 static void 287 unreg_foreach_done(struct spdk_io_channel_iter *i, int status) 288 { 289 struct unreg_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 290 291 ctx->foreach_done = true; 292 } 293 294 static void 295 for_each_channel_unreg(void) 296 { 297 struct spdk_io_channel *ch0; 298 struct io_device *dev; 299 struct unreg_ctx ctx = {}; 300 int io_target; 301 302 allocate_threads(1); 303 CU_ASSERT(TAILQ_EMPTY(&g_io_devices)); 304 spdk_io_device_register(&io_target, channel_create, channel_destroy, sizeof(int)); 305 CU_ASSERT(!TAILQ_EMPTY(&g_io_devices)); 306 dev = TAILQ_FIRST(&g_io_devices); 307 SPDK_CU_ASSERT_FATAL(dev != NULL); 308 CU_ASSERT(TAILQ_NEXT(dev, tailq) == NULL); 309 set_thread(0); 310 ch0 = spdk_get_io_channel(&io_target); 311 spdk_for_each_channel(&io_target, unreg_ch_done, &ctx, unreg_foreach_done); 312 313 spdk_io_device_unregister(&io_target, NULL); 314 /* 315 * There is an outstanding foreach call on the io_device, so the unregister should not 316 * have removed the device. 317 */ 318 CU_ASSERT(dev == TAILQ_FIRST(&g_io_devices)); 319 spdk_io_device_register(&io_target, channel_create, channel_destroy, sizeof(int)); 320 /* 321 * There is already a device registered at &io_target, so a new io_device should not 322 * have been added to g_io_devices. 323 */ 324 CU_ASSERT(dev == TAILQ_FIRST(&g_io_devices)); 325 CU_ASSERT(TAILQ_NEXT(dev, tailq) == NULL); 326 327 poll_thread(0); 328 CU_ASSERT(ctx.ch_done == true); 329 CU_ASSERT(ctx.foreach_done == true); 330 /* 331 * There are no more foreach operations outstanding, so we can unregister the device, 332 * even though a channel still exists for the device. 333 */ 334 spdk_io_device_unregister(&io_target, NULL); 335 CU_ASSERT(TAILQ_EMPTY(&g_io_devices)); 336 337 set_thread(0); 338 spdk_put_io_channel(ch0); 339 340 poll_threads(); 341 342 free_threads(); 343 } 344 345 static void 346 thread_name(void) 347 { 348 struct spdk_thread *thread; 349 const char *name; 350 351 /* Create thread with no name */ 352 spdk_allocate_thread(_send_msg, NULL, NULL, NULL, NULL); 353 thread = spdk_get_thread(); 354 SPDK_CU_ASSERT_FATAL(thread != NULL); 355 name = spdk_thread_get_name(thread); 356 CU_ASSERT(name == NULL); 357 spdk_free_thread(); 358 359 /* Create thread named "test_thread" */ 360 spdk_allocate_thread(_send_msg, NULL, NULL, NULL, "test_thread"); 361 thread = spdk_get_thread(); 362 SPDK_CU_ASSERT_FATAL(thread != NULL); 363 name = spdk_thread_get_name(thread); 364 SPDK_CU_ASSERT_FATAL(name != NULL); 365 CU_ASSERT(strcmp(name, "test_thread") == 0); 366 spdk_free_thread(); 367 } 368 369 static uint64_t device1; 370 static uint64_t device2; 371 static uint64_t device3; 372 373 static uint64_t ctx1 = 0x1111; 374 static uint64_t ctx2 = 0x2222; 375 376 static int g_create_cb_calls = 0; 377 static int g_destroy_cb_calls = 0; 378 379 static int 380 create_cb_1(void *io_device, void *ctx_buf) 381 { 382 CU_ASSERT(io_device == &device1); 383 *(uint64_t *)ctx_buf = ctx1; 384 g_create_cb_calls++; 385 return 0; 386 } 387 388 static void 389 destroy_cb_1(void *io_device, void *ctx_buf) 390 { 391 CU_ASSERT(io_device == &device1); 392 CU_ASSERT(*(uint64_t *)ctx_buf == ctx1); 393 g_destroy_cb_calls++; 394 } 395 396 static int 397 create_cb_2(void *io_device, void *ctx_buf) 398 { 399 CU_ASSERT(io_device == &device2); 400 *(uint64_t *)ctx_buf = ctx2; 401 g_create_cb_calls++; 402 return 0; 403 } 404 405 static void 406 destroy_cb_2(void *io_device, void *ctx_buf) 407 { 408 CU_ASSERT(io_device == &device2); 409 CU_ASSERT(*(uint64_t *)ctx_buf == ctx2); 410 g_destroy_cb_calls++; 411 } 412 413 static void 414 channel(void) 415 { 416 struct spdk_io_channel *ch1, *ch2; 417 void *ctx; 418 419 spdk_allocate_thread(_send_msg, NULL, NULL, NULL, "thread0"); 420 spdk_io_device_register(&device1, create_cb_1, destroy_cb_1, sizeof(ctx1)); 421 spdk_io_device_register(&device2, create_cb_2, destroy_cb_2, sizeof(ctx2)); 422 423 g_create_cb_calls = 0; 424 ch1 = spdk_get_io_channel(&device1); 425 CU_ASSERT(g_create_cb_calls == 1); 426 SPDK_CU_ASSERT_FATAL(ch1 != NULL); 427 428 g_create_cb_calls = 0; 429 ch2 = spdk_get_io_channel(&device1); 430 CU_ASSERT(g_create_cb_calls == 0); 431 CU_ASSERT(ch1 == ch2); 432 SPDK_CU_ASSERT_FATAL(ch2 != NULL); 433 434 g_destroy_cb_calls = 0; 435 spdk_put_io_channel(ch2); 436 CU_ASSERT(g_destroy_cb_calls == 0); 437 438 g_create_cb_calls = 0; 439 ch2 = spdk_get_io_channel(&device2); 440 CU_ASSERT(g_create_cb_calls == 1); 441 CU_ASSERT(ch1 != ch2); 442 SPDK_CU_ASSERT_FATAL(ch2 != NULL); 443 444 ctx = spdk_io_channel_get_ctx(ch2); 445 CU_ASSERT(*(uint64_t *)ctx == ctx2); 446 447 g_destroy_cb_calls = 0; 448 spdk_put_io_channel(ch1); 449 CU_ASSERT(g_destroy_cb_calls == 1); 450 451 g_destroy_cb_calls = 0; 452 spdk_put_io_channel(ch2); 453 CU_ASSERT(g_destroy_cb_calls == 1); 454 455 ch1 = spdk_get_io_channel(&device3); 456 CU_ASSERT(ch1 == NULL); 457 458 spdk_io_device_unregister(&device1, NULL); 459 spdk_io_device_unregister(&device2, NULL); 460 CU_ASSERT(TAILQ_EMPTY(&g_io_devices)); 461 spdk_free_thread(); 462 CU_ASSERT(TAILQ_EMPTY(&g_threads)); 463 } 464 465 int 466 main(int argc, char **argv) 467 { 468 CU_pSuite suite = NULL; 469 unsigned int num_failures; 470 471 if (CU_initialize_registry() != CUE_SUCCESS) { 472 return CU_get_error(); 473 } 474 475 suite = CU_add_suite("io_channel", NULL, NULL); 476 if (suite == NULL) { 477 CU_cleanup_registry(); 478 return CU_get_error(); 479 } 480 481 if ( 482 CU_add_test(suite, "thread_alloc", thread_alloc) == NULL || 483 CU_add_test(suite, "thread_send_msg", thread_send_msg) == NULL || 484 CU_add_test(suite, "thread_poller", thread_poller) == NULL || 485 CU_add_test(suite, "thread_for_each", thread_for_each) == NULL || 486 CU_add_test(suite, "for_each_channel_remove", for_each_channel_remove) == NULL || 487 CU_add_test(suite, "for_each_channel_unreg", for_each_channel_unreg) == NULL || 488 CU_add_test(suite, "thread_name", thread_name) == NULL || 489 CU_add_test(suite, "channel", channel) == NULL 490 ) { 491 CU_cleanup_registry(); 492 return CU_get_error(); 493 } 494 495 CU_basic_set_mode(CU_BRM_VERBOSE); 496 CU_basic_run_tests(); 497 num_failures = CU_get_number_of_failures(); 498 CU_cleanup_registry(); 499 return num_failures; 500 } 501