xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/test/regress_thread.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: regress_thread.c,v 1.1.1.1 2013/12/27 23:31:30 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 #include "util-internal.h"
29 
30 /* The old tests here need assertions to work. */
31 #undef NDEBUG
32 
33 #include "event2/event-config.h"
34 
35 #include <sys/types.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #ifdef EVENT__HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 #ifdef EVENT__HAVE_SYS_WAIT_H
43 #include <sys/wait.h>
44 #endif
45 
46 #ifdef EVENT__HAVE_PTHREADS
47 #include <pthread.h>
48 #elif defined(_WIN32)
49 #include <process.h>
50 #endif
51 #include <assert.h>
52 #ifdef EVENT__HAVE_UNISTD_H
53 #include <unistd.h>
54 #endif
55 #include <time.h>
56 
57 #include "sys/queue.h"
58 
59 #include "event2/event.h"
60 #include "event2/event_struct.h"
61 #include "event2/thread.h"
62 #include "event2/util.h"
63 #include "evthread-internal.h"
64 #include "event-internal.h"
65 #include "defer-internal.h"
66 #include "regress.h"
67 #include "tinytest_macros.h"
68 #include "time-internal.h"
69 
70 #ifdef EVENT__HAVE_PTHREADS
71 #define THREAD_T pthread_t
72 #define THREAD_FN void *
73 #define THREAD_RETURN() return (NULL)
74 #define THREAD_START(threadvar, fn, arg) \
75 	pthread_create(&(threadvar), NULL, fn, arg)
76 #define THREAD_JOIN(th) pthread_join(th, NULL)
77 #else
78 #define THREAD_T HANDLE
79 #define THREAD_FN unsigned __stdcall
80 #define THREAD_RETURN() return (0)
81 #define THREAD_START(threadvar, fn, arg) do {		\
82 	uintptr_t threadhandle = _beginthreadex(NULL,0,fn,(arg),0,NULL); \
83 	(threadvar) = (HANDLE) threadhandle; \
84 	} while (0)
85 #define THREAD_JOIN(th) WaitForSingleObject(th, INFINITE)
86 #endif
87 
88 struct cond_wait {
89 	void *lock;
90 	void *cond;
91 };
92 
93 static void
94 wake_all_timeout(evutil_socket_t fd, short what, void *arg)
95 {
96 	struct cond_wait *cw = arg;
97 	EVLOCK_LOCK(cw->lock, 0);
98 	EVTHREAD_COND_BROADCAST(cw->cond);
99 	EVLOCK_UNLOCK(cw->lock, 0);
100 
101 }
102 
103 static void
104 wake_one_timeout(evutil_socket_t fd, short what, void *arg)
105 {
106 	struct cond_wait *cw = arg;
107 	EVLOCK_LOCK(cw->lock, 0);
108 	EVTHREAD_COND_SIGNAL(cw->cond);
109 	EVLOCK_UNLOCK(cw->lock, 0);
110 }
111 
112 #define NUM_THREADS	100
113 #define NUM_ITERATIONS  100
114 void *count_lock;
115 static int count;
116 
117 static THREAD_FN
118 basic_thread(void *arg)
119 {
120 	struct cond_wait cw;
121 	struct event_base *base = arg;
122 	struct event ev;
123 	int i = 0;
124 
125 	EVTHREAD_ALLOC_LOCK(cw.lock, 0);
126 	EVTHREAD_ALLOC_COND(cw.cond);
127 	assert(cw.lock);
128 	assert(cw.cond);
129 
130 	evtimer_assign(&ev, base, wake_all_timeout, &cw);
131 	for (i = 0; i < NUM_ITERATIONS; i++) {
132 		struct timeval tv;
133 		evutil_timerclear(&tv);
134 		tv.tv_sec = 0;
135 		tv.tv_usec = 3000;
136 
137 		EVLOCK_LOCK(cw.lock, 0);
138 		/* we need to make sure that event does not happen before
139 		 * we get to wait on the conditional variable */
140 		assert(evtimer_add(&ev, &tv) == 0);
141 
142 		assert(EVTHREAD_COND_WAIT(cw.cond, cw.lock) == 0);
143 		EVLOCK_UNLOCK(cw.lock, 0);
144 
145 		EVLOCK_LOCK(count_lock, 0);
146 		++count;
147 		EVLOCK_UNLOCK(count_lock, 0);
148 	}
149 
150 	/* exit the loop only if all threads fired all timeouts */
151 	EVLOCK_LOCK(count_lock, 0);
152 	if (count >= NUM_THREADS * NUM_ITERATIONS)
153 		event_base_loopexit(base, NULL);
154 	EVLOCK_UNLOCK(count_lock, 0);
155 
156 	EVTHREAD_FREE_LOCK(cw.lock, 0);
157 	EVTHREAD_FREE_COND(cw.cond);
158 
159 	THREAD_RETURN();
160 }
161 
162 static int notification_fd_used = 0;
163 #ifndef _WIN32
164 static int got_sigchld = 0;
165 static void
166 sigchld_cb(evutil_socket_t fd, short event, void *arg)
167 {
168 	struct timeval tv;
169 	struct event_base *base = arg;
170 
171 	got_sigchld++;
172 	tv.tv_usec = 100000;
173 	tv.tv_sec = 0;
174 	event_base_loopexit(base, &tv);
175 }
176 
177 
178 static void
179 notify_fd_cb(evutil_socket_t fd, short event, void *arg)
180 {
181 	++notification_fd_used;
182 }
183 #endif
184 
185 static void
186 thread_basic(void *arg)
187 {
188 	THREAD_T threads[NUM_THREADS];
189 	struct event ev;
190 	struct timeval tv;
191 	int i;
192 	struct basic_test_data *data = arg;
193 	struct event_base *base = data->base;
194 
195 	struct event *notification_event = NULL;
196 	struct event *sigchld_event = NULL;
197 
198 	EVTHREAD_ALLOC_LOCK(count_lock, 0);
199 	tt_assert(count_lock);
200 
201 	tt_assert(base);
202 	if (evthread_make_base_notifiable(base)<0) {
203 		tt_abort_msg("Couldn't make base notifiable!");
204 	}
205 
206 #ifndef _WIN32
207 	if (data->setup_data && !strcmp(data->setup_data, "forking")) {
208 		pid_t pid;
209 		int status;
210 		sigchld_event = evsignal_new(base, SIGCHLD, sigchld_cb, base);
211 		/* This piggybacks on the th_notify_fd weirdly, and looks
212 		 * inside libevent internals.  Not a good idea in non-testing
213 		 * code! */
214 		notification_event = event_new(base,
215 		    base->th_notify_fd[0], EV_READ|EV_PERSIST, notify_fd_cb,
216 		    NULL);
217 		event_add(sigchld_event, NULL);
218 		event_add(notification_event, NULL);
219 
220 		if ((pid = fork()) == 0) {
221 			event_del(notification_event);
222 			if (event_reinit(base) < 0) {
223 				TT_FAIL(("reinit"));
224 				exit(1);
225 			}
226 			event_assign(notification_event, base,
227 			    base->th_notify_fd[0], EV_READ|EV_PERSIST,
228 			    notify_fd_cb, NULL);
229 			event_add(notification_event, NULL);
230 	 		goto child;
231 		}
232 
233 		event_base_dispatch(base);
234 
235 		if (waitpid(pid, &status, 0) == -1)
236 			tt_abort_perror("waitpid");
237 		TT_BLATHER(("Waitpid okay\n"));
238 
239 		tt_assert(got_sigchld);
240 		tt_int_op(notification_fd_used, ==, 0);
241 
242 		goto end;
243 	}
244 
245 child:
246 #endif
247 	for (i = 0; i < NUM_THREADS; ++i)
248 		THREAD_START(threads[i], basic_thread, base);
249 
250 	evtimer_assign(&ev, base, NULL, NULL);
251 	evutil_timerclear(&tv);
252 	tv.tv_sec = 1000;
253 	event_add(&ev, &tv);
254 
255 	event_base_dispatch(base);
256 
257 	for (i = 0; i < NUM_THREADS; ++i)
258 		THREAD_JOIN(threads[i]);
259 
260 	event_del(&ev);
261 
262 	tt_int_op(count, ==, NUM_THREADS * NUM_ITERATIONS);
263 
264 	EVTHREAD_FREE_LOCK(count_lock, 0);
265 
266 	TT_BLATHER(("notifiations==%d", notification_fd_used));
267 
268 end:
269 
270 	if (notification_event)
271 		event_free(notification_event);
272 	if (sigchld_event)
273 		event_free(sigchld_event);
274 }
275 
276 #undef NUM_THREADS
277 #define NUM_THREADS 10
278 
279 struct alerted_record {
280 	struct cond_wait *cond;
281 	struct timeval delay;
282 	struct timeval alerted_at;
283 	int timed_out;
284 };
285 
286 static THREAD_FN
287 wait_for_condition(void *arg)
288 {
289 	struct alerted_record *rec = arg;
290 	int r;
291 
292 	EVLOCK_LOCK(rec->cond->lock, 0);
293 	if (rec->delay.tv_sec || rec->delay.tv_usec) {
294 		r = EVTHREAD_COND_WAIT_TIMED(rec->cond->cond, rec->cond->lock,
295 		    &rec->delay);
296 	} else {
297 		r = EVTHREAD_COND_WAIT(rec->cond->cond, rec->cond->lock);
298 	}
299 	EVLOCK_UNLOCK(rec->cond->lock, 0);
300 
301 	evutil_gettimeofday(&rec->alerted_at, NULL);
302 	if (r == 1)
303 		rec->timed_out = 1;
304 
305 	THREAD_RETURN();
306 }
307 
308 static void
309 thread_conditions_simple(void *arg)
310 {
311 	struct timeval tv_signal, tv_timeout, tv_broadcast;
312 	struct alerted_record alerted[NUM_THREADS];
313 	THREAD_T threads[NUM_THREADS];
314 	struct cond_wait cond;
315 	int i;
316 	struct timeval launched_at;
317 	struct event wake_one;
318 	struct event wake_all;
319 	struct basic_test_data *data = arg;
320 	struct event_base *base = data->base;
321 	int n_timed_out=0, n_signal=0, n_broadcast=0;
322 
323 	tv_signal.tv_sec = tv_timeout.tv_sec = tv_broadcast.tv_sec = 0;
324 	tv_signal.tv_usec = 30*1000;
325 	tv_timeout.tv_usec = 150*1000;
326 	tv_broadcast.tv_usec = 500*1000;
327 
328 	EVTHREAD_ALLOC_LOCK(cond.lock, EVTHREAD_LOCKTYPE_RECURSIVE);
329 	EVTHREAD_ALLOC_COND(cond.cond);
330 	tt_assert(cond.lock);
331 	tt_assert(cond.cond);
332 	for (i = 0; i < NUM_THREADS; ++i) {
333 		memset(&alerted[i], 0, sizeof(struct alerted_record));
334 		alerted[i].cond = &cond;
335 	}
336 
337 	/* Threads 5 and 6 will be allowed to time out */
338 	memcpy(&alerted[5].delay, &tv_timeout, sizeof(tv_timeout));
339 	memcpy(&alerted[6].delay, &tv_timeout, sizeof(tv_timeout));
340 
341 	evtimer_assign(&wake_one, base, wake_one_timeout, &cond);
342 	evtimer_assign(&wake_all, base, wake_all_timeout, &cond);
343 
344 	evutil_gettimeofday(&launched_at, NULL);
345 
346 	/* Launch the threads... */
347 	for (i = 0; i < NUM_THREADS; ++i) {
348 		THREAD_START(threads[i], wait_for_condition, &alerted[i]);
349 	}
350 
351 	/* Start the timers... */
352 	tt_int_op(event_add(&wake_one, &tv_signal), ==, 0);
353 	tt_int_op(event_add(&wake_all, &tv_broadcast), ==, 0);
354 
355 	/* And run for a bit... */
356 	event_base_dispatch(base);
357 
358 	/* And wait till the threads are done. */
359 	for (i = 0; i < NUM_THREADS; ++i)
360 		THREAD_JOIN(threads[i]);
361 
362 	/* Now, let's see what happened. At least one of 5 or 6 should
363 	 * have timed out. */
364 	n_timed_out = alerted[5].timed_out + alerted[6].timed_out;
365 	tt_int_op(n_timed_out, >=, 1);
366 	tt_int_op(n_timed_out, <=, 2);
367 
368 	for (i = 0; i < NUM_THREADS; ++i) {
369 		const struct timeval *target_delay;
370 		struct timeval target_time, actual_delay;
371 		if (alerted[i].timed_out) {
372 			TT_BLATHER(("%d looks like a timeout\n", i));
373 			target_delay = &tv_timeout;
374 			tt_assert(i == 5 || i == 6);
375 		} else if (evutil_timerisset(&alerted[i].alerted_at)) {
376 			long diff1,diff2;
377 			evutil_timersub(&alerted[i].alerted_at,
378 			    &launched_at, &actual_delay);
379 			diff1 = timeval_msec_diff(&actual_delay,
380 			    &tv_signal);
381 			diff2 = timeval_msec_diff(&actual_delay,
382 			    &tv_broadcast);
383 			if (abs(diff1) < abs(diff2)) {
384 				TT_BLATHER(("%d looks like a signal\n", i));
385 				target_delay = &tv_signal;
386 				++n_signal;
387 			} else {
388 				TT_BLATHER(("%d looks like a broadcast\n", i));
389 				target_delay = &tv_broadcast;
390 				++n_broadcast;
391 			}
392 		} else {
393 			TT_FAIL(("Thread %d never got woken", i));
394 			continue;
395 		}
396 		evutil_timeradd(target_delay, &launched_at, &target_time);
397 		test_timeval_diff_leq(&target_time, &alerted[i].alerted_at,
398 		    0, 50);
399 	}
400 	tt_int_op(n_broadcast + n_signal + n_timed_out, ==, NUM_THREADS);
401 	tt_int_op(n_signal, ==, 1);
402 
403 end:
404 	;
405 }
406 
407 #define CB_COUNT 128
408 #define QUEUE_THREAD_COUNT 8
409 
410 static void
411 SLEEP_MS(int ms)
412 {
413 	struct timeval tv;
414 	tv.tv_sec = ms/1000;
415 	tv.tv_usec = (ms%1000)*1000;
416 	evutil_usleep_(&tv);
417 }
418 
419 struct deferred_test_data {
420 	struct event_callback cbs[CB_COUNT];
421 	struct event_base *queue;
422 };
423 
424 static struct timeval timer_start = {0,0};
425 static struct timeval timer_end = {0,0};
426 static unsigned callback_count = 0;
427 static THREAD_T load_threads[QUEUE_THREAD_COUNT];
428 static struct deferred_test_data deferred_data[QUEUE_THREAD_COUNT];
429 
430 static void
431 deferred_callback(struct event_callback *cb, void *arg)
432 {
433 	SLEEP_MS(1);
434 	callback_count += 1;
435 }
436 
437 static THREAD_FN
438 load_deferred_queue(void *arg)
439 {
440 	struct deferred_test_data *data = arg;
441 	size_t i;
442 
443 	for (i = 0; i < CB_COUNT; ++i) {
444 		event_deferred_cb_init_(&data->cbs[i], 0, deferred_callback,
445 		    NULL);
446 		event_deferred_cb_schedule_(data->queue, &data->cbs[i]);
447 		SLEEP_MS(1);
448 	}
449 
450 	THREAD_RETURN();
451 }
452 
453 static void
454 timer_callback(evutil_socket_t fd, short what, void *arg)
455 {
456 	evutil_gettimeofday(&timer_end, NULL);
457 }
458 
459 static void
460 start_threads_callback(evutil_socket_t fd, short what, void *arg)
461 {
462 	int i;
463 
464 	for (i = 0; i < QUEUE_THREAD_COUNT; ++i) {
465 		THREAD_START(load_threads[i], load_deferred_queue,
466 				&deferred_data[i]);
467 	}
468 }
469 
470 static void
471 thread_deferred_cb_skew(void *arg)
472 {
473 	struct timeval tv_timer = {1, 0};
474 	struct event_base *base = NULL;
475 	struct event_config *cfg = NULL;
476 	struct timeval elapsed;
477 	int elapsed_usec;
478 	int i;
479 
480 	cfg = event_config_new();
481 	tt_assert(cfg);
482 	event_config_set_max_dispatch_interval(cfg, NULL, 16, 0);
483 
484 	base = event_base_new_with_config(cfg);
485 	tt_assert(base);
486 
487 	for (i = 0; i < QUEUE_THREAD_COUNT; ++i)
488 		deferred_data[i].queue = base;
489 
490 	evutil_gettimeofday(&timer_start, NULL);
491 	event_base_once(base, -1, EV_TIMEOUT, timer_callback, NULL,
492 			&tv_timer);
493 	event_base_once(base, -1, EV_TIMEOUT, start_threads_callback,
494 			NULL, NULL);
495 	event_base_dispatch(base);
496 
497 	evutil_timersub(&timer_end, &timer_start, &elapsed);
498 	TT_BLATHER(("callback count, %u", callback_count));
499 	elapsed_usec =
500 	    (unsigned)(elapsed.tv_sec*1000000 + elapsed.tv_usec);
501 	TT_BLATHER(("elapsed time, %u usec", elapsed_usec));
502 
503 	/* XXX be more intelligent here.  just make sure skew is
504 	 * within .4 seconds for now. */
505 	tt_assert(elapsed_usec >= 600000 && elapsed_usec <= 1400000);
506 
507 end:
508 	for (i = 0; i < QUEUE_THREAD_COUNT; ++i)
509 		THREAD_JOIN(load_threads[i]);
510 	if (base)
511 		event_base_free(base);
512 	if (cfg)
513 		event_config_free(cfg);
514 }
515 
516 static struct event time_events[5];
517 static struct timeval times[5];
518 static struct event_base *exit_base = NULL;
519 static void
520 note_time_cb(evutil_socket_t fd, short what, void *arg)
521 {
522 	evutil_gettimeofday(arg, NULL);
523 	if (arg == &times[4]) {
524 		event_base_loopbreak(exit_base);
525 	}
526 }
527 static THREAD_FN
528 register_events_subthread(void *arg)
529 {
530 	struct timeval tv = {0,0};
531 	SLEEP_MS(100);
532 	event_active(&time_events[0], EV_TIMEOUT, 1);
533 	SLEEP_MS(100);
534 	event_active(&time_events[1], EV_TIMEOUT, 1);
535 	SLEEP_MS(100);
536 	tv.tv_usec = 100*1000;
537 	event_add(&time_events[2], &tv);
538 	tv.tv_usec = 150*1000;
539 	event_add(&time_events[3], &tv);
540 	SLEEP_MS(200);
541 	event_active(&time_events[4], EV_TIMEOUT, 1);
542 
543 	THREAD_RETURN();
544 }
545 
546 static void
547 thread_no_events(void *arg)
548 {
549 	THREAD_T thread;
550 	struct basic_test_data *data = arg;
551 	struct timeval starttime, endtime;
552 	int i;
553 	exit_base = data->base;
554 
555 	memset(times,0,sizeof(times));
556 	for (i=0;i<5;++i) {
557 		event_assign(&time_events[i], data->base,
558 		    -1, 0, note_time_cb, &times[i]);
559 	}
560 
561 	evutil_gettimeofday(&starttime, NULL);
562 	THREAD_START(thread, register_events_subthread, data->base);
563 	event_base_loop(data->base, EVLOOP_NO_EXIT_ON_EMPTY);
564 	evutil_gettimeofday(&endtime, NULL);
565 	tt_assert(event_base_got_break(data->base));
566 	THREAD_JOIN(thread);
567 	for (i=0; i<5; ++i) {
568 		struct timeval diff;
569 		double sec;
570 		evutil_timersub(&times[i], &starttime, &diff);
571 		sec = diff.tv_sec + diff.tv_usec/1.0e6;
572 		TT_BLATHER(("event %d at %.4f seconds", i, sec));
573 	}
574 	test_timeval_diff_eq(&starttime, &times[0], 100);
575 	test_timeval_diff_eq(&starttime, &times[1], 200);
576 	test_timeval_diff_eq(&starttime, &times[2], 400);
577 	test_timeval_diff_eq(&starttime, &times[3], 450);
578 	test_timeval_diff_eq(&starttime, &times[4], 500);
579 	test_timeval_diff_eq(&starttime, &endtime,  500);
580 
581 end:
582 	;
583 }
584 
585 #define TEST(name)							\
586 	{ #name, thread_##name, TT_FORK|TT_NEED_THREADS|TT_NEED_BASE,	\
587 	  &basic_setup, NULL }
588 
589 struct testcase_t thread_testcases[] = {
590 	{ "basic", thread_basic, TT_FORK|TT_NEED_THREADS|TT_NEED_BASE,
591 	  &basic_setup, NULL },
592 #ifndef _WIN32
593 	{ "forking", thread_basic, TT_FORK|TT_NEED_THREADS|TT_NEED_BASE,
594 	  &basic_setup, (char*)"forking" },
595 #endif
596 	TEST(conditions_simple),
597 	{ "deferred_cb_skew", thread_deferred_cb_skew,
598 	  TT_FORK|TT_NEED_THREADS|TT_OFF_BY_DEFAULT,
599 	  &basic_setup, NULL },
600 	TEST(no_events),
601 	END_OF_TESTCASES
602 };
603 
604