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: 269d1650ac99193ad2bad75cfcb8d47c268cfab1 $
19 *
20 * @brief Network / worker thread scheduling
21 * @file io/schedule.c
22 *
23 * @copyright 2016 Alan DeKok (aland@freeradius.org)
24 */
25RCSID("$Id: 269d1650ac99193ad2bad75cfcb8d47c268cfab1 $")
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/io/thread.h>
33#include <freeradius-devel/util/dlist.h>
34#include <freeradius-devel/util/rb.h>
35#include <freeradius-devel/util/syserror.h>
36#include <freeradius-devel/server/trigger.h>
37#include <freeradius-devel/util/semaphore.h>
38
39#include <pthread.h>
40
41/** Scheduler specific information for worker threads
42 *
43 * Wraps a fr_worker_t, tracking additional information that
44 * the scheduler uses.
45 */
46typedef struct {
47 fr_thread_t thread; //!< common thread structure - must be first!
48
49 int uses; //!< how many network threads are using it
50 fr_time_t cpu_time; //!< how much CPU time this worker has used
51
52 fr_schedule_t *sc; //!< the scheduler we are running under
53
54 fr_worker_t *worker; //!< the worker data structure
56
57/** Scheduler specific information for network threads
58 *
59 * Wraps a fr_network_t, tracking additional information that
60 * the scheduler uses.
61 */
62typedef struct {
63 fr_thread_t thread; //!< common thread structure - must be first!
64
65 fr_schedule_t *sc; //!< the scheduler we are running under
66
67 fr_network_t *nr; //!< the receive data structure
68
69 fr_timer_t *ev; //!< timer for stats_interval
71
72
73/**
74 * The scheduler
75 */
77 bool running; //!< is the scheduler running?
78
79 CONF_SECTION *cs; //!< thread pool configuration section
80 fr_event_list_t *el; //!< event list for single-threaded mode.
81 bool single_threaded; //!< true if running in single-threaded mode.
82
83 fr_log_t *log; //!< log destination
84 fr_log_lvl_t lvl; //!< log level
85
86 fr_schedule_config_t *config; //!< configuration
87
88 unsigned int num_workers_exited; //!< number of exited workers
89
90 fr_sem_t *worker_sem; //!< for inter-thread signaling
91 fr_sem_t *network_sem; //!< for inter-thread signaling
92 fr_sem_t *coord_sem; //!< for inter-thread signaling
93
96
97 fr_dlist_head_t workers; //!< list of workers
98 fr_dlist_head_t networks; //!< list of networks
99
100 fr_network_t *single_network; //!< for single-threaded mode
101 fr_worker_t *single_worker; //!< for single-threaded mode
102};
103
104static _Thread_local int worker_id = -1; //!< Internal ID of the current worker thread.
105
106/** Return the worker id for the current thread
107 *
108 * @return worker ID
109 */
111{
112 return worker_id;
113}
114
115/** Explicitly set the worker id for the current thread
116 *
117 * **Only to be used in test programs like unit_test_module**
118 */
120{
121 worker_id = id;
122}
123
124/** Entry point for worker threads
125 *
126 * @param[in] arg the fr_schedule_worker_t
127 * @return NULL
128 */
129static void *fr_schedule_worker_thread(void *arg)
130{
131 fr_schedule_worker_t *sw = talloc_get_type_abort(arg, fr_schedule_worker_t);
132 fr_schedule_t *sc = sw->sc;
134 char worker_name[32];
135
136 worker_id = sw->thread.id; /* Store the current worker ID */
137
138 snprintf(worker_name, sizeof(worker_name), "Worker %d", sw->thread.id);
139
140 if (fr_thread_setup(&sw->thread, worker_name) < 0) goto fail;
141
142 sw->worker = fr_worker_alloc(sw->thread.ctx, sw->thread.el, worker_name, sc->log, sc->lvl, &sc->config->worker);
143 if (!sw->worker) {
144 PERROR("%s - Failed creating worker", worker_name);
145 goto fail;
146 }
147
148 /*
149 * @todo make this a registry
150 */
151 if (sc->worker_thread_instantiate) {
152 CONF_SECTION *cs;
153 char section_name[32];
154
155 snprintf(section_name, sizeof(section_name), "%u", sw->thread.id);
156
157 cs = cf_section_find(sc->cs, "worker", section_name);
158 if (!cs) cs = cf_section_find(sc->cs, "worker", NULL);
159
160 if (sc->worker_thread_instantiate(sw->thread.ctx, sw->thread.el, cs) < 0) {
161 PERROR("%s - Worker thread instantiation failed", worker_name);
162 goto fail;
163 }
164 }
165
166 /*
167 * Add this worker to all network threads.
168 */
169 fr_dlist_foreach(&sc->networks, fr_schedule_network_t, sn) {
170 if (unlikely(fr_network_worker_add(sn->nr, sw->worker) < 0)) {
171 PERROR("%s - Failed adding worker to network %u", worker_name, sn->thread.id);
172 goto fail; /* FIXME - Should maybe try to undo partial adds? */
173 }
174 }
175
176 /*
177 * Tell the originator that the thread has started.
178 */
179 fr_thread_start(&sw->thread, sc->worker_sem);
180
181 /*
182 * Do all of the work.
183 */
184 fr_worker(sw->worker);
185
186 status = FR_THREAD_EXITED;
187
188fail:
189 if (sw->worker) {
191 sw->worker = NULL;
192 }
193
194 if (sc->worker_thread_detach) sc->worker_thread_detach(NULL); /* Fixme once we figure out what uctx should be */
195
196 fr_thread_exit(&sw->thread, status, sc->worker_sem);
197
198 return NULL;
199}
200
201
202static void stats_timer(fr_timer_list_t *tl, fr_time_t now, void *uctx)
203{
204 fr_schedule_network_t *sn = talloc_get_type_abort(uctx, fr_schedule_network_t);
205
206 fr_network_stats_log(sn->nr, sn->sc->log);
207
208 (void) fr_timer_at(sn, tl, &sn->ev, fr_time_add(now, sn->sc->config->stats_interval), false, stats_timer, sn);
209}
210
211/** Initialize and run the network thread.
212 *
213 * @param[in] arg the fr_schedule_network_t
214 * @return NULL
215 */
216static void *fr_schedule_network_thread(void *arg)
217{
218 fr_schedule_network_t *sn = talloc_get_type_abort(arg, fr_schedule_network_t);
219 fr_schedule_t *sc = sn->sc;
221 char network_name[32];
222
223 snprintf(network_name, sizeof(network_name), "Network %d", sn->thread.id);
224
225 if (fr_thread_setup(&sn->thread, network_name) < 0) goto fail;
226
227 sn->nr = fr_network_create(sn->thread.ctx, sn->thread.el, network_name, sc->log, sc->lvl, &sc->config->network);
228 if (!sn->nr) {
229 PERROR("%s - Failed creating network", network_name);
230 goto fail;
231 }
232
233 /*
234 * Tell the originator that the thread has started.
235 */
236 fr_thread_start(&sn->thread, sc->network_sem);
237
238 /*
239 * Print out statistics for this network IO handler.
240 */
241 if (fr_time_delta_ispos(sc->config->stats_interval)) {
242 (void) fr_timer_in(sn, sn->thread.el->tl, &sn->ev, sn->sc->config->stats_interval, false, stats_timer, sn);
243 }
244
245 /*
246 * Call the main event processing loop of the network
247 * thread Will not return until the worker is about
248 * to exit.
249 */
250 fr_network(sn->nr);
251
252 status = FR_THREAD_EXITED;
253
254fail:
255 fr_thread_exit(&sn->thread, status, sc->network_sem);
256
257 return NULL;
258}
259
260/** Create a scheduler and spawn the child threads.
261 *
262 * @param[in] ctx talloc context.
263 * @param[in] single_threaded no workers are spawned, everything runs in a common event loop.
264 * @param[in] el event list, only for single-threaded mode.
265 * @param[in] logger destination for all logging messages.
266 * @param[in] lvl log level.
267 * @param[in] worker_thread_instantiate callback for new worker threads.
268 * @param[in] worker_thread_detach callback to destroy resources
269 * allocated by worker_thread_instantiate.
270 * @param[in] config configuration for the scheduler
271 * @return
272 * - NULL on error
273 * - fr_schedule_t new scheduler
274 */
276 bool single_threaded,
278 fr_log_t *logger, fr_log_lvl_t lvl,
279 fr_schedule_thread_instantiate_t worker_thread_instantiate,
280 fr_schedule_thread_detach_t worker_thread_detach,
282{
283 unsigned int i;
284 fr_schedule_worker_t *sw, *next_sw;
285 fr_schedule_network_t *sn, *next_sn;
287
288 sc = talloc_zero(ctx, fr_schedule_t);
289 if (!sc) {
290 fr_strerror_const("Failed allocating memory");
291 return NULL;
292 }
293
294 /*
295 * Parse any scheduler-specific configuration.
296 */
297 if (!config) {
298 MEM(sc->config = talloc_zero(sc, fr_schedule_config_t));
299 sc->config->max_networks = 1;
300 sc->config->max_workers = 4;
301 } else {
302 sc->config = config;
303
304 if (sc->config->max_networks < 1) sc->config->max_networks = 1;
305 if (sc->config->max_networks > 64) sc->config->max_networks = 64;
306 if (sc->config->max_workers < 1) sc->config->max_workers = 1;
307 if (sc->config->max_workers > 64) sc->config->max_workers = 64;
308 }
309
310 sc->el = el;
311 sc->single_threaded = single_threaded;
312 sc->log = logger;
313 sc->lvl = lvl;
314 sc->cs = sc->config->cs;
315
316 sc->worker_thread_instantiate = worker_thread_instantiate;
317 sc->worker_thread_detach = worker_thread_detach;
318 sc->running = true;
319
320 /*
321 * If we're single-threaded, create network / worker, and insert them into the event loop.
322 */
323 if (single_threaded) {
324 sc->single_network = fr_network_create(sc, el, "Network", sc->log, sc->lvl, &sc->config->network);
325 if (!sc->single_network) {
326 PERROR("Failed creating network");
327 pre_instantiate_st_fail:
329 return NULL;
330 }
331
332 if (fr_coords_create(sc, el) < 0) {
333 PERROR("Failed creating coordinators");
334 if (unlikely(fr_network_destroy(sc->single_network) < 0)) {
335 PERROR("Failed destroying network");
336 }
337 goto pre_instantiate_st_fail;
338 }
339
340 worker_id = 0;
341 sc->single_worker = fr_worker_alloc(sc, el, "Worker", sc->log, sc->lvl, &sc->config->worker);
342 if (!sc->single_worker) {
343 PERROR("Failed creating worker");
344 if (unlikely(fr_network_destroy(sc->single_network) < 0)) {
345 PERROR("Failed destroying network");
346 }
347 goto pre_instantiate_st_fail;
348 }
349
350 /*
351 * Parent thread-specific data from the single_worker
352 */
353 if (sc->worker_thread_instantiate) {
354 CONF_SECTION *subcs;
355
356 subcs = cf_section_find(sc->cs, "worker", "0");
357 if (!subcs) subcs = cf_section_find(sc->cs, "worker", NULL);
358
359 if (sc->worker_thread_instantiate(sc->single_worker, el, subcs) < 0) {
360 PERROR("Worker thread instantiation failed");
361 destroy_both:
362 if (unlikely(fr_network_destroy(sc->single_network) < 0)) {
363 PERROR("Failed destroying network");
364 }
365 fr_worker_destroy(sc->single_worker);
366 goto pre_instantiate_st_fail;
367 }
368 }
369
370 if (fr_command_register_hook(NULL, "0", sc->single_worker, cmd_worker_table) < 0) {
371 PERROR("Failed adding worker commands");
372 st_fail:
373 if (sc->worker_thread_detach) sc->worker_thread_detach(NULL);
374 goto destroy_both;
375 }
376
377 if (fr_command_register_hook(NULL, "0", sc->single_network, cmd_network_table) < 0) {
378 PERROR("Failed adding network commands");
379 goto st_fail;
380 }
381
382 /*
383 * Register the worker with the network, so
384 * things like fr_network_send_request() work.
385 */
386 fr_network_worker_add_self(sc->single_network, sc->single_worker);
387 DEBUG("Scheduler created in single-threaded mode");
388
389 if (fr_event_pre_insert(el, fr_worker_pre_event, sc->single_worker) < 0) {
390 fr_strerror_const("Failed adding pre-check to event list");
391 goto st_fail;
392 }
393
394 if (fr_coord_pre_event_insert(el) < 0) {
395 fr_strerror_const("Failed adding coordinator pre-check to event list");
396 goto st_fail;
397 }
398
399 /*
400 * Add the event which processes request_t packets.
401 */
402 if (fr_event_post_insert(el, fr_worker_post_event, sc->single_worker) < 0) {
403 fr_strerror_const("Failed inserting post-processing event");
404 goto st_fail;
405 }
406
408 fr_strerror_const("Failed adding coordinator post-processing to event list");
409 goto st_fail;
410 }
411
412 return sc;
413 }
414
415 /*
416 * Create the lists which hold the workers and networks.
417 */
418 fr_dlist_init(&sc->workers, fr_schedule_worker_t, thread.entry);
419 fr_dlist_init(&sc->networks, fr_schedule_network_t, thread.entry);
420
421 sc->network_sem = fr_sem_alloc();
422 if (!sc->network_sem) {
423 sem_fail:
424 ERROR("Failed creating semaphore: %s", fr_syserror(errno));
425 fr_sem_free(sc->network_sem);
426 fr_sem_free(sc->worker_sem);
428 return NULL;
429 }
430
431 sc->worker_sem = fr_sem_alloc();
432 if (!sc->worker_sem) goto sem_fail;
433
434 sc->coord_sem = fr_sem_alloc();
435 if (!sc->coord_sem) goto sem_fail;
436
437 /*
438 * Create the network threads first.
439 */
440 for (i = 0; i < sc->config->max_networks; i++) {
441 DEBUG3("Creating %u/%u networks", i + 1, sc->config->max_networks);
442
443 /*
444 * Create a worker "glue" structure
445 */
446 sn = talloc_zero(sc, fr_schedule_network_t);
447 if (!sn) {
448 ERROR("Network %u - Failed allocating memory", i);
449 break;
450 }
451
452 sn->thread.id = i;
453 sn->sc = sc;
455
457 talloc_free(sn);
458 PERROR("Failed creating network %u", i);
459 break;
460 }
461
462 fr_dlist_insert_head(&sc->networks, sn);
463 }
464
465 /*
466 * Wait for all of the networks to signal us that either
467 * they've started, OR there's been a problem and they
468 * can't start.
469 */
470 if (fr_thread_wait_list(sc->network_sem, &sc->networks) < 0) {
472 return NULL;
473 }
474
475 /*
476 * Create the coordination threads
477 */
478 if (fr_coord_start(sc->config->max_workers, sc->coord_sem) < 0) {
480 return NULL;
481 };
482
483 /*
484 * Create all of the workers.
485 */
486 for (i = 0; i < sc->config->max_workers; i++) {
487 DEBUG3("Creating %u/%u workers", i + 1, sc->config->max_workers);
488
489 /*
490 * Create a worker "glue" structure
491 */
492 sw = talloc_zero(sc, fr_schedule_worker_t);
493 if (!sw) {
494 ERROR("Worker %u - Failed allocating memory", i);
495 break;
496 }
497
498 sw->thread.id = i;
499 sw->sc = sc;
501
503 talloc_free(sw);
504 PERROR("Failed creating worker %u", i);
505 break;
506 }
507
508 fr_dlist_insert_head(&sc->workers, sw);
509 }
510
511 /*
512 * Wait for all of the workers to signal us that either
513 * they've started, OR there's been a problem and they
514 * can't start.
515 */
516 if (fr_thread_wait_list(sc->worker_sem, &sc->workers) < 0) {
518 return NULL;
519 }
520
521 for (sw = fr_dlist_head(&sc->workers), i = 0;
522 sw != NULL;
523 sw = next_sw, i++) {
524 char buffer[32];
525
526 next_sw = fr_dlist_next(&sc->workers, sw);
527
528 snprintf(buffer, sizeof(buffer), "%d", i);
530 PERROR("Failed adding worker commands");
531 mt_fail:
533 return NULL;
534 }
535 }
536
537 for (sn = fr_dlist_head(&sc->networks), i = 0;
538 sn != NULL;
539 sn = next_sn, i++) {
540 char buffer[32];
541
542 next_sn = fr_dlist_next(&sc->networks, sn);
543
544 snprintf(buffer, sizeof(buffer), "%d", i);
546 PERROR("Failed adding network commands");
547 goto mt_fail;
548 }
549 }
550
551 if (sc) INFO("Scheduler created successfully with %u networks and %u workers",
552 sc->config->max_networks, (unsigned int)fr_dlist_num_elements(&sc->workers));
553
554 /*
555 * Instantiate thread-local data for the main thread too.
556 * In single-threaded mode this is done above. In
557 * multi-worker mode the main thread also needs module
558 * thread data so that triggers can use module xlats.
559 */
560 if (sc->worker_thread_instantiate &&
561 unlikely((sc->worker_thread_instantiate(sc, el, NULL) < 0))) {
562 PERROR("Main thread instantiation failed");
563 goto mt_fail;
564 }
565
566 return sc;
567}
568
569/** Destroy a scheduler, and tell its child threads to exit.
570 *
571 * @note This may be called with no worker or network threads in the case of a
572 * instantiation error. This function _should_ deal with that condition
573 * gracefully.
574 *
575 * @param[in] sc_to_free the scheduler
576 * @return
577 * - <0 on error
578 * - 0 on success
579 */
581{
582 fr_schedule_t *sc = *sc_to_free;
583 unsigned int i;
586 int ret;
587
588 if (!sc) return 0;
589
590 sc->running = false;
591
592
593
594 /*
595 * Single threaded mode: kill the only network / worker we have.
596 */
597 if (sc->single_threaded) {
598 /*
599 * Destroy the network side first. It tells the
600 * workers to close.
601 */
602 if (unlikely(fr_network_destroy(sc->single_network) < 0)) {
603 ERROR("Failed destroying network");
604 }
605 fr_worker_destroy(sc->single_worker);
607
608 goto done;
609 } else {
610 /*
611 * Detach thread-local data for the main thread.
612 * Worker threads handle their own detach, but
613 * the main thread was instantiated explicitly
614 * by fr_schedule_create.
615 */
616 if (sc->worker_thread_detach) sc->worker_thread_detach(NULL);
617 }
618
619 /*
620 * Signal each network thread to exit.
621 */
622 fr_dlist_foreach(&sc->networks, fr_schedule_network_t, sne) {
623 if (fr_network_exit(sne->nr) < 0) {
624 PERROR("Failed signaling network %i to exit", sne->thread.id);
625 }
626 }
627
628 /*
629 * If the network threads are running, tell them to exit,
630 * and wait for them to do so. Each network thread tells
631 * all of its worker threads that it's exiting. It then
632 * closes the channels. When the workers see that there
633 * are no input channels, they exit, too.
634 */
635 for (i = 0; i < (unsigned int)fr_dlist_num_elements(&sc->networks); i++) {
636 DEBUG2("Scheduler - Waiting for semaphore indicating network exit %u/%u", i + 1,
637 (unsigned int)fr_dlist_num_elements(&sc->networks));
638 SEM_WAIT_INTR(sc->network_sem);
639 }
640 DEBUG2("Scheduler - All networks indicated exit complete");
641
642 while ((sn = fr_dlist_pop_head(&sc->networks)) != NULL) {
643 /*
644 * Ensure that the thread has exited before
645 * cleaning up the context.
646 *
647 * This also ensures that the child threads have
648 * exited before the main thread cleans up the
649 * module instances.
650 */
651 if ((ret = pthread_join(sn->thread.pthread_id, NULL)) != 0) {
652 ERROR("Failed joining network %i: %s", sn->thread.id, fr_syserror(ret));
653 } else {
654 DEBUG2("Network %i joined (cleaned up)", sn->thread.id);
655 }
656 }
657
658 /*
659 * Wait for all worker threads to finish. THEN clean up
660 * modules. Otherwise, the modules will be removed from
661 * underneath the workers!
662 */
663 for (i = 0; i < (unsigned int)fr_dlist_num_elements(&sc->workers); i++) {
664 DEBUG2("Scheduler - Waiting for semaphore indicating worker exit %u/%u", i + 1,
665 (unsigned int)fr_dlist_num_elements(&sc->workers));
666 SEM_WAIT_INTR(sc->worker_sem);
667 }
668 DEBUG2("Scheduler - All workers indicated exit complete");
669
670 /*
671 * Clean up the exited workers.
672 */
673 while ((sw = fr_dlist_pop_head(&sc->workers)) != NULL) {
674 /*
675 * Ensure that the thread has exited before
676 * cleaning up the context.
677 *
678 * This also ensures that the child threads have
679 * exited before the main thread cleans up the
680 * module instances.
681 */
682 if ((ret = pthread_join(sw->thread.pthread_id, NULL)) != 0) {
683 ERROR("Failed joining worker %i: %s", sw->thread.id, fr_syserror(ret));
684 } else {
685 DEBUG2("Worker %i joined (cleaned up)", sw->thread.id);
686 }
687 }
688
689 fr_sem_free(sc->coord_sem);
690 fr_sem_free(sc->network_sem);
691 fr_sem_free(sc->worker_sem);
692
693done:
694 /*
695 * Now that all of the workers are done, we can return to
696 * the caller, and have it dlclose() the modules.
697 */
699 *sc_to_free = NULL;
700
701 return 0;
702}
703
704/** Add a fr_listen_t to a scheduler.
705 *
706 * @param[in] sc the scheduler
707 * @param[in] li the ctx and callbacks for the transport.
708 * @return
709 * - NULL on error
710 * - the fr_network_t that the socket was added to.
711 */
713{
714 fr_network_t *nr;
715
716 (void) talloc_get_type_abort(sc, fr_schedule_t);
717
718 if (sc->single_threaded) {
719 nr = sc->single_network;
720 } else {
722
723 /*
724 * @todo - round robin it among the listeners?
725 * or maybe add it to the same parent thread?
726 */
727 sn = fr_dlist_head(&sc->networks);
728 nr = sn->nr;
729 }
730
731 if (fr_network_listen_add(nr, li) < 0) return NULL;
732
733 return nr;
734}
735
736/** Add a directory NOTE_EXTEND to a scheduler.
737 *
738 * @param[in] sc the scheduler
739 * @param[in] li the ctx and callbacks for the transport.
740 * @return
741 * - NULL on error
742 * - the fr_network_t that the socket was added to.
743 */
745{
746 fr_network_t *nr;
747
748 (void) talloc_get_type_abort(sc, fr_schedule_t);
749
750 if (sc->single_threaded) {
751 nr = sc->single_network;
752 } else {
754
755 /*
756 * @todo - round robin it among the listeners?
757 * or maybe add it to the same parent thread?
758 */
759 sn = fr_dlist_head(&sc->networks);
760 nr = sn->nr;
761 }
762
763 if (fr_network_directory_add(nr, li) < 0) return NULL;
764
765 return nr;
766}
static int const char char buffer[256]
Definition acutest.h:576
#define RCSID(id)
Definition build.h:512
#define unlikely(_x)
Definition build.h:407
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:1029
fr_command_register_hook_t fr_command_register_hook
Definition command.c:41
int fr_coords_create(TALLOC_CTX *ctx, fr_event_list_t *el)
Start coordinators in single threaded mode.
Definition coord.c:592
void fr_coords_destroy(void)
Clean up coordinators in single threaded mode.
Definition coord.c:574
int fr_coord_post_event_insert(fr_event_list_t *el)
Insert instance specific post-event callbacks.
Definition coord.c:833
int fr_coord_pre_event_insert(fr_event_list_t *el)
Insert instance specific pre-event callbacks.
Definition coord.c:808
int fr_coord_start(uint32_t num_workers, fr_sem_t *sem)
Start all registered coordinator threads in multi-threaded mode.
Definition coord.c:524
#define MEM(x)
Definition debug.h:46
#define ERROR(fmt,...)
Definition dhcpclient.c:40
#define DEBUG(fmt,...)
Definition dhcpclient.c:38
#define fr_dlist_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:242
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:468
#define fr_dlist_foreach(_list_head, _type, _iter)
Iterate over the contents of a list.
Definition dlist.h:98
static unsigned int fr_dlist_num_elements(fr_dlist_head_t const *head)
Return the number of elements in the dlist.
Definition dlist.h:921
static void * fr_dlist_pop_head(fr_dlist_head_t *list_head)
Remove the head item in a list.
Definition dlist.h:654
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:320
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:537
Head of a doubly linked list.
Definition dlist.h:51
talloc_free(hp)
fr_cmd_table_t cmd_network_table[]
Definition network.c:2138
int fr_network_listen_add(fr_network_t *nr, fr_listen_t *li)
Add a fr_listen_t to a network.
Definition network.c:231
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:300
int fr_network_destroy(fr_network_t *nr)
Stop a network thread in an orderly way.
Definition network.c:1721
int fr_network_directory_add(fr_network_t *nr, fr_listen_t *li)
Add a "watch directory" call to a network.
Definition network.c:285
void fr_network(fr_network_t *nr)
The main network worker function.
Definition network.c:1837
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:320
int fr_network_exit(fr_network_t *nr)
Signal a network thread to exit.
Definition network.c:1892
void fr_network_stats_log(fr_network_t const *nr, fr_log_t const *log)
Definition network.c:2071
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:1925
#define PERROR(_fmt,...)
Definition log.h:228
#define DEBUG3(_fmt,...)
Definition log.h:266
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:1953
int fr_event_post_insert(fr_event_list_t *el, fr_event_post_cb_t callback, void *uctx)
Add a post-event callback to the event list.
Definition event.c:2000
Stores all information relating to an event list.
Definition event.c:377
fr_log_lvl_t
Definition log.h:64
static const conf_parser_t config[]
Definition base.c:163
#define DEBUG2(fmt,...)
static bool done
Definition radclient.c:80
#define INFO(fmt,...)
Definition radict.c:63
CONF_SECTION * cs
thread pool configuration section
Definition schedule.c:79
fr_thread_t thread
common thread structure - must be first!
Definition schedule.c:63
fr_timer_t * ev
timer for stats_interval
Definition schedule.c:69
static _Thread_local int worker_id
Internal ID of the current worker thread.
Definition schedule.c:104
fr_sem_t * coord_sem
for inter-thread signaling
Definition schedule.c:92
fr_schedule_t * fr_schedule_create(TALLOC_CTX *ctx, bool single_threaded, 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:275
fr_event_list_t * el
event list for single-threaded mode.
Definition schedule.c:80
fr_schedule_t * sc
the scheduler we are running under
Definition schedule.c:65
fr_sem_t * worker_sem
for inter-thread signaling
Definition schedule.c:90
fr_worker_t * single_worker
for single-threaded mode
Definition schedule.c:101
fr_log_lvl_t lvl
log level
Definition schedule.c:84
static void stats_timer(fr_timer_list_t *tl, fr_time_t now, void *uctx)
Definition schedule.c:202
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:744
fr_schedule_config_t * config
configuration
Definition schedule.c:86
fr_network_t * single_network
for single-threaded mode
Definition schedule.c:100
fr_schedule_thread_instantiate_t worker_thread_instantiate
thread instantiation callback
Definition schedule.c:94
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:712
fr_network_t * nr
the receive data structure
Definition schedule.c:67
fr_schedule_thread_detach_t worker_thread_detach
Definition schedule.c:95
bool running
is the scheduler running?
Definition schedule.c:77
void fr_schedule_worker_id_set(int id)
Explicitly set the worker id for the current thread.
Definition schedule.c:119
int fr_schedule_worker_id(void)
Return the worker id for the current thread.
Definition schedule.c:110
static void * fr_schedule_worker_thread(void *arg)
Entry point for worker threads.
Definition schedule.c:129
fr_sem_t * network_sem
for inter-thread signaling
Definition schedule.c:91
unsigned int num_workers_exited
number of exited workers
Definition schedule.c:88
int fr_schedule_destroy(fr_schedule_t **sc_to_free)
Destroy a scheduler, and tell its child threads to exit.
Definition schedule.c:580
bool single_threaded
true if running in single-threaded mode.
Definition schedule.c:81
fr_dlist_head_t networks
list of networks
Definition schedule.c:98
fr_log_t * log
log destination
Definition schedule.c:83
fr_dlist_head_t workers
list of workers
Definition schedule.c:97
static void * fr_schedule_network_thread(void *arg)
Initialize and run the network thread.
Definition schedule.c:216
Scheduler specific information for network threads.
Definition schedule.c:62
The scheduler.
Definition schedule.c:76
int(* fr_schedule_thread_instantiate_t)(TALLOC_CTX *ctx, fr_event_list_t *el, void *uctx)
Setup a new thread.
Definition schedule.h:56
void(* fr_schedule_thread_detach_t)(void *uctx)
Explicitly free resources allocated by fr_schedule_thread_instantiate_t.
Definition schedule.h:62
fr_time_delta_t stats_interval
print channel statistics
Definition schedule.h:71
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
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:93
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
void fr_thread_start(fr_thread_t *thread, fr_sem_t *sem)
Signal the parent that we're done.
Definition thread.c:203
int fr_thread_wait_list(fr_sem_t *sem, fr_dlist_head_t *head)
Wait for multiple threads to signal readiness via a semaphore.
Definition thread.c:79
int fr_thread_create(pthread_t *thread, fr_thread_entry_t func, void *arg)
Create a joinable thread.
Definition thread.c:46
int fr_thread_setup(fr_thread_t *out, char const *name)
Common setup for child threads: block signals, allocate a talloc context, and create an event list.
Definition thread.c:112
void fr_thread_exit(fr_thread_t *thread, fr_thread_status_t status, fr_sem_t *sem)
Signal the parent that we're done.
Definition thread.c:218
fr_thread_status_t status
running, etc.
Definition thread.h:49
fr_event_list_t * el
our event list
Definition thread.h:53
int id
unique ID for this thread
Definition thread.h:48
fr_thread_status_t
Track the child thread status.
Definition thread.h:38
@ FR_THREAD_INITIALIZING
initialized, but not running
Definition thread.h:40
@ FR_THREAD_EXITED
exited, and in the exited queue
Definition thread.h:42
@ FR_THREAD_FAIL
failed, and in the exited queue
Definition thread.h:43
TALLOC_CTX * ctx
our allocation ctx
Definition thread.h:52
pthread_t pthread_id
of this thread
Definition thread.h:50
#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
An event timer list.
Definition timer.c:49
A timer event.
Definition timer.c:83
#define fr_timer_in(...)
Definition timer.h:87
#define fr_timer_at(...)
Definition timer.h:81
static fr_event_list_t * el
#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:1560
fr_worker_t * fr_worker_alloc(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:1350
fr_cmd_table_t cmd_worker_table[]
Definition worker.c:1751
void fr_worker_destroy(fr_worker_t *worker)
Destroy a worker.
Definition worker.c:1010
void fr_worker_post_event(UNUSED fr_event_list_t *el, UNUSED fr_time_t now, void *uctx)
Post-event handler.
Definition worker.c:1581
void fr_worker(fr_worker_t *worker)
The main loop and entry point of the stand-alone worker thread.
Definition worker.c:1502
A worker which takes packets from a master, and processes them.
Definition worker.c:90
int uses
how many network threads are using it
Definition schedule.c:49
fr_schedule_t * sc
the scheduler we are running under
Definition schedule.c:52
fr_thread_t thread
common thread structure - must be first!
Definition schedule.c:47
fr_time_t cpu_time
how much CPU time this worker has used
Definition schedule.c:50
fr_worker_t * worker
the worker data structure
Definition schedule.c:54
Scheduler specific information for worker threads.
Definition schedule.c:46