The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
schedule.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 5f85964fe7b49c741b07ec97a8a9aec32345a67f $
19 *
20 * @brief Network / worker thread scheduling
21 * @file io/schedule.c
22 *
23 * @copyright 2016 Alan DeKok (aland@freeradius.org)
24 */
25RCSID("$Id: 5f85964fe7b49c741b07ec97a8a9aec32345a67f $")
26
27#define LOG_DST sc->log
28
29#include <freeradius-devel/autoconf.h>
30
31#include <freeradius-devel/io/schedule.h>
32#include <freeradius-devel/util/dlist.h>
33#include <freeradius-devel/util/rb.h>
34#include <freeradius-devel/util/syserror.h>
35#include <freeradius-devel/server/trigger.h>
36
37#include <pthread.h>
38
39/*
40 * Other OS's have sem_init, OS X doesn't.
41 */
42#ifdef HAVE_SEMAPHORE_H
43#include <semaphore.h>
44#endif
45
46#define SEMAPHORE_LOCKED (0)
47
48#ifdef __APPLE__
49#include <mach/task.h>
50#include <mach/mach_init.h>
51#include <mach/semaphore.h>
52
53#undef sem_t
54#define sem_t semaphore_t
55#undef sem_init
56#define sem_init(s,p,c) semaphore_create(mach_task_self(),s,SYNC_POLICY_FIFO,c)
57#undef sem_wait
58#define sem_wait(s) semaphore_wait(*s)
59#undef sem_post
60#define sem_post(s) semaphore_signal(*s)
61#undef sem_destroy
62#define sem_destroy(s) semaphore_destroy(mach_task_self(),*s)
63#endif /* __APPLE__ */
64
65#define SEM_WAIT_INTR(_x) do {if (sem_wait(_x) == 0) break;} while (errno == EINTR)
66
67/**
68 * Track the child thread status.
69 */
71 FR_CHILD_FREE = 0, //!< child is free
72 FR_CHILD_INITIALIZING, //!< initialized, but not running
73 FR_CHILD_RUNNING, //!< running, and in the running queue
74 FR_CHILD_EXITED, //!< exited, and in the exited queue
75 FR_CHILD_FAIL //!< failed, and in the exited queue
77
78/** Scheduler specific information for worker threads
79 *
80 * Wraps a fr_worker_t, tracking additional information that
81 * the scheduler uses.
82 */
83typedef struct {
84 TALLOC_CTX *ctx; //!< our allocation ctx
85 fr_event_list_t *el; //!< our event list
86 pthread_t pthread_id; //!< the thread of this worker
87
88 unsigned int id; //!< a unique ID
89 int uses; //!< how many network threads are using it
90 fr_time_t cpu_time; //!< how much CPU time this worker has used
91
92 fr_dlist_t entry; //!< our entry into the linked list of workers
93
94 fr_schedule_t *sc; //!< the scheduler we are running under
95
96 fr_schedule_child_status_t status; //!< status of the worker
97 fr_worker_t *worker; //!< the worker data structure
99
100/** Scheduler specific information for network threads
101 *
102 * Wraps a fr_network_t, tracking additional information that
103 * the scheduler uses.
104 */
105typedef struct {
106 TALLOC_CTX *ctx; //!< our allocation ctx
107 pthread_t pthread_id; //!< the thread of this network
108
109 unsigned int id; //!< a unique ID
110
111 fr_dlist_t entry; //!< our entry into the linked list of networks
112
113 fr_schedule_t *sc; //!< the scheduler we are running under
114
115 fr_schedule_child_status_t status; //!< status of the worker
116 fr_network_t *nr; //!< the receive data structure
117
118 fr_event_timer_t const *ev; //!< timer for stats_interval
120
121
122/**
123 * The scheduler
124 */
126 bool running; //!< is the scheduler running?
127
128 CONF_SECTION *cs; //!< thread pool configuration section
129 fr_event_list_t *el; //!< event list for single-threaded mode.
130
131 fr_log_t *log; //!< log destination
132 fr_log_lvl_t lvl; //!< log level
133
134 fr_schedule_config_t *config; //!< configuration
135
136 unsigned int num_workers_exited; //!< number of exited workers
137
138 sem_t worker_sem; //!< for inter-thread signaling
139 sem_t network_sem; //!< for inter-thread signaling
140
143
144 fr_dlist_head_t workers; //!< list of workers
145 fr_dlist_head_t networks; //!< list of networks
146
147 fr_network_t *single_network; //!< for single-threaded mode
148 fr_worker_t *single_worker; //!< for single-threaded mode
149};
150
151static _Thread_local int worker_id; //!< Internal ID of the current worker thread.
152
153/** Return the worker id for the current thread
154 *
155 * @return worker ID
156 */
158{
159 return worker_id;
160}
161
162/** Entry point for worker threads
163 *
164 * @param[in] arg the fr_schedule_worker_t
165 * @return NULL
166 */
167static void *fr_schedule_worker_thread(void *arg)
168{
169 TALLOC_CTX *ctx;
170 fr_schedule_worker_t *sw = talloc_get_type_abort(arg, fr_schedule_worker_t);
171 fr_schedule_t *sc = sw->sc;
174 char worker_name[32];
175
176#ifndef __APPLE__
177 /*
178 * This ifdef is because macOS doesn't use pthread_signmask in its
179 * setcontext function, and seems to apply the signal mask of the thread
180 * to the entire process when setcontext is called.
181 *
182 * * frame #0: 0x00000001934118b0 libsystem_kernel.dylib`sigprocmask
183 * frame #1: 0x0000000193481f3c libsystem_platform.dylib`setcontext + 44
184 * frame #2: 0x0000000100f27298 libcrypto.3.dylib`async_fibre_swapcontext + 52
185 * frame #3: 0x0000000100f274a0 libcrypto.3.dylib`ASYNC_start_job + 496
186 * frame #4: 0x0000000100b17884 libssl.3.dylib`ssl_start_async_job + 116
187 * frame #5: 0x0000000100b17804 libssl.3.dylib`ssl_read_internal + 356
188 * frame #6: 0x0000000100b17a0c libssl.3.dylib`SSL_read + 28
189 * frame #7: 0x00000001004f5b94 libfreeradius-tls.dylib`tls_session_async_handshake_cont(p_result=0x0000000112815c7c, priority=0x0000000112815edc, request=0x0000000112815a80, uctx=0x0000000139160060) at session.c:1366:26
190 */
191 sigset_t sigset;
192
193 sigfillset(&sigset);
194
195 /*
196 * Ensure workers aren't interrupted by signals.
197 * The main thread, and main event loop are mostly
198 * idle, so they can handle signals.
199 */
200 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
201#endif
202
203 worker_id = sw->id; /* Store the current worker ID */
204
205 snprintf(worker_name, sizeof(worker_name), "Worker %d", sw->id);
206
207 sw->ctx = ctx = talloc_init("%s", worker_name);
208 if (!ctx) {
209 ERROR("%s - Failed allocating memory", worker_name);
210 goto fail;
211 }
212
213 INFO("%s - Starting", worker_name);
214
215 sw->el = fr_event_list_alloc(ctx, NULL, NULL);
216 if (!sw->el) {
217 PERROR("%s - Failed creating event list", worker_name);
218 goto fail;
219 }
220
221
222 sw->worker = fr_worker_create(ctx, sw->el, worker_name, sc->log, sc->lvl, &sc->config->worker);
223 if (!sw->worker) {
224 PERROR("%s - Failed creating worker", worker_name);
225 goto fail;
226 }
227
228 /*
229 * @todo make this a registry
230 */
231 if (sc->worker_thread_instantiate) {
232 CONF_SECTION *cs;
233 char section_name[32];
234
235 snprintf(section_name, sizeof(section_name), "%u", sw->id);
236
237 cs = cf_section_find(sc->cs, "worker", section_name);
238 if (!cs) cs = cf_section_find(sc->cs, "worker", NULL);
239
240 if (sc->worker_thread_instantiate(sw->ctx, sw->el, cs) < 0) {
241 PERROR("%s - Worker thread instantiation failed", worker_name);
242 goto fail;
243 }
244 }
245
247
248 /*
249 * Add this worker to all network threads.
250 */
251 for (sn = fr_dlist_head(&sc->networks);
252 sn != NULL;
253 sn = fr_dlist_next(&sc->networks, sn)) {
254 (void) fr_network_worker_add(sn->nr, sw->worker);
255 }
256
257 DEBUG3("%s - Started", worker_name);
258
259 /*
260 * Tell the originator that the thread has started.
261 */
262 sem_post(&sc->worker_sem);
263
264 /*
265 * Do all of the work.
266 */
267 fr_worker(sw->worker);
268
269 status = FR_CHILD_EXITED;
270
271fail:
272 sw->status = status;
273
274 if (sw->worker) {
276 sw->worker = NULL;
277 }
278
279 INFO("%s - Exiting", worker_name);
280
281 if (sc->worker_thread_detach) sc->worker_thread_detach(NULL); /* Fixme once we figure out what uctx should be */
282
283 /*
284 * Not looping at this point, but may catch timer/fd
285 * insertions being done after the thread should have
286 * exited.
287 */
288 if (sw->el) fr_event_loop_exit(sw->el, 1);
289
290 /*
291 * Tell the scheduler we're done.
292 */
293 sem_post(&sc->worker_sem);
294
295 talloc_free(ctx);
296
297 return NULL;
298}
299
300
301static void stats_timer(fr_event_list_t *el, fr_time_t now, void *uctx)
302{
303 fr_schedule_network_t *sn = talloc_get_type_abort(uctx, fr_schedule_network_t);
304
305 fr_network_stats_log(sn->nr, sn->sc->log);
306
307 (void) fr_event_timer_at(sn, el, &sn->ev, fr_time_add(now, sn->sc->config->stats_interval), stats_timer, sn);
308}
309
310/** Initialize and run the network thread.
311 *
312 * @param[in] arg the fr_schedule_network_t
313 * @return NULL
314 */
315static void *fr_schedule_network_thread(void *arg)
316{
317 TALLOC_CTX *ctx;
318 fr_schedule_network_t *sn = talloc_get_type_abort(arg, fr_schedule_network_t);
319 fr_schedule_t *sc = sn->sc;
322 char network_name[32];
323
324#ifndef __APPLE__
325 /*
326 * This ifdef is because macOS doesn't use pthread_signmask in its
327 * setcontext function, and seems to apply the signal mask of the thread
328 * to the entire process when setcontext is called.
329 *
330 * * frame #0: 0x00000001934118b0 libsystem_kernel.dylib`sigprocmask
331 * frame #1: 0x0000000193481f3c libsystem_platform.dylib`setcontext + 44
332 * frame #2: 0x0000000100f27298 libcrypto.3.dylib`async_fibre_swapcontext + 52
333 * frame #3: 0x0000000100f274a0 libcrypto.3.dylib`ASYNC_start_job + 496
334 * frame #4: 0x0000000100b17884 libssl.3.dylib`ssl_start_async_job + 116
335 * frame #5: 0x0000000100b17804 libssl.3.dylib`ssl_read_internal + 356
336 * frame #6: 0x0000000100b17a0c libssl.3.dylib`SSL_read + 28
337 * frame #7: 0x00000001004f5b94 libfreeradius-tls.dylib`tls_session_async_handshake_cont(p_result=0x0000000112815c7c, priority=0x0000000112815edc, request=0x0000000112815a80, uctx=0x0000000139160060) at session.c:1366:26
338 */
339 sigset_t sigset;
340
341 sigfillset(&sigset);
342
343 /*
344 * Ensure workers aren't interrupted by signals.
345 * The main thread, and main event loop are mostly
346 * idle, so they can handle signals.
347 */
348 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
349#endif
350
351 snprintf(network_name, sizeof(network_name), "Network %d", sn->id);
352
353 INFO("%s - Starting", network_name);
354
355 sn->ctx = ctx = talloc_init("%s", network_name);
356 if (!ctx) {
357 ERROR("%s - Failed allocating memory", network_name);
358 goto fail;
359 }
360
361 el = fr_event_list_alloc(ctx, NULL, NULL);
362 if (!el) {
363 PERROR("%s - Failed creating event list", network_name);
364 goto fail;
365 }
366
367 sn->nr = fr_network_create(ctx, el, network_name, sc->log, sc->lvl, &sc->config->network);
368 if (!sn->nr) {
369 PERROR("%s - Failed creating network", network_name);
370 goto fail;
371 }
372
374
375 /*
376 * Tell the originator that the thread has started.
377 */
378 sem_post(&sc->network_sem);
379
380 DEBUG3("%s - Started", network_name);
381
382 /*
383 * Print out statistics for this network IO handler.
384 */
385 if (fr_time_delta_ispos(sc->config->stats_interval)) {
386 (void) fr_event_timer_in(sn, el, &sn->ev, sn->sc->config->stats_interval, stats_timer, sn);
387 }
388 /*
389 * Call the main event processing loop of the network
390 * thread Will not return until the worker is about
391 * to exit.
392 */
393 fr_network(sn->nr);
394
395 status = FR_CHILD_EXITED;
396
397fail:
398 sn->status = status;
399
400 INFO("%s - Exiting", network_name);
401
402 /*
403 * Tell the scheduler we're done.
404 */
405 sem_post(&sc->network_sem);
406
407 talloc_free(ctx);
408
409 return NULL;
410}
411
412/** Creates a new thread using our standard set of options
413 *
414 * New threads are:
415 * - Joinable, i.e. you can call pthread_join on them to confirm they've exited
416 * - Immune to catchable signals.
417 *
418 * @param[out] thread handled that was created by pthread_create.
419 * @param[in] func entry point for the thread.
420 * @param[in] arg Argument to pass to func.
421 * @return
422 * - 0 on success.
423 * - -1 on failure.
424 */
425int fr_schedule_pthread_create(pthread_t *thread, void *(*func)(void *), void *arg)
426{
427 pthread_attr_t attr;
428 int ret;
429
430 /*
431 * Set the thread to wait around after it's exited
432 * so it can be joined. This is more of a useful
433 * mechanism for the parent to determine if all
434 * the threads have exited so it can continue with
435 * a graceful shutdown.
436 */
437 pthread_attr_init(&attr);
438 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
439
440 ret = pthread_create(thread, &attr, func, arg);
441 if (ret != 0) {
442 fr_strerror_printf("Failed creating thread: %s", fr_syserror(ret));
443 return -1;
444 }
445
446 return 0;
447}
448
449/** Create a scheduler and spawn the child threads.
450 *
451 * @param[in] ctx talloc context.
452 * @param[in] el event list, only for single-threaded mode.
453 * @param[in] logger destination for all logging messages.
454 * @param[in] lvl log level.
455 * @param[in] worker_thread_instantiate callback for new worker threads.
456 * @param[in] worker_thread_detach callback to destroy resources
457 * allocated by worker_thread_instantiate.
458 * @param[in] config configuration for the scheduler
459 * @return
460 * - NULL on error
461 * - fr_schedule_t new scheduler
462 */
464 fr_log_t *logger, fr_log_lvl_t lvl,
465 fr_schedule_thread_instantiate_t worker_thread_instantiate,
466 fr_schedule_thread_detach_t worker_thread_detach,
468{
469 unsigned int i;
470 fr_schedule_worker_t *sw, *next_sw;
471 fr_schedule_network_t *sn, *next_sn;
473
474 sc = talloc_zero(ctx, fr_schedule_t);
475 if (!sc) {
476 fr_strerror_const("Failed allocating memory");
477 return NULL;
478 }
479
480 sc->config = config;
481 sc->el = el;
482 sc->log = logger;
483 sc->lvl = lvl;
484
485 sc->worker_thread_instantiate = worker_thread_instantiate;
486 sc->worker_thread_detach = worker_thread_detach;
487 sc->running = true;
488
489 /*
490 * If we're single-threaded, create network / worker, and insert them into the event loop.
491 */
492 if (el) {
493 sc->single_network = fr_network_create(sc, el, "Network", sc->log, sc->lvl, &sc->config->network);
494 if (!sc->single_network) {
495 PERROR("Failed creating network");
496 pre_instantiate_st_fail:
498 return NULL;
499 }
500
501 sc->single_worker = fr_worker_create(sc, el, "Worker", sc->log, sc->lvl, &sc->config->worker);
502 if (!sc->single_worker) {
503 PERROR("Failed creating worker");
504 fr_network_destroy(sc->single_network);
505 goto pre_instantiate_st_fail;
506 }
507
508 /*
509 * Parent thread-specific data from the single_worker
510 */
511 if (sc->worker_thread_instantiate) {
512 CONF_SECTION *subcs;
513
514 subcs = cf_section_find(sc->cs, "worker", "0");
515 if (!subcs) subcs = cf_section_find(sc->cs, "worker", NULL);
516
517 if (sc->worker_thread_instantiate(sc->single_worker, el, subcs) < 0) {
518 PERROR("Worker thread instantiation failed");
519 destroy_both:
520 fr_network_destroy(sc->single_network);
521 fr_worker_destroy(sc->single_worker);
522 goto pre_instantiate_st_fail;
523 }
524 }
525
526 if (fr_command_register_hook(NULL, "0", sc->single_worker, cmd_worker_table) < 0) {
527 PERROR("Failed adding worker commands");
528 st_fail:
529 if (sc->worker_thread_detach) sc->worker_thread_detach(NULL);
530 goto destroy_both;
531 }
532
533 if (fr_command_register_hook(NULL, "0", sc->single_network, cmd_network_table) < 0) {
534 PERROR("Failed adding network commands");
535 goto st_fail;
536 }
537
538 /*
539 * Register the worker with the network, so
540 * things like fr_network_send_request() work.
541 */
542 fr_network_worker_add_self(sc->single_network, sc->single_worker);
543 DEBUG("Scheduler created in single-threaded mode");
544
545 if (fr_event_pre_insert(el, fr_worker_pre_event, sc->single_worker) < 0) {
546 fr_strerror_const("Failed adding pre-check to event list");
547 goto st_fail;
548 }
549
550 /*
551 * Add the event which processes request_t packets.
552 */
553 if (fr_event_post_insert(el, fr_worker_post_event, sc->single_worker) < 0) {
554 fr_strerror_const("Failed inserting post-processing event");
555 goto st_fail;
556 }
557
558 return sc;
559 }
560
561 /*
562 * Parse any scheduler-specific configuration.
563 */
564 if (!config) {
565 MEM(sc->config = talloc_zero(sc, fr_schedule_config_t));
566 sc->config->max_networks = 1;
567 sc->config->max_workers = 4;
568 } else {
569 sc->config = config;
570
571 if (sc->config->max_networks < 1) sc->config->max_networks = 1;
572 if (sc->config->max_networks > 64) sc->config->max_networks = 64;
573 if (sc->config->max_workers < 1) sc->config->max_workers = 1;
574 if (sc->config->max_workers > 64) sc->config->max_workers = 64;
575 }
576
577 /*
578 * Create the lists which hold the workers and networks.
579 */
580 fr_dlist_init(&sc->workers, fr_schedule_worker_t, entry);
581 fr_dlist_init(&sc->networks, fr_schedule_network_t, entry);
582
583 memset(&sc->network_sem, 0, sizeof(sc->network_sem));
584 if (sem_init(&sc->network_sem, 0, SEMAPHORE_LOCKED) != 0) {
585 ERROR("Failed creating semaphore: %s", fr_syserror(errno));
587 return NULL;
588 }
589
590 memset(&sc->worker_sem, 0, sizeof(sc->worker_sem));
591 if (sem_init(&sc->worker_sem, 0, SEMAPHORE_LOCKED) != 0) {
592 ERROR("Failed creating semaphore: %s", fr_syserror(errno));
594 return NULL;
595 }
596
597 /*
598 * Create the network threads first.
599 */
600 for (i = 0; i < sc->config->max_networks; i++) {
601 DEBUG3("Creating %u/%u networks", i + 1, sc->config->max_networks);
602
603 /*
604 * Create a worker "glue" structure
605 */
606 sn = talloc_zero(sc, fr_schedule_network_t);
607 if (!sn) {
608 ERROR("Network %u - Failed allocating memory", i);
609 break;
610 }
611
612 sn->id = i;
613 sn->sc = sc;
615 fr_dlist_insert_head(&sc->networks, sn);
616
618 PERROR("Failed creating network %u", i);
619 break;
620 }
621 }
622
623 /*
624 * Wait for all of the networks to signal us that either
625 * they've started, OR there's been a problem and they
626 * can't start.
627 */
628 for (i = 0; i < (unsigned int)fr_dlist_num_elements(&sc->networks); i++) {
629 DEBUG3("Waiting for semaphore from network %u/%u",
630 i + 1, (unsigned int)fr_dlist_num_elements(&sc->networks));
631 SEM_WAIT_INTR(&sc->network_sem);
632 }
633
634 /*
635 * See if all of the networks have started.
636 */
637 for (sn = fr_dlist_head(&sc->networks);
638 sn != NULL;
639 sn = next_sn) {
640 next_sn = fr_dlist_next(&sc->networks, sn);
641
642 if (sn->status != FR_CHILD_RUNNING) {
643 fr_dlist_remove(&sc->networks, sn);
644 continue;
645 }
646 }
647
648 /*
649 * Failed to start some workers, refuse to do anything!
650 */
651 if ((unsigned int)fr_dlist_num_elements(&sc->networks) < sc->config->max_networks) {
653 return NULL;
654 }
655
656 /*
657 * Create all of the workers.
658 */
659 for (i = 0; i < sc->config->max_workers; i++) {
660 DEBUG3("Creating %u/%u workers", i + 1, sc->config->max_workers);
661
662 /*
663 * Create a worker "glue" structure
664 */
665 sw = talloc_zero(sc, fr_schedule_worker_t);
666 if (!sw) {
667 ERROR("Worker %u - Failed allocating memory", i);
668 break;
669 }
670
671 sw->id = i;
672 sw->sc = sc;
674 fr_dlist_insert_head(&sc->workers, sw);
675
677 PERROR("Failed creating worker %u", i);
678 break;
679 }
680 }
681
682 /*
683 * Wait for all of the workers to signal us that either
684 * they've started, OR there's been a problem and they
685 * can't start.
686 */
687 for (i = 0; i < (unsigned int)fr_dlist_num_elements(&sc->workers); i++) {
688 DEBUG3("Waiting for semaphore from worker %u/%u",
689 i + 1, (unsigned int)fr_dlist_num_elements(&sc->workers));
690 SEM_WAIT_INTR(&sc->worker_sem);
691 }
692
693 /*
694 * See if all of the workers have started.
695 */
696 for (sw = fr_dlist_head(&sc->workers);
697 sw != NULL;
698 sw = next_sw) {
699
700 next_sw = fr_dlist_next(&sc->workers, sw);
701
702 if (sw->status != FR_CHILD_RUNNING) {
703 fr_dlist_remove(&sc->workers, sw);
704 continue;
705 }
706 }
707
708 /*
709 * Failed to start some workers, refuse to do anything!
710 */
711 if ((unsigned int)fr_dlist_num_elements(&sc->workers) < sc->config->max_workers) {
713 return NULL;
714 }
715
716 for (sw = fr_dlist_head(&sc->workers), i = 0;
717 sw != NULL;
718 sw = next_sw, i++) {
719 char buffer[32];
720
721 next_sw = fr_dlist_next(&sc->workers, sw);
722
723 snprintf(buffer, sizeof(buffer), "%d", i);
725 PERROR("Failed adding worker commands");
726 goto st_fail;
727 }
728 }
729
730 for (sn = fr_dlist_head(&sc->networks), i = 0;
731 sn != NULL;
732 sn = next_sn, i++) {
733 char buffer[32];
734
735 next_sn = fr_dlist_next(&sc->networks, sn);
736
737 snprintf(buffer, sizeof(buffer), "%d", i);
739 PERROR("Failed adding network commands");
740 goto st_fail;
741 }
742 }
743
744 if (sc) INFO("Scheduler created successfully with %u networks and %u workers",
745 sc->config->max_networks, (unsigned int)fr_dlist_num_elements(&sc->workers));
746
747 return sc;
748}
749
750/** Destroy a scheduler, and tell its child threads to exit.
751 *
752 * @note This may be called with no worker or network threads in the case of a
753 * instantiation error. This function _should_ deal with that condition
754 * gracefully.
755 *
756 * @param[in] sc_to_free the scheduler
757 * @return
758 * - <0 on error
759 * - 0 on success
760 */
762{
763 fr_schedule_t *sc = *sc_to_free;
764 unsigned int i;
767 int ret;
768
769 if (!sc) return 0;
770
771 sc->running = false;
772
773 /*
774 * Single threaded mode: kill the only network / worker we have.
775 */
776 if (sc->el) {
777 /*
778 * Destroy the network side first. It tells the
779 * workers to close.
780 */
781 fr_network_destroy(sc->single_network);
782 fr_worker_destroy(sc->single_worker);
783 goto done;
784 }
785
786 /*
787 * Signal each network thread to exit.
788 */
789 for (sn = fr_dlist_head(&sc->networks);
790 sn != NULL;
791 sn = fr_dlist_next(&sc->networks, sn)) {
792 fr_network_exit(sn->nr);
793 }
794
795 /*
796 * If the network threads are running, tell them to exit,
797 * and wait for them to do so. Each network thread tells
798 * all of its worker threads that it's exiting. It then
799 * closes the channels. When the workers see that there
800 * are no input channels, they exit, too.
801 */
802 for (i = 0; i < (unsigned int)fr_dlist_num_elements(&sc->networks); i++) {
803 DEBUG2("Scheduler - Waiting for semaphore indicating network exit %u/%u", i + 1,
804 (unsigned int)fr_dlist_num_elements(&sc->networks));
805 SEM_WAIT_INTR(&sc->network_sem);
806 }
807 DEBUG2("Scheduler - All networks indicated exit complete");
808
809 while ((sn = fr_dlist_head(&sc->networks)) != NULL) {
810 fr_dlist_remove(&sc->networks, sn);
811
812 /*
813 * Ensure that the thread has exited before
814 * cleaning up the context.
815 *
816 * This also ensures that the child threads have
817 * exited before the main thread cleans up the
818 * module instances.
819 */
820 if ((ret = pthread_join(sn->pthread_id, NULL)) != 0) {
821 ERROR("Failed joining network %i: %s", sn->id, fr_syserror(ret));
822 } else {
823 DEBUG2("Network %i joined (cleaned up)", sn->id);
824 }
825 }
826
827 /*
828 * Wait for all worker threads to finish. THEN clean up
829 * modules. Otherwise, the modules will be removed from
830 * underneath the workers!
831 */
832 for (i = 0; i < (unsigned int)fr_dlist_num_elements(&sc->workers); i++) {
833 DEBUG2("Scheduler - Waiting for semaphore indicating worker exit %u/%u", i + 1,
834 (unsigned int)fr_dlist_num_elements(&sc->workers));
835 SEM_WAIT_INTR(&sc->worker_sem);
836 }
837 DEBUG2("Scheduler - All workers indicated exit complete");
838
839 /*
840 * Clean up the exited workers.
841 */
842 while ((sw = fr_dlist_head(&sc->workers)) != NULL) {
843 fr_dlist_remove(&sc->workers, sw);
844
845 /*
846 * Ensure that the thread has exited before
847 * cleaning up the context.
848 *
849 * This also ensures that the child threads have
850 * exited before the main thread cleans up the
851 * module instances.
852 */
853 if ((ret = pthread_join(sw->pthread_id, NULL)) != 0) {
854 ERROR("Failed joining worker %i: %s", sw->id, fr_syserror(ret));
855 } else {
856 DEBUG2("Worker %i joined (cleaned up)", sw->id);
857 }
858 }
859
860 sem_destroy(&sc->network_sem);
861 sem_destroy(&sc->worker_sem);
862done:
863 /*
864 * Now that all of the workers are done, we can return to
865 * the caller, and have it dlclose() the modules.
866 */
868 *sc_to_free = NULL;
869
870 return 0;
871}
872
873/** Add a fr_listen_t to a scheduler.
874 *
875 * @param[in] sc the scheduler
876 * @param[in] li the ctx and callbacks for the transport.
877 * @return
878 * - NULL on error
879 * - the fr_network_t that the socket was added to.
880 */
882{
883 fr_network_t *nr;
884
885 (void) talloc_get_type_abort(sc, fr_schedule_t);
886
887 if (sc->el) {
888 nr = sc->single_network;
889 } else {
891
892 /*
893 * @todo - round robin it among the listeners?
894 * or maybe add it to the same parent thread?
895 */
896 sn = fr_dlist_head(&sc->networks);
897 nr = sn->nr;
898 }
899
900 if (fr_network_listen_add(nr, li) < 0) return NULL;
901
902 return nr;
903}
904
905/** Add a directory NOTE_EXTEND to a scheduler.
906 *
907 * @param[in] sc the scheduler
908 * @param[in] li the ctx and callbacks for the transport.
909 * @return
910 * - NULL on error
911 * - the fr_network_t that the socket was added to.
912 */
914{
915 fr_network_t *nr;
916
917 (void) talloc_get_type_abort(sc, fr_schedule_t);
918
919 if (sc->el) {
920 nr = sc->single_network;
921 } else {
923
924 /*
925 * @todo - round robin it among the listeners?
926 * or maybe add it to the same parent thread?
927 */
928 sn = fr_dlist_head(&sc->networks);
929 nr = sn->nr;
930 }
931
932 if (fr_network_directory_add(nr, li) < 0) return NULL;
933
934 return nr;
935}
static int const char char buffer[256]
Definition acutest.h:576
#define RCSID(id)
Definition build.h:483
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
CONF_SECTION * cf_section_find(CONF_SECTION const *cs, char const *name1, char const *name2)
Find a CONF_SECTION with name1 and optionally name2.
Definition cf_util.c:1028
fr_command_register_hook_t fr_command_register_hook
Definition command.c:42
#define MEM(x)
Definition debug.h:36
#define ERROR(fmt,...)
Definition dhcpclient.c:41
#define DEBUG(fmt,...)
Definition dhcpclient.c:39
#define fr_dlist_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:260
static void * fr_dlist_head(fr_dlist_head_t const *list_head)
Return the HEAD item of a list or NULL if the list is empty.
Definition dlist.h:486
static void * fr_dlist_remove(fr_dlist_head_t *list_head, void *ptr)
Remove an item from the list.
Definition dlist.h:638
static unsigned int fr_dlist_num_elements(fr_dlist_head_t const *head)
Return the number of elements in the dlist.
Definition dlist.h:939
static int fr_dlist_insert_head(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the head of a list.
Definition dlist.h:338
static void * fr_dlist_next(fr_dlist_head_t const *list_head, void const *ptr)
Get the next item in a list.
Definition dlist.h:555
Head of a doubly linked list.
Definition dlist.h:51
Entry in a doubly linked list.
Definition dlist.h:41
#define fr_event_timer_at(...)
Definition event.h:250
#define fr_event_timer_in(...)
Definition event.h:255
fr_cmd_table_t cmd_network_table[]
Definition network.c:2090
int fr_network_listen_add(fr_network_t *nr, fr_listen_t *li)
Add a fr_listen_t to a network.
Definition network.c:236
int fr_network_worker_add(fr_network_t *nr, fr_worker_t *worker)
Add a worker to a network in a different thread.
Definition network.c:293
int fr_network_destroy(fr_network_t *nr)
Stop a network thread in an orderly way.
Definition network.c:1681
int fr_network_directory_add(fr_network_t *nr, fr_listen_t *li)
Add a "watch directory" call to a network.
Definition network.c:278
void fr_network(fr_network_t *nr)
The main network worker function.
Definition network.c:1794
void fr_network_worker_add_self(fr_network_t *nr, fr_worker_t *worker)
Add a worker to a network in the same thread.
Definition network.c:313
int fr_network_exit(fr_network_t *nr)
Signal a network thread to exit.
Definition network.c:1849
void fr_network_stats_log(fr_network_t const *nr, fr_log_t const *log)
Definition network.c:2023
fr_network_t * fr_network_create(TALLOC_CTX *ctx, fr_event_list_t *el, char const *name, fr_log_t const *logger, fr_log_lvl_t lvl, fr_network_config_t const *config)
Create a network.
Definition network.c:1882
#define PERROR(_fmt,...)
Definition log.h:228
#define DEBUG3(_fmt,...)
Definition log.h:266
talloc_free(reap)
int fr_event_post_insert(fr_event_list_t *el, fr_event_timer_cb_t callback, void *uctx)
Add a post-event callback to the event list.
Definition event.c:2313
fr_event_list_t * fr_event_list_alloc(TALLOC_CTX *ctx, fr_event_status_cb_t status, void *status_uctx)
Initialise a new event list.
Definition event.c:2899
int fr_event_pre_insert(fr_event_list_t *el, fr_event_status_cb_t callback, void *uctx)
Add a pre-event callback to the event list.
Definition event.c:2259
void fr_event_loop_exit(fr_event_list_t *el, int code)
Signal an event loop exit with the specified code.
Definition event.c:2744
Stores all information relating to an event list.
Definition event.c:411
A timer event.
Definition event.c:102
fr_log_lvl_t
Definition log.h:67
static const conf_parser_t config[]
Definition base.c:183
static bool done
Definition radclient.c:80
#define DEBUG2(fmt,...)
Definition radclient.h:43
#define INFO(fmt,...)
Definition radict.c:54
CONF_SECTION * cs
thread pool configuration section
Definition schedule.c:128
TALLOC_CTX * ctx
our allocation ctx
Definition schedule.c:106
sem_t worker_sem
for inter-thread signaling
Definition schedule.c:138
fr_event_timer_t const * ev
timer for stats_interval
Definition schedule.c:118
fr_schedule_child_status_t status
status of the worker
Definition schedule.c:115
static _Thread_local int worker_id
Internal ID of the current worker thread.
Definition schedule.c:151
fr_event_list_t * el
event list for single-threaded mode.
Definition schedule.c:129
fr_schedule_t * sc
the scheduler we are running under
Definition schedule.c:113
fr_worker_t * single_worker
for single-threaded mode
Definition schedule.c:148
static void stats_timer(fr_event_list_t *el, fr_time_t now, void *uctx)
Definition schedule.c:301
fr_log_lvl_t lvl
log level
Definition schedule.c:132
fr_network_t * fr_schedule_directory_add(fr_schedule_t *sc, fr_listen_t *li)
Add a directory NOTE_EXTEND to a scheduler.
Definition schedule.c:913
#define SEM_WAIT_INTR(_x)
Definition schedule.c:65
fr_schedule_config_t * config
configuration
Definition schedule.c:134
fr_network_t * single_network
for single-threaded mode
Definition schedule.c:147
fr_schedule_thread_instantiate_t worker_thread_instantiate
thread instantiation callback
Definition schedule.c:141
fr_schedule_child_status_t
Track the child thread status.
Definition schedule.c:70
@ FR_CHILD_FAIL
failed, and in the exited queue
Definition schedule.c:75
@ FR_CHILD_FREE
child is free
Definition schedule.c:71
@ FR_CHILD_RUNNING
running, and in the running queue
Definition schedule.c:73
@ FR_CHILD_EXITED
exited, and in the exited queue
Definition schedule.c:74
@ FR_CHILD_INITIALIZING
initialized, but not running
Definition schedule.c:72
fr_network_t * fr_schedule_listen_add(fr_schedule_t *sc, fr_listen_t *li)
Add a fr_listen_t to a scheduler.
Definition schedule.c:881
fr_network_t * nr
the receive data structure
Definition schedule.c:116
fr_schedule_thread_detach_t worker_thread_detach
Definition schedule.c:142
bool running
is the scheduler running?
Definition schedule.c:126
int fr_schedule_worker_id(void)
Return the worker id for the current thread.
Definition schedule.c:157
static void * fr_schedule_worker_thread(void *arg)
Entry point for worker threads.
Definition schedule.c:167
int fr_schedule_pthread_create(pthread_t *thread, void *(*func)(void *), void *arg)
Creates a new thread using our standard set of options.
Definition schedule.c:425
fr_schedule_t * fr_schedule_create(TALLOC_CTX *ctx, fr_event_list_t *el, fr_log_t *logger, fr_log_lvl_t lvl, fr_schedule_thread_instantiate_t worker_thread_instantiate, fr_schedule_thread_detach_t worker_thread_detach, fr_schedule_config_t *config)
Create a scheduler and spawn the child threads.
Definition schedule.c:463
unsigned int num_workers_exited
number of exited workers
Definition schedule.c:136
sem_t network_sem
for inter-thread signaling
Definition schedule.c:139
int fr_schedule_destroy(fr_schedule_t **sc_to_free)
Destroy a scheduler, and tell its child threads to exit.
Definition schedule.c:761
unsigned int id
a unique ID
Definition schedule.c:109
fr_dlist_head_t networks
list of networks
Definition schedule.c:145
pthread_t pthread_id
the thread of this network
Definition schedule.c:107
fr_log_t * log
log destination
Definition schedule.c:131
fr_dlist_head_t workers
list of workers
Definition schedule.c:144
static void * fr_schedule_network_thread(void *arg)
Initialize and run the network thread.
Definition schedule.c:315
fr_dlist_t entry
our entry into the linked list of networks
Definition schedule.c:111
#define SEMAPHORE_LOCKED
Definition schedule.c:46
Scheduler specific information for network threads.
Definition schedule.c:105
The scheduler.
Definition schedule.c:125
int(* fr_schedule_thread_instantiate_t)(TALLOC_CTX *ctx, fr_event_list_t *el, void *uctx)
Setup a new thread.
Definition schedule.h:55
void(* fr_schedule_thread_detach_t)(void *uctx)
Explicitly free resources allocated by fr_schedule_thread_instantiate_t.
Definition schedule.h:61
fr_time_delta_t stats_interval
print channel statistics
Definition schedule.h:70
static const uchar sc[16]
Definition smbdes.c:115
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
Definition log.h:96
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#define fr_time_delta_ispos(_a)
Definition time.h:290
#define fr_time_add(_a, _b)
Add a time/time delta together.
Definition time.h:196
"server local" time.
Definition time.h:69
static fr_event_list_t * el
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
int fr_worker_pre_event(UNUSED fr_time_t now, UNUSED fr_time_delta_t wake, void *uctx)
Pre-event handler.
Definition worker.c:1551
fr_worker_t * fr_worker_create(TALLOC_CTX *ctx, fr_event_list_t *el, char const *name, fr_log_t const *logger, fr_log_lvl_t lvl, fr_worker_config_t *config)
Create a worker.
Definition worker.c:1356
fr_cmd_table_t cmd_worker_table[]
Definition worker.c:1738
void fr_worker_destroy(fr_worker_t *worker)
Destroy a worker.
Definition worker.c:1012
void fr_worker_post_event(UNUSED fr_event_list_t *el, UNUSED fr_time_t now, void *uctx)
Post-event handler.
Definition worker.c:1572
void fr_worker(fr_worker_t *worker)
The main loop and entry point of the stand-alone worker thread.
Definition worker.c:1493
A worker which takes packets from a master, and processes them.
Definition worker.c:94
unsigned int id
a unique ID
Definition schedule.c:88
int uses
how many network threads are using it
Definition schedule.c:89
pthread_t pthread_id
the thread of this worker
Definition schedule.c:86
fr_schedule_t * sc
the scheduler we are running under
Definition schedule.c:94
TALLOC_CTX * ctx
our allocation ctx
Definition schedule.c:84
fr_event_list_t * el
our event list
Definition schedule.c:85
fr_time_t cpu_time
how much CPU time this worker has used
Definition schedule.c:90
fr_dlist_t entry
our entry into the linked list of workers
Definition schedule.c:92
fr_schedule_child_status_t status
status of the worker
Definition schedule.c:96
fr_worker_t * worker
the worker data structure
Definition schedule.c:97
Scheduler specific information for worker threads.
Definition schedule.c:83