The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
control_test.c
Go to the documentation of this file.
1/*
2 * control_test.c Tests for control planes
3 *
4 * Version: $Id: 08d17c87bccc5dad2cd0d0b7ba9beaa01491f45a $
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 *
20 * @copyright 2016 Alan DeKok (aland@freeradius.org)
21 */
22
23RCSID("$Id: 08d17c87bccc5dad2cd0d0b7ba9beaa01491f45a $")
24
25#include <freeradius-devel/server/base.h>
26#include <freeradius-devel/io/control.h>
27#include <freeradius-devel/util/debug.h>
28#include <freeradius-devel/util/rand.h>
29#include <freeradius-devel/util/syserror.h>
30#include <freeradius-devel/util/talloc.h>
31#include <freeradius-devel/util/semaphore.h>
32
33#include <sys/event.h>
34#include <sys/socket.h>
35#include <pthread.h>
36#include <sched.h>
37
38#ifdef HAVE_GETOPT_H
39# include <getopt.h>
40#endif
41
42#undef MEM
43#define MEM(x) do { if (!(x)) { fprintf(stderr, "%s[%d] OUT OF MEMORY\n", __FILE__, __LINE__); _exit(EXIT_FAILURE); } } while (0)
44#define MPRINT1 if (debug_lvl) printf
45#define MPRINT2 if (debug_lvl >= 2) printf
46#define MPRINT3 if (debug_lvl >= 3) printf
47#define CONTROL_MAGIC 0xabcd6809
48
49/** Events on the test's control planes
50 *
51 * The test stands in for the network, and only needs one table.
52 */
56
57/** Events whose messages carry a socket, numbered separately from those above
58 */
60 CONTROL_TEST_SOCKET = 0 //!< one end of a socketpair, with the worker's id in it.
62
63typedef struct {
64 size_t id;
66
67static int debug_lvl = 0;
68static size_t max_messages = 10;
69static size_t num_workers = 1;
70static int aq_size = 16;
71static bool el_announces = false; //!< the master never announces, the event list does it.
72static fr_control_t **control = NULL;
73static fr_event_list_t *el = NULL;
74static bool single_aq = true;
75static size_t num_aq = 1;
76static uint32_t max_pause_usec = 0; //!< workers pause up to this long between sends.
77static uint32_t watchdog_secs = 30; //!< fail if the master makes no progress for this long.
78
79static pthread_mutex_t wd_mutex = PTHREAD_MUTEX_INITIALIZER;
80static pthread_cond_t wd_cond = PTHREAD_COND_INITIALIZER;
81static bool finished = false;
82static _Atomic(size_t) progress = 0; //!< messages and sockets the master has received, for the watchdog.
83static _Atomic(size_t) master_sleeps = 0; //!< times the master blocked.
84static _Atomic(size_t) master_rescued = 0; //!< times announcing found messages, so it did not block.
85
86/**********************************************************************/
87
88static NEVER_RETURNS void usage(void)
89{
90 fprintf(stderr, "usage: control_test [OPTS]\n");
91 fprintf(stderr, " -m <messages> Send number of messages.\n");
92 fprintf(stderr, " -w <workers> Number of workers.\n");
93 fprintf(stderr, " -q Use per-worker atomic queues.\n");
94 fprintf(stderr, " -e The master never announces it is blocking, the event list does.\n");
95 fprintf(stderr, " -P <usec> Pause each worker up to this long between sends,\n");
96 fprintf(stderr, " so the master runs dry and blocks (default: 0).\n");
97 fprintf(stderr, " -t <seconds> Fail if the master receives nothing for this long (default: 30).\n");
98 fprintf(stderr, " -x Debugging mode.\n");
99
100 fr_exit_now(EXIT_SUCCESS);
101}
102
103typedef struct {
105 size_t counter;
106 size_t worker;
108
109typedef struct {
113
114static void recv_control_callback(void const *data, size_t data_size, UNUSED fr_time_t now, void *uctx)
115{
116 my_message_t const *m = data;
117 master_ctx_t *master_ctx = uctx;
118
120 fr_assert(data_size == sizeof(*m));
121
122 MPRINT2("Master got worker %zu message %zu, size %zu.\n", m->worker, m->counter, data_size);
123 if (m->counter == (max_messages - 1)) MPRINT1("Master seen all messages from worker %zu\n", m->worker);
124 master_ctx->num_messages++;
125 atomic_store_explicit(&progress, master_ctx->num_messages + master_ctx->num_sockets,
127}
128
129/** Receive a socket, read the sending worker's id through it, send it back, and close it
130 */
131static void recv_socket_callback(int sock, UNUSED fr_time_t now, void *uctx)
132{
133 master_ctx_t *master_ctx = uctx;
134 size_t id;
135 ssize_t slen;
136
137 slen = read(sock, &id, sizeof(id));
138 if (slen != sizeof(id)) {
139 fprintf(stderr, "Read of worker id through socket %d returned %zd: %s\n", sock, slen,
140 (slen < 0) ? fr_syserror(errno) : "no data");
141 fr_exit_now(EXIT_FAILURE);
142 }
144 slen = write(sock, &id, sizeof(id));
145 if (slen != sizeof(id)) {
146 fprintf(stderr, "Write of worker id back through socket %d returned %zd: %s\n", sock, slen,
147 (slen < 0) ? fr_syserror(errno) : "short write");
148 fr_exit_now(EXIT_FAILURE);
149 }
150 close(sock);
151
152 MPRINT1("Master got socket, read worker id %zu through it\n", id);
153 master_ctx->num_sockets++;
154 atomic_store_explicit(&progress, master_ctx->num_messages + master_ctx->num_sockets,
156}
157
158/** Busy wait for a random time up to max_usec
159 *
160 * Deliberately a spin, so the worker never blocks and the master is
161 * the only thread which sleeps.
162 */
163static void spin(uint32_t max_usec)
164{
165 fr_time_t until;
166
167 if (!max_usec) return;
168
169 until = fr_time_add(fr_time(), fr_time_delta_from_usec(fr_rand() % (max_usec + 1)));
170 while (fr_time_lt(fr_time(), until)) {
171 /* spin */
172 }
173}
174
175/** Fail the test if the master stops receiving
176 *
177 * With the wakeup gated on the master announcing it is blocking, a
178 * missed wakeup leaves the master asleep with messages queued, which
179 * would otherwise hang the test rather than fail it.
180 */
181static void *watchdog_thread(UNUSED void *arg)
182{
183 size_t last = 0;
184
185 pthread_mutex_lock(&wd_mutex);
186 while (!finished) {
187 struct timespec ts;
188 int ret;
189 size_t now;
190
191 clock_gettime(CLOCK_REALTIME, &ts);
192 ts.tv_sec += watchdog_secs;
193
194 ret = pthread_cond_timedwait(&wd_cond, &wd_mutex, &ts);
195 if (finished) break;
196 if (ret != ETIMEDOUT) continue;
197
199 if (now == last) {
200 fprintf(stderr, "STALL: master received %zu of %zu messages and sockets, then nothing for "
201 "%u seconds, slept %zu times, rescued %zu times\n",
205 fr_exit_now(EXIT_FAILURE);
206 }
207 MPRINT1("watchdog: %zu messages\n", now);
208 last = now;
209 }
210 pthread_mutex_unlock(&wd_mutex);
211
212 return NULL;
213}
214
215static void *control_master(void *arg)
216{
217 TALLOC_CTX *ctx;
218 master_ctx_t *master_ctx;
219 size_t i;
220 fr_time_t start;
221 int64_t elapsed_usec;
222 fr_sem_t *master_sem = arg;
223
224 MEM(ctx = talloc_init_const("control_master"));
225
226 master_ctx = talloc_zero(ctx, master_ctx_t);
227
228 MPRINT1("Master started.\n");
229
230 for (i = 0; i < num_aq; i++) {
232 recv_control_callback, master_ctx);
234 recv_socket_callback, master_ctx);
236 }
237
238 sem_post(master_sem);
239
240 start = fr_time();
241 while ((master_ctx->num_messages < (max_messages * num_workers)) ||
242 (master_ctx->num_sockets < num_workers)) {
243 int num_events;
244 bool wait = true;
245
246 MPRINT3("Master waiting for events (seen %zu).\n", master_ctx->num_messages);
247
248 /*
249 * Announce that we are about to block, on every control
250 * we read. If any held messages, service the rest
251 * without blocking. Or leave all of that to the event
252 * list, as a loop which knows nothing of the controls
253 * it reads would.
254 */
255 if (!el_announces) {
256 for (i = 0; i < num_aq; i++) {
257 if (fr_control_sleeping(control[i])) wait = false;
258 }
259 }
260 if (wait) {
262 } else {
264 }
265
266 num_events = fr_event_corral(el, fr_time(), wait);
267 if (!el_announces) {
268 for (i = 0; i < num_aq; i++) fr_control_awake(control[i]);
269 }
270 if (num_events < 0) {
271 fprintf(stderr, "Failed reading kevent: %s\n", fr_syserror(errno));
272 fr_exit_now(EXIT_FAILURE);
273 }
274 if (num_events > 0) {
276 }
277 }
278 elapsed_usec = fr_time_delta_to_usec(fr_time_sub_time_time(fr_time(), start));
279 printf("messages %zu from %zu workers\n", master_ctx->num_messages, num_workers);
280 printf("sockets %zu\n", master_ctx->num_sockets);
281 printf("master slept %zu times\n", atomic_load_explicit(&master_sleeps, memory_order_relaxed));
282 printf("rescued by drain %zu times\n", atomic_load_explicit(&master_rescued, memory_order_relaxed));
283 printf("elapsed %.3fs, %.0f messages/s\n", (double) elapsed_usec / 1e6,
284 ((double)master_ctx->num_messages * 1e6) / (double)elapsed_usec);
285
286 talloc_free(ctx);
287
288 return NULL;
289}
290
291static void *control_worker(void *arg)
292{
293 size_t i, aq_num;
294 TALLOC_CTX *ctx;
295 worker_args_t *wa = (worker_args_t *) arg;
296
297 MEM(ctx = talloc_init_const("control_worker"));
298
299 aq_num = single_aq ? 0 : wa->id;
300
301 MPRINT1("\tWorker %zu started using queue %zu.\n", wa->id, aq_num);
302
303 for (i = 0; i < max_messages; i++) {
304 my_message_t m;
305
307 m.counter = i;
308 m.worker = wa->id;
309
310 /*
311 * Go quiet for a bit so the master runs dry, announces
312 * and blocks. The next send then lands on a master which
313 * is asleep, or is in the middle of deciding to be.
314 */
316
317retry:
319 &m, sizeof(m)) < 0) {
320 char const *err;
321 MPRINT1("\tWorker %zu retrying message %zu\n", wa->id, i);
322 while ((err = fr_strerror_pop())) {
323 MPRINT1("\t%s\n", err);
324 }
325
326 /*
327 * The queue is full. Give the master a chance to
328 * drain it, without sleeping for a growing delay
329 * which would turn the test into a benchmark of
330 * the sleep.
331 */
332 sched_yield();
333 goto retry;
334 }
335
336 MPRINT2("\tWorker %zu sent message %zu\n", wa->id, i);
337 }
338
339 /*
340 * Send the master one end of a socketpair with our id in it,
341 * and wait for the master to send the id back before closing
342 * either end. On macOS, if we closed them while the end was
343 * still in flight, the id would sometimes be gone when it
344 * arrived. The master gets its own descriptor for the end
345 * it was sent.
346 */
347 {
348 int sv[2];
349 size_t id;
350 ssize_t slen;
351
352 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
353 fprintf(stderr, "Failed creating socketpair: %s\n", fr_syserror(errno));
354 fr_exit_now(EXIT_FAILURE);
355 }
356 slen = write(sv[1], &wa->id, sizeof(wa->id));
357 if (slen != sizeof(wa->id)) {
358 fprintf(stderr, "Worker %zu failed writing its id into the socketpair: %s\n", wa->id,
359 (slen < 0) ? fr_syserror(errno) : "short write");
360 fr_exit_now(EXIT_FAILURE);
361 }
362
363retry_socket:
365 sv[0]) < 0) {
366 char const *err;
367 MPRINT1("\tWorker %zu retrying socket message\n", wa->id);
368 while ((err = fr_strerror_pop())) {
369 MPRINT1("\t%s\n", err);
370 }
371 sched_yield();
372 goto retry_socket;
373 }
374 MPRINT1("\tWorker %zu sent socket\n", wa->id);
375
376 slen = read(sv[1], &id, sizeof(id));
377 if ((slen != sizeof(id)) || (id != wa->id)) {
378 fprintf(stderr, "Worker %zu did not get its id back from the master: returned %zd, id %zu\n",
379 wa->id, slen, id);
380 fr_exit_now(EXIT_FAILURE);
381 }
382 close(sv[0]);
383 close(sv[1]);
384 }
385
386 MPRINT1("\tWorker %zu exiting.\n", wa->id);
387
388 talloc_free(ctx);
389
390 return NULL;
391}
392
393
394
395int main(int argc, char *argv[])
396{
397 int c;
398 TALLOC_CTX *autofree = talloc_autofree_context();
399 pthread_attr_t attr;
400 pthread_t master_id, *worker_id, wd_id;
401 size_t i;
402 worker_args_t *worker_args;
403 fr_sem_t *master_sem;
404
406
407 while ((c = getopt(argc, argv, "ehm:P:qt:w:x")) != -1) switch (c) {
408 case 'x':
409 debug_lvl++;
410 break;
411
412 case 'e':
413 el_announces = true;
414 break;
415
416 case 'P':
417 max_pause_usec = (uint32_t) strtoul(optarg, NULL, 10);
418 break;
419
420 case 't':
421 watchdog_secs = (uint32_t) strtoul(optarg, NULL, 10);
422 break;
423
424 case 'm':
425 max_messages = atoi(optarg);
426 break;
427
428 case 'q':
429 single_aq = false;
430 break;
431
432 case 'w':
433 num_workers = atoi(optarg);
434 break;
435
436 case 'h':
437 default:
438 usage();
439 }
440
441#if 0
442 argc -= (optind - 1);
443 argv += (optind - 1);
444#endif
445
448
451
452 control = talloc_array(autofree, fr_control_t *, num_aq);
453 for (i = 0; i < num_aq; i++) {
455 if (!control[i]) {
456 fprintf(stderr, "control_test: Failed to create control plane\n");
457 fr_exit_now(EXIT_FAILURE);
458 }
459 }
460
461 /*
462 * Start the threads, with the channel.
463 */
464 (void) pthread_attr_init(&attr);
465 (void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
466
467 master_sem = fr_sem_alloc();
468 fr_assert(master_sem != NULL);
469 (void) pthread_create(&wd_id, &attr, watchdog_thread, NULL);
470 (void) pthread_create(&master_id, &attr, control_master, master_sem);
471 SEM_WAIT_INTR(master_sem);
472
473 worker_id = talloc_array(autofree, pthread_t, num_workers);
474 worker_args = talloc_array(autofree, worker_args_t, num_workers);
475 for (i = 0; i < num_workers; i++) {
476 worker_args[i].id = i;
477 (void) pthread_create(&worker_id[i], &attr, control_worker, &worker_args[i]);
478 }
479
480 (void) pthread_join(master_id, NULL);
481 for (i = 0; i < num_workers; i++) {
482 (void) pthread_join(worker_id[i], NULL);
483 }
484
485 pthread_mutex_lock(&wd_mutex);
486 finished = true;
487 pthread_cond_signal(&wd_cond);
488 pthread_mutex_unlock(&wd_mutex);
489 (void) pthread_join(wd_id, NULL);
490
491 fr_sem_free(master_sem);
493
495
496 fflush(stdout);
497 fr_exit_now(EXIT_SUCCESS);
498}
#define RCSID(id)
Definition build.h:609
#define NEVER_RETURNS
Should be placed before the function return type.
Definition build.h:413
#define UNUSED
Definition build.h:415
TALLOC_CTX * autofree
Definition common.c:29
@ FR_CONTROL_EVENTSET_NETWORK
Events the network defines.
Definition control.h:63
#define FR_CONTROL_MAX_SIZE
Definition control.h:76
#define FR_CONTROL_MAX_MESSAGES
Definition control.h:75
static void recv_socket_callback(int sock, UNUSED fr_time_t now, void *uctx)
Receive a socket, read the sending worker's id through it, send it back, and close it.
static void recv_control_callback(void const *data, size_t data_size, UNUSED fr_time_t now, void *uctx)
int main(int argc, char *argv[])
static size_t num_workers
#define CONTROL_MAGIC
static bool single_aq
static int aq_size
static pthread_cond_t wd_cond
static bool el_announces
the master never announces, the event list does it.
static void * watchdog_thread(UNUSED void *arg)
Fail the test if the master stops receiving.
static size_t num_aq
static void * control_worker(void *arg)
static uint32_t max_pause_usec
workers pause up to this long between sends.
#define MPRINT3
control_test_event_t
Events on the test's control planes.
@ CONTROL_TEST_MESSAGE
static pthread_mutex_t wd_mutex
static void spin(uint32_t max_usec)
Busy wait for a random time up to max_usec.
static bool finished
control_test_socket_event_t
Events whose messages carry a socket, numbered separately from those above.
@ CONTROL_TEST_SOCKET
one end of a socketpair, with the worker's id in it.
static uint32_t watchdog_secs
fail if the master makes no progress for this long.
size_t num_messages
#define MPRINT2
static fr_event_list_t * el
uint32_t header
static fr_control_t ** control
#define MPRINT1
static size_t max_messages
size_t num_sockets
#define MEM(x)
static int debug_lvl
#define fr_exit_now(_x)
Exit without calling atexit() handlers, producing a log message in debug builds.
Definition debug.h:272
static NEVER_RETURNS void usage(void)
Definition dhcpclient.c:113
talloc_free(hp)
bool fr_control_sleeping(fr_control_t *c)
Record that the reader of this control plane is about to block.
Definition control.c:843
void fr_control_awake(fr_control_t *c)
Record that the reader of this control plane is running again.
Definition control.c:860
int fr_control_open(fr_control_t *c)
Open the control-plane signalling path.
Definition control.c:446
fr_control_t * fr_control_create(TALLOC_CTX *ctx, fr_event_list_t *el, uint16_t slots, uint16_t max_size)
Create a control-plane signaling path.
Definition control.c:427
int fr_control_callback_add(fr_control_t *c, fr_control_eventset_t es, uint16_t ev, fr_control_callback_t callback, void *uctx)
Register a callback for a message.
Definition control.c:659
int fr_control_socket_send(fr_control_t *c, fr_control_eventset_t es, uint16_t ev, int sock)
Send a control-plane message carrying a socket.
Definition control.c:604
int fr_control_message_send(fr_control_t *c, fr_control_eventset_t es, uint16_t ev, void const *data, size_t data_size)
Send a control-plane message.
Definition control.c:544
int fr_control_socket_callback_add(fr_control_t *c, fr_control_eventset_t es, uint16_t ev, fr_control_socket_callback_t callback, void *uctx)
Register a callback for a message which carries a socket.
Definition control.c:727
The control structure.
Definition control.c:211
void fr_event_service(fr_event_list_t *el)
Service any outstanding timer or file descriptor events.
Definition event.c:2441
int fr_event_corral(fr_event_list_t *el, fr_time_t now, bool wait)
Gather outstanding timer and file descriptor events.
Definition event.c:2128
#define fr_time()
Definition event.c:61
Stores all information relating to an event list.
Definition event.c:415
fr_event_list_t * main_loop_event_list(void)
Return the main loop event list.
Definition main_loop.c:173
int main_loop_init(void)
Initialise the main event loop, setting up signal handlers.
Definition main_loop.c:262
void main_loop_free(void)
Definition main_loop.c:198
unsigned short uint16_t
unsigned int uint32_t
long int ssize_t
#define fr_assert(_expr)
Definition rad_assert.h:37
uint32_t fr_rand(void)
Return a 32-bit random number.
Definition rand.c:128
static _Thread_local int worker_id
Internal ID of the current worker thread.
Definition schedule.c:105
void fr_sem_free(fr_sem_t *sem)
Definition semaphore.c:50
fr_sem_t * fr_sem_alloc(void)
Definition semaphore.c:30
#define SEM_WAIT_INTR(_x)
Definition semaphore.h:56
sem_t fr_sem_t
Definition semaphore.h:53
@ memory_order_relaxed
Definition stdatomic.h:127
#define atomic_fetch_add_explicit(object, operand, order)
Definition stdatomic.h:302
#define _Atomic(T)
Definition stdatomic.h:77
#define atomic_load_explicit(object, order)
Definition stdatomic.h:312
#define atomic_store_explicit(object, desired, order)
Definition stdatomic.h:314
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
static TALLOC_CTX * talloc_init_const(char const *name)
Allocate a top level chunk with a constant name.
Definition talloc.h:127
#define talloc_autofree_context
The original function is deprecated, so replace it with our version.
Definition talloc.h:55
int fr_time_start(void)
Initialize the local time.
Definition time.c:157
static fr_time_delta_t fr_time_sub_time_time(fr_time_t a, fr_time_t b)
Definition time.h:207
static int64_t fr_time_delta_to_usec(fr_time_delta_t delta)
Definition time.h:641
#define fr_time_add(_a, _b)
Add a time/time delta together.
Definition time.h:196
static fr_time_delta_t fr_time_delta_from_usec(int64_t usec)
Definition time.h:577
#define fr_time_lt(_a, _b)
Definition time.h:239
"server local" time.
Definition time.h:69
char const * fr_strerror_pop(void)
Pop the last library error.
Definition strerror.c:685
static fr_slen_t data
Definition value.h:1366
static fr_control_t * control_master
Definition worker_test.c:56