xref: /spdk/test/unit/lib/event/reactor.c/reactor_ut.c (revision 06b537bfdb4393dea857e204b85d8df46a351d8a)
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 #include "common/lib/test_env.c"
38 #include "event/reactor.c"
39 
40 static void
41 test_create_reactor(void)
42 {
43 	struct spdk_reactor reactor = {};
44 
45 	g_reactors = &reactor;
46 
47 	reactor_construct(&reactor, 0);
48 
49 	CU_ASSERT(spdk_reactor_get(0) == &reactor);
50 
51 	spdk_ring_free(reactor.events);
52 	g_reactors = NULL;
53 }
54 
55 static void
56 test_init_reactors(void)
57 {
58 	uint32_t core;
59 
60 	allocate_cores(3);
61 
62 	CU_ASSERT(spdk_reactors_init() == 0);
63 
64 	CU_ASSERT(g_reactor_state == SPDK_REACTOR_STATE_INITIALIZED);
65 	for (core = 0; core < 3; core++) {
66 		CU_ASSERT(spdk_reactor_get(core) != NULL);
67 	}
68 
69 	spdk_reactors_fini();
70 
71 	free_cores();
72 }
73 
74 static void
75 ut_event_fn(void *arg1, void *arg2)
76 {
77 	uint8_t *test1 = arg1;
78 	uint8_t *test2 = arg2;
79 
80 	*test1 = 1;
81 	*test2 = 0xFF;
82 }
83 
84 static void
85 test_event_call(void)
86 {
87 	uint8_t test1 = 0, test2 = 0;
88 	struct spdk_event *evt;
89 	struct spdk_reactor *reactor;
90 
91 	allocate_cores(1);
92 
93 	CU_ASSERT(spdk_reactors_init() == 0);
94 
95 	evt = spdk_event_allocate(0, ut_event_fn, &test1, &test2);
96 	CU_ASSERT(evt != NULL);
97 
98 	spdk_event_call(evt);
99 
100 	reactor = spdk_reactor_get(0);
101 	CU_ASSERT(reactor != NULL);
102 
103 	CU_ASSERT(event_queue_run_batch(reactor) == 1);
104 	CU_ASSERT(test1 == 1);
105 	CU_ASSERT(test2 == 0xFF);
106 
107 	spdk_reactors_fini();
108 
109 	free_cores();
110 }
111 
112 static void
113 test_schedule_thread(void)
114 {
115 	struct spdk_cpuset cpuset = {};
116 	struct spdk_thread *thread;
117 	struct spdk_reactor *reactor;
118 	struct spdk_lw_thread *lw_thread;
119 
120 	allocate_cores(5);
121 
122 	CU_ASSERT(spdk_reactors_init() == 0);
123 
124 	spdk_cpuset_set_cpu(&cpuset, 3, true);
125 	g_next_core = 4;
126 
127 	/* _reactor_schedule_thread() will be called in spdk_thread_create()
128 	 * at its end because it is passed to SPDK thread library by
129 	 * spdk_thread_lib_init().
130 	 */
131 	thread = spdk_thread_create(NULL, &cpuset);
132 	CU_ASSERT(thread != NULL);
133 
134 	reactor = spdk_reactor_get(3);
135 	CU_ASSERT(reactor != NULL);
136 
137 	MOCK_SET(spdk_env_get_current_core, 3);
138 
139 	CU_ASSERT(event_queue_run_batch(reactor) == 1);
140 
141 	MOCK_CLEAR(spdk_env_get_current_core);
142 
143 	lw_thread = TAILQ_FIRST(&reactor->threads);
144 	CU_ASSERT(lw_thread != NULL);
145 	CU_ASSERT(spdk_thread_get_from_ctx(lw_thread) == thread);
146 
147 	TAILQ_REMOVE(&reactor->threads, lw_thread, link);
148 	reactor->thread_count--;
149 	spdk_set_thread(thread);
150 	spdk_thread_exit(thread);
151 	while (!spdk_thread_is_exited(thread)) {
152 		spdk_thread_poll(thread, 0, 0);
153 	}
154 	spdk_thread_destroy(thread);
155 	spdk_set_thread(NULL);
156 
157 	spdk_reactors_fini();
158 
159 	free_cores();
160 }
161 
162 static void
163 test_reschedule_thread(void)
164 {
165 	struct spdk_cpuset cpuset = {};
166 	struct spdk_thread *thread;
167 	struct spdk_reactor *reactor;
168 	struct spdk_lw_thread *lw_thread;
169 
170 	allocate_cores(3);
171 
172 	CU_ASSERT(spdk_reactors_init() == 0);
173 
174 	spdk_cpuset_set_cpu(&g_reactor_core_mask, 0, true);
175 	spdk_cpuset_set_cpu(&g_reactor_core_mask, 1, true);
176 	spdk_cpuset_set_cpu(&g_reactor_core_mask, 2, true);
177 	g_next_core = 0;
178 
179 	/* Create and schedule the thread to core 1. */
180 	spdk_cpuset_set_cpu(&cpuset, 1, true);
181 
182 	thread = spdk_thread_create(NULL, &cpuset);
183 	CU_ASSERT(thread != NULL);
184 	lw_thread = spdk_thread_get_ctx(thread);
185 
186 	reactor = spdk_reactor_get(1);
187 	CU_ASSERT(reactor != NULL);
188 	MOCK_SET(spdk_env_get_current_core, 1);
189 
190 	CU_ASSERT(event_queue_run_batch(reactor) == 1);
191 	CU_ASSERT(TAILQ_FIRST(&reactor->threads) == lw_thread);
192 
193 	spdk_set_thread(thread);
194 
195 	/* Call spdk_thread_set_cpumask() twice with different cpumask values.
196 	 * The cpumask of the 2nd call will be used in reschedule operation.
197 	 */
198 
199 	spdk_cpuset_zero(&cpuset);
200 	spdk_cpuset_set_cpu(&cpuset, 0, true);
201 	CU_ASSERT(spdk_thread_set_cpumask(&cpuset) == 0);
202 
203 	spdk_cpuset_zero(&cpuset);
204 	spdk_cpuset_set_cpu(&cpuset, 2, true);
205 	CU_ASSERT(spdk_thread_set_cpumask(&cpuset) == 0);
206 
207 	CU_ASSERT(lw_thread->resched == true);
208 
209 	reactor_run(reactor);
210 
211 	CU_ASSERT(lw_thread->resched == false);
212 	CU_ASSERT(TAILQ_EMPTY(&reactor->threads));
213 
214 	reactor = spdk_reactor_get(0);
215 	CU_ASSERT(reactor != NULL);
216 	MOCK_SET(spdk_env_get_current_core, 0);
217 
218 	CU_ASSERT(event_queue_run_batch(reactor) == 0);
219 
220 	reactor = spdk_reactor_get(2);
221 	CU_ASSERT(reactor != NULL);
222 	MOCK_SET(spdk_env_get_current_core, 2);
223 
224 	CU_ASSERT(event_queue_run_batch(reactor) == 1);
225 
226 	CU_ASSERT(TAILQ_FIRST(&reactor->threads) == lw_thread);
227 
228 	MOCK_CLEAR(spdk_env_get_current_core);
229 
230 	TAILQ_REMOVE(&reactor->threads, lw_thread, link);
231 	reactor->thread_count--;
232 	spdk_set_thread(thread);
233 	spdk_thread_exit(thread);
234 	while (!spdk_thread_is_exited(thread)) {
235 		spdk_thread_poll(thread, 0, 0);
236 	}
237 	spdk_thread_destroy(thread);
238 	spdk_set_thread(NULL);
239 
240 	spdk_reactors_fini();
241 
242 	free_cores();
243 }
244 
245 static void
246 for_each_reactor_done(void *arg1, void *arg2)
247 {
248 	uint32_t *count = arg1;
249 	bool *done = arg2;
250 
251 	(*count)++;
252 	*done = true;
253 }
254 
255 static void
256 for_each_reactor_cb(void *arg1, void *arg2)
257 {
258 	uint32_t *count = arg1;
259 
260 	(*count)++;
261 }
262 
263 static void
264 test_for_each_reactor(void)
265 {
266 	uint32_t count = 0, i;
267 	bool done = false;
268 	struct spdk_reactor *reactor;
269 
270 	allocate_cores(5);
271 
272 	CU_ASSERT(spdk_reactors_init() == 0);
273 
274 	MOCK_SET(spdk_env_get_current_core, 0);
275 
276 	spdk_for_each_reactor(for_each_reactor_cb, &count, &done, for_each_reactor_done);
277 
278 	MOCK_CLEAR(spdk_env_get_current_core);
279 
280 	/* We have not processed any event yet, so count and done should be 0 and false,
281 	 *  respectively.
282 	 */
283 	CU_ASSERT(count == 0);
284 
285 	/* Poll each reactor to verify the event is passed to each */
286 	for (i = 0; i < 5; i++) {
287 		reactor = spdk_reactor_get(i);
288 		CU_ASSERT(reactor != NULL);
289 
290 		event_queue_run_batch(reactor);
291 		CU_ASSERT(count == (i + 1));
292 		CU_ASSERT(done == false);
293 	}
294 
295 	/* After each reactor is called, the completion calls it one more time. */
296 	reactor = spdk_reactor_get(0);
297 	CU_ASSERT(reactor != NULL);
298 
299 	event_queue_run_batch(reactor);
300 	CU_ASSERT(count == 6);
301 	CU_ASSERT(done == true);
302 
303 	spdk_reactors_fini();
304 
305 	free_cores();
306 }
307 
308 static int
309 poller_run_idle(void *ctx)
310 {
311 	uint64_t delay_us = (uint64_t)ctx;
312 
313 	spdk_delay_us(delay_us);
314 
315 	return 0;
316 }
317 
318 static int
319 poller_run_busy(void *ctx)
320 {
321 	uint64_t delay_us = (uint64_t)ctx;
322 
323 	spdk_delay_us(delay_us);
324 
325 	return 1;
326 }
327 
328 static void
329 test_reactor_stats(void)
330 {
331 	struct spdk_cpuset cpuset = {};
332 	struct spdk_thread *thread1, *thread2;
333 	struct spdk_reactor *reactor;
334 	struct spdk_poller *busy1, *idle1, *busy2, *idle2;
335 	int rc __attribute__((unused));
336 
337 	/* Test case is the following:
338 	 * Create a reactor on CPU core0.
339 	 * Create thread1 and thread2 simultaneously on reactor0 at TSC = 100.
340 	 * Reactor runs
341 	 * - thread1 for 100 with busy
342 	 * - thread2 for 200 with idle
343 	 * - thread1 for 300 with idle
344 	 * - thread2 for 400 with busy.
345 	 * Then,
346 	 * - both elapsed TSC of thread1 and thread2 should be 1000 (= 100 + 900).
347 	 * - busy TSC of reactor should be 500 (= 100 + 400).
348 	 * - idle TSC of reactor should be 500 (= 200 + 300).
349 	 */
350 
351 	allocate_cores(1);
352 
353 	CU_ASSERT(spdk_reactors_init() == 0);
354 
355 	spdk_cpuset_set_cpu(&cpuset, 0, true);
356 
357 	MOCK_SET(spdk_env_get_current_core, 0);
358 	MOCK_SET(spdk_get_ticks, 100);
359 
360 	thread1 = spdk_thread_create(NULL, &cpuset);
361 	SPDK_CU_ASSERT_FATAL(thread1 != NULL);
362 
363 	thread2 = spdk_thread_create(NULL, &cpuset);
364 	SPDK_CU_ASSERT_FATAL(thread2 != NULL);
365 
366 	reactor = spdk_reactor_get(0);
367 	SPDK_CU_ASSERT_FATAL(reactor != NULL);
368 
369 	reactor->tsc_last = 100;
370 
371 	spdk_set_thread(thread1);
372 	busy1 = spdk_poller_register(poller_run_busy, (void *)100, 0);
373 	CU_ASSERT(busy1 != NULL);
374 
375 	spdk_set_thread(thread2);
376 	idle2 = spdk_poller_register(poller_run_idle, (void *)300, 0);
377 	CU_ASSERT(idle2 != NULL);
378 
379 	_reactor_run(reactor);
380 
381 	CU_ASSERT(thread1->tsc_last == 200);
382 	CU_ASSERT(thread1->stats.busy_tsc == 100);
383 	CU_ASSERT(thread1->stats.idle_tsc == 0);
384 	CU_ASSERT(thread2->tsc_last == 500);
385 	CU_ASSERT(thread2->stats.busy_tsc == 0);
386 	CU_ASSERT(thread2->stats.idle_tsc == 300);
387 
388 	CU_ASSERT(reactor->busy_tsc == 100);
389 	CU_ASSERT(reactor->idle_tsc == 300);
390 
391 	spdk_set_thread(thread1);
392 	spdk_poller_unregister(&busy1);
393 	idle1 = spdk_poller_register(poller_run_idle, (void *)200, 0);
394 	CU_ASSERT(idle1 != NULL);
395 
396 	spdk_set_thread(thread2);
397 	spdk_poller_unregister(&idle2);
398 	busy2 = spdk_poller_register(poller_run_busy, (void *)400, 0);
399 	CU_ASSERT(busy2 != NULL);
400 
401 	_reactor_run(reactor);
402 
403 	CU_ASSERT(thread1->tsc_last == 700);
404 	CU_ASSERT(thread1->stats.busy_tsc == 100);
405 	CU_ASSERT(thread1->stats.idle_tsc == 200);
406 	CU_ASSERT(thread2->tsc_last == 1100);
407 	CU_ASSERT(thread2->stats.busy_tsc == 400);
408 	CU_ASSERT(thread2->stats.idle_tsc == 300);
409 
410 	CU_ASSERT(reactor->busy_tsc == 500);
411 	CU_ASSERT(reactor->idle_tsc == 500);
412 
413 	spdk_set_thread(thread1);
414 	spdk_poller_unregister(&idle1);
415 	spdk_thread_exit(thread1);
416 
417 	spdk_set_thread(thread2);
418 	spdk_poller_unregister(&busy2);
419 	spdk_thread_exit(thread2);
420 
421 	_reactor_run(reactor);
422 
423 	CU_ASSERT(TAILQ_EMPTY(&reactor->threads));
424 
425 	spdk_reactors_fini();
426 
427 	free_cores();
428 }
429 
430 int
431 main(int argc, char **argv)
432 {
433 	CU_pSuite suite = NULL;
434 	unsigned int num_failures;
435 
436 	CU_set_error_action(CUEA_ABORT);
437 	CU_initialize_registry();
438 
439 	suite = CU_add_suite("app_suite", NULL, NULL);
440 
441 	CU_ADD_TEST(suite, test_create_reactor);
442 	CU_ADD_TEST(suite, test_init_reactors);
443 	CU_ADD_TEST(suite, test_event_call);
444 	CU_ADD_TEST(suite, test_schedule_thread);
445 	CU_ADD_TEST(suite, test_reschedule_thread);
446 	CU_ADD_TEST(suite, test_for_each_reactor);
447 	CU_ADD_TEST(suite, test_reactor_stats);
448 
449 	CU_basic_set_mode(CU_BRM_VERBOSE);
450 	CU_basic_run_tests();
451 	num_failures = CU_get_number_of_failures();
452 	CU_cleanup_registry();
453 
454 	return num_failures;
455 }
456