1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
|
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2021-2022, Intel Corporation */
/*
* mtt.c -- a multithreaded tests' runner
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <librpma.h>
#include "mtt.h"
#define TIMEOUT_SECONDS 60
static struct {
/* mutex and conditional used to start all threads synchronously */
pthread_mutex_t mtx;
pthread_cond_t cond;
volatile unsigned threads_num_waiting;
struct timespec timeout;
} mtt_sync;
struct mtt_thread_args {
unsigned id; /* a thread id */
void *state; /* a thread-specific state */
struct mtt_test *test;
struct mtt_result ret; /* a thread return object */
};
/*
* call either init or fini function (func) using the provided test object
* (test), the thread arguments, and the result object.
*/
#define MTT_CALL_INIT_FINI(test, func, thread_args, result) \
(test)->func((thread_args)->id, (test)->prestate, \
&(thread_args)->state, (result))
/*
* mtt_thread_main -- wait for the synchronization conditional and run the test
*/
static void *
mtt_thread_main(void *arg)
{
struct mtt_thread_args *ta = (struct mtt_thread_args *)arg;
struct mtt_test *test = ta->test;
struct mtt_result *tr = &ta->ret;
struct mtt_result tr_dummy = {0};
int result;
if (test->thread_init_func) {
MTT_CALL_INIT_FINI(test, thread_init_func, ta, tr);
if (tr->ret) {
/* unblock the main thread waiting for this one */
++mtt_sync.threads_num_waiting;
return tr;
}
}
/* wait for all threads to start */
result = pthread_mutex_lock(&mtt_sync.mtx);
if (result) {
MTT_ERR(tr, "pthread_mutex_lock", result);
return tr;
}
++mtt_sync.threads_num_waiting;
result = pthread_cond_timedwait(&mtt_sync.cond, &mtt_sync.mtx, &mtt_sync.timeout);
if (result) {
MTT_ERR(tr, "pthread_cond_timedwait", result);
(void) pthread_mutex_unlock(&mtt_sync.mtx);
goto err_thread_fini_func;
}
result = pthread_mutex_unlock(&mtt_sync.mtx);
if (result) {
MTT_ERR(tr, "pthread_mutex_unlock", result);
goto err_thread_fini_func;
}
test->thread_func(ta->id, test->prestate, ta->state, tr);
err_thread_fini_func:
if (test->thread_fini_func) {
/*
* if the thread result is already non-zero provide tr_dummy
* instead to avoid overwriting the result
*/
MTT_CALL_INIT_FINI(test, thread_fini_func, ta, (tr->ret ? &tr_dummy : tr));
}
return tr;
}
/* print an error message for errors not related to any specific thread */
#define MTT_INTERNAL_ERR(fmt, ...) \
fprintf(stderr, "error: " fmt "\n", ##__VA_ARGS__)
/*
* mtt_threads_sync_unblock -- wait for threads to spawn and unblock all
* threads synchronously
*/
static int
mtt_threads_sync_unblock(unsigned threads_num)
{
int ret;
int done = 0;
do {
ret = pthread_mutex_lock(&mtt_sync.mtx);
if (ret) {
MTT_INTERNAL_ERR("pthread_mutex_lock() failed: %s", strerror(ret));
return ret;
}
if (mtt_sync.threads_num_waiting == threads_num) {
ret = pthread_cond_broadcast(&mtt_sync.cond);
if (ret) {
MTT_INTERNAL_ERR("pthread_cond_broadcast() failed: %s",
strerror(ret));
}
/*
* If broadcasting has failed the waiting threads will
* time out so the test will exit with appropriate
* error messages. Nothing more can be done.
*/
done = 1;
}
ret = pthread_mutex_unlock(&mtt_sync.mtx);
if (ret) {
MTT_INTERNAL_ERR("pthread_mutex_unlock() failed: %s", strerror(ret));
return ret;
}
} while (!done);
return ret;
}
/*
* mtt_init -- initialize the global state
*/
static int
mtt_init()
{
int ret;
ret = pthread_mutex_init(&mtt_sync.mtx, NULL);
if (ret) {
MTT_INTERNAL_ERR("pthread_mutex_init() failed: %s", strerror(ret));
return ret;
}
ret = pthread_cond_init(&mtt_sync.cond, NULL);
if (ret) {
MTT_INTERNAL_ERR("pthread_cond_init() failed: %s", strerror(ret));
(void) pthread_mutex_destroy(&mtt_sync.mtx);
return ret;
}
ret = clock_gettime(CLOCK_REALTIME, &mtt_sync.timeout);
if (ret) {
MTT_INTERNAL_ERR("clock_gettime() failed: %s", strerror(errno));
(void) pthread_cond_destroy(&mtt_sync.cond);
(void) pthread_mutex_destroy(&mtt_sync.mtx);
return ret;
}
mtt_sync.timeout.tv_sec += TIMEOUT_SECONDS;
mtt_sync.threads_num_waiting = 0;
return 0;
}
/*
* mtt_fini -- clean up the global state
*/
static int
mtt_fini()
{
int ret;
int result = 0;
ret = pthread_mutex_destroy(&mtt_sync.mtx);
if (ret)
result = ret;
ret = pthread_cond_destroy(&mtt_sync.cond);
if (ret)
result = ret;
return result;
}
/*
* mtt_parse_args -- process the command line arguments
* RETURN -1 on error
*/
int
mtt_parse_args(int argc, char *argv[], struct mtt_args *args)
{
if (argc < 3) {
fprintf(stderr, "usage: %s <threads_num> <addr> [<base_port>]\n", argv[0]);
return -1;
}
args->threads_num = strtoul(argv[1], NULL, 10);
args->addr = argv[2];
if (argc > 3)
args->port = strtoul(argv[3], NULL, 10);
return 0;
}
/*
* mtt_base_file_name -- find the first character of the file name in the full
* file path by tracking the '/' characters. This allows extracting the file
* name from the path in-situ and using the file name without introducing any
* additional resources.
*/
const char *
mtt_base_file_name(const char *file_name)
{
const char *base_file_name = strrchr(file_name, '/');
if (!base_file_name)
base_file_name = file_name;
else
/* skip '/' */
base_file_name++;
return base_file_name;
}
/*
* mtt_malloc_aligned -- allocate an aligned chunk of memory
*/
void *
mtt_malloc_aligned(size_t size, struct mtt_result *tr)
{
long pagesize = sysconf(_SC_PAGESIZE);
if (pagesize < 0) {
if (tr)
MTT_ERR(tr, "sysconf", errno);
else
CHILD_ERR("[CHILD]", "sysconf", strerror(errno));
return NULL;
}
/* allocate a page size aligned local memory pool */
void *mem;
int ret = posix_memalign(&mem, (size_t)pagesize, size);
if (ret) {
if (tr)
MTT_ERR(tr, "posix_memalign", errno);
else
CHILD_ERR("[CHILD]", "posix_memalign", strerror(errno));
return NULL;
}
/* zero the allocated memory */
memset(mem, 0, size);
return mem;
}
/* print an error message prepended with thread's number */
#define MTT_TEST_ERR(thread_num, fmt, ...) \
fprintf(stderr, "[thread #%d] " fmt "\n", \
thread_num, ##__VA_ARGS__)
/*
* mtt_start_child_process -- start the child process
*/
static int
mtt_start_child_process(mtt_child_process_func child_process_func, void *prestate)
{
int pid;
int ret;
/*
* The another side of the connection (server or client)
* has to be started as a separate process using fork()
* (not as a thread), because those tests are run under
* valgrind and valgrind must not check MT-safety of
* server vs. client, because they are always separate
* processes.
*/
switch ((pid = fork())) {
case 0: /* this is the child */
ret = child_process_func(prestate);
exit(ret);
case -1: /* fork failed */
perror("fork");
return -1;
}
/* the parent returns a PID of the child process */
return pid;
}
/*
* mtt_check_child_process -- check if the child process terminated
*/
static int
mtt_check_child_process(int child_pid)
{
int status;
if (child_pid && waitpid(child_pid, &status, WNOHANG) != 0) {
if (WIFEXITED(status) && WEXITSTATUS(status)) {
MTT_INTERNAL_ERR("child process failed with status %i",
WEXITSTATUS(status));
} else {
MTT_INTERNAL_ERR("child process has already exited with status 0");
}
return -1;
}
return 0;
}
/*
* mtt_run -- run the provided test using provided number of threads
*/
int
mtt_run(struct mtt_test *test, unsigned threads_num)
{
pthread_t *threads;
struct mtt_thread_args *threads_args;
struct mtt_thread_args *ta;
struct mtt_result *tr;
struct mtt_result tr_local = {0};
unsigned threads_num_to_join = 0;
unsigned threads_num_to_fini = 0;
int child_pid = 0;
int result = 0;
unsigned i;
int ret;
/* configure logging thresholds to see more details */
rpma_log_set_threshold(RPMA_LOG_THRESHOLD, RPMA_LOG_LEVEL_INFO);
rpma_log_set_threshold(RPMA_LOG_THRESHOLD_AUX, RPMA_LOG_LEVEL_INFO);
if (test->child_start == MTT_START_CHILD_BEFORE_PRESTATE_INIT_FUNC &&
test->child_process_func) {
child_pid = mtt_start_child_process(test->child_process_func, test->child_prestate);
if (child_pid == -1) {
MTT_INTERNAL_ERR("starting the child process failed");
return -1;
}
if (child_pid && (result = mtt_check_child_process(child_pid)))
goto err_kill_child;
}
/* initialize the prestate */
if (test->prestate_init_func) {
test->prestate_init_func(test->prestate, &tr_local);
if (tr_local.ret) {
MTT_INTERNAL_ERR("%s", tr_local.errmsg);
result = tr_local.ret;
if (child_pid)
(void) mtt_check_child_process(child_pid);
goto err_kill_child;
}
}
if (child_pid && (result = mtt_check_child_process(child_pid)))
goto err_prestate_fini_func;
/* allocate threads and their arguments */
threads = calloc(threads_num, sizeof(pthread_t));
if (threads == NULL) {
MTT_INTERNAL_ERR("calloc failed");
result = -1;
goto err_prestate_fini_func;
}
threads_args = calloc(threads_num, sizeof(struct mtt_thread_args));
if (threads_args == NULL) {
MTT_INTERNAL_ERR("calloc failed");
result = -1;
goto err_free_threads;
}
if (test->child_start == MTT_START_CHILD_BEFORE_THREAD_SEQ_INIT_FUNC &&
test->child_process_func) {
child_pid = mtt_start_child_process(test->child_process_func, test->child_prestate);
if (child_pid == -1) {
MTT_INTERNAL_ERR("starting the child process failed");
result = -1;
goto err_free_threads_args;
}
if (child_pid && (result = mtt_check_child_process(child_pid)))
goto err_free_threads_args;
}
/* initialize threads' arguments */
for (i = 0; i < threads_num; i++) {
ta = &threads_args[i];
ta->id = i;
ta->test = test;
if (test->thread_seq_init_func) {
MTT_CALL_INIT_FINI(test, thread_seq_init_func, ta, &ta->ret);
if (ta->ret.ret) {
result = ta->ret.ret;
MTT_TEST_ERR(ta->id, "%s", ta->ret.errmsg);
goto err_thread_seq_fini_func;
}
}
++threads_num_to_fini;
}
if (child_pid && (result = mtt_check_child_process(child_pid)))
goto err_thread_seq_fini_func;
if (test->child_start == MTT_START_CHILD_BEFORE_THREAD_INIT_FUNC &&
test->child_process_func) {
child_pid = mtt_start_child_process(test->child_process_func, test->child_prestate);
if (child_pid == -1) {
MTT_INTERNAL_ERR("starting the child process failed");
result = -1;
goto err_thread_seq_fini_func;
}
if (child_pid && (result = mtt_check_child_process(child_pid)))
goto err_thread_seq_fini_func;
}
/*
* The global initialization has to be as close as possible to spawning
* threads since it also calculates an absolute timeout value common
* for all threads.
*/
result = mtt_init();
if (result)
goto err_thread_seq_fini_func;
/* create threads */
for (i = 0; i < threads_num; i++) {
result = pthread_create(&threads[i], NULL, mtt_thread_main, &threads_args[i]);
if (result != 0) {
MTT_TEST_ERR(i, "pthread_create() failed: %s", strerror(result));
break;
}
++threads_num_to_join;
}
if (child_pid && (result = mtt_check_child_process(child_pid)))
goto err_mtt_fini;
if (test->child_start == MTT_START_CHILD_BEFORE_THREAD_FUNC &&
test->child_process_func) {
child_pid = mtt_start_child_process(test->child_process_func, test->child_prestate);
if (child_pid == -1) {
MTT_INTERNAL_ERR("starting the child process failed");
result = -1;
goto err_mtt_fini;
}
if (child_pid && (result = mtt_check_child_process(child_pid)))
goto err_mtt_fini;
}
ret = mtt_threads_sync_unblock(threads_num_to_join);
if (ret)
result = ret;
if (child_pid && (result = mtt_check_child_process(child_pid)))
goto err_mtt_fini;
if (test->child_start == MTT_START_CHILD_BEFORE_JOINING_THREADS &&
test->child_process_func) {
child_pid = mtt_start_child_process(test->child_process_func, test->child_prestate);
if (child_pid == -1) {
MTT_INTERNAL_ERR("starting the child process failed");
result = -1;
goto err_mtt_fini;
}
if (child_pid && (result = mtt_check_child_process(child_pid)))
goto err_mtt_fini;
}
/* wait for threads to join */
for (i = 0; i < threads_num_to_join; i++) {
ret = pthread_join(threads[i], (void **)&tr);
if (ret != 0) {
MTT_TEST_ERR(i, "pthread_join() failed: %s", strerror(ret));
result = ret;
} else if (tr == NULL) {
MTT_TEST_ERR(i, "returned a NULL result");
result = -1;
} else if (tr->ret != 0) {
MTT_TEST_ERR(i, "%s", tr->errmsg);
result = tr->ret;
}
}
err_mtt_fini:
ret = mtt_fini();
if (ret)
result = ret;
err_thread_seq_fini_func:
/* clean up threads' arguments */
if (test->thread_seq_fini_func) {
for (i = 0; i < threads_num_to_fini; i++) {
ta = &threads_args[i];
MTT_CALL_INIT_FINI(test, thread_seq_fini_func, ta, &tr_local);
if (tr_local.ret) {
MTT_TEST_ERR(i, "%s", tr_local.errmsg);
result = tr_local.ret;
tr_local.ret = 0;
}
}
}
err_free_threads_args:
free(threads_args);
err_free_threads:
free(threads);
err_prestate_fini_func:
/* clean up the prestate */
if (test->prestate_fini_func) {
test->prestate_fini_func(test->prestate, &tr_local);
if (tr_local.ret) {
MTT_INTERNAL_ERR("%s", tr_local.errmsg);
result = tr_local.ret;
}
}
err_kill_child:
if (child_pid) {
if (child_pid == -1) {
/* starting the child process failed */
return -1;
}
/* check if the child process is still running */
if (kill(child_pid, 0) == 0) {
/* kill the child process */
if (kill(child_pid, SIGTERM))
(void) kill(child_pid, SIGKILL);
}
}
return result;
}
|