xref: /spdk/test/common/lib/ut_multithread.c (revision b30d57cdad6d2bc75cc1e4e2ebbcebcb0d98dcfa)
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_cunit.h"
35 #include "spdk/thread.h"
36 #include "spdk_internal/mock.h"
37 
38 #include "common/lib/test_env.c"
39 
40 static uint32_t g_ut_num_threads;
41 
42 int allocate_threads(int num_threads);
43 void free_threads(void);
44 void poll_threads(void);
45 bool poll_thread(uintptr_t thread_id);
46 bool poll_thread_times(uintptr_t thread_id, uint32_t max_polls);
47 
48 struct ut_msg {
49 	spdk_msg_fn		fn;
50 	void			*ctx;
51 	TAILQ_ENTRY(ut_msg)	link;
52 };
53 
54 struct ut_thread {
55 	struct spdk_thread	*thread;
56 	struct spdk_io_channel	*ch;
57 };
58 
59 struct ut_thread *g_ut_threads;
60 
61 #define INVALID_THREAD 0x1000
62 
63 static uint64_t g_ut_thread_id = INVALID_THREAD;
64 
65 static void
66 set_thread(uintptr_t thread_id)
67 {
68 	g_ut_thread_id = thread_id;
69 	if (thread_id == INVALID_THREAD) {
70 		spdk_set_thread(NULL);
71 	} else {
72 		spdk_set_thread(g_ut_threads[thread_id].thread);
73 	}
74 
75 }
76 
77 int
78 allocate_threads(int num_threads)
79 {
80 	struct spdk_thread *thread;
81 	uint32_t i;
82 
83 	spdk_thread_lib_init(NULL, 0);
84 
85 	g_ut_num_threads = num_threads;
86 
87 	g_ut_threads = calloc(num_threads, sizeof(*g_ut_threads));
88 	assert(g_ut_threads != NULL);
89 
90 	for (i = 0; i < g_ut_num_threads; i++) {
91 		set_thread(i);
92 		thread = spdk_thread_create(NULL, NULL);
93 		assert(thread != NULL);
94 		g_ut_threads[i].thread = thread;
95 	}
96 
97 	set_thread(INVALID_THREAD);
98 	return 0;
99 }
100 
101 void
102 free_threads(void)
103 {
104 	uint32_t i, num_threads;
105 	struct spdk_thread *thread;
106 
107 	for (i = 0; i < g_ut_num_threads; i++) {
108 		set_thread(i);
109 		thread = g_ut_threads[i].thread;
110 		spdk_thread_exit(thread);
111 	}
112 
113 	num_threads = g_ut_num_threads;
114 
115 	while (num_threads != 0) {
116 		for (i = 0; i < g_ut_num_threads; i++) {
117 			set_thread(i);
118 			thread = g_ut_threads[i].thread;
119 			if (thread == NULL) {
120 				continue;
121 			}
122 
123 			if (spdk_thread_is_exited(thread)) {
124 				g_ut_threads[i].thread = NULL;
125 				num_threads--;
126 				spdk_thread_destroy(thread);
127 			} else {
128 				spdk_thread_poll(thread, 0, 0);
129 			}
130 		}
131 	}
132 
133 	g_ut_num_threads = 0;
134 	free(g_ut_threads);
135 	g_ut_threads = NULL;
136 
137 	spdk_thread_lib_fini();
138 }
139 
140 bool
141 poll_thread_times(uintptr_t thread_id, uint32_t max_polls)
142 {
143 	bool busy = false;
144 	struct ut_thread *thread = &g_ut_threads[thread_id];
145 	uintptr_t original_thread_id;
146 	uint32_t polls_executed = 0;
147 	uint64_t now;
148 
149 	if (max_polls == 0) {
150 		/* If max_polls is set to 0,
151 		 * poll until no operation is pending. */
152 		return poll_thread(thread_id);
153 	}
154 	assert(thread_id != (uintptr_t)INVALID_THREAD);
155 	assert(thread_id < g_ut_num_threads);
156 
157 	original_thread_id = g_ut_thread_id;
158 	set_thread(INVALID_THREAD);
159 
160 	now = spdk_get_ticks();
161 	while (polls_executed < max_polls) {
162 		if (spdk_thread_poll(thread->thread, 1, now) > 0) {
163 			busy = true;
164 		}
165 		now = spdk_thread_get_last_tsc(thread->thread);
166 		polls_executed++;
167 	}
168 
169 	set_thread(original_thread_id);
170 
171 	return busy;
172 }
173 
174 bool
175 poll_thread(uintptr_t thread_id)
176 {
177 	bool busy = false;
178 	struct ut_thread *thread = &g_ut_threads[thread_id];
179 	uintptr_t original_thread_id;
180 	uint64_t now;
181 
182 	assert(thread_id != (uintptr_t)INVALID_THREAD);
183 	assert(thread_id < g_ut_num_threads);
184 
185 	original_thread_id = g_ut_thread_id;
186 	set_thread(INVALID_THREAD);
187 
188 	now = spdk_get_ticks();
189 	while (spdk_thread_poll(thread->thread, 0, now) > 0) {
190 		now = spdk_thread_get_last_tsc(thread->thread);
191 		busy = true;
192 	}
193 
194 	set_thread(original_thread_id);
195 
196 	return busy;
197 }
198 
199 void
200 poll_threads(void)
201 {
202 	while (true) {
203 		bool busy = false;
204 
205 		for (uint32_t i = 0; i < g_ut_num_threads; i++) {
206 			busy = busy || poll_thread(i);
207 		}
208 
209 		if (!busy) {
210 			break;
211 		}
212 	}
213 }
214