2022-06-03 19:15:11 +00:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause
|
2022-11-01 20:26:26 +00:00
|
|
|
* Copyright (C) 2017 Intel Corporation.
|
2017-09-11 20:12:09 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "spdk_cunit.h"
|
2018-06-11 20:32:15 +00:00
|
|
|
#include "spdk/thread.h"
|
2017-09-11 20:12:09 +00:00
|
|
|
#include "spdk_internal/mock.h"
|
|
|
|
|
2018-08-31 17:58:36 +00:00
|
|
|
#include "common/lib/test_env.c"
|
|
|
|
|
2017-09-11 20:12:09 +00:00
|
|
|
static uint32_t g_ut_num_threads;
|
|
|
|
|
|
|
|
int allocate_threads(int num_threads);
|
|
|
|
void free_threads(void);
|
|
|
|
void poll_threads(void);
|
2018-08-31 17:58:36 +00:00
|
|
|
bool poll_thread(uintptr_t thread_id);
|
2020-02-12 12:26:57 +00:00
|
|
|
bool poll_thread_times(uintptr_t thread_id, uint32_t max_polls);
|
2017-09-11 20:12:09 +00:00
|
|
|
|
|
|
|
struct ut_msg {
|
2018-10-10 21:05:04 +00:00
|
|
|
spdk_msg_fn fn;
|
2017-09-11 20:12:09 +00:00
|
|
|
void *ctx;
|
|
|
|
TAILQ_ENTRY(ut_msg) link;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ut_thread {
|
|
|
|
struct spdk_thread *thread;
|
|
|
|
struct spdk_io_channel *ch;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ut_thread *g_ut_threads;
|
|
|
|
|
2018-07-10 17:47:14 +00:00
|
|
|
#define INVALID_THREAD 0x1000
|
|
|
|
|
2020-02-04 05:02:46 +00:00
|
|
|
static uint64_t g_ut_thread_id = INVALID_THREAD;
|
2017-09-11 20:12:09 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
set_thread(uintptr_t thread_id)
|
|
|
|
{
|
2020-02-04 05:02:46 +00:00
|
|
|
g_ut_thread_id = thread_id;
|
2018-07-10 17:47:14 +00:00
|
|
|
if (thread_id == INVALID_THREAD) {
|
2018-09-17 23:18:53 +00:00
|
|
|
spdk_set_thread(NULL);
|
2018-07-10 17:47:14 +00:00
|
|
|
} else {
|
2018-09-17 23:18:53 +00:00
|
|
|
spdk_set_thread(g_ut_threads[thread_id].thread);
|
2018-07-10 17:47:14 +00:00
|
|
|
}
|
2018-09-17 23:18:53 +00:00
|
|
|
|
2017-09-11 20:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
allocate_threads(int num_threads)
|
|
|
|
{
|
|
|
|
struct spdk_thread *thread;
|
|
|
|
uint32_t i;
|
|
|
|
|
2019-05-02 19:34:22 +00:00
|
|
|
spdk_thread_lib_init(NULL, 0);
|
|
|
|
|
2017-09-11 20:12:09 +00:00
|
|
|
g_ut_num_threads = num_threads;
|
|
|
|
|
|
|
|
g_ut_threads = calloc(num_threads, sizeof(*g_ut_threads));
|
2018-12-17 18:30:13 +00:00
|
|
|
assert(g_ut_threads != NULL);
|
2017-09-11 20:12:09 +00:00
|
|
|
|
|
|
|
for (i = 0; i < g_ut_num_threads; i++) {
|
|
|
|
set_thread(i);
|
2019-04-22 20:29:06 +00:00
|
|
|
thread = spdk_thread_create(NULL, NULL);
|
2018-12-17 18:30:13 +00:00
|
|
|
assert(thread != NULL);
|
2017-09-11 20:12:09 +00:00
|
|
|
g_ut_threads[i].thread = thread;
|
|
|
|
}
|
|
|
|
|
2018-07-10 17:47:14 +00:00
|
|
|
set_thread(INVALID_THREAD);
|
2017-09-11 20:12:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
free_threads(void)
|
|
|
|
{
|
2020-04-04 23:19:43 +00:00
|
|
|
uint32_t i, num_threads;
|
2020-04-03 02:16:36 +00:00
|
|
|
struct spdk_thread *thread;
|
2017-09-11 20:12:09 +00:00
|
|
|
|
|
|
|
for (i = 0; i < g_ut_num_threads; i++) {
|
|
|
|
set_thread(i);
|
2020-04-03 02:16:36 +00:00
|
|
|
thread = g_ut_threads[i].thread;
|
2020-04-04 23:19:43 +00:00
|
|
|
spdk_thread_exit(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
num_threads = g_ut_num_threads;
|
|
|
|
|
|
|
|
while (num_threads != 0) {
|
|
|
|
for (i = 0; i < g_ut_num_threads; i++) {
|
|
|
|
set_thread(i);
|
|
|
|
thread = g_ut_threads[i].thread;
|
|
|
|
if (thread == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spdk_thread_is_exited(thread)) {
|
|
|
|
g_ut_threads[i].thread = NULL;
|
|
|
|
num_threads--;
|
|
|
|
spdk_thread_destroy(thread);
|
|
|
|
} else {
|
|
|
|
spdk_thread_poll(thread, 0, 0);
|
|
|
|
}
|
2020-04-03 02:16:36 +00:00
|
|
|
}
|
2017-09-11 20:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_ut_num_threads = 0;
|
|
|
|
free(g_ut_threads);
|
|
|
|
g_ut_threads = NULL;
|
2019-05-02 19:34:22 +00:00
|
|
|
|
|
|
|
spdk_thread_lib_fini();
|
2017-09-11 20:12:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 12:26:57 +00:00
|
|
|
bool
|
|
|
|
poll_thread_times(uintptr_t thread_id, uint32_t max_polls)
|
|
|
|
{
|
|
|
|
bool busy = false;
|
|
|
|
struct ut_thread *thread = &g_ut_threads[thread_id];
|
|
|
|
uintptr_t original_thread_id;
|
|
|
|
uint32_t polls_executed = 0;
|
2020-03-13 02:53:00 +00:00
|
|
|
uint64_t now;
|
2020-02-12 12:26:57 +00:00
|
|
|
|
|
|
|
if (max_polls == 0) {
|
|
|
|
/* If max_polls is set to 0,
|
|
|
|
* poll until no operation is pending. */
|
|
|
|
return poll_thread(thread_id);
|
|
|
|
}
|
|
|
|
assert(thread_id != (uintptr_t)INVALID_THREAD);
|
|
|
|
assert(thread_id < g_ut_num_threads);
|
|
|
|
|
2020-02-04 05:02:46 +00:00
|
|
|
original_thread_id = g_ut_thread_id;
|
2020-02-12 12:26:57 +00:00
|
|
|
set_thread(INVALID_THREAD);
|
|
|
|
|
2020-03-13 02:53:00 +00:00
|
|
|
now = spdk_get_ticks();
|
2020-02-12 12:26:57 +00:00
|
|
|
while (polls_executed < max_polls) {
|
2020-03-13 02:53:00 +00:00
|
|
|
if (spdk_thread_poll(thread->thread, 1, now) > 0) {
|
2020-02-12 12:26:57 +00:00
|
|
|
busy = true;
|
|
|
|
}
|
2020-03-13 02:53:00 +00:00
|
|
|
now = spdk_thread_get_last_tsc(thread->thread);
|
2020-02-12 12:26:57 +00:00
|
|
|
polls_executed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_thread(original_thread_id);
|
|
|
|
|
|
|
|
return busy;
|
|
|
|
}
|
|
|
|
|
2018-08-31 17:58:36 +00:00
|
|
|
bool
|
2017-09-11 20:12:09 +00:00
|
|
|
poll_thread(uintptr_t thread_id)
|
|
|
|
{
|
2018-08-31 17:58:36 +00:00
|
|
|
bool busy = false;
|
2017-09-11 20:12:09 +00:00
|
|
|
struct ut_thread *thread = &g_ut_threads[thread_id];
|
|
|
|
uintptr_t original_thread_id;
|
2020-03-13 02:53:00 +00:00
|
|
|
uint64_t now;
|
2017-09-11 20:12:09 +00:00
|
|
|
|
2018-12-17 21:40:51 +00:00
|
|
|
assert(thread_id != (uintptr_t)INVALID_THREAD);
|
|
|
|
assert(thread_id < g_ut_num_threads);
|
2017-09-11 20:12:09 +00:00
|
|
|
|
2020-02-04 05:02:46 +00:00
|
|
|
original_thread_id = g_ut_thread_id;
|
2018-09-17 23:18:53 +00:00
|
|
|
set_thread(INVALID_THREAD);
|
2017-09-11 20:12:09 +00:00
|
|
|
|
2020-03-13 02:53:00 +00:00
|
|
|
now = spdk_get_ticks();
|
|
|
|
while (spdk_thread_poll(thread->thread, 0, now) > 0) {
|
|
|
|
now = spdk_thread_get_last_tsc(thread->thread);
|
2018-08-31 17:58:36 +00:00
|
|
|
busy = true;
|
2017-09-11 20:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set_thread(original_thread_id);
|
|
|
|
|
2018-08-31 17:58:36 +00:00
|
|
|
return busy;
|
2017-09-11 20:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
poll_threads(void)
|
|
|
|
{
|
|
|
|
while (true) {
|
2018-08-31 17:58:36 +00:00
|
|
|
bool busy = false;
|
2017-09-11 20:12:09 +00:00
|
|
|
|
2018-08-31 17:58:36 +00:00
|
|
|
for (uint32_t i = 0; i < g_ut_num_threads; i++) {
|
|
|
|
busy = busy || poll_thread(i);
|
2017-09-11 20:12:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 17:58:36 +00:00
|
|
|
if (!busy) {
|
2017-09-11 20:12:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|