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: 9e8cc03f39e4e22a305416a4f6e47cc343631c06 $
19 *
20 * @brief Network / worker thread scheduling
21 * @file io/schedule.c
22 *
23 * @copyright 2016 Alan DeKok (aland@freeradius.org)
24 */
25RCSID("$Id: 9e8cc03f39e4e22a305416a4f6e47cc343631c06 $")
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/** Entry point for worker threads
116 *
117 * @param[in] arg the fr_schedule_worker_t
118 * @return NULL
119 */
120static void *fr_schedule_worker_thread(void *arg)
121{
122 fr_schedule_worker_t *sw = talloc_get_type_abort(arg, fr_schedule_worker_t);
123 fr_schedule_t *sc = sw->sc;
125 char worker_name[32];
126
127 worker_id = sw->thread.id; /* Store the current worker ID */
128
129 snprintf(worker_name, sizeof(worker_name), "Worker %d", sw->thread.id);
130
131 if (fr_thread_setup(&sw->thread, worker_name) < 0) goto fail;
132
133 sw->worker = fr_worker_alloc(sw->thread.ctx, sw->thread.el, worker_name, sc->log, sc->lvl, &sc->config->worker);
134 if (!sw->worker) {
135 PERROR("%s - Failed creating worker", worker_name);
136 goto fail;
137 }
138
139 /*
140 * @todo make this a registry
141 */
142 if (sc->worker_thread_instantiate) {
143 CONF_SECTION *cs;
144 char section_name[32];
145
146 snprintf(section_name, sizeof(section_name), "%u", sw->thread.id);
147
148 cs = cf_section_find(sc->cs, "worker", section_name);
149 if (!cs) cs = cf_section_find(sc->cs, "worker", NULL);
150
151 if (sc->worker_thread_instantiate(sw->thread.ctx, sw->thread.el, cs) < 0) {
152 PERROR("%s - Worker thread instantiation failed", worker_name);
153 goto fail;
154 }
155 }
156
157 /*
158 * Add this worker to all network threads.
159 */
160 fr_dlist_foreach(&sc->networks, fr_schedule_network_t, sn) {
161 if (unlikely(fr_network_worker_add(sn->nr, sw->worker) < 0)) {
162 PERROR("%s - Failed adding worker to network %u", worker_name, sn->thread.id);
163 goto fail; /* FIXME - Should maybe try to undo partial adds? */
164 }
165 }
166
167 /*
168 * Tell the originator that the thread has started.
169 */
170 fr_thread_start(&sw->thread, sc->worker_sem);
171
172 /*
173 * Do all of the work.
174 */
175 fr_worker(sw->worker);
176
177 status = FR_THREAD_EXITED;
178
179fail:
180 if (sw->worker) {
182 sw->worker = NULL;
183 }
184
185 if (sc->worker_thread_detach) sc->worker_thread_detach(NULL); /* Fixme once we figure out what uctx should be */
186
187 fr_thread_exit(&sw->thread, status, sc->worker_sem);
188
189 return NULL;
190}
191
192
193static void stats_timer(fr_timer_list_t *tl, fr_time_t now, void *uctx)
194{
195 fr_schedule_network_t *sn = talloc_get_type_abort(uctx, fr_schedule_network_t);
196
197 fr_network_stats_log(sn->nr, sn->sc->log);
198
199 (void) fr_timer_at(sn, tl, &sn->ev, fr_time_add(now, sn->sc->config->stats_interval), false, stats_timer, sn);
200}
201
202/** Initialize and run the network thread.
203 *
204 * @param[in] arg the fr_schedule_network_t
205 * @return NULL
206 */
207static void *fr_schedule_network_thread(void *arg)
208{
209 fr_schedule_network_t *sn = talloc_get_type_abort(arg, fr_schedule_network_t);
210 fr_schedule_t *sc = sn->sc;
212 char network_name[32];
213
214 snprintf(network_name, sizeof(network_name), "Network %d", sn->thread.id);
215
216 if (fr_thread_setup(&sn->thread, network_name) < 0) goto fail;
217
218 sn->nr = fr_network_create(sn->thread.ctx, sn->thread.el, network_name, sc->log, sc->lvl, &sc->config->network);
219 if (!sn->nr) {
220 PERROR("%s - Failed creating network", network_name);
221 goto fail;
222 }
223
224 /*
225 * Tell the originator that the thread has started.
226 */
227 fr_thread_start(&sn->thread, sc->network_sem);
228
229 /*
230 * Print out statistics for this network IO handler.
231 */
232 if (fr_time_delta_ispos(sc->config->stats_interval)) {
233 (void) fr_timer_in(sn, sn->thread.el->tl, &sn->ev, sn->sc->config->stats_interval, false, stats_timer, sn);
234 }
235
236 /*
237 * Call the main event processing loop of the network
238 * thread Will not return until the worker is about
239 * to exit.
240 */
241 fr_network(sn->nr);
242
243 status = FR_THREAD_EXITED;
244
245fail:
246 fr_thread_exit(&sn->thread, status, sc->network_sem);
247
248 return NULL;
249}
250
251/** Create a scheduler and spawn the child threads.
252 *
253 * @param[in] ctx talloc context.
254 * @param[in] single_threaded no workers are spawned, everything runs in a common event loop.
255 * @param[in] el event list, only for single-threaded mode.
256 * @param[in] logger destination for all logging messages.
257 * @param[in] lvl log level.
258 * @param[in] worker_thread_instantiate callback for new worker threads.
259 * @param[in] worker_thread_detach callback to destroy resources
260 * allocated by worker_thread_instantiate.
261 * @param[in] config configuration for the scheduler
262 * @return
263 * - NULL on error
264 * - fr_schedule_t new scheduler
265 */
267 bool single_threaded,
269 fr_log_t *logger, fr_log_lvl_t lvl,
270 fr_schedule_thread_instantiate_t worker_thread_instantiate,
271 fr_schedule_thread_detach_t worker_thread_detach,
273{
274 unsigned int i;
275 fr_schedule_worker_t *sw, *next_sw;
276 fr_schedule_network_t *sn, *next_sn;
278
279 sc = talloc_zero(ctx, fr_schedule_t);
280 if (!sc) {
281 fr_strerror_const("Failed allocating memory");
282 return NULL;
283 }
284
285 /*
286 * Parse any scheduler-specific configuration.
287 */
288 if (!config) {
289 MEM(sc->config = talloc_zero(sc, fr_schedule_config_t));
290 sc->config->max_networks = 1;
291 sc->config->max_workers = 4;
292 } else {
293 sc->config = config;
294
295 if (sc->config->max_networks < 1) sc->config->max_networks = 1;
296 if (sc->config->max_networks > 64) sc->config->max_networks = 64;
297 if (sc->config->max_workers < 1) sc->config->max_workers = 1;
298 if (sc->config->max_workers > 64) sc->config->max_workers = 64;
299 }
300
301 sc->el = el;
302 sc->single_threaded = single_threaded;
303 sc->log = logger;
304 sc->lvl = lvl;
305 sc->cs = sc->config->cs;
306
307 sc->worker_thread_instantiate = worker_thread_instantiate;
308 sc->worker_thread_detach = worker_thread_detach;
309 sc->running = true;
310
311 /*
312 * If we're single-threaded, create network / worker, and insert them into the event loop.
313 */
314 if (single_threaded) {
315 sc->single_network = fr_network_create(sc, el, "Network", sc->log, sc->lvl, &sc->config->network);
316 if (!sc->single_network) {
317 PERROR("Failed creating network");
318 pre_instantiate_st_fail:
320 return NULL;
321 }
322
323 if (fr_coords_create(sc, el) < 0) {
324 PERROR("Failed creating coordinators");
325 if (unlikely(fr_network_destroy(sc->single_network) < 0)) {
326 PERROR("Failed destroying network");
327 }
328 goto pre_instantiate_st_fail;
329 }
330
331 worker_id = 0;
332 sc->single_worker = fr_worker_alloc(sc, el, "Worker", sc->log, sc->lvl, &sc->config->worker);
333 if (!sc->single_worker) {
334 PERROR("Failed creating worker");
335 if (unlikely(fr_network_destroy(sc->single_network) < 0)) {
336 PERROR("Failed destroying network");
337 }
338 goto pre_instantiate_st_fail;
339 }
340
341 /*
342 * Parent thread-specific data from the single_worker
343 */
344 if (sc->worker_thread_instantiate) {
345 CONF_SECTION *subcs;
346
347 subcs = cf_section_find(sc->cs, "worker", "0");
348 if (!subcs) subcs = cf_section_find(sc->cs, "worker", NULL);
349
350 if (sc->worker_thread_instantiate(sc->single_worker, el, subcs) < 0) {
351 PERROR("Worker thread instantiation failed");
352 destroy_both:
353 if (unlikely(fr_network_destroy(sc->single_network) < 0)) {
354 PERROR("Failed destroying network");
355 }
356 fr_worker_destroy(sc->single_worker);
357 goto pre_instantiate_st_fail;
358 }
359 }
360
361 if (fr_command_register_hook(NULL, "0", sc->single_worker, cmd_worker_table) < 0) {
362 PERROR("Failed adding worker commands");
363 st_fail:
364 if (sc->worker_thread_detach) sc->worker_thread_detach(NULL);
365 goto destroy_both;
366 }
367
368 if (fr_command_register_hook(NULL, "0", sc->single_network, cmd_network_table) < 0) {
369 PERROR("Failed adding network commands");
370 goto st_fail;
371 }
372
373 /*
374 * Register the worker with the network, so
375 * things like fr_network_send_request() work.
376 */
377 fr_network_worker_add_self(sc->single_network, sc->single_worker);
378 DEBUG("Scheduler created in single-threaded mode");
379
380 if (fr_event_pre_insert(el, fr_worker_pre_event, sc->single_worker) < 0) {
381 fr_strerror_const("Failed adding pre-check to event list");
382 goto st_fail;
383 }
384
385 if (fr_coord_pre_event_insert(el) < 0) {
386 fr_strerror_const("Failed adding coordinator pre-check to event list");
387 goto st_fail;
388 }
389
390 /*
391 * Add the event which processes request_t packets.
392 */
393 if (fr_event_post_insert(el, fr_worker_post_event, sc->single_worker) < 0) {
394 fr_strerror_const("Failed inserting post-processing event");
395 goto st_fail;
396 }
397
399 fr_strerror_const("Failed adding coordinator post-processing to event list");
400 goto st_fail;
401 }
402
403 return sc;
404 }
405
406 /*
407 * Create the lists which hold the workers and networks.
408 */
409 fr_dlist_init(&sc->workers, fr_schedule_worker_t, thread.entry);
410 fr_dlist_init(&sc->networks, fr_schedule_network_t, thread.entry);
411
412 sc->network_sem = fr_sem_alloc();
413 if (!sc->network_sem) {
414 sem_fail:
415 ERROR("Failed creating semaphore: %s", fr_syserror(errno));
416 fr_sem_free(sc->network_sem);
417 fr_sem_free(sc->worker_sem);
419 return NULL;
420 }
421
422 sc->worker_sem = fr_sem_alloc();
423 if (!sc->worker_sem) goto sem_fail;
424
425 sc->coord_sem = fr_sem_alloc();
426 if (!sc->coord_sem) goto sem_fail;
427
428 /*
429 * Create the network threads first.
430 */
431 for (i = 0; i < sc->config->max_networks; i++) {
432 DEBUG3("Creating %u/%u networks", i + 1, sc->config->max_networks);
433
434 /*
435 * Create a worker "glue" structure
436 */
437 sn = talloc_zero(sc, fr_schedule_network_t);
438 if (!sn) {
439 ERROR("Network %u - Failed allocating memory", i);
440 break;
441 }
442
443 sn->thread.id = i;
444 sn->sc = sc;
446
448 talloc_free(sn);
449 PERROR("Failed creating network %u", i);
450 break;
451 }
452
453 fr_dlist_insert_head(&sc->networks, sn);
454 }
455
456 /*
457 * Wait for all of the networks to signal us that either
458 * they've started, OR there's been a problem and they
459 * can't start.
460 */
461 if (fr_thread_wait_list(sc->network_sem, &sc->networks) < 0) {
463 return NULL;
464 }
465
466 /*
467 * Create the coordination threads
468 */
469 if (fr_coord_start(sc->config->max_workers, sc->coord_sem) < 0) {
471 return NULL;
472 };
473
474 /*
475 * Create all of the workers.
476 */
477 for (i = 0; i < sc->config->max_workers; i++) {
478 DEBUG3("Creating %u/%u workers", i + 1, sc->config->max_workers);
479
480 /*
481 * Create a worker "glue" structure
482 */
483 sw = talloc_zero(sc, fr_schedule_worker_t);
484 if (!sw) {
485 ERROR("Worker %u - Failed allocating memory", i);
486 break;
487 }
488
489 sw->thread.id = i;
490 sw->sc = sc;
492
494 talloc_free(sw);
495 PERROR("Failed creating worker %u", i);
496 break;
497 }
498
499 fr_dlist_insert_head(&sc->workers, sw);
500 }
501
502 /*
503 * Wait for all of the workers to signal us that either
504 * they've started, OR there's been a problem and they
505 * can't start.
506 */
507 if (fr_thread_wait_list(sc->worker_sem, &sc->workers) < 0) {
509 return NULL;
510 }
511
512 for (sw = fr_dlist_head(&sc->workers), i = 0;
513 sw != NULL;
514 sw = next_sw, i++) {
515 char buffer[32];
516
517 next_sw = fr_dlist_next(&sc->workers, sw);
518
519 snprintf(buffer, sizeof(buffer), "%d", i);
521 PERROR("Failed adding worker commands");
522 mt_fail:
524 return NULL;
525 }
526 }
527
528 for (sn = fr_dlist_head(&sc->networks), i = 0;
529 sn != NULL;
530 sn = next_sn, i++) {
531 char buffer[32];
532
533 next_sn = fr_dlist_next(&sc->networks, sn);
534
535 snprintf(buffer, sizeof(buffer), "%d", i);
537 PERROR("Failed adding network commands");
538 goto mt_fail;
539 }
540 }
541
542 if (sc) INFO("Scheduler created successfully with %u networks and %u workers",
543 sc->config->max_networks, (unsigned int)fr_dlist_num_elements(&sc->workers));
544
545 /*
546 * Instantiate thread-local data for the main thread too.
547 * In single-threaded mode this is done above. In
548 * multi-worker mode the main thread also needs module
549 * thread data so that triggers can use module xlats.
550 */
551 if (sc->worker_thread_instantiate &&
552 unlikely((sc->worker_thread_instantiate(sc, el, NULL) < 0))) {
553 PERROR("Main thread instantiation failed");
554 goto mt_fail;
555 }
556
557 return sc;
558}
559
560/** Destroy a scheduler, and tell its child threads to exit.
561 *
562 * @note This may be called with no worker or network threads in the case of a
563 * instantiation error. This function _should_ deal with that condition
564 * gracefully.
565 *
566 * @param[in] sc_to_free the scheduler
567 * @return
568 * - <0 on error
569 * - 0 on success
570 */
572{
573 fr_schedule_t *sc = *sc_to_free;
574 unsigned int i;
577 int ret;
578
579 if (!sc) return 0;
580
581 sc->running = false;
582
583
584
585 /*
586 * Single threaded mode: kill the only network / worker we have.
587 */
588 if (sc->single_threaded) {
589 /*
590 * Destroy the network side first. It tells the
591 * workers to close.
592 */
593 if (unlikely(fr_network_destroy(sc->single_network) < 0)) {
594 ERROR("Failed destroying network");
595 }
596 fr_worker_destroy(sc->single_worker);
598
599 goto done;
600 } else {
601 /*
602 * Detach thread-local data for the main thread.
603 * Worker threads handle their own detach, but
604 * the main thread was instantiated explicitly
605 * by fr_schedule_create.
606 */
607 if (sc->worker_thread_detach) sc->worker_thread_detach(NULL);
608 }
609
610 /*
611 * Signal each network thread to exit.
612 */
613 fr_dlist_foreach(&sc->networks, fr_schedule_network_t, sne) {
614 if (fr_network_exit(sne->nr) < 0) {
615 PERROR("Failed signaling network %i to exit", sne->thread.id);
616 }
617 }
618
619 /*
620 * If the network threads are running, tell them to exit,
621 * and wait for them to do so. Each network thread tells
622 * all of its worker threads that it's exiting. It then
623 * closes the channels. When the workers see that there
624 * are no input channels, they exit, too.
625 */
626 for (i = 0; i < (unsigned int)fr_dlist_num_elements(&sc->networks); i++) {
627 DEBUG2("Scheduler - Waiting for semaphore indicating network exit %u/%u", i + 1,
628 (unsigned int)fr_dlist_num_elements(&sc->networks));
629 SEM_WAIT_INTR(sc->network_sem);
630 }
631 DEBUG2("Scheduler - All networks indicated exit complete");
632
633 while ((sn = fr_dlist_pop_head(&sc->networks)) != NULL) {
634 /*
635 * Ensure that the thread has exited before
636 * cleaning up the context.
637 *
638 * This also ensures that the child threads have
639 * exited before the main thread cleans up the
640 * module instances.
641 */
642 if ((ret = pthread_join(sn->thread.pthread_id, NULL)) != 0) {
643 ERROR("Failed joining network %i: %s", sn->thread.id, fr_syserror(ret));
644 } else {
645 DEBUG2("Network %i joined (cleaned up)", sn->thread.id);
646 }
647 }
648
649 /*
650 * Wait for all worker threads to finish. THEN clean up
651 * modules. Otherwise, the modules will be removed from
652 * underneath the workers!
653 */
654 for (i = 0; i < (unsigned int)fr_dlist_num_elements(&sc->workers); i++) {
655 DEBUG2("Scheduler - Waiting for semaphore indicating worker exit %u/%u", i + 1,
656 (unsigned int)fr_dlist_num_elements(&sc->workers));
657 SEM_WAIT_INTR(sc->worker_sem);
658 }
659 DEBUG2("Scheduler - All workers indicated exit complete");
660
661 /*
662 * Clean up the exited workers.
663 */
664 while ((sw = fr_dlist_pop_head(&sc->workers)) != NULL) {
665 /*
666 * Ensure that the thread has exited before
667 * cleaning up the context.
668 *
669 * This also ensures that the child threads have
670 * exited before the main thread cleans up the
671 * module instances.
672 */
673 if ((ret = pthread_join(sw->thread.pthread_id, NULL)) != 0) {
674 ERROR("Failed joining worker %i: %s", sw->thread.id, fr_syserror(ret));
675 } else {
676 DEBUG2("Worker %i joined (cleaned up)", sw->thread.id);
677 }
678 }
679
680 fr_sem_free(sc->coord_sem);
681 fr_sem_free(sc->network_sem);
682 fr_sem_free(sc->worker_sem);
683
684done:
685 /*
686 * Now that all of the workers are done, we can return to
687 * the caller, and have it dlclose() the modules.
688 */
690 *sc_to_free = NULL;
691
692 return 0;
693}
694
695/** Add a fr_listen_t to a scheduler.
696 *
697 * @param[in] sc the scheduler
698 * @param[in] li the ctx and callbacks for the transport.
699 * @return
700 * - NULL on error
701 * - the fr_network_t that the socket was added to.
702 */
704{
705 fr_network_t *nr;
706
707 (void) talloc_get_type_abort(sc, fr_schedule_t);
708
709 if (sc->single_threaded) {
710 nr = sc->single_network;
711 } else {
713
714 /*
715 * @todo - round robin it among the listeners?
716 * or maybe add it to the same parent thread?
717 */
718 sn = fr_dlist_head(&sc->networks);
719 nr = sn->nr;
720 }
721
722 if (fr_network_listen_add(nr, li) < 0) return NULL;
723
724 return nr;
725}
726
727/** Add a directory NOTE_EXTEND to a scheduler.
728 *
729 * @param[in] sc the scheduler
730 * @param[in] li the ctx and callbacks for the transport.
731 * @return
732 * - NULL on error
733 * - the fr_network_t that the socket was added to.
734 */
736{
737 fr_network_t *nr;
738
739 (void) talloc_get_type_abort(sc, fr_schedule_t);
740
741 if (sc->single_threaded) {
742 nr = sc->single_network;
743 } else {
745
746 /*
747 * @todo - round robin it among the listeners?
748 * or maybe add it to the same parent thread?
749 */
750 sn = fr_dlist_head(&sc->networks);
751 nr = sn->nr;
752 }
753
754 if (fr_network_directory_add(nr, li) < 0) return NULL;
755
756 return nr;
757}
static int const char char buffer[256]
Definition acutest.h:576
#define RCSID(id)
Definition build.h:506
#define unlikely(_x)
Definition build.h:402
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:1027
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:593
void fr_coords_destroy(void)
Clean up coordinators in single threaded mode.
Definition coord.c:575
int fr_coord_post_event_insert(fr_event_list_t *el)
Insert instance specific post-event callbacks.
Definition coord.c:834
int fr_coord_pre_event_insert(fr_event_list_t *el)
Insert instance specific pre-event callbacks.
Definition coord.c:809
int fr_coord_start(uint32_t num_workers, fr_sem_t *sem)
Start all registered coordinator threads in multi-threaded mode.
Definition coord.c:525
#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:266
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:193
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:735
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:703
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
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:120
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:571
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:207
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