The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
worker.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: f6c4d25882303f2dd1f39a357f23c3f52e2329f1 $
19 *
20 * @brief Worker thread functions.
21 * @file io/worker.c
22 *
23 * The "worker" thread is the one responsible for the bulk of the
24 * work done when processing a request. Workers are spawned by the
25 * scheduler, and create a kqueue (KQ) and control-plane
26 * Atomic Queue (AQ) for control-plane communication.
27 *
28 * When a network thread discovers that it needs more workers, it
29 * asks the scheduler for a KQ/AQ combination. The network thread
30 * then creates a channel dedicated to that worker, and sends the
31 * channel to the worker in a "new channel" message. The worker
32 * receives the channel, and sends an ACK back to the network thread.
33 *
34 * The network thread then sends the worker new packets, which the
35 * worker receives and processes.
36 *
37 * When a packet is decoded, it is put into the "runnable" heap, and
38 * also into the timeout sublist. The main loop fr_worker() then
39 * pulls new requests off of this heap and runs them. The main event
40 * loop checks the head of the timeout sublist, and forcefully terminates
41 * any requests which have been running for too long.
42 *
43 * If a request is yielded, it is placed onto the yielded list in
44 * the worker "tracking" data structure.
45 *
46 * @copyright 2016 Alan DeKok (aland@freeradius.org)
47 */
48
49RCSID("$Id: f6c4d25882303f2dd1f39a357f23c3f52e2329f1 $")
50
51#define LOG_PREFIX worker->name
52#define LOG_DST worker->log
53
54#include <freeradius-devel/io/channel.h>
55#include <freeradius-devel/io/listen.h>
56#include <freeradius-devel/io/worker.h>
57#include <freeradius-devel/unlang/base.h>
58#include <freeradius-devel/util/minmax_heap.h>
59#include <freeradius-devel/util/timer.h>
60
61#include <stdalign.h>
62
63#ifdef WITH_VERIFY_PTR
64static void worker_verify(fr_worker_t *worker);
65#define WORKER_VERIFY worker_verify(worker)
66#else
67#define WORKER_VERIFY
68#endif
69
70static _Atomic(uint64_t) request_number = 0;
71
74
75static _Thread_local fr_ring_buffer_t *fr_worker_rb;
76
77typedef struct {
78 fr_channel_t *ch;
79
80 /*
81 * To save time, we don't care about num_elements here. Which means that we don't
82 * need to cache or lookup the fr_worker_listen_t when we free a request.
83 */
84 fr_dlist_head_t dlist;
86
87/**
88 * A worker which takes packets from a master, and processes them.
89 */
91 char const *name; //!< name of this worker
92 fr_worker_config_t config; //!< external configuration
93
94 unlang_interpret_t *intp; //!< Worker's local interpreter.
95
96 pthread_t thread_id; //!< my thread ID
97
98 fr_log_t const *log; //!< log destination
99 fr_log_lvl_t lvl; //!< log level
100
101 fr_atomic_queue_t *aq_control; //!< atomic queue for control messages sent to me
102
103 fr_control_t *control; //!< the control plane
104
105 fr_event_list_t *el; //!< our event list
106
107 int num_channels; //!< actual number of channels
108
109 fr_heap_t *runnable; //!< current runnable requests which we've spent time processing
110
111 fr_timer_list_t *timeout; //!< Track when requests timeout using a dlist.
112 fr_time_delta_t max_request_time; //!< maximum time a request can be processed
113
114 fr_rb_tree_t *dedup; //!< de-dup tree
115
116 fr_rb_tree_t *listeners; //!< so we can cancel requests when a listener goes away
117
118 fr_io_stats_t stats; //!< input / output stats
119 fr_time_elapsed_t cpu_time; //!< histogram of total CPU time per request
120 fr_time_elapsed_t wall_clock; //!< histogram of wall clock time per request
121
122 uint64_t num_naks; //!< number of messages which were nak'd
123 uint64_t num_active; //!< number of active requests
124
125 fr_time_delta_t predicted; //!< How long we predict a request will take to execute.
126 fr_time_tracking_t tracking; //!< how much time the worker has spent doing things.
127
128 bool was_sleeping; //!< used to suppress multiple sleep signals in a row
129 bool exiting; //!< are we exiting?
130
131 fr_worker_channel_t *channel; //!< list of channels
132
133 request_slab_list_t *slab; //!< slab allocator for request_t
134};
135
136typedef struct {
137 fr_listen_t const *listener; //!< incoming packets
138
139 fr_rb_node_t node; //!< in tree of listeners
140
141 /*
142 * To save time, we don't care about num_elements here. Which means that we don't
143 * need to cache or lookup the fr_worker_listen_t when we free a request.
144 */
145 fr_dlist_head_t dlist; //!< of requests associated with this listener.
147
148
149static int8_t worker_listener_cmp(void const *one, void const *two)
150{
151 fr_worker_listen_t const *a = one, *b = two;
152
153 return CMP(a->listener, b->listener);
154}
155
156
157/*
158 * Explicitly cleanup the memory allocated to the ring buffer,
159 * just in case valgrind complains about it.
160 */
161static int _fr_worker_rb_free(void *arg)
162{
163 return talloc_free(arg);
164}
165
166/** Initialise thread local storage
167 *
168 * @return fr_ring_buffer_t for messages
169 */
171{
173
174 rb = fr_worker_rb;
175 if (rb) return rb;
176
178 if (!rb) {
179 fr_perror("Failed allocating memory for worker ring buffer");
180 return NULL;
181 }
182
184
185 return rb;
186}
187
188static inline bool is_worker_thread(fr_worker_t const *worker)
189{
190 return (pthread_equal(pthread_self(), worker->thread_id) != 0);
191}
192
194static void worker_send_reply(fr_worker_t *worker, request_t *request, bool do_not_respond, fr_time_t now);
195
196/** Callback which handles a message being received on the worker side.
197 *
198 * @param[in] ctx the worker
199 * @param[in] ch the channel to drain
200 * @param[in] cd the message (if any) to start with
201 */
202static void worker_recv_request(void *ctx, fr_channel_t *ch, fr_channel_data_t *cd)
203{
204 fr_worker_t *worker = ctx;
205
206 worker->stats.in++;
207 DEBUG3("Received request %" PRIu64 "", worker->stats.in);
208 cd->channel.ch = ch;
209 worker_request_bootstrap(worker, cd, fr_time());
210}
211
213{
214 request_t *request;
215
216 while ((request = fr_dlist_pop_head(&ch->dlist)) != NULL) {
218 }
219}
220
221static void worker_exit(fr_worker_t *worker)
222{
223 worker->exiting = true;
224
225 /*
226 * Don't allow the post event to run
227 * any more requests. They'll be
228 * signalled to stop before we exit.
229 *
230 * This only has an effect in single
231 * threaded mode.
232 */
233 (void)fr_event_post_delete(worker->el, fr_worker_post_event, worker);
234}
235
236/** Handle a control plane message sent to the worker via a channel
237 *
238 * @param[in] ctx the worker
239 * @param[in] data the message
240 * @param[in] data_size size of the data
241 * @param[in] now the current time
242 */
243static void worker_channel_callback(void *ctx, void const *data, size_t data_size, fr_time_t now)
244{
245 int i;
246 unsigned int num;
247 bool ok, was_sleeping;
248 fr_channel_t *ch;
251 fr_worker_t *worker = ctx;
252
253 was_sleeping = worker->was_sleeping;
254 worker->was_sleeping = false;
255
256 /*
257 * We were woken up by a signal to do something. We're
258 * not sleeping.
259 */
260 ce = fr_channel_service_message(now, &ch, data, data_size);
261 DEBUG3("Channel %s",
262 fr_table_str_by_value(channel_signals, ce, "<INVALID>"));
263 switch (ce) {
264 case FR_CHANNEL_ERROR:
265 return;
266
267 case FR_CHANNEL_EMPTY:
268 return;
269
270 case FR_CHANNEL_NOOP:
271 return;
272
274 fr_assert(0 == 1);
275 break;
276
278 fr_assert(ch != NULL);
279
280 if (!fr_channel_recv_request(ch)) {
281 worker->was_sleeping = was_sleeping;
282
283 } else while (fr_channel_recv_request(ch));
284 break;
285
286 case FR_CHANNEL_OPEN:
287 fr_assert(ch != NULL);
288
289 ok = false;
290 for (i = 0; i < worker->config.max_channels; i++) {
291 fr_assert(worker->channel[i].ch != ch);
292
293 if (worker->channel[i].ch != NULL) continue;
294
295 worker->channel[i].ch = ch;
296 fr_dlist_init(&worker->channel[i].dlist, fr_async_t, entry);
297
298 DEBUG3("Received channel %p into array entry %d", ch, i);
299
300 ms = fr_message_set_create(worker, worker->config.message_set_size,
301 sizeof(fr_channel_data_t),
302 worker->config.ring_buffer_size, false);
303 fr_assert(ms != NULL);
305
306 worker->num_channels++;
307 ok = true;
308 break;
309 }
310
311 fr_cond_assert(ok);
312 break;
313
314 case FR_CHANNEL_CLOSE:
315 fr_assert(ch != NULL);
316
317 ok = false;
318
319 /*
320 * Locate the signalling channel in the list
321 * of channels.
322 */
323 for (i = 0; i < worker->config.max_channels; i++) {
324 if (!worker->channel[i].ch) continue;
325
326 if (worker->channel[i].ch != ch) continue;
327
328 worker_requests_cancel(&worker->channel[i]);
329
331
332 fr_assert_msg(fr_dlist_num_elements(&worker->channel[i].dlist) == 0,
333 "Network added messages to channel after sending FR_CHANNEL_CLOSE");
334
335 /*
336 * Should be nothing left: the network is not supposed
337 * to enqueue anything once it has signalled the close,
338 * which is what the assert above claims. Hand back
339 * whatever we find anyway, so the messages do not
340 * strand the ring buffer they came from, and complain,
341 * because these produce no reply and so the network
342 * never decrements its outstanding count for them.
343 */
345 if (num > 0) PWARN("Discarded %u request(s) still queued at close", num);
346
348 fr_assert(ms != NULL);
350 talloc_free(ms);
351
352 worker->channel[i].ch = NULL;
353
354 fr_assert(!fr_dlist_head(&worker->channel[i].dlist)); /* we can't look at num_elements */
355 fr_assert(worker->num_channels > 0);
356
357 worker->num_channels--;
358 ok = true;
359 break;
360 }
361
362 fr_cond_assert(ok);
363
364 /*
365 * Our last input channel closed,
366 * time to die.
367 */
368 if (worker->num_channels == 0) worker_exit(worker);
369 break;
370 }
371}
372
374{
376 request_t *request;
377
378 wl = fr_rb_find(worker->listeners, &(fr_worker_listen_t) { .listener = li });
379 if (!wl) return -1;
380
381 while ((request = fr_dlist_pop_head(&wl->dlist)) != NULL) {
382 RERROR("Cancelling request due to socket being closed");
384 }
385
386 (void) fr_rb_delete(worker->listeners, wl);
387 talloc_free(wl);
388
389 return 0;
390}
391
392
393/** A socket is going away, so clean up any requests which use this socket.
394 *
395 * @param[in] ctx the worker
396 * @param[in] data the message
397 * @param[in] data_size size of the data
398 * @param[in] now the current time
399 */
400static void worker_listen_cancel_callback(void *ctx, void const *data, NDEBUG_UNUSED size_t data_size, UNUSED fr_time_t now)
401{
402 fr_listen_t const *li;
403 fr_worker_t *worker = ctx;
404
405 fr_assert(data_size == sizeof(li));
406
407 memcpy(&li, data, sizeof(li));
408
409 (void) fr_worker_listen_cancel_self(worker, li);
410}
411
412/** Send a NAK to the network thread
413 *
414 * The network thread believes that a worker is running a request until that request has been NAK'd.
415 * We typically NAK requests when they've been hanging around in the worker's backlog too long,
416 * or there was an error executing the request.
417 *
418 * @param[in] worker the worker
419 * @param[in] cd the message to NAK
420 * @param[in] now when the message is NAKd
421 */
422static void worker_nak(fr_worker_t *worker, fr_channel_data_t *cd, fr_time_t now)
423{
424 size_t size;
425 fr_channel_data_t *reply;
426 fr_channel_t *ch;
428 fr_listen_t *listen;
429
430 worker->num_naks++;
431
432 /*
433 * Cache the outbound channel. We'll need it later.
434 */
435 ch = cd->channel.ch;
436 listen = cd->listen;
437
438 /*
439 * If the channel has been closed, but we haven't
440 * been informed, that is extremely bad.
441 *
442 * Try to continue working... but we'll likely
443 * leak memory or SEGV soon.
444 */
445 if (!fr_cond_assert_msg(fr_channel_active(ch), "Wanted to send NAK but channel has been closed")) {
446 fr_message_done(&cd->m);
447 return;
448 }
449
451 fr_assert(ms != NULL);
452
453 size = listen->app_io->default_reply_size;
454 if (!size) size = listen->app_io->default_message_size;
455
456 /*
457 * Allocate a default message size.
458 */
460
461 /*
462 * Encode a NAK
463 */
464 if (listen->app_io->nak) {
465 size = listen->app_io->nak(listen, cd->packet_ctx, cd->m.data,
466 cd->m.data_size, reply->m.data, reply->m.rb_size);
467 } else {
468 size = 1; /* rely on them to figure it the heck out */
469 }
470
471 (void) fr_message_and_data_commit(ms, &reply->m, size);
472
473 /*
474 * Fill in the NAK.
475 */
476 reply->m.when = now;
477 reply->reply.cpu_time = worker->tracking.running_total;
478 reply->reply.processing_time = fr_time_delta_from_msec(1); /* @todo - set to something better? */
479 reply->reply.request_time = cd->request.recv_time;
480
481 reply->listen = cd->listen;
482 reply->packet_ctx = cd->packet_ctx;
483
484 /*
485 * Mark the original message as done.
486 */
487 fr_message_done(&cd->m);
488
489 /*
490 * Send the reply, which also polls the request queue.
491 */
492 if (fr_channel_send_reply(ch, reply) < 0) {
493 DEBUG2("Failed sending reply to channel");
494 }
495
496 worker->stats.out++;
497}
498
499/** Signal the unlang interpreter that it needs to stop running the request
500 *
501 * Signalling is a synchronous operation. Whatever I/O requests the request
502 * is currently performing are immediately cancelled, and all the frames are
503 * popped off the unlang stack.
504 *
505 * Modules and unlang keywords explicitly register signal handlers to deal
506 * with their yield points being cancelled/interrupted via this function.
507 *
508 * The caller should assume the request is no longer viable after calling
509 * this function.
510 *
511 * @param[in] request request to cancel. The request may still run to completion.
512 */
513static void worker_stop_request(request_t *request)
514{
515 /*
516 * Also marks the request as done and runs
517 * the internal/external callbacs.
518 */
520}
521
522/** Enforce max_request_time
523 *
524 * Run periodically, and tries to clean up requests which were received by the network
525 * thread more than max_request_time seconds ago. In the interest of not adding a
526 * timer for every packet, the requests are given a 1 second leeway.
527 *
528 * @param[in] tl the worker's timer list.
529 * @param[in] when the current time
530 * @param[in] uctx the request_t timing out.
531 */
533{
534 request_t *request = talloc_get_type_abort(uctx, request_t);
535
536 /*
537 * Waiting too long, delete it.
538 */
539 REDEBUG("Request has reached max_request_time - signalling it to stop");
540 worker_stop_request(request);
541
542 /*
543 * This ensures the finally section can run timeout specific policies
544 */
545 request->rcode = RLM_MODULE_TIMEOUT;
546}
547
548
549/** Start time tracking for a request, and mark it as runnable.
550 *
551 */
553{
554 /*
555 * New requests are inserted into the time order heap in
556 * strict time priority. Once they are in the list, they
557 * are only removed when the request is done / free'd.
558 */
559 fr_assert(!fr_timer_armed(request->timeout));
560
561 if (unlikely(fr_timer_in(request, worker->timeout, &request->timeout, worker->config.max_request_time,
562 true, _worker_request_timeout, request) < 0)) {
563 RERROR("Failed to set request timeout timer");
564 return -1;
565 }
566
567 /*
568 * Bootstrap the async state machine with the initial
569 * state of the request.
570 */
571 RDEBUG3("Time tracking started in yielded state");
572 fr_time_tracking_start(&worker->tracking, &request->async->tracking, now);
573 fr_time_tracking_yield(&request->async->tracking, now);
574 worker->num_active++;
575
576 fr_assert(!fr_heap_entry_inserted(request->runnable));
577 (void) fr_heap_insert(&worker->runnable, request);
578
579 return 0;
580}
581
583{
584 RDEBUG3("Time tracking ended");
585 fr_time_tracking_end(&worker->predicted, &request->async->tracking, now);
586 fr_assert(worker->num_active > 0);
587 worker->num_active--;
588
589 TALLOC_FREE(request->timeout); /* Disarm the reques timer */
590}
591
592/** Send a response packet to the network side
593 *
594 * @param[in] worker This worker.
595 * @param[in] request we're sending a reply for.
596 * @param[in] send_reply whether the network side sends a reply
597 * @param[in] now The current time
598 */
599static void worker_send_reply(fr_worker_t *worker, request_t *request, bool send_reply, fr_time_t now)
600{
601 fr_channel_data_t *reply;
602 fr_channel_t *ch;
604 size_t size = 1;
605
606 REQUEST_VERIFY(request);
607
608 /*
609 * If we're sending a reply, then it's no longer runnable.
610 */
611 fr_assert(!fr_heap_entry_inserted(request->runnable));
612
613 if (send_reply) {
614 size = request->async->listen->app_io->default_reply_size;
615 if (!size) size = request->async->listen->app_io->default_message_size;
616 }
617
618 /*
619 * Allocate and send the reply.
620 */
621 ch = request->async->channel;
622 fr_assert(ch != NULL);
623
624 /*
625 * If the channel has been closed, but we haven't
626 * been informed, that is extremely bad.
627 *
628 * Try to continue working... but we'll likely
629 * leak memory or SEGV soon.
630 */
631 if (!fr_cond_assert_msg(fr_channel_active(ch), "Wanted to send reply but channel has been closed")) {
632 return;
633 }
634
636 fr_assert(ms != NULL);
637
639 fr_assert(reply != NULL);
640
641 /*
642 * Encode it, if required.
643 */
644 if (send_reply) {
645 ssize_t slen = 0;
646 fr_listen_t const *listen = request->async->listen;
647
648 if (listen->app_io->encode) {
649 slen = listen->app_io->encode(listen->app_io_instance, request,
650 reply->m.data, reply->m.rb_size);
651 } else if (listen->app->encode) {
652 slen = listen->app->encode(listen->app_instance, request,
653 reply->m.data, reply->m.rb_size);
654 }
655 if (slen < 0) {
656 RPERROR("Failed encoding request");
657 *reply->m.data = 0;
658 slen = 1;
659 }
660
661 /*
662 * Shrink the buffer to the actual packet size.
663 *
664 * This will ALWAYS return the same message as we put in.
665 */
666 fr_assert((size_t) slen <= reply->m.rb_size);
667 (void) fr_message_and_data_commit(ms, &reply->m, slen);
668 }
669
670 /*
671 * Fill in the rest of the fields in the channel message.
672 *
673 * sequence / ack will be filled in by fr_channel_send_reply()
674 */
675 reply->m.when = now;
676 reply->reply.cpu_time = worker->tracking.running_total;
677 reply->reply.processing_time = request->async->tracking.running_total;
678 reply->reply.request_time = request->async->recv_time;
679
680 reply->listen = request->async->listen;
681 reply->packet_ctx = request->async->packet_ctx;
682
683 /*
684 * Update the various timers.
685 */
686 fr_time_elapsed_update(&worker->cpu_time, now, fr_time_add(now, reply->reply.processing_time));
687 fr_time_elapsed_update(&worker->wall_clock, reply->reply.request_time, now);
688
689 RDEBUG("Finished request");
690
691 /*
692 * Send the reply, which also polls the request queue.
693 */
694 if (fr_channel_send_reply(ch, reply) < 0) {
695 /*
696 * Should only happen if the TO_REQUESTOR
697 * channel is full, or it's not yet active.
698 *
699 * Not much we can do except complain
700 * loudly and cleanup the request.
701 */
702 RPERROR("Failed sending reply to network thread");
703 }
704
705 worker->stats.out++;
706
707 fr_assert(!fr_timer_armed(request->timeout));
708 fr_assert(!fr_heap_entry_inserted(request->runnable));
709
710 fr_dlist_entry_unlink(&request->listen_entry);
711
712#ifndef NDEBUG
713 request->async->el = NULL;
714 request->async->channel = NULL;
715 request->async->packet_ctx = NULL;
716 request->async->listen = NULL;
717#endif
718}
719
720/*
721 * talloc_typed_asprintf() is horrifically slow for printing
722 * simple numbers.
723 */
724static char *itoa_internal(TALLOC_CTX *ctx, uint64_t number)
725{
726 char buffer[32];
727 char *p;
728 char const *numbers = "0123456789";
729
730 p = buffer + 30;
731 *(p--) = '\0';
732
733 while (number > 0) {
734 *(p--) = numbers[number % 10];
735 number /= 10;
736 }
737
738 if (p[1]) return talloc_strdup(ctx, p + 1);
739
740 return talloc_strdup(ctx, "0");
741}
742
743/** Initialize various request fields needed by the worker.
744 *
745 */
746static inline CC_HINT(always_inline)
748{
749 /*
750 * For internal requests request->packet
751 * and request->reply are already populated.
752 */
753 if (!request->packet) MEM(request->packet = fr_packet_alloc(request, false));
754 if (!request->reply) MEM(request->reply = fr_packet_alloc(request, false));
755
756 request->packet->timestamp = now;
757 request->async = talloc_zero(request, fr_async_t);
758 request->async->recv_time = now;
759 request->async->el = worker->el;
760 fr_dlist_entry_init(&request->async->entry);
761}
762
763static inline CC_HINT(always_inline)
765{
766 request->number = atomic_fetch_add_explicit(&request_number, 1, memory_order_seq_cst);
767 if (request->name) talloc_const_free(request->name);
768 request->name = itoa_internal(request, request->number);
769}
770
771static inline CC_HINT(always_inline)
773{
774 return fr_timer_list_num_events(worker->timeout);
775}
776
777static int _worker_request_deinit(request_t *request, UNUSED void *uctx)
778{
779 return request_slab_deinit(request);
780}
781
783{
784 int ret = -1;
785 request_t *request;
786 fr_listen_t *listen = cd->listen;
787
788 if (worker_num_requests(worker) >= (uint32_t) worker->config.max_requests) {
789 RATE_LIMIT_GLOBAL(ERROR, "Worker at max requests");
790 goto nak;
791 }
792
793 /*
794 * Receive a message to the worker queue, and decode it
795 * to a request.
796 */
797 fr_assert(listen != NULL);
798
799 request = request_slab_reserve(worker->slab);
800 if (!request) {
801 RATE_LIMIT_GLOBAL(ERROR, "Worker failed allocating new request");
802 goto nak;
803 }
804 /*
805 * Ensures that both the deinit function runs AND
806 * the request is returned to the slab if something
807 * calls talloc_free() on it.
808 */
809 request_slab_element_set_destructor(request, _worker_request_deinit, worker);
810
811 /*
812 * Have to initialise the request manually because namspace
813 * changes based on the listener that allocated it.
814 */
815 if (request_init(request, REQUEST_TYPE_EXTERNAL, (&(request_init_args_t){ .namespace = listen->dict })) < 0) {
816 request_slab_release(request);
817 goto nak;
818 }
819
820 /*
821 * Do normal worker init that's shared between internal
822 * and external requests.
823 */
824 worker_request_init(worker, request, now);
826
827 /*
828 * Associate our interpreter with the request
829 */
830 unlang_interpret_set(request, worker->intp);
831
832 request->packet->timestamp = cd->request.recv_time; /* Legacy - Remove once everything looks at request->async */
833
834 /*
835 * Update the transport-specific fields.
836 */
837 request->async->channel = cd->channel.ch;
838
839 request->async->recv_time = cd->request.recv_time;
840
841 request->async->listen = listen;
842 request->async->packet_ctx = cd->packet_ctx;
843 request->priority = cd->priority;
844
845 /*
846 * Now that the "request" structure has been initialized, go decode the packet.
847 *
848 * Note that this also sets the "async process" function.
849 */
850 if (listen->app->decode) {
851 ret = listen->app->decode(listen->app_instance, request, cd->m.data, cd->m.data_size);
852 } else if (listen->app_io->decode) {
853 ret = listen->app_io->decode(listen->app_io_instance, request, cd->m.data, cd->m.data_size);
854 }
855
856 if (ret < 0) {
857 fail:
858 fr_assert(talloc_parent(request->stack) == request);
859 request_slab_release(request);
860
861 nak:
862 worker_nak(worker, cd, now);
863 return;
864 }
865
866 /*
867 * Set the entry point for this virtual server.
868 */
869 if (unlang_call_push(NULL, request, cd->listen->server_cs, UNLANG_TOP_FRAME) < 0) {
870 RERROR("Protocol failed to set 'process' function");
871 goto fail;
872 }
873
874 /*
875 * Look for conflicting / duplicate packets, but only if
876 * requested to do so.
877 */
878 if (request->async->listen->track_duplicates) {
879 request_t *old;
880
881 old = fr_rb_find(worker->dedup, request);
882 if (!old) {
883 goto insert_new;
884 }
885
886 fr_assert(old->async->listen == request->async->listen);
887 fr_assert(old->async->channel == request->async->channel);
888
889 /*
890 * There's a new packet. Do we keep the old one,
891 * or the new one? This decision is made by
892 * checking the recv_time, which is a
893 * nanosecond-resolution timer. If the time is
894 * identical, then the new packet is the same as
895 * the old one.
896 *
897 * If the new packet is a duplicate of the old
898 * one, then we can just discard the new one. We
899 * have to tell the channel that we've "eaten"
900 * this reply, so the sequence number should
901 * increase.
902 *
903 * @todo - fix the channel code to do queue
904 * depth, and not sequence / ack.
905 */
906 if (fr_time_eq(old->async->recv_time, request->async->recv_time)) {
907 RWARN("Discarding duplicate of request (%"PRIu64")", old->number);
908
909 fr_channel_null_reply(request->async->channel);
910 request_slab_release(request);
911
912 /*
913 * Signal there's a dup, and ignore the
914 * return code. We don't bother replying
915 * here, as an FD event or timer will
916 * wake up the request, and cause it to
917 * continue.
918 *
919 * @todo - the old request is NOT
920 * running, but is yielded. It MAY clean
921 * itself up, or do something...
922 */
924 worker->stats.dup++;
925
926 fr_message_done(&cd->m);
927 return;
928 }
929
930 /*
931 * Stop the old request, and decrement the number
932 * of active requests.
933 */
934 RWARN("Got conflicting packet for request (%" PRIu64 "), telling old request to stop", old->number);
935
937 worker->stats.dropped++;
938 (void) fr_rb_remove(worker->dedup, old); /* remove, but do NOT free it */
939
940 insert_new:
941 (void) fr_rb_insert(worker->dedup, request);
942 }
943
944 if (worker_request_time_tracking_start(worker, request, now) < 0) {
945 if (request->async->listen->track_duplicates) (void) fr_rb_remove(worker->dedup, request);
946 goto fail;
947 }
948
949 /*
950 * We're done with this message.
951 */
952 fr_message_done(&cd->m);
953
954 {
956
957 wl = fr_rb_find(worker->listeners, &(fr_worker_listen_t) { .listener = listen });
958 if (!wl) {
959 MEM(wl = talloc_zero(worker, fr_worker_listen_t));
960 fr_dlist_init(&wl->dlist, request_t, listen_entry);
961 wl->listener = listen;
962
963 (void) fr_rb_insert(worker->listeners, wl);
964 }
965
966 fr_dlist_insert_tail(&wl->dlist, request);
967 }
968}
969
970/**
971 * Track a request_t in the "runnable" heap.
972 * Higher priorities take precedence, followed by lower sequence numbers
973 */
974static int8_t worker_runnable_cmp(void const *one, void const *two)
975{
976 request_t const *a = one, *b = two;
977 int ret;
978
979 /*
980 * Prefer higher priority packets.
981 */
982 ret = CMP_PREFER_LARGER(b->priority, a->priority);
983 if (ret != 0) return ret;
984
985 /*
986 * Prefer packets which are further along in their processing sequence.
987 */
988 ret = CMP_PREFER_LARGER(a->sequence, b->sequence);
989 if (ret != 0) return ret;
990
991 /*
992 * Smaller timestamp (i.e. earlier) is more important.
993 */
994 return fr_time_cmp(a->async->recv_time, b->async->recv_time);
995}
996
997/**
998 * Track a request_t in the "dedup" tree
999 */
1000static int8_t worker_dedup_cmp(void const *one, void const *two)
1001{
1002 int ret;
1003 request_t const *a = one, *b = two;
1004
1005 ret = CMP(a->async->listen, b->async->listen);
1006 if (ret) return ret;
1007
1008 return CMP(a->async->packet_ctx, b->async->packet_ctx);
1009}
1010
1011/** Destroy a worker
1012 *
1013 * The input channels are signaled, and local messages are cleaned up.
1014 *
1015 * This should be called to _EXPLICITLY_ destroy a worker, when some fatal
1016 * error has occurred on the worker side, and we need to destroy it.
1017 *
1018 * We signal all pending requests in the backlog to stop, and tell the
1019 * network side that it should not send us any more requests.
1020 *
1021 * @param[in] worker the worker to destroy.
1022 */
1024{
1025 int i, count, ret;
1026
1027// WORKER_VERIFY;
1028
1029 /*
1030 * Stop any new requests running with this interpreter
1031 */
1033
1034 /*
1035 * Destroy all of the active requests. These are ones
1036 * which are still waiting for timers or file descriptor
1037 * events.
1038 */
1039 count = 0;
1040
1041 /*
1042 * Force the timeout event to fire for all requests that
1043 * are still running.
1044 */
1045 ret = fr_timer_list_force_run(worker->timeout);
1046 if (unlikely(ret < 0)) {
1047 fr_assert_msg(0, "Failed to force run the timeout list");
1048 } else {
1049 count += ret;
1050 }
1051
1053
1054 DEBUG("Worker is exiting - stopped %u requests", count);
1055
1056 /*
1057 * Signal the channels that we're closing.
1058 *
1059 * The other end owns the channel, and will take care of
1060 * popping messages in the TO_RESPONDER queue, and marking
1061 * them FR_MESSAGE_DONE. It will ignore the messages in
1062 * the TO_REQUESTOR queue, as we own those. They will be
1063 * automatically freed when our talloc context is freed.
1064 */
1065 for (i = 0; i < worker->config.max_channels; i++) {
1066 if (!worker->channel[i].ch) continue;
1067
1068 worker_requests_cancel(&worker->channel[i]);
1069
1070 fr_assert_msg(fr_dlist_num_elements(&worker->channel[i].dlist) == 0,
1071 "Pending messages in channel after cancelling request");
1072
1074 }
1075
1076 talloc_free(worker);
1077}
1078
1079/** Internal request (i.e. one generated by the interpreter) is now complete
1080 *
1081 */
1082static void _worker_request_internal_init(request_t *request, void *uctx)
1083{
1084 fr_worker_t *worker = talloc_get_type_abort(uctx, fr_worker_t);
1085 fr_time_t now = fr_time();
1086
1087 worker_request_init(worker, request, now);
1088
1089 /*
1090 * Requests generated by the interpreter
1091 * are always marked up as internal.
1092 */
1094 if (worker_request_time_tracking_start(worker, request, now) < 0) {
1096 }
1097}
1098
1099
1100/** External request is now complete
1101 *
1102 */
1103static void _worker_request_done_external(request_t *request, UNUSED rlm_rcode_t rcode, void *uctx)
1104{
1105 fr_worker_t *worker = talloc_get_type_abort(uctx, fr_worker_t);
1106 fr_time_t now = fr_time();
1107
1108 /*
1109 * All external requests MUST have a listener.
1110 */
1112 fr_assert(request->async->listen != NULL);
1113
1114 /*
1115 * Only real packets are in the dedup tree. And even
1116 * then, only some of the time.
1117 */
1118 if (request->async->listen->track_duplicates && fr_rb_node_inline_in_tree(&request->dedup_node)) {
1119 (void) fr_rb_delete(worker->dedup, request);
1120 }
1121
1122 /*
1123 * If we're running a real request, then the final
1124 * indentation MUST be zero. Otherwise we skipped
1125 * something!
1126 *
1127 * Also check that the request is NOT marked as
1128 * "yielded", but is in fact done.
1129 *
1130 * @todo - check that the stack is at frame 0, otherwise
1131 * more things have gone wrong.
1132 */
1133 fr_assert_msg(request_is_internal(request) || request_is_detached(request) || (request->log.indent.unlang == 0),
1134 "Request %s bad log indentation - expected 0 got %u", request->name, request->log.indent.unlang);
1136 "Request %s is marked as yielded at end of processing", request->name);
1138 "Request %s stack depth %u > 0", request->name, unlang_interpret_stack_depth(request));
1139 RDEBUG("Done request");
1140
1141 /*
1142 * The request is done. Track that.
1143 */
1144 worker_request_time_tracking_end(worker, request, now);
1145
1146 /*
1147 * Remove it from the list of requests associated with this channel.
1148 */
1149 if (fr_dlist_entry_in_list(&request->async->entry)) {
1150 fr_dlist_entry_unlink(&request->async->entry);
1151 }
1152
1153 /*
1154 * These conditions are true when the server is
1155 * exiting and we're stopping all the requests.
1156 *
1157 * This should never happen otherwise.
1158 */
1159 if (unlikely(!fr_channel_active(request->async->channel))) {
1160 fr_dlist_entry_unlink(&request->listen_entry);
1161 request_slab_release(request);
1162 return;
1163 }
1164
1165 worker_send_reply(worker, request, !unlang_request_is_cancelled(request), now);
1166 request_slab_release(request);
1167}
1168
1169/** Internal request (i.e. one generated by the interpreter) is now complete
1170 *
1171 * Whatever generated the request is now responsible for freeing it.
1172 */
1173static void _worker_request_done_internal(request_t *request, UNUSED rlm_rcode_t rcode, void *uctx)
1174{
1175 fr_worker_t *worker = talloc_get_type_abort(uctx, fr_worker_t);
1176
1177 worker_request_time_tracking_end(worker, request, fr_time());
1178
1179 fr_assert(!fr_heap_entry_inserted(request->runnable));
1180 fr_assert(!fr_timer_armed(request->timeout));
1181 fr_assert(!fr_dlist_entry_in_list(&request->async->entry));
1182}
1183
1184/** Detached request (i.e. one generated by the interpreter with no parent) is now complete
1185 *
1186 * As the request has no parent, then there's nothing to free it
1187 * so we have to.
1188 */
1189static void _worker_request_done_detached(request_t *request, UNUSED rlm_rcode_t rcode, UNUSED void *uctx)
1190{
1191 /*
1192 * No time tracking for detached requests
1193 * so we don't need to call
1194 * worker_request_time_tracking_end.
1195 */
1196 fr_assert(!fr_heap_entry_inserted(request->runnable));
1197
1198 /*
1199 * Normally worker_request_time_tracking_end
1200 * would remove the request from the time
1201 * order heap, but we need to do that for
1202 * detached requests.
1203 */
1204 TALLOC_FREE(request->timeout);
1205
1206 fr_assert(!fr_dlist_entry_in_list(&request->async->entry));
1207
1208 /*
1209 * Detached requests have to be freed by us
1210 * as nothing else can free them.
1211 *
1212 * All other requests must be freed by the
1213 * code which allocated them.
1214 */
1215 talloc_free(request);
1216}
1217
1218
1219/** Make us responsible for running the request
1220 *
1221 */
1222static void _worker_request_detach(request_t *request, void *uctx)
1223{
1224 fr_worker_t *worker = talloc_get_type_abort(uctx, fr_worker_t);
1225 fr_time_t now = fr_time();
1226
1227 RDEBUG4("%s - Request detaching", __FUNCTION__);
1228
1229 if (request_is_detachable(request)) {
1230 /*
1231 * End the time tracking... We don't track detached requests,
1232 * because they don't contribute for the time consumed by an
1233 * external request.
1234 */
1235 if (request->async->tracking.state == FR_TIME_TRACKING_YIELDED) {
1236 RDEBUG3("Forcing time tracking to running state, from yielded, for request detach");
1237 fr_time_tracking_resume(&request->async->tracking, now);
1238 }
1239 worker_request_time_tracking_end(worker, request, now);
1240
1241 if (request_detach(request) < 0) RPEDEBUG("Failed detaching request");
1242
1243 RDEBUG3("Request is detached");
1244 } else {
1245 fr_assert_msg(0, "Request is not detachable");
1246 }
1247
1248 return;
1249}
1250
1251/** Request is now runnable
1252 *
1253 */
1254static void _worker_request_runnable(request_t *request, void *uctx)
1255{
1256 fr_worker_t *worker = uctx;
1257
1258 RDEBUG4("%s - Request marked as runnable", __FUNCTION__);
1259 fr_heap_insert(&worker->runnable, request);
1260}
1261
1262/** Interpreter yielded request
1263 *
1264 */
1265static void _worker_request_yield(request_t *request, UNUSED void *uctx)
1266{
1267 RDEBUG4("%s - Request yielded", __FUNCTION__);
1268 if (likely(!request_is_detached(request))) fr_time_tracking_yield(&request->async->tracking, fr_time());
1269}
1270
1271/** Interpreter is starting to work on request again
1272 *
1273 */
1274static void _worker_request_resume(request_t *request, UNUSED void *uctx)
1275{
1276 RDEBUG4("%s - Request resuming", __FUNCTION__);
1277 if (likely(!request_is_detached(request))) fr_time_tracking_resume(&request->async->tracking, fr_time());
1278}
1279
1280/** Check if a request is scheduled
1281 *
1282 */
1283static bool _worker_request_scheduled(request_t const *request, UNUSED void *uctx)
1284{
1285 return fr_heap_entry_inserted(request->runnable);
1286}
1287
1288/** Update a request's priority
1289 *
1290 */
1291static void _worker_request_prioritise(request_t *request, void *uctx)
1292{
1293 fr_worker_t *worker = talloc_get_type_abort(uctx, fr_worker_t);
1294
1295 RDEBUG4("%s - Request priority changed", __FUNCTION__);
1296
1297 /* Extract the request from the runnable queue _if_ it's in the runnable queue */
1298 if (fr_heap_extract(&worker->runnable, request) < 0) return;
1299
1300 /* Reinsert it to re-evaluate its new priority */
1301 fr_heap_insert(&worker->runnable, request);
1302}
1303
1304/** Run a request
1305 *
1306 * Until it either yields, or is done.
1307 *
1308 * This function is also responsible for sending replies, and
1309 * cleaning up the request.
1310 *
1311 * @param[in] worker the worker
1312 * @param[in] start the current time
1313 */
1314static inline CC_HINT(always_inline) void worker_run_request(fr_worker_t *worker, fr_time_t start)
1315{
1316 request_t *request;
1317 fr_time_t now;
1318
1320
1321 now = start;
1322
1323 /*
1324 * Busy-loop running requests for 1ms. We still poll the
1325 * event loop 1000 times a second, OR when there's no
1326 * more work to do. This allows us to make progress with
1327 * ongoing requests, at the expense of sometimes ignoring
1328 * new ones.
1329 */
1330 while (fr_time_delta_lt(fr_time_sub(now, start), fr_time_delta_from_msec(1)) &&
1331 ((request = fr_heap_pop(&worker->runnable)) != NULL)) {
1332
1333 REQUEST_VERIFY(request);
1334 fr_assert(!fr_heap_entry_inserted(request->runnable));
1335
1336 /*
1337 * For real requests, if the channel is gone,
1338 * just stop the request and free it.
1339 */
1340 if (request->async->channel && !fr_channel_active(request->async->channel)) {
1341 worker_stop_request(request);
1342 continue;
1343 }
1344
1346
1347 now = fr_time();
1348 }
1349}
1350
1351/** Create a worker
1352 *
1353 * @param[in] ctx the talloc context
1354 * @param[in] name the name of this worker
1355 * @param[in] el the event list
1356 * @param[in] logger the destination for all logging messages
1357 * @param[in] lvl log level
1358 * @param[in] config various configuration parameters
1359 * @return
1360 * - NULL on error
1361 * - fr_worker_t on success
1362 */
1363fr_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,
1365{
1366 fr_worker_t *worker;
1367
1368 worker = talloc_zero(ctx, fr_worker_t);
1369 if (!worker) {
1370nomem:
1371 fr_strerror_const("Failed allocating memory");
1372 return NULL;
1373 }
1374
1375 worker->name = talloc_strdup(worker, name); /* thread locality */
1376
1377 if (config) worker->config = *config;
1378
1379#define CHECK_CONFIG(_x, _min, _max) do { \
1380 if (!worker->config._x) worker->config._x = _min; \
1381 if (worker->config._x < _min) worker->config._x = _min; \
1382 if (worker->config._x > _max) worker->config._x = _max; \
1383 } while (0)
1384
1385#define CHECK_CONFIG_TIME_DELTA(_x, _min, _max) do { \
1386 if (fr_time_delta_lt(worker->config._x, _min)) worker->config._x = _min; \
1387 if (fr_time_delta_gt(worker->config._x, _max)) worker->config._x = _max; \
1388 } while (0)
1389
1390 CHECK_CONFIG(max_requests,1024,(1 << 30));
1391 CHECK_CONFIG(max_channels, 64, 1024);
1392 CHECK_CONFIG(reuse.child_pool_size, 4096, 65536);
1393 CHECK_CONFIG(message_set_size, 1024, 8192);
1394 CHECK_CONFIG(ring_buffer_size, (1 << 17), (1 << 20));
1396
1397 worker->channel = talloc_zero_array(worker, fr_worker_channel_t, worker->config.max_channels);
1398 if (!worker->channel) {
1399 talloc_free(worker);
1400 goto nomem;
1401 }
1402
1403 worker->thread_id = pthread_self();
1404 worker->el = el;
1405 worker->log = logger;
1406 worker->lvl = lvl;
1407
1408 /*
1409 * The worker thread starts now. Manually initialize it,
1410 * because we're tracking request time, not the time that
1411 * the worker thread is running.
1412 */
1413 memset(&worker->tracking, 0, sizeof(worker->tracking));
1414
1415 worker->aq_control = fr_atomic_queue_talloc(worker, 1024);
1416 if (!worker->aq_control) {
1417 fr_strerror_const("Failed creating atomic queue");
1418 fail:
1419 talloc_free(worker);
1420 return NULL;
1421 }
1422
1423 worker->control = fr_control_create(worker, el, worker->aq_control, 7);
1424 if (!worker->control) {
1425 fr_strerror_const_push("Failed creating control plane");
1426 goto fail;
1427 }
1428
1430 fr_strerror_const_push("Failed adding control channel");
1431 goto fail;
1432 }
1433
1435 fr_strerror_const_push("Failed adding callback for listeners");
1436 goto fail;
1437 }
1438
1439 if (fr_control_open(worker->control) < 0) {
1440 fr_strerror_const_push("Failed opening control plane");
1441 goto fail;
1442 }
1443
1444 worker->runnable = fr_heap_talloc_alloc(worker, worker_runnable_cmp, request_t, runnable, 0);
1445 if (!worker->runnable) {
1446 fr_strerror_const("Failed creating runnable heap");
1447 goto fail;
1448 }
1449
1450 worker->timeout = fr_timer_list_ordered_alloc(worker, el->tl);
1451 if (!worker->timeout) {
1452 fr_strerror_const("Failed creating timeouts list");
1453 goto fail;
1454 }
1455
1456 worker->dedup = fr_rb_inline_talloc_alloc(worker, request_t, dedup_node, worker_dedup_cmp, NULL);
1457 if (!worker->dedup) {
1458 fr_strerror_const("Failed creating de_dup tree");
1459 goto fail;
1460 }
1461
1463 if (!worker->listeners) {
1464 fr_strerror_const("Failed creating listener tree");
1465 goto fail;
1466 }
1467
1468 worker->intp = unlang_interpret_init(worker, el,
1470 .init_internal = _worker_request_internal_init,
1471
1472 .done_external = _worker_request_done_external,
1473 .done_internal = _worker_request_done_internal,
1474 .done_detached = _worker_request_done_detached,
1475
1476 .detach = _worker_request_detach,
1477 .yield = _worker_request_yield,
1478 .resume = _worker_request_resume,
1479 .mark_runnable = _worker_request_runnable,
1480
1481 .scheduled = _worker_request_scheduled,
1482 .prioritise = _worker_request_prioritise
1483 },
1484 worker);
1485 if (!worker->intp){
1486 fr_strerror_const("Failed initialising interpreter");
1487 goto fail;
1488 }
1489
1490 {
1493
1494 if (!(worker->slab = request_slab_list_alloc(worker, el, &worker->config.reuse, NULL, NULL,
1495 UNCONST(void *, worker), true, false))) {
1496 fr_strerror_const("Failed creating request slab list");
1497 goto fail;
1498 }
1499 }
1500
1502
1503 return worker;
1504}
1505
1506
1507/** The main loop and entry point of the stand-alone worker thread.
1508 *
1509 * Where there is only one thread, the event loop runs fr_worker_pre_event() and fr_worker_post_event()
1510 * instead, And then fr_worker_post_event() takes care of calling worker_run_request() to actually run the
1511 * request.
1512 *
1513 * @param[in] worker the worker data structure to manage
1514 */
1516{
1518
1519 while (true) {
1520 bool wait_for_event;
1521 int num_events;
1522
1524
1525 /*
1526 * There are runnable requests. We still service
1527 * the event loop, but we don't wait for events.
1528 */
1529 wait_for_event = (fr_heap_num_elements(worker->runnable) == 0);
1530 if (wait_for_event) {
1531 if (worker->exiting && (worker_num_requests(worker) == 0)) break;
1532
1533 DEBUG4("Ready to process requests");
1534 }
1535
1536 /*
1537 * Check the event list. If there's an error
1538 * (e.g. exit), we stop looping and clean up.
1539 */
1540 DEBUG4("Gathering events - %s", wait_for_event ? "will wait" : "Will not wait");
1541 num_events = fr_event_corral(worker->el, fr_time(), wait_for_event);
1542 if (num_events < 0) {
1543 if (fr_event_loop_exiting(worker->el)) {
1544 DEBUG4("Event loop exiting");
1545 break;
1546 }
1547
1548 PERROR("Failed retrieving events");
1549 break;
1550 }
1551
1552 DEBUG4("%u event(s) pending", num_events);
1553
1554 /*
1555 * Service outstanding events.
1556 */
1557 if (num_events > 0) {
1558 DEBUG4("Servicing event(s)");
1559 fr_event_service(worker->el);
1560 }
1561
1562 /*
1563 * Run any outstanding requests.
1564 */
1565 worker_run_request(worker, fr_time());
1566 }
1567}
1568
1569/** Pre-event handler
1570 *
1571 * This should be run ONLY in single-threaded mode!
1572 */
1574{
1575 fr_worker_t *worker = talloc_get_type_abort(uctx, fr_worker_t);
1576 request_t *request;
1577
1578 request = fr_heap_peek(worker->runnable);
1579 if (!request) return 0;
1580
1581 /*
1582 * There's work to do. Tell the event handler to poll
1583 * for IO / timers, but also immediately return to the
1584 * calling function, which has more work to do.
1585 */
1586 return 1;
1587}
1588
1589
1590/** Post-event handler
1591 *
1592 * This should be run ONLY in single-threaded mode!
1593 */
1595{
1596 fr_worker_t *worker = talloc_get_type_abort(uctx, fr_worker_t);
1597
1598 worker_run_request(worker, fr_time()); /* Event loop time can be too old, and trigger asserts */
1599}
1600
1601/** Print debug information about the worker structure
1602 *
1603 * @param[in] worker the worker
1604 * @param[in] fp the file where the debug output is printed.
1605 */
1606void fr_worker_debug(fr_worker_t *worker, FILE *fp)
1607{
1609
1610 fprintf(fp, "\tnum_channels = %d\n", worker->num_channels);
1611 fprintf(fp, "\tstats.in = %" PRIu64 "\n", worker->stats.in);
1612
1613 fprintf(fp, "\tcalculated (predicted) total CPU time = %" PRIu64 "\n",
1614 fr_time_delta_unwrap(worker->predicted) * worker->stats.in);
1615 if (worker->stats.in) {
1616 fprintf(fp, "\tcalculated (counted) per request time = %" PRIu64 "\n",
1618 }
1619
1620 fr_time_tracking_debug(&worker->tracking, fp);
1621
1622}
1623
1624/** Create a channel to the worker
1625 *
1626 * Called by the master (i.e. network) thread when it needs to create
1627 * a new channel to a particuler worker.
1628 *
1629 * @param[in] worker the worker
1630 * @param[in] master the control plane of the master
1631 * @param[in] ctx the context in which the channel will be created
1632 */
1634{
1635 fr_channel_t *ch;
1636 pthread_t id;
1637 bool same;
1638
1640
1641 id = pthread_self();
1642 same = (pthread_equal(id, worker->thread_id) != 0);
1643
1644 ch = fr_channel_create(ctx, master, worker->control, same);
1645 if (!ch) return NULL;
1646
1648
1649 /*
1650 * Tell the worker about the channel
1651 */
1652 if (fr_channel_signal_open(ch) < 0) {
1653 talloc_free(ch);
1654 return NULL;
1655 }
1656
1657 return ch;
1658}
1659
1661{
1662 fr_ring_buffer_t *rb;
1663
1664 /*
1665 * Skip a bunch of work if we're already in the worker thread.
1666 */
1667 if (is_worker_thread(worker)) {
1668 return fr_worker_listen_cancel_self(worker, li);
1669 }
1670
1671 rb = fr_worker_rb_init();
1672 if (!rb) return -1;
1673
1674 return fr_control_message_send(worker->control, rb, FR_CONTROL_ID_LISTEN_DEAD, &li, sizeof(li));
1675}
1676
1677#ifdef WITH_VERIFY_PTR
1678/** Verify the worker data structures.
1679 *
1680 * @param[in] worker the worker
1681 */
1682static void worker_verify(fr_worker_t *worker)
1683{
1684 int i;
1685
1686 (void) talloc_get_type_abort(worker, fr_worker_t);
1687 fr_atomic_queue_verify(worker->aq_control);
1688
1689 fr_assert(worker->control != NULL);
1690 (void) talloc_get_type_abort(worker->control, fr_control_t);
1691
1692 fr_assert(worker->el != NULL);
1693 (void) talloc_get_type_abort(worker->el, fr_event_list_t);
1694
1695 fr_assert(worker->runnable != NULL);
1696 (void) talloc_get_type_abort(worker->runnable, fr_heap_t);
1697
1698 fr_assert(worker->dedup != NULL);
1699 (void) talloc_get_type_abort(worker->dedup, fr_rb_tree_t);
1700
1701 for (i = 0; i < worker->config.max_channels; i++) {
1702 if (!worker->channel[i].ch) continue;
1703
1704 (void) talloc_get_type_abort(worker->channel[i].ch, fr_channel_t);
1705 }
1706}
1707#endif
1708
1709int fr_worker_stats(fr_worker_t const *worker, int num, uint64_t *stats)
1710{
1711 if (num < 0) return -1;
1712 if (num == 0) return 0;
1713
1714 stats[0] = worker->stats.in;
1715 if (num >= 2) stats[1] = worker->stats.out;
1716 if (num >= 3) stats[2] = worker->stats.dup;
1717 if (num >= 4) stats[3] = worker->stats.dropped;
1718 if (num >= 5) stats[4] = worker->num_naks;
1719 if (num >= 6) stats[5] = worker->num_active;
1720
1721 if (num <= 6) return num;
1722
1723 return 6;
1724}
1725
1726static int cmd_stats_worker(FILE *fp, UNUSED FILE *fp_err, void *ctx, fr_cmd_info_t const *info)
1727{
1728 fr_worker_t const *worker = ctx;
1729 fr_time_delta_t when;
1730
1731 if ((info->argc == 0) || (strcmp(info->argv[0], "count") == 0)) {
1732 fprintf(fp, "count.in\t\t\t%" PRIu64 "\n", worker->stats.in);
1733 fprintf(fp, "count.out\t\t\t%" PRIu64 "\n", worker->stats.out);
1734 fprintf(fp, "count.dup\t\t\t%" PRIu64 "\n", worker->stats.dup);
1735 fprintf(fp, "count.dropped\t\t\t%" PRIu64 "\n", worker->stats.dropped);
1736 fprintf(fp, "count.naks\t\t\t%" PRIu64 "\n", worker->num_naks);
1737 fprintf(fp, "count.active\t\t\t%" PRIu64 "\n", worker->num_active);
1738 fprintf(fp, "count.runnable\t\t\t%u\n", fr_heap_num_elements(worker->runnable));
1739 }
1740
1741 if ((info->argc == 0) || (strcmp(info->argv[0], "cpu") == 0)) {
1742 when = worker->predicted;
1743 fprintf(fp, "cpu.request_time_rtt\t\t%.9f\n", fr_time_delta_unwrap(when) / (double)NSEC);
1744
1745 when = worker->tracking.running_total;
1746 if (fr_time_delta_ispos(when) && (worker->stats.in > worker->stats.dropped)) {
1747 when = fr_time_delta_div(when, fr_time_delta_wrap(worker->stats.in - worker->stats.dropped));
1748 }
1749 fprintf(fp, "cpu.average_request_time\t%.9f\n", fr_time_delta_unwrap(when) / (double)NSEC);
1750
1751 when = worker->tracking.running_total;
1752 fprintf(fp, "cpu.used\t\t\t%.6f\n", fr_time_delta_unwrap(when) / (double)NSEC);
1753
1754 when = worker->tracking.waiting_total;
1755 fprintf(fp, "cpu.waiting\t\t\t%.3f\n", fr_time_delta_unwrap(when) / (double)NSEC);
1756
1757 fr_time_elapsed_fprint(fp, &worker->cpu_time, "cpu.requests", 4);
1758 fr_time_elapsed_fprint(fp, &worker->wall_clock, "time.requests", 4);
1759 }
1760
1761 return 0;
1762}
1763
1765 {
1766 .parent = "stats",
1767 .name = "worker",
1768 .help = "Statistics for workers threads.",
1769 .read_only = true
1770 },
1771
1772 {
1773 .parent = "stats worker",
1774 .add_name = true,
1775 .name = "self",
1776 .syntax = "[(count|cpu)]",
1777 .func = cmd_stats_worker,
1778 .help = "Show statistics for a specific worker thread.",
1779 .read_only = true
1780 },
1781
1783};
static int const char char buffer[256]
Definition acutest.h:576
fr_io_encode_t encode
Pack fr_pair_ts back into a byte array.
Definition app_io.h:55
size_t default_reply_size
same for replies
Definition app_io.h:40
size_t default_message_size
Usually maximum message size.
Definition app_io.h:39
fr_io_nak_t nak
Function to send a NAK.
Definition app_io.h:62
fr_io_decode_t decode
Translate raw bytes into fr_pair_ts and metadata.
Definition app_io.h:54
fr_io_decode_t decode
Translate raw bytes into fr_pair_ts and metadata.
Definition application.h:80
fr_io_encode_t encode
Pack fr_pair_ts back into a byte array.
Definition application.h:85
#define _Thread_local
Definition atexit.h:213
#define fr_atexit_thread_local(_name, _free, _uctx)
Definition atexit.h:224
fr_atomic_queue_t * fr_atomic_queue_talloc(TALLOC_CTX *ctx, size_t size)
Create fixed-size atomic queue.
Structure to hold the atomic queue.
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:186
#define RCSID(id)
Definition build.h:560
#define NDEBUG_UNUSED
Definition build.h:395
#define CMP_PREFER_LARGER(_a, _b)
Evaluates to -1 for a > b, and +1 for a < b.
Definition build.h:109
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
Definition build.h:113
#define unlikely(_x)
Definition build.h:455
#define UNUSED
Definition build.h:384
unlang_action_t unlang_call_push(unlang_result_t *p_result, request_t *request, CONF_SECTION *server_cs, bool top_frame)
Push a virtual server CONF_SECTION as a call frame onto the stack.
Definition call.c:151
fr_table_num_sorted_t const channel_signals[]
Definition channel.c:151
unsigned int fr_channel_responder_discard(fr_channel_t *ch)
Discard any requests the requestor queued but we never received.
Definition channel.c:874
fr_channel_t * fr_channel_create(TALLOC_CTX *ctx, fr_control_t *requestor, fr_control_t *responder, bool same)
Create a new channel.
Definition channel.c:181
fr_channel_event_t fr_channel_service_message(fr_time_t when, fr_channel_t **p_channel, void const *data, size_t data_size)
Service a control-plane message.
Definition channel.c:687
int fr_channel_set_recv_request(fr_channel_t *ch, void *uctx, fr_channel_recv_callback_t recv_request)
Definition channel.c:977
void * fr_channel_responder_uctx_get(fr_channel_t *ch)
Get responder-specific data from a channel.
Definition channel.c:936
bool fr_channel_recv_request(fr_channel_t *ch)
Receive a request message from the channel.
Definition channel.c:470
int fr_channel_null_reply(fr_channel_t *ch)
Don't send a reply message into the channel.
Definition channel.c:626
void fr_channel_responder_uctx_add(fr_channel_t *ch, void *uctx)
Add responder-specific data to a channel.
Definition channel.c:924
int fr_channel_send_reply(fr_channel_t *ch, fr_channel_data_t *cd)
Send a reply message into the channel.
Definition channel.c:509
bool fr_channel_active(fr_channel_t *ch)
Check if a channel is active.
Definition channel.c:829
int fr_channel_responder_ack_close(fr_channel_t *ch)
Acknowledge that the channel is closing.
Definition channel.c:895
int fr_channel_signal_open(fr_channel_t *ch)
Send a channel to a responder.
Definition channel.c:991
A full channel, which consists of two ends.
Definition channel.c:142
fr_message_t m
the message header
Definition channel.h:107
fr_channel_event_t
Definition channel.h:69
@ FR_CHANNEL_NOOP
Definition channel.h:76
@ FR_CHANNEL_EMPTY
Definition channel.h:77
@ FR_CHANNEL_CLOSE
Definition channel.h:74
@ FR_CHANNEL_ERROR
Definition channel.h:70
@ FR_CHANNEL_DATA_READY_REQUESTOR
Definition channel.h:72
@ FR_CHANNEL_OPEN
Definition channel.h:73
@ FR_CHANNEL_DATA_READY_RESPONDER
Definition channel.h:71
void * packet_ctx
Packet specific context for holding client information, and other proto_* specific information that n...
Definition channel.h:144
fr_listen_t * listen
for tracking packet transport, etc.
Definition channel.h:148
#define FR_CONTROL_ID_CHANNEL
Definition channel.h:67
uint32_t priority
Priority of this packet.
Definition channel.h:142
Channel information which is added to a message.
Definition channel.h:106
int argc
current argument count
Definition command.h:39
char const * parent
e.g. "show module"
Definition command.h:52
#define CMD_TABLE_END
Definition command.h:62
char const ** argv
text version of commands
Definition command.h:42
#define FR_CONTROL_MAX_SIZE
Definition control.h:51
#define FR_CONTROL_MAX_MESSAGES
Definition control.h:50
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:131
#define fr_assert_msg(_x, _msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:202
#define fr_cond_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:148
#define MEM(x)
Definition debug.h:36
#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
static bool fr_dlist_entry_in_list(fr_dlist_t const *entry)
Check if a list entry is part of a list.
Definition dlist.h:145
static void fr_dlist_entry_unlink(fr_dlist_t *entry)
Remove an item from the dlist when we don't have access to the head.
Definition dlist.h:128
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_tail(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the tail of a list.
Definition dlist.h:360
static void fr_dlist_entry_init(fr_dlist_t *entry)
Initialise a linked list without metadata.
Definition dlist.h:120
Head of a doubly linked list.
Definition dlist.h:51
int fr_heap_insert(fr_heap_t **hp, void *data)
Insert a new element into the heap.
Definition heap.c:146
void * fr_heap_pop(fr_heap_t **hp)
Remove a node from the heap.
Definition heap.c:325
int fr_heap_extract(fr_heap_t **hp, void *data)
Remove a node from the heap.
Definition heap.c:239
static void * fr_heap_peek(fr_heap_t *h)
Return the item from the top of the heap but don't pop it.
Definition heap.h:136
static bool fr_heap_entry_inserted(fr_heap_index_t heap_idx)
Check if an entry is inserted into a heap.
Definition heap.h:124
static unsigned int fr_heap_num_elements(fr_heap_t *h)
Return the number of elements in the heap.
Definition heap.h:179
#define fr_heap_talloc_alloc(_ctx, _cmp, _talloc_type, _field, _init)
Creates a heap that verifies elements are of a specific talloc type.
Definition heap.h:115
The main heap structure.
Definition heap.h:66
talloc_free(hp)
rlm_rcode_t unlang_interpret(request_t *request, bool running)
Run the interpreter for a current request.
Definition interpret.c:1300
void unlang_interpret_set(request_t *request, unlang_interpret_t *intp)
Set a specific interpreter for a request.
Definition interpret.c:2431
int unlang_interpret_stack_depth(request_t *request)
Return the depth of the request's stack.
Definition interpret.c:1919
void unlang_interpret_set_thread_default(unlang_interpret_t *intp)
Set the default interpreter for this thread.
Definition interpret.c:2462
unlang_interpret_t * unlang_interpret_init(TALLOC_CTX *ctx, fr_event_list_t *el, unlang_request_func_t *funcs, void *uctx)
Initialize a unlang compiler / interpret.
Definition interpret.c:2390
bool unlang_request_is_cancelled(request_t const *request)
Return whether a request has been cancelled.
Definition interpret.c:1969
void unlang_interpret_signal(request_t *request, fr_signal_t action)
Send a signal (usually stop) to a request.
Definition interpret.c:1787
bool unlang_interpret_is_resumable(request_t *request)
Check if a request as resumable.
Definition interpret.c:1988
#define UNLANG_REQUEST_RESUME
Definition interpret.h:48
#define UNLANG_TOP_FRAME
Definition interpret.h:36
External functions provided by the owner of the interpret.
Definition interpret.h:116
uint64_t out
Definition base.h:43
uint64_t dup
Definition base.h:44
uint64_t dropped
Definition base.h:45
uint64_t in
Definition base.h:42
fr_control_t * fr_control_create(TALLOC_CTX *ctx, fr_event_list_t *el, fr_atomic_queue_t *aq, size_t num_callbacks)
Create a control-plane signaling path.
Definition control.c:152
int fr_control_callback_add(fr_control_t **c, uint32_t id, void *ctx, fr_control_callback_t callback)
Register a callback for an ID.
Definition control.c:444
int fr_control_open(fr_control_t *c)
Open the control-plane signalling path.
Definition control.c:176
int fr_control_message_send(fr_control_t *c, fr_ring_buffer_t *rb, uint32_t id, void *data, size_t data_size)
Send a control-plane message.
Definition control.c:355
The control structure.
Definition control.c:76
#define PERROR(_fmt,...)
Definition log.h:233
#define DEBUG3(_fmt,...)
Definition log.h:271
#define RDEBUG3(fmt,...)
Definition log.h:360
#define RWARN(fmt,...)
Definition log.h:314
#define PWARN(_fmt,...)
Definition log.h:232
#define RERROR(fmt,...)
Definition log.h:315
#define DEBUG4(_fmt,...)
Definition log.h:272
#define RPERROR(fmt,...)
Definition log.h:319
#define RPEDEBUG(fmt,...)
Definition log.h:393
#define RDEBUG4(fmt,...)
Definition log.h:361
#define RATE_LIMIT_GLOBAL(_log, _fmt,...)
Rate limit messages using a global limiting entry.
Definition log.h:658
void fr_event_service(fr_event_list_t *el)
Service any outstanding timer or file descriptor events.
Definition event.c:2204
int fr_event_corral(fr_event_list_t *el, fr_time_t now, bool wait)
Gather outstanding timer and file descriptor events.
Definition event.c:2072
int fr_event_post_delete(fr_event_list_t *el, fr_event_post_cb_t callback, void *uctx)
Delete a post-event callback from the event list.
Definition event.c:2050
#define fr_time()
Definition event.c:60
bool fr_event_loop_exiting(fr_event_list_t *el)
Check to see whether the event loop is in the process of exiting.
Definition event.c:2393
Stores all information relating to an event list.
Definition event.c:377
fr_log_lvl_t
Definition log.h:64
fr_packet_t * fr_packet_alloc(TALLOC_CTX *ctx, bool new_vector)
Allocate a new fr_packet_t.
Definition packet.c:38
void const * app_instance
Definition listen.h:39
fr_app_t const * app
Definition listen.h:38
void const * app_io_instance
I/O path configuration context.
Definition listen.h:33
CONF_SECTION * server_cs
CONF_SECTION of the server.
Definition listen.h:42
fr_dict_t const * dict
dictionary for this listener
Definition listen.h:30
fr_app_io_t const * app_io
I/O path functions.
Definition listen.h:32
Minimal data structure to use the new code.
Definition listen.h:63
unsigned int uint32_t
long int ssize_t
fr_message_set_t * fr_message_set_create(TALLOC_CTX *ctx, int num_messages, size_t message_size, size_t ring_buffer_size, bool unlimited_size)
Create a message set.
Definition message.c:127
int fr_message_done(fr_message_t *m)
Mark a message as done.
Definition message.c:195
fr_message_t * fr_message_and_data_commit(fr_message_set_t *ms, fr_message_t *m, size_t total_size)
Commit a previously reserved message, allocating exactly total_size bytes of packet data.
Definition message.c:1051
void fr_message_set_gc(fr_message_set_t *ms)
Garbage collect the message set.
Definition message.c:1321
fr_message_t * fr_message_and_data_reserve(fr_message_set_t *ms, size_t reserve_size)
Reserve a message.
Definition message.c:973
A Message set, composed of message headers and ring buffer data.
Definition message.c:94
size_t rb_size
cache-aligned size in the ring buffer
Definition message.h:51
fr_time_t when
when this message was sent
Definition message.h:47
uint8_t * data
pointer to the data in the ring buffer
Definition message.h:49
size_t data_size
size of the data in the ring buffer
Definition message.h:50
static const conf_parser_t config[]
Definition base.c:162
#define fr_assert(_expr)
Definition rad_assert.h:37
#define REDEBUG(fmt,...)
#define RDEBUG(fmt,...)
#define DEBUG2(fmt,...)
static void send_reply(int sockfd, fr_channel_data_t *reply)
void * fr_rb_remove(fr_rb_tree_t *tree, void const *data)
Remove an entry from the tree, without freeing the data.
Definition rb.c:695
void * fr_rb_find(fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
Definition rb.c:577
bool fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
Definition rb.c:626
bool fr_rb_delete(fr_rb_tree_t *tree, void const *data)
Remove node and free data (if a free function was specified)
Definition rb.c:741
#define fr_rb_inline_talloc_alloc(_ctx, _type, _field, _data_cmp, _data_free)
Allocs a red black that verifies elements are of a specific talloc type.
Definition rb.h:244
static bool fr_rb_node_inline_in_tree(fr_rb_node_t const *node)
Check to see if an item is in a tree by examining its inline fr_rb_node_t.
Definition rb.h:312
The main red black tree structure.
Definition rb.h:71
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:44
@ RLM_MODULE_TIMEOUT
Module (or section) timed out.
Definition rcode.h:56
int request_slab_deinit(request_t *request)
Callback for slabs to deinitialise the request.
Definition request.c:385
int request_detach(request_t *child)
Unlink a subrequest from its parent.
Definition request.c:544
#define REQUEST_VERIFY(_x)
Definition request.h:310
#define request_is_detached(_x)
Definition request.h:187
#define request_is_external(_x)
Definition request.h:185
#define request_is_internal(_x)
Definition request.h:186
@ REQUEST_TYPE_EXTERNAL
A request received on the wire.
Definition request.h:179
#define request_is_detachable(_x)
Definition request.h:188
#define REQUEST_POOL_NUM_OBJECTS
Definition request.h:68
#define request_init(_ctx, _type, _args)
Definition request.h:322
#define REQUEST_POOL_SIZE
Definition request.h:81
Optional arguments for initialising requests.
Definition request.h:288
fr_ring_buffer_t * fr_ring_buffer_create(TALLOC_CTX *ctx, size_t size)
Create a ring buffer.
Definition ring_buffer.c:64
static char const * name
@ FR_SIGNAL_DUP
A duplicate request was received.
Definition signal.h:44
@ FR_SIGNAL_CANCEL
Request has been cancelled.
Definition signal.h:40
#define FR_SLAB_FUNCS(_name, _type)
Define type specific wrapper functions for slabs and slab elements.
Definition slab.h:124
#define FR_SLAB_TYPES(_name, _type)
Define type specific wrapper structs for slabs and slab elements.
Definition slab.h:75
unsigned int num_children
How many child allocations are expected off each element.
Definition slab.h:48
size_t child_pool_size
Size of pool space to be allocated to each element.
Definition slab.h:49
return count
Definition module.c:155
@ memory_order_seq_cst
Definition stdatomic.h:132
#define atomic_fetch_add_explicit(object, operand, order)
Definition stdatomic.h:302
#define _Atomic(T)
Definition stdatomic.h:77
Definition log.h:93
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:804
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:288
#define talloc_strdup(_ctx, _str)
Definition talloc.h:149
void fr_time_elapsed_update(fr_time_elapsed_t *elapsed, fr_time_t start, fr_time_t end)
Definition time.c:563
void fr_time_elapsed_fprint(FILE *fp, fr_time_elapsed_t const *elapsed, char const *prefix, int tab_offset)
Definition time.c:608
static fr_time_delta_t fr_time_delta_from_msec(int64_t msec)
Definition time.h:575
static int64_t fr_time_delta_unwrap(fr_time_delta_t time)
Definition time.h:154
#define fr_time_delta_lt(_a, _b)
Definition time.h:285
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:590
#define fr_time_delta_wrap(_time)
Definition time.h:152
#define fr_time_delta_ispos(_a)
Definition time.h:290
#define fr_time_eq(_a, _b)
Definition time.h:241
#define NSEC
Definition time.h:379
#define fr_time_add(_a, _b)
Add a time/time delta together.
Definition time.h:196
#define fr_time_sub(_a, _b)
Subtract one time from another.
Definition time.h:229
static fr_time_delta_t fr_time_delta_div(fr_time_delta_t a, fr_time_delta_t b)
Definition time.h:267
static int8_t fr_time_cmp(fr_time_t a, fr_time_t b)
Compare two fr_time_t values.
Definition time.h:916
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
@ FR_TIME_TRACKING_YIELDED
We're currently tracking time in the yielded state.
static void fr_time_tracking_yield(fr_time_tracking_t *tt, fr_time_t now)
Transition to the yielded state, recording the time we just spent running.
static void fr_time_tracking_end(fr_time_delta_t *predicted, fr_time_tracking_t *tt, fr_time_t now)
End time tracking for this entity.
fr_time_delta_t waiting_total
total time spent waiting
fr_time_delta_t running_total
total time spent running
static void fr_time_tracking_start(fr_time_tracking_t *parent, fr_time_tracking_t *tt, fr_time_t now)
Start time tracking for a tracked entity.
static void fr_time_tracking_resume(fr_time_tracking_t *tt, fr_time_t now)
Track that a request resumed.
static void fr_time_tracking_debug(fr_time_tracking_t *tt, FILE *fp)
Print debug information about the time tracking structure.
uint64_t fr_timer_list_num_events(fr_timer_list_t *tl)
Return number of pending events.
Definition timer.c:1148
fr_timer_list_t * fr_timer_list_ordered_alloc(TALLOC_CTX *ctx, fr_timer_list_t *parent)
Allocate a new sorted event timer list.
Definition timer.c:1290
int fr_timer_list_force_run(fr_timer_list_t *tl)
Forcibly run all events in an event loop.
Definition timer.c:920
An event timer list.
Definition timer.c:49
#define fr_timer_in(...)
Definition timer.h:87
static bool fr_timer_armed(fr_timer_t *ev)
Definition timer.h:120
static fr_event_list_t * el
void fr_perror(char const *fmt,...)
Print the current error to stderr with a prefix.
Definition strerror.c:737
#define fr_strerror_const_push(_msg)
Definition strerror.h:227
#define fr_strerror_const(_msg)
Definition strerror.h:223
static fr_slen_t data
Definition value.h:1340
fr_heap_t * runnable
current runnable requests which we've spent time processing
Definition worker.c:109
static void worker_request_time_tracking_end(fr_worker_t *worker, request_t *request, fr_time_t now)
Definition worker.c:582
static void _worker_request_yield(request_t *request, UNUSED void *uctx)
Interpreter yielded request.
Definition worker.c:1265
static void worker_channel_callback(void *ctx, void const *data, size_t data_size, fr_time_t now)
Handle a control plane message sent to the worker via a channel.
Definition worker.c:243
fr_event_list_t * el
our event list
Definition worker.c:105
int fr_worker_pre_event(UNUSED fr_time_t now, UNUSED fr_time_delta_t wake, void *uctx)
Pre-event handler.
Definition worker.c:1573
static void worker_send_reply(fr_worker_t *worker, request_t *request, bool do_not_respond, fr_time_t now)
Send a response packet to the network side.
Definition worker.c:599
fr_channel_t * fr_worker_channel_create(fr_worker_t *worker, TALLOC_CTX *ctx, fr_control_t *master)
Create a channel to the worker.
Definition worker.c:1633
fr_rb_tree_t * listeners
so we can cancel requests when a listener goes away
Definition worker.c:116
static void worker_run_request(fr_worker_t *worker, fr_time_t start)
Run a request.
Definition worker.c:1314
static void worker_exit(fr_worker_t *worker)
Definition worker.c:221
#define WORKER_VERIFY
Definition worker.c:67
bool was_sleeping
used to suppress multiple sleep signals in a row
Definition worker.c:128
static int cmd_stats_worker(FILE *fp, UNUSED FILE *fp_err, void *ctx, fr_cmd_info_t const *info)
Definition worker.c:1726
static void _worker_request_runnable(request_t *request, void *uctx)
Request is now runnable.
Definition worker.c:1254
static char * itoa_internal(TALLOC_CTX *ctx, uint64_t number)
Definition worker.c:724
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:1363
static int8_t worker_dedup_cmp(void const *one, void const *two)
Track a request_t in the "dedup" tree.
Definition worker.c:1000
fr_worker_channel_t * channel
list of channels
Definition worker.c:131
char const * name
name of this worker
Definition worker.c:91
uint64_t num_active
number of active requests
Definition worker.c:123
fr_cmd_table_t cmd_worker_table[]
Definition worker.c:1764
static int worker_request_time_tracking_start(fr_worker_t *worker, request_t *request, fr_time_t now)
Start time tracking for a request, and mark it as runnable.
Definition worker.c:552
int fr_worker_stats(fr_worker_t const *worker, int num, uint64_t *stats)
Definition worker.c:1709
static int _worker_request_deinit(request_t *request, UNUSED void *uctx)
Definition worker.c:777
static void _worker_request_done_detached(request_t *request, UNUSED rlm_rcode_t rcode, UNUSED void *uctx)
Detached request (i.e.
Definition worker.c:1189
static void _worker_request_resume(request_t *request, UNUSED void *uctx)
Interpreter is starting to work on request again.
Definition worker.c:1274
fr_rb_tree_t * dedup
de-dup tree
Definition worker.c:114
fr_atomic_queue_t * aq_control
atomic queue for control messages sent to me
Definition worker.c:101
static void worker_nak(fr_worker_t *worker, fr_channel_data_t *cd, fr_time_t now)
Send a NAK to the network thread.
Definition worker.c:422
static void worker_request_name_number(request_t *request)
Definition worker.c:764
static void _worker_request_timeout(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t when, void *uctx)
Enforce max_request_time.
Definition worker.c:532
static void worker_request_bootstrap(fr_worker_t *worker, fr_channel_data_t *cd, fr_time_t now)
Definition worker.c:782
fr_log_t const * log
log destination
Definition worker.c:98
fr_io_stats_t stats
input / output stats
Definition worker.c:118
#define CHECK_CONFIG(_x, _min, _max)
static void _worker_request_detach(request_t *request, void *uctx)
Make us responsible for running the request.
Definition worker.c:1222
static int _fr_worker_rb_free(void *arg)
Definition worker.c:161
fr_time_tracking_t tracking
how much time the worker has spent doing things.
Definition worker.c:126
static void _worker_request_done_external(request_t *request, UNUSED rlm_rcode_t rcode, void *uctx)
External request is now complete.
Definition worker.c:1103
void fr_worker_destroy(fr_worker_t *worker)
Destroy a worker.
Definition worker.c:1023
uint64_t num_naks
number of messages which were nak'd
Definition worker.c:122
static void worker_request_init(fr_worker_t *worker, request_t *request, fr_time_t now)
Initialize various request fields needed by the worker.
Definition worker.c:747
fr_worker_config_t config
external configuration
Definition worker.c:92
fr_listen_t const * listener
incoming packets
Definition worker.c:137
unlang_interpret_t * intp
Worker's local interpreter.
Definition worker.c:94
static int fr_worker_listen_cancel_self(fr_worker_t *worker, fr_listen_t const *li)
Definition worker.c:373
static void worker_stop_request(request_t *request)
Signal the unlang interpreter that it needs to stop running the request.
Definition worker.c:513
static void _worker_request_prioritise(request_t *request, void *uctx)
Update a request's priority.
Definition worker.c:1291
bool exiting
are we exiting?
Definition worker.c:129
fr_log_lvl_t lvl
log level
Definition worker.c:99
static void worker_requests_cancel(fr_worker_channel_t *ch)
Definition worker.c:212
int num_channels
actual number of channels
Definition worker.c:107
fr_time_delta_t max_request_time
maximum time a request can be processed
Definition worker.c:112
fr_time_elapsed_t cpu_time
histogram of total CPU time per request
Definition worker.c:119
fr_rb_node_t node
in tree of listeners
Definition worker.c:139
int fr_worker_listen_cancel(fr_worker_t *worker, fr_listen_t const *li)
Definition worker.c:1660
void fr_worker_post_event(UNUSED fr_event_list_t *el, UNUSED fr_time_t now, void *uctx)
Post-event handler.
Definition worker.c:1594
fr_dlist_head_t dlist
of requests associated with this listener.
Definition worker.c:145
void fr_worker(fr_worker_t *worker)
The main loop and entry point of the stand-alone worker thread.
Definition worker.c:1515
request_slab_list_t * slab
slab allocator for request_t
Definition worker.c:133
static uint32_t worker_num_requests(fr_worker_t *worker)
Definition worker.c:772
fr_time_delta_t predicted
How long we predict a request will take to execute.
Definition worker.c:125
pthread_t thread_id
my thread ID
Definition worker.c:96
static void worker_recv_request(void *ctx, fr_channel_t *ch, fr_channel_data_t *cd)
Callback which handles a message being received on the worker side.
Definition worker.c:202
fr_time_elapsed_t wall_clock
histogram of wall clock time per request
Definition worker.c:120
static int8_t worker_listener_cmp(void const *one, void const *two)
Definition worker.c:149
static bool is_worker_thread(fr_worker_t const *worker)
Definition worker.c:188
fr_worker_channel_t
Definition worker.c:85
fr_timer_list_t * timeout
Track when requests timeout using a dlist.
Definition worker.c:111
static fr_ring_buffer_t * fr_worker_rb_init(void)
Initialise thread local storage.
Definition worker.c:170
fr_control_t * control
the control plane
Definition worker.c:103
static bool _worker_request_scheduled(request_t const *request, UNUSED void *uctx)
Check if a request is scheduled.
Definition worker.c:1283
static void _worker_request_done_internal(request_t *request, UNUSED rlm_rcode_t rcode, void *uctx)
Internal request (i.e.
Definition worker.c:1173
void fr_worker_debug(fr_worker_t *worker, FILE *fp)
Print debug information about the worker structure.
Definition worker.c:1606
static void _worker_request_internal_init(request_t *request, void *uctx)
Internal request (i.e.
Definition worker.c:1082
#define CHECK_CONFIG_TIME_DELTA(_x, _min, _max)
static int8_t worker_runnable_cmp(void const *one, void const *two)
Track a request_t in the "runnable" heap.
Definition worker.c:974
static void worker_listen_cancel_callback(void *ctx, void const *data, NDEBUG_UNUSED size_t data_size, UNUSED fr_time_t now)
A socket is going away, so clean up any requests which use this socket.
Definition worker.c:400
A worker which takes packets from a master, and processes them.
Definition worker.c:90
int message_set_size
default start number of messages
Definition worker.h:73
#define FR_CONTROL_ID_LISTEN_DEAD
Definition worker.h:40
int max_requests
max requests this worker will handle
Definition worker.h:69
int max_channels
maximum number of channels
Definition worker.h:71
fr_slab_config_t reuse
slab allocator configuration
Definition worker.h:78
int ring_buffer_size
default start size for the ring buffers
Definition worker.h:74
fr_time_delta_t max_request_time
maximum time a request can be processed
Definition worker.h:76