![]() |
The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
|
Worker thread functions. More...
#include <freeradius-devel/io/channel.h>#include <freeradius-devel/io/listen.h>#include <freeradius-devel/io/worker.h>#include <freeradius-devel/unlang/base.h>#include <freeradius-devel/unlang/finally.h>#include <freeradius-devel/util/minmax_heap.h>#include <freeradius-devel/util/timer.h>#include <stdalign.h>
Include dependency graph for worker.c:Go to the source code of this file.
Data Structures | |
| struct | fr_worker_listen_t |
| struct | fr_worker_s |
| A worker which takes packets from a master, and processes them. More... | |
Macros | |
| #define | CHECK_CONFIG(_x, _min, _max) |
| #define | CHECK_CONFIG_TIME_DELTA(_x, _min, _max) |
| #define | LOG_DST worker->log |
| #define | LOG_PREFIX worker->name |
| #define | WORKER_VERIFY |
Functions | |
| static | _Atomic (uint64_t) |
| static int | _worker_request_deinit (request_t *request, UNUSED void *uctx) |
| static void | _worker_request_detach (request_t *request, void *uctx) |
| Make us responsible for running the request. | |
| static void | _worker_request_done_detached (request_t *request, UNUSED rlm_rcode_t rcode, UNUSED void *uctx) |
| Detached request (i.e. | |
| static void | _worker_request_done_external (request_t *request, UNUSED rlm_rcode_t rcode, void *uctx) |
| External request is now complete. | |
| static void | _worker_request_done_internal (request_t *request, UNUSED rlm_rcode_t rcode, void *uctx) |
| Internal request (i.e. | |
| static void | _worker_request_internal_init (request_t *request, void *uctx) |
| Internal request (i.e. | |
| static void | _worker_request_prioritise (request_t *request, void *uctx) |
| Update a request's priority. | |
| static void | _worker_request_resume (request_t *request, UNUSED void *uctx) |
| Interpreter is starting to work on request again. | |
| static void | _worker_request_runnable (request_t *request, void *uctx) |
| Request is now runnable. | |
| static bool | _worker_request_scheduled (request_t const *request, UNUSED void *uctx) |
| Check if a request is scheduled. | |
| static void | _worker_request_timeout (UNUSED fr_timer_list_t *tl, UNUSED fr_time_t when, void *uctx) |
| Enforce max_request_time. | |
| static void | _worker_request_yield (request_t *request, UNUSED void *uctx) |
| Interpreter yielded request. | |
| static int | cmd_stats_worker (FILE *fp, UNUSED FILE *fp_err, void *ctx, fr_cmd_info_t const *info) |
| void | fr_worker (fr_worker_t *worker) |
| The main loop and entry point of the stand-alone worker thread. | |
| 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. | |
| fr_channel_t * | fr_worker_channel_create (fr_worker_t *worker, TALLOC_CTX *ctx, fr_control_t *master, fr_message_set_t *reply_ms) |
| Create a channel to the worker. | |
| int | fr_worker_close_event_insert (fr_worker_t *worker) |
| Add events to the loop which will send close ack to the network. | |
| fr_worker_config_t const * | fr_worker_config (fr_worker_t *worker) |
| void | fr_worker_debug (fr_worker_t *worker, FILE *fp) |
| Print debug information about the worker structure. | |
| void | fr_worker_exit (fr_worker_t *worker) |
| Tidy up a worker on normal exit. | |
| int | fr_worker_listen_cancel (fr_worker_t *worker, fr_listen_instance_t const *li) |
| static int | fr_worker_listen_cancel_self (fr_worker_t *worker, fr_listen_instance_t const *li) |
| static void | fr_worker_post_close_event (UNUSED fr_event_list_t *el, UNUSED fr_time_t now, void *uctx) |
| void | fr_worker_post_event (UNUSED fr_event_list_t *el, UNUSED fr_time_t now, void *uctx) |
| Post-event handler. | |
| static int | fr_worker_pre_close_event (UNUSED fr_time_t now, UNUSED fr_time_delta_t wake, void *uctx) |
| int | fr_worker_pre_event (UNUSED fr_time_t now, UNUSED fr_time_delta_t wake, void *uctx) |
| Pre-event handler. | |
| void | fr_worker_shutting_down (fr_worker_t *worker) |
| Tell the interpreter of the worker to cancel requests, instead of running them. | |
| int | fr_worker_stats (fr_worker_t const *worker, int num, uint64_t *stats) |
| static bool | is_worker_thread (fr_worker_t const *worker) |
| static char * | itoa_internal (TALLOC_CTX *ctx, uint64_t number) |
| static void | worker_channel_close (fr_channel_t *ch, UNUSED fr_time_t now, void *uctx) |
| The network is closing a channel to us. | |
| static void | worker_channel_close_ack (fr_worker_t *worker) |
| Acknowledge channel close after requests are completed. | |
| static void | worker_channel_open (fr_channel_t *ch, fr_message_set_t *reply_ms, UNUSED fr_time_t now, void *uctx) |
| The network has opened a channel to us. | |
| static fr_cmp_ret_t | worker_dedup_cmp (void const *one, void const *two) |
| Track a request_t in the "dedup" tree. | |
| static void | worker_listen_cancel_callback (void const *data, NDEBUG_UNUSED size_t data_size, UNUSED fr_time_t now, void *uctx) |
| A socket is going away, so clean up any requests which use this socket. | |
| static fr_cmp_ret_t | worker_listener_cmp (void const *one, void const *two) |
| static void | worker_nak (fr_worker_t *worker, fr_channel_data_t *cd, fr_time_t now) |
| Send a NAK to the network thread. | |
| static uint64_t | worker_num_requests (fr_worker_t *worker) |
| static void | worker_recv_request (fr_channel_t *ch, fr_channel_data_t *cd, void *uctx) |
| Callback which handles a message being received on the worker side. | |
| static void | worker_request_bootstrap (fr_worker_channel_t *wc, fr_channel_data_t *cd, fr_time_t now) |
| static void | worker_request_init (fr_worker_t *worker, request_t *request, fr_time_t now) |
| Initialize various request fields needed by the worker. | |
| static void | worker_request_name_number (request_t *request) |
| static void | worker_request_time_tracking_end (fr_worker_t *worker, request_t *request, fr_time_t now) |
| 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. | |
| static void | worker_requests_cancel (fr_worker_t *worker, fr_worker_channel_t *ch) |
| static void | worker_run_request (fr_worker_t *worker, fr_time_t start) |
| Run a request. | |
| static fr_cmp_ret_t | worker_runnable_cmp (void const *one, void const *two) |
| Track a request_t in the "runnable" heap. | |
| static void | worker_send_reply (fr_worker_t *worker, request_t *request, bool send_reply, fr_time_t now) |
| Send a response packet to the network side. | |
| static bool | worker_sleeping (fr_worker_t *worker) |
| Announce we are about to block, then drain every channel one last time. | |
| static void | worker_stop_request (request_t *request) |
| Signal the unlang interpreter that it needs to stop running the request. | |
Variables | |
| fr_cmd_table_t | cmd_worker_table [] |
| fr_worker_channel_t | |
| static fr_channel_callbacks_t const | worker_channel_callbacks |
| Channel events the worker handles. | |
Worker thread functions.
The "worker" thread is the one responsible for the bulk of the work done when processing a request. Workers are spawned by the scheduler, and create a kqueue (KQ) and control-plane Atomic Queue (AQ) for control-plane communication.
When a network thread discovers that it needs more workers, it asks the scheduler for a KQ/AQ combination. The network thread then creates a channel dedicated to that worker, and sends the channel to the worker in a "new channel" message. The worker receives the channel, and sends an ACK back to the network thread.
The network thread then sends the worker new packets, which the worker receives and processes.
When a packet is decoded, it is put into the "runnable" heap, and also into the timeout sublist. The main loop fr_worker() then pulls new requests off of this heap and runs them. The main event loop checks the head of the timeout sublist, and forcefully terminates any requests which have been running for too long.
If a request is yielded, it is placed onto the yielded list in the worker "tracking" data structure.
Definition in file worker.c.
| struct fr_worker_listen_t |
Collaboration diagram for fr_worker_listen_t:| Data Fields | ||
|---|---|---|
| fr_dlist_head_t | dlist | of requests associated with this listener. |
| fr_listen_instance_t const * | listener | incoming packets |
| fr_rb_node_t | node | in tree of listeners |
| struct fr_worker_s |
A worker which takes packets from a master, and processes them.
Collaboration diagram for fr_worker_s:| Data Fields | ||
|---|---|---|
| fr_worker_channel_t * | channel | list of channels |
| fr_worker_config_t | config | external configuration |
| fr_control_t * | control | the control plane |
| fr_time_elapsed_t | cpu_time | histogram of total CPU time per request |
| fr_rb_tree_t * | dedup | de-dup tree |
| fr_event_list_t * | el | our event list |
| bool | exiting | are we exiting? |
| unlang_interpret_t * | intp | Worker's local interpreter. |
| fr_rb_tree_t * | listeners | so we can cancel requests when a listener goes away |
| fr_log_t const * | log | log destination |
| fr_log_lvl_t | lvl | log level |
| fr_time_delta_t | max_request_time | maximum time a request can be processed |
| char const * | name | name of this worker |
| uint64_t | num_active | number of active requests |
| int | num_channels | actual number of channels |
| int | num_closing | number of channels closing |
| uint64_t | num_naks | number of messages which were nak'd |
| fr_time_delta_t | predicted | How long we predict a request will take to execute. |
| fr_heap_t * | runnable | current runnable requests which we've spent time processing |
| request_slab_list_t * | slab | slab allocator for request_t |
| fr_io_stats_t | stats | input / output stats |
| pthread_t | thread_id | my thread ID |
| fr_timer_list_t * | timeout | Track when requests timeout using a dlist. |
| fr_time_tracking_t | tracking | how much time the worker has spent doing things. |
| fr_time_elapsed_t | wall_clock | histogram of wall clock time per request |
| #define CHECK_CONFIG | ( | _x, | |
| _min, | |||
| _max | |||
| ) |
| #define CHECK_CONFIG_TIME_DELTA | ( | _x, | |
| _min, | |||
| _max | |||
| ) |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
Enforce max_request_time.
Run periodically, and tries to clean up requests which were received by the network thread more than max_request_time seconds ago. In the interest of not adding a timer for every packet, the requests are given a 1 second leeway.
| [in] | tl | the worker's timer list. |
| [in] | when | the current time |
| [in] | uctx | the request_t timing out. |
Definition at line 473 of file worker.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
| void fr_worker | ( | fr_worker_t * | worker | ) |
The main loop and entry point of the stand-alone worker thread.
Where there is only one thread, the event loop runs fr_worker_pre_event() and fr_worker_post_event() instead, And then fr_worker_post_event() takes care of calling worker_run_request() to actually run the request.
| [in] | worker | the worker data structure to manage |
Definition at line 1463 of file worker.c.
Here is the call graph for this function:
Here is the caller graph for this function:| 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.
| [in] | ctx | the talloc context |
| [in] | name | the name of this worker |
| [in] | el | the event list |
| [in] | logger | the destination for all logging messages |
| [in] | lvl | log level |
| [in] | config | various configuration parameters |
Definition at line 1260 of file worker.c.
Here is the call graph for this function:
Here is the caller graph for this function:| fr_channel_t * fr_worker_channel_create | ( | fr_worker_t * | worker, |
| TALLOC_CTX * | ctx, | ||
| fr_control_t * | master, | ||
| fr_message_set_t * | reply_ms | ||
| ) |
Create a channel to the worker.
Called by the master (i.e. network) thread when it needs to create a new channel to a particular worker.
| [in] | worker | the worker |
| [in] | ctx | the context in which the channel will be created |
| [in] | master | the control plane of the master |
| [in] | reply_ms | the message set the worker allocates replies from |
Definition at line 1653 of file worker.c.
Here is the call graph for this function:
Here is the caller graph for this function:| int fr_worker_close_event_insert | ( | fr_worker_t * | worker | ) |
| fr_worker_config_t const * fr_worker_config | ( | fr_worker_t * | worker | ) |
| void fr_worker_debug | ( | fr_worker_t * | worker, |
| FILE * | fp | ||
| ) |
| void fr_worker_exit | ( | fr_worker_t * | worker | ) |
| int fr_worker_listen_cancel | ( | fr_worker_t * | worker, |
| fr_listen_instance_t const * | li | ||
| ) |
|
static |
|
static |
| void fr_worker_post_event | ( | UNUSED fr_event_list_t * | el, |
| UNUSED fr_time_t | now, | ||
| void * | uctx | ||
| ) |
|
static |
| int fr_worker_pre_event | ( | UNUSED fr_time_t | now, |
| UNUSED fr_time_delta_t | wake, | ||
| void * | uctx | ||
| ) |
| void fr_worker_shutting_down | ( | fr_worker_t * | worker | ) |
Tell the interpreter of the worker to cancel requests, instead of running them.
The single threaded scheduler calls this function before it detaches the thread data, so that the requests which the detach cancels do not go into the runnable heap of the worker. A worker thread does the same when the last input channel closes.
| [in] | worker | which is shutting down. |
Definition at line 1602 of file worker.c.
Here is the call graph for this function:
Here is the caller graph for this function:| int fr_worker_stats | ( | fr_worker_t const * | worker, |
| int | num, | ||
| uint64_t * | stats | ||
| ) |
|
inlinestatic |
|
static |
|
static |
|
inlinestatic |
Acknowledge channel close after requests are completed.
When the channel close is acknowledged, the network frees the channel which then causes issues with any requests still referencing the channel.
Definition at line 1439 of file worker.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
|
static |
|
static |
A socket is going away, so clean up any requests which use this socket.
| [in] | data | the message |
| [in] | data_size | size of the data |
| [in] | now | the current time |
| [in] | uctx | the worker |
Definition at line 338 of file worker.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
|
static |
Send a NAK to the network thread.
The network thread believes that a worker is running a request until that request has been NAK'd. We typically NAK requests when they've been hanging around in the worker's backlog too long, or there was an error executing the request.
| [in] | worker | the worker |
| [in] | cd | the message to NAK |
| [in] | now | when the message is NAKd |
Definition at line 360 of file worker.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
inlinestatic |
|
static |
Callback which handles a message being received on the worker side.
| [in] | ch | the channel to drain |
| [in] | cd | the message (if any) to start with |
| [in] | uctx | the worker channel slot the message came in on |
Definition at line 167 of file worker.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
|
inlinestatic |
|
inlinestatic |
|
static |
|
static |
|
static |
|
inlinestatic |
Run a request.
Until it either yields, or is done.
This function is also responsible for sending replies, and cleaning up the request.
| [in] | worker | the worker |
| [in] | start | the current time |
Definition at line 1220 of file worker.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
|
static |
Send a response packet to the network side.
| [in] | worker | This worker. |
| [in] | request | we're sending a reply for. |
| [in] | send_reply | whether the network side sends a reply |
| [in] | now | The current time |
Definition at line 540 of file worker.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
inlinestatic |
Announce we are about to block, then drain every channel one last time.
See fr_control_sleeping() for the ordering this relies on.
Definition at line 1406 of file worker.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
Signal the unlang interpreter that it needs to stop running the request.
Signalling is a synchronous operation. Whatever I/O requests the request is currently performing are signalled to be cancelled. Depending on the specific back end, the actual cancellation may be asynchronously handled by the trunk code.
Modules and unlang keywords explicitly register signal handlers to deal with their yield points being cancelled/interrupted via this function.
Following this, the request still needs to be run through the interpreter to tidy up and potentially run a finally section.
| [in] | request | request to cancel. The request may still run to completion. |
Definition at line 454 of file worker.c.
Here is the call graph for this function:
Here is the caller graph for this function:| fr_cmd_table_t cmd_worker_table[] |
|
static |
Channel events the worker handles.
1.9.8