The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
timer.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/** Various types of event timer list
18 *
19 * @file src/lib/util/timer.c
20 *
21 * @copyright 2025 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
22 */
23
24#define _TIMER_PRIVATE 1
26
27#include <freeradius-devel/util/debug.h>
28#include <freeradius-devel/util/time.h>
29#include <freeradius-devel/util/dlist.h>
30#include <freeradius-devel/util/event.h>
31#include <freeradius-devel/util/value.h>
32#include <freeradius-devel/util/lst.h>
33
34FR_DLIST_TYPES(timer)
35FR_DLIST_TYPEDEFS(timer, fr_timer_head_t, fr_timer_entry_t)
36
37/** What type of event list the timer is inserted into
38 *
39 */
40typedef enum : uint8_t {
41 TIMER_LIST_TYPE_LST = 1, //!< Self-sorting timer list based on a left leaning skeleton tree.
42 TIMER_LIST_TYPE_ORDERED = 2, //!< Strictly ordered list of events in a dlist.
43 TIMER_LIST_TYPE_SHARED = 3 //!< all events share one event callback
45
46/** Set by fr_timer_list_run(), for the lifetime of the call
47 *
48 * fr_timer_list_run() sets this locally, and then also in tl->run. If a callback frees the list out from
49 * under fr_timer_list_run(), the this structure tracks that. We can't access a list after it's been freed.
50 * The destructor for the list sets run->freed (in the parents stack frame). fr_timer_list_run() can then
51 * check its' local on-stack variable to see whether the list has been freed, and whether it should stop
52 * running the list
53 */
54typedef struct timer_run_s timer_run_t;
55
57 timer_run_t *prev; //!< The frame of the enclosing call, when a callback runs
58 ///< the same list again.
59 bool freed; //!< A callback freed the list.
60};
61
62/** An event timer list
63 *
64 */
66 struct fr_timer_list_pub_s pub; //!< Public interface to the event timer list.
67
68 union {
69 fr_lst_t *lst; //!< of timer events to be executed.
70 timer_head_t ordered; //!< A list of timer events to be executed.
71 struct {
72 fr_rb_tree_t *rb; //!< a tree of raw pointers
73 fr_rb_tree_t *deferred; //!< a tree of deferred things
74 size_t time_offset; //!< offset from uctx to the fr_time_t it contains
75 size_t node_offset; //!< offset from uctx to the fr_rb_node it contains
76 fr_timer_cb_t callback; //!< the callback to run
77 } shared;
78 };
80 bool in_handler; //!< Whether we're currently in a callback.
81 timer_run_t *run; //!< Track whether we freed the list in local variables.
82 bool disarmed; //!< the entire timer list is disarmed
83
84 timer_head_t deferred; //!< A list of timer events to be inserted, after
85 ///< the current batch has been processed.
86 ///< This prevents "busy" timer loops, where
87 ///< other events may starve, or we may never exit.
88
89 fr_timer_list_t *parent; //!< Parent list to insert event into (if any).
90 fr_timer_t *parent_ev; //!< Event in the parent's event loop.
91
92#ifdef WITH_EVENT_DEBUG
93 fr_timer_t *report; //!< Used to trigger periodic reports about the event timer list.
94#endif
95};
96
97/** A timer event
98 *
99 */
101 fr_time_t when; //!< When this timer should fire.
102
103 fr_timer_cb_t callback; //!< Callback to execute when the timer fires.
104 void const *uctx; //!< Context pointer to pass to the callback.
105
106 TALLOC_CTX *linked_ctx; //!< talloc ctx this event was bound to.
107
108 fr_timer_t **parent; //!< A pointer to the parent structure containing the timer
109 ///< event.
110
111 fr_timer_entry_t entry; //!< Entry in a list of timer events.
112 union {
113 fr_dlist_t ordered_entry; //!< Entry in an ordered list of timer events.
114 fr_lst_index_t lst_idx; //!< Where to store opaque lst data, not used for ordered lists.
115 };
116 bool free_on_fire; //!< Whether to free the event when it fires.
117
118 fr_timer_list_t *tl; //!< The event list this timer is part of.
119 ///< This is set to NULL when an event is disarmed,
120 ///< but all other fields are left intact.
121
122#ifndef NDEBUG
123 char const *file; //!< Source file this event was last updated in.
124 int line; //!< Line this event was last updated on.
125#endif
126};
127
128FR_DLIST_FUNCS(timer, fr_timer_t, entry)
129
130#define CHECK_PARENT(_ev) \
131 fr_assert_msg(!(_ev)->parent || (*(_ev)->parent == ev), \
132 "Event %p, allocd %s[%d], parent field points to %p", (_ev), (_ev)->file, (_ev)->line, *(_ev)->parent)
133
134#define TIMER_UCTX_TO_TIME(_tl, _x) ((fr_time_t *)(((uintptr_t) (_x)) + (_tl)->shared.time_offset))
135
136/*
137 * When looping over a timer list, stop if the entry in the list is in the future.
138 *
139 * OR stop if a callback disarmed the list. When the list is re-armed with fr_timer_list_arm(), that
140 * function will take care of running any pending timer events.
141 */
142#define TIMER_STOP_IF_DONE(_next) \
143 do { \
144 if (fr_time_gt(_next, *when) || tl->disarmed) { \
145 *when = _next; \
146 goto done; \
147 } \
148 } while (0)
149
150#define TIMER_RETURN_DONE \
151 do { \
152 *when = fr_time_wrap(0); \
153 done: \
154 return fired; \
155 } while (0)
156
157/*
158 * After running a callback, stop if the callback freed the list. The list is gone, so the code must
159 * not read the list.
160 */
161#define TIMER_RETURN_IF_FREED \
162 do { \
163 if (unlikely(run->freed)) { \
164 *when = fr_time_wrap(0); \
165 return fired; \
166 } \
167 } while (0)
168
169/** Specialisation function to insert a timer
170 *
171 * @param[in] tl Timer list to insert into.
172 * @param[in] ev Timer event to insert.
173 * @return
174 * - 0 on success.
175 * - -1 on failure.
176 */
178
179/** Specialisation function to delete a timer
180 *
181 * @param[in] ev Timer event to delete.
182 * @return
183 * - 0 on success.
184 * - -1 on failure.
185 */
186typedef int (*timer_disarm_t)(fr_timer_t *ev);
187
188/** Specialisation function to execute any pending timers
189 *
190 * @param[in] tl Timer list to execute.
191 * @param[in,out] when Our current time, updated to the next event time (i.e. the next time we'll need to run something)
192 * @param[in] run The call to fr_timer_list_run(). A callback may free the list. After each callback,
193 * the function reads `run->freed`, and returns without reading `tl` if the value is true.
194 * @return
195 * - 0 no timer events fired.
196 * - 1 a timer event fired.
197 */
198typedef int (*timer_list_run_t)(fr_timer_list_t *tl, fr_time_t *when, timer_run_t *run);
199
200/** Return the soonest timer event
201 *
202 * @param[in] tl to get the head of.
203 * @return
204 * - The head of the list.
205 * - NULL if the list is empty.
206 */
207typedef fr_timer_t *(*timer_list_head_t)(fr_timer_list_t *tl);
208
209/** Process any deferred timer events
210 *
211 * @param[in] tl to process deferred events for.
212 * @return
213 * - The head of the list.
214 * - NULL if the list is empty.
215 */
217
218/** Return the number of elements in the list
219 *
220 * @param[in] tl to get the number of elements from.
221 * @return
222 * - The number of elements in the list.
223 */
225
226typedef struct {
227 timer_insert_t insert; //!< Function to insert a timer event.
228 timer_disarm_t disarm; //!< Function to delete a timer event.
229
230 timer_list_run_t run; //!< Function to run a timer event.
231 timer_list_head_t head; //!< Function to get the head of the list.
232 timer_list_deferred_t deferred; //!< Function to process deferred events.
233 timer_list_num_elements_t num_events; //!< Function to get the number of elements in the list.
235
236#define EVENT_ARMED(_ev) ((_ev)->tl != NULL)
237
239
242
243static int timer_lst_disarm(fr_timer_t *ev);
244static int timer_ordered_disarm(fr_timer_t *ev);
245
246static int timer_list_lst_run(fr_timer_list_t *tl, fr_time_t *when, timer_run_t *run);
249
252
256
257static uint64_t timer_list_lst_num_events(fr_timer_list_t *tl);
260
261/** Functions for performing operations on various types of timer list
262 *
263 */
267 .disarm = timer_lst_disarm,
268
269 .run = timer_list_lst_run,
270 .head = timer_list_lst_head,
271 .deferred = timer_list_lst_deferred,
272 .num_events = timer_list_lst_num_events
273 },
275 .insert = timer_ordered_insert_at,
276 .disarm = timer_ordered_disarm,
277
280 .deferred = timer_list_ordered_deferred,
282 },
284// .insert = timer_shared_insert_at,
285// .disarm = timer_shared_disarm,
286
288// .head = timer_list_shared_head,
289 .deferred = timer_list_shared_deferred,
290 .num_events = timer_list_shared_num_events
291 },
292};
293
294/** Compare two timer events to see which one should occur first
295 *
296 * @param[in] a the first timer event.
297 * @param[in] b the second timer event.
298 * @return
299 * - +1 if a should occur later than b.
300 * - -1 if a should occur earlier than b.
301 * - 0 if both events occur at the same time.
302 */
303static fr_cmp_ret_t timer_cmp(void const *a, void const *b)
304{
305 fr_timer_t const *ev_a = a, *ev_b = b;
306
307 return fr_time_cmp(ev_a->when, ev_b->when);
308}
309
310
311/** This callback fires in the parent to execute events in this sublist
312 *
313 * @param[in] parent_tl Parent event timer list.
314 * @param[in] when When the parent timer fired.
315 * @param[in] uctx Sublist to execute.
316 */
317static void _parent_timer_cb(UNUSED fr_timer_list_t *parent_tl, fr_time_t when, void *uctx)
318{
319 fr_timer_list_t *tl = talloc_get_type_abort(uctx, fr_timer_list_t);
320
321 fr_assert(!tl->disarmed);
322
323 /*
324 * We're in the parent timer, so we need to run the
325 * events in the child timer list.
326 */
327 (void)fr_timer_list_run(tl, &when);
328}
329
330/** Utility function to update parent timers
331 *
332 * @param[in] tl to update parent timers for.
333 * @return
334 * - 0 on success.
335 * - -1 on failure.
336 */
337static inline CC_HINT(always_inline) int timer_list_parent_update(fr_timer_list_t *tl)
338{
339 fr_time_t *when;
340
341 if (!tl->parent) return 0;
342
343 when = timer_list_when(tl);
344
345 /*
346 * No events, disarm the timer
347 */
348 if (!when) {
349 /*
350 * Disables the timer in the parent, does not free the memory
351 */
353 return 0;
354 }
355
356 /*
357 * We have an active event, we can suppress changes which have no effect.
358 */
359 if (tl->parent_ev && EVENT_ARMED(tl->parent_ev)) {
360 fr_assert(!tl->disarmed); /* fr_timer_list_disarm() must disarm it */
361
362 if (fr_time_eq(*when, tl->parent_ev->when)) {
363 return 0;
364 }
365 }
366
367 /*
368 * This is a child list which is disabled. Don't update the parent.
369 */
370 if (tl->disarmed) {
371 fr_assert(tl->parent);
372
374 return 0;
375 }
376
377 /*
378 * The list is armed and the head has changed, so we change the event in the parent list.
379 */
380 if (fr_timer_at(tl, tl->parent, &tl->parent_ev,
381 *when, false, _parent_timer_cb, tl) < 0) return -1;
382
383 return 0;
384}
385
386/** Insert a timer event into a single event timer list
387 *
388 * @param[in] tl to insert the event into.
389 * @param[in] ev to insert.
390 * @return
391 * - 0 on success.
392 * - -1 on failure.
393 */
395{
396 if (unlikely(fr_lst_insert(tl->lst, ev) < 0)) {
397 fr_strerror_const_push("Failed inserting timer into lst");
398 return -1;
399 }
400
401 return 0;
402}
403
404/** Insert an event into an ordered timer list
405 *
406 * Timer must be in order, i.e. either before first event, or after last event
407 *
408 * @param[in] tl to insert the event into.
409 * @param[in] ev to insert.
410 * @return
411 * - 0 on success.
412 * - -1 on failure.
413 */
415{
416 fr_timer_t *tail;
417
418 tail = timer_tail(&tl->ordered);
419 if (tail && fr_time_lt(ev->when, tail->when)) {
420 fr_strerror_const("Event being inserted must occurr _after_ the last event");
421 return -1;
422 }
423
424 if (unlikely(timer_insert_tail(&tl->ordered, ev) < 0)) {
425 fr_strerror_const_push("Failed inserting timer into ordered list");
426 return -1;
427 }
428
429 return 0;
430}
431
432/** Remove an event from the event loop
433 *
434 * @param[in] ev to free.
435 * @return
436 * - 0 on success.
437 * - -1 on failure.
438 */
439static int _timer_free(fr_timer_t *ev)
440{
441 fr_timer_t **ev_p;
442 int ret;
443
444 ret = fr_timer_disarm(ev); /* Is a noop if ev->tl == NULL */
445 if (ret < 0) return ret;
446
447 CHECK_PARENT(ev);
448 ev_p = ev->parent;
449 *ev_p = NULL;
450
451 return 0;
452}
453
454/** Insert a timer event into an event list
455 *
456 * @note The talloc parent of the memory returned in ev_p must not be changed.
457 * If the lifetime of the event needs to be bound to another context
458 * this function should be called with the existing event pointed to by
459 * ev_p.
460 *
461 * @param[in] ctx to bind lifetime of the event to.
462 * @param[in] tl to insert event into.
463 * @param[in,out] ev_p If not NULL modify this event instead of creating a new one. This is a parent
464 * in a temporal sense, not in a memory structure or dependency sense.
465 * @param[in] when we should run the event.
466 * @param[in] free_on_fire Whether event memory should be freed if the event fires.
467 * @param[in] callback function to execute if the event fires.
468 * @param[in] uctx user data to pass to the event.
469 * @return
470 * - 0 on success.
471 * - -1 on failure.
472 */
474 TALLOC_CTX *ctx, fr_timer_list_t *tl, fr_timer_t **ev_p,
475 fr_time_t when,
476 bool free_on_fire, fr_timer_cb_t callback, void const *uctx)
477{
478 fr_timer_t *ev;
479
481
482 /*
483 * If there is an event, reuse it instead of freeing it
484 * and allocating a new one. This is to reduce memory
485 * churn for repeat events.
486 */
487 if (!*ev_p) {
488 new_event:
489 ev = talloc_zero(tl, fr_timer_t);
490 if (unlikely(!ev)) {
491 fr_strerror_const("Out of memory");
492 return -1;
493 }
494
495 EVENT_DEBUG("%p - " NDEBUG_LOCATION_FMT "Added new timer %p", tl, NDEBUG_LOCATION_VALS ev);
496 /*
497 * Bind the lifetime of the event to the specified
498 * talloc ctx. If the talloc ctx is freed, the
499 * event will also be freed.
500 */
501 if (ctx != tl) talloc_link_ctx(ctx, ev);
502
503 talloc_set_destructor(ev, _timer_free);
504 } else {
505 ev = talloc_get_type_abort(UNCONST(fr_timer_t *, *ev_p), fr_timer_t);
506
507 EVENT_DEBUG("%p - " NDEBUG_LOCATION_FMT "Re-armed timer %p", tl, NDEBUG_LOCATION_VALS ev);
508
509 /*
510 * We can't disarm the linking context due to
511 * limitations in talloc, so if the linking
512 * context changes, we need to free the old
513 * event, and allocate a new one.
514 *
515 * Freeing the event also removes it from the lst.
516 */
517 if (unlikely(ev->linked_ctx != ctx)) {
518 talloc_free(ev);
519 goto new_event;
520 }
521
522 /*
523 * If the event is associated with a list, we need
524 * to disarm it, before we can rearm it.
525 */
526 if (EVENT_ARMED(ev)) {
527 int ret;
528 char const *err_file;
529 int err_line;
530
531 /*
532 * Removed event from the event list or the
533 * deferred list.
534 */
535 ret = fr_timer_disarm(ev);
536#ifndef NDEBUG
537 err_file = ev->file;
538 err_line = ev->line;
539#else
540 err_file = "not-available";
541 err_line = 0;
542#endif
543
544 /*
545 * Events MUST be in the lst (or the insertion list).
546 */
547 if (!fr_cond_assert_msg(ret == 0,
548 "Event %p, allocd %s[%d], was not found in the event "
549 "list or deferred list when re-armed: %s", ev,
550 err_file, err_line, fr_strerror())) return -1;
551 }
552 }
553
554 ev->tl = tl; /* This indicates the event memory is bound to an event loop */
555 ev->when = when;
556 ev->free_on_fire = free_on_fire;
557 ev->callback = callback;
558 ev->uctx = uctx;
559 ev->linked_ctx = ctx;
560 ev->parent = ev_p;
561#ifndef NDEBUG
562 ev->file = file;
563 ev->line = line;
564#endif
565
566 /*
567 * No updating needed as the events are deferred
568 */
569 if (tl->in_handler) {
570 /*
571 * ...a little hacky, but we need to verify that
572 * we're not inserting an event that's earlier
573 * than the last event in the list for ordered
574 * lists.
575 *
576 * Otherwise we'd end up doing this when we tried
577 * to move all the deferred events into the timer
578 * list, and end up making that O(n) instead of O(1).
579 */
580 if (tl->type == TIMER_LIST_TYPE_ORDERED) {
582
583 if (head && fr_time_lt(ev->when, head->when)) {
584 fr_strerror_const("Event being inserted must occurr _after_ the last event");
585
586 insert_failed:
587 talloc_set_destructor(ev, NULL);
588 talloc_free(ev);
589 *ev_p = NULL;
590 return -1;
591 }
592 }
593
594 if (!fr_cond_assert_msg(timer_insert_tail(&tl->deferred, ev) == 0,
595 "Failed inserting event into deferred list")) {
596 goto insert_failed;
597 }
598 } else {
599 int ret;
600
601 ret = timer_funcs[tl->type].insert(tl, ev);
602 if (unlikely(ret < 0)) goto insert_failed;
603
604 /*
605 * We need to update the parent timer
606 * to ensure it fires at the correct time.
607 */
608 if (unlikely(timer_list_parent_update(tl) < 0)) return -1;
609 }
610
611 *ev_p = ev;
612
613 return 0;
614}
615
616/** Insert a timer event into an event list
617 *
618 * @note The talloc parent of the memory returned in ev_p must not be changed.
619 * If the lifetime of the event needs to be bound to another context
620 * this function should be called with the existing event pointed to by
621 * ev_p.
622 *
623 * @param[in] ctx to bind lifetime of the event to.
624 * @param[in] tl to insert event into.
625 * @param[in,out] ev_p If not NULL modify this event instead of creating a new one. This is a parent
626 * in a temporal sense, not in a memory structure or dependency sense.
627 * @param[in] delta In how many nanoseconds to wait before should we execute the event.
628 * @param[in] free_on_fire Whether event memory should be freed if the event fires.
629 * @param[in] callback function to execute if the event fires.
630 * @param[in] uctx user data to pass to the event.
631 * @return
632 * - 0 on success.
633 * - -1 on failure.
634 */
636 TALLOC_CTX *ctx, fr_timer_list_t *tl, fr_timer_t **ev_p,
637 fr_time_delta_t delta,
638 bool free_on_fire, fr_timer_cb_t callback, void const *uctx)
639{
641 ctx, tl, ev_p, fr_time_add(tl->pub.time(), delta),
642 free_on_fire, callback, uctx);
643}
644
646{
647 fr_timer_list_t *tl = ev->tl;
648
649 if (timer_in_list(&tl->deferred,ev)) {
650 (void)timer_remove(&tl->deferred, ev);
651 } else {
652 int ret = fr_lst_extract(tl->lst, ev);
653 char const *err_file;
654 int err_line;
655
656#ifndef NDEBUG
657 err_file = ev->file;
658 err_line = ev->line;
659#else
660 err_file = "not-available";
661 err_line = 0;
662#endif
663
664
665 /*
666 * Events MUST be in the lst (or the insertion list).
667 */
668 if (!fr_cond_assert_msg(ret == 0,
669 "Event %p, lst_id %u, allocd %s[%d], was not found in the event lst or "
670 "insertion list when freed: %s", ev, ev->lst_idx, err_file, err_line,
671 fr_strerror())) return -1;
672 }
673
674 return 0;
675}
676
677/** Remove a timer from a timer list, but don't free it
678 *
679 * @param[in] ev to remove.
680 */
682{
683 /*
684 * Check the check is still valid (sanity check)
685 */
686 (void)talloc_get_type_abort(ev, fr_timer_t);
687
688 /*
689 * Already dissassociated from a list, nothing to do.
690 */
691 if (!ev->tl) return 0;
692
693 /*
694 * This *MUST* be in a timer list if it has a non-NULL tl pointer.
695 */
696 if (unlikely(!fr_cond_assert(timer_in_list(&ev->tl->ordered, ev)))) return -1;
697
698 (void)timer_remove(&ev->tl->ordered, ev);
699
700 return 0;
701}
702
703/** Remove an event from the event list, but don't free the memory
704 *
705 * @param[in] ev to remove from the event list.
706 */
708{
709 fr_timer_list_t *tl;
710
711 if (!ev || !EVENT_ARMED(ev)) {
712 EVENT_DEBUG("Asked to disarm inactive timer %p (noop)", ev);
713 return 0; /* Noop */
714 }
715
716 tl = ev->tl;
717
718 EVENT_DEBUG("Disarming timer %p", ev);
719
720 CHECK_PARENT(ev);
721
722 /*
723 * If the event is deferred, it's not in the event list proper
724 * so just remove it, and set the tl pointer to NULL.
725 */
726 if (timer_in_list(&tl->deferred,ev)) {
727 (void)timer_remove(&tl->deferred, ev);
728 } else {
729 int ret = timer_funcs[ev->tl->type].disarm(ev);
730 if (ret < 0) return ret;
731 }
732 ev->tl = NULL;
733
734 return timer_list_parent_update(tl);
735}
736
737/** Delete a timer event and free its memory
738 *
739 * @param[in] ev_p of the event being deleted.
740 * @return
741 * - 0 on success.
742 * - -1 on failure.
743 */
745{
746 fr_timer_t *ev;
747 int ret;
748
749 if (unlikely(!*ev_p)) return 0;
750
751 ev = *ev_p;
752 ret = talloc_free(ev); /* Destructor removed event from any lists */
753
754 /*
755 * Don't leave a garbage pointer value
756 * if parent is not ev_p.
757 */
758 if (likely(ret == 0)) {
759 *ev_p = NULL;
760 return 0;
761 }
762
763 EVENT_DEBUG("Deleting timer %p failed: %s", ev, fr_strerror_peek());
764 return -1;
765}
766
767/** Internal timestamp representing when the timer should fire
768 *
769 * @return When the timestamp should fire.
770 */
772{
773 if (!fr_timer_armed(ev)) return fr_time_wrap(0);
774 return ev->when;
775}
776
777/** Return time delta between now and when the timer should fire
778 *
779 * @param[in] ev to get the time delta for.
780 */
782{
783 if (!fr_timer_armed(ev)) return fr_time_delta_wrap(0);
784 return fr_time_sub(ev->when, ev->tl->pub.time());
785}
786
787/** Check if a timer event is armed
788 *
789 * @param[in] ev to check.
790 * @return
791 * - true if the event is armed.
792 * - false if the event is not armed.
793 */
795{
796 return EVENT_ARMED(ev);
797}
798
799/** Run all scheduled timer events in a lst
800 *
801 * @param[in] tl containing the timer events.
802 * @param[in] when Process events scheduled to run before or at this time.
803 * - Set to 0 if no more events.
804 * - Set to the next event time if there are more events.
805 * @return
806 * - 0 no timer events fired.
807 * - 1 a timer event fired.
808 */
809CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private fr_timer_list_t trips --fsanitize=function*/
811{
812 fr_timer_cb_t callback;
813 void *uctx;
814 void *item;
815 fr_timer_t *ev;
816 int fired = 0;
817
818 while (fr_lst_num_elements(tl->lst) > 0) {
819 fr_lst_peek(&item, tl->lst);
820 ev = talloc_get_type_abort(item, fr_timer_t);
821
823
824 callback = ev->callback;
825 memcpy(&uctx, &ev->uctx, sizeof(uctx));
826
827 CHECK_PARENT(ev);
828
829 /*
830 * Disarm the event before calling it.
831 *
832 * This leaves the memory in place,
833 * but dissassociates it from the list.
834 *
835 * We use the public function as it
836 * handles more cases.
837 */
838 if (!fr_cond_assert(fr_timer_disarm(ev) == 0)) return -2;
839 EVENT_DEBUG("Running timer %p", ev);
840 if (ev->free_on_fire) talloc_free(ev);
841
842 callback(tl, *when, uctx);
843
844 fired++;
845
847 }
848
850}
851
852/** Run all scheduled events in an ordered list
853 *
854 * @param[in] tl containing the timer events.
855 * @param[in] when Process events scheduled to run before or at this time.
856 * - Set to 0 if no more events.
857 * - Set to the next event time if there are more events.
858 * @return
859 * - < 0 if we failed to updated the parent list.
860 * - 0 no timer events fired.
861 * - >0 number of timer event fired.
862 */
863CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private fr_timer_list_t trips --fsanitize=function*/
865{
866 fr_timer_cb_t callback;
867 void *uctx;
868 fr_timer_t *ev;
869 unsigned int fired = 0;
870
871 while ((ev = timer_head(&tl->ordered))) {
872 (void)talloc_get_type_abort(ev, fr_timer_t);
873
875
876 callback = ev->callback;
877 memcpy(&uctx, &ev->uctx, sizeof(uctx));
878
879 CHECK_PARENT(ev);
880
881 /*
882 * Disarm the event before calling it.
883 *
884 * This leaves the memory in place,
885 * but dissassociates it from the list.
886 *
887 * We use the public function as it
888 * handles more cases.
889 */
890 if (!fr_cond_assert(fr_timer_disarm(ev) == 0)) return -2;
891 EVENT_DEBUG("Running timer %p", ev);
892 if (ev->free_on_fire) talloc_free(ev);
893
894 callback(tl, *when, uctx);
895
896 fired++;
897
899 }
900
902}
903
904/** Run all scheduled events in an ordered list
905 *
906 * @param[in] tl containing the timer events.
907 * @param[in] when Process events scheduled to run before or at this time.
908 * - Set to 0 if no more events.
909 * - Set to the next event time if there are more events.
910 * @return
911 * - < 0 if we failed to updated the parent list.
912 * - 0 no timer events fired.
913 * - >0 number of timer event fired.
914 */
915CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private fr_timer_list_t trips --fsanitize=function*/
917{
918 void *uctx;
919 unsigned int fired = 0;
920
921 while ((uctx = fr_rb_first(tl->shared.rb)) != NULL) {
922 fr_time_t const *next;
923
924 next = TIMER_UCTX_TO_TIME(tl, uctx);
925
926 TIMER_STOP_IF_DONE(*next);
927
928 fr_rb_remove(NULL, tl->shared.rb, uctx);
929
930 tl->shared.callback(tl, *when, uctx);
931
932 fired++;
933
935 }
936
938}
939
940
941/** Forcibly run all events in an event loop.
942 *
943 * This is used to forcefully run every event in the event loop.
944 *
945 * We pass in the real time, which may theoretically cause issues if timer
946 * callbacks are checking... But the uses of this function are very limited.
947 *
948 * @return
949 * - < 0 if we failed to update the parent list.
950 * - 0 no timer events fired.
951 * - > 0 number of timer event fired.
952 */
954{
955 fr_time_t when = fr_time_max();
956
957 return fr_timer_list_run(tl, &when);
958}
959
960/** Execute any pending events in the event loop
961 *
962 * A callback may free the list which is running the callback, or maybe free the talloc parent of the list.
963 * We can't check the list to see if it's freed, so we put the status into #timer_run_t.
964 *
965 * @param[in] tl to execute events in.
966 * @param[in] when Process events scheduled to run before or at this time.
967 * - Set to 0 if no more events.
968 * - Set to the next event time if there are more events.
969 * @return
970 * - < 0 if we failed to update the parent list.
971 * - 0 no timer events fired.
972 * - >0 number of timer event fired.
973 */
975{
976 int ret;
977 bool in_handler = tl->in_handler; /* allow nested timer execution */
978 timer_run_t run = { .prev = tl->run, .freed = false };
979
980 tl->in_handler = true;
981 tl->run = &run;
982 ret = timer_funcs[tl->type].run(tl, when, &run);
983
984 /*
985 * Has a callback freed the list? If so, stop processing it. The destructors have already
986 * removed all events from the list(s).
987 */
988 if (unlikely(run.freed)) return ret;
989
990 tl->run = run.prev;
991 tl->in_handler = in_handler;
992
993 /*
994 * Now we've executed all the pending events,
995 * now merge the deferred events into the main
996 * event list.
997 *
998 * The events don't need to be modified as they
999 * were initialised completely before being
1000 * placed in the deferred list.
1001 */
1002 if (timer_num_elements(&tl->deferred) > 0) {
1003 if (unlikely(timer_funcs[tl->type].deferred(tl) < 0)) return -1;
1004 if (unlikely(timer_list_parent_update(tl) < 0)) return -1;
1005 /*
1006 * We ran some events, and have no deferred
1007 * events to insert, so we need to forcefully
1008 * update the parent timer.
1009 */
1010 } else if (ret > 0) {
1011 if (unlikely(timer_list_parent_update(tl) < 0)) return -1;
1012 }
1013
1014 return ret;
1015}
1016
1017/** Return the head of the lst
1018 *
1019 * @param[in] tl to get the head of.
1020 * @return
1021 * - The head of the trie.
1022 * - NULL, if there's no head.
1023 */
1025{
1026 fr_timer_t *ev;
1027
1028 fr_lst_peek((void **)&ev, tl->lst);
1029
1030 return ev;
1031}
1032
1033/** Return the head of the ordered list
1034 *
1035 * @param[in] tl to get the head of.
1036 * @return
1037 * - The head of the trie.
1038 * - NULL, if there's no head.
1039 */
1041{
1042 return timer_head(&tl->ordered);
1043}
1044
1045
1046/** Move all deferred events into the lst
1047 *
1048 * @param[in] tl to move events in.
1049 * @return
1050 * - 0 on success.
1051 * - -1 on failure.
1052 */
1054{
1055 fr_timer_t *ev;
1056
1057 while((ev = timer_pop_head(&tl->deferred))) {
1058 if (unlikely(timer_lst_insert_at(tl, ev) < 0)) {
1059 timer_insert_head(&tl->deferred, ev); /* Don't lose track of events we failed to insert */
1060 return -1;
1061 }
1062 }
1063
1064 return 0;
1065}
1066
1067/** Move all deferred events into the ordered event list
1068 *
1069 * This operation is O(1).
1070 *
1071 * @param[in] tl to move events in.
1072 * @return
1073 * - 0 on success.
1074 * - -1 on failure.
1075 */
1077{
1078 fr_timer_t *ev;
1079#ifndef NDEBUG
1080 {
1081 fr_timer_t *head, *tail;
1082
1083 head = timer_head(&tl->deferred);
1084 tail = timer_tail(&tl->ordered);
1085
1086 /*
1087 * Something has gone catastrophically wrong if the
1088 * deferred event is earlier than the last event in
1089 * the ordered list, given all the checks we do.
1090 */
1091 fr_cond_assert_msg(!head || !tail || fr_time_gteq(head->when, tail->when),
1092 "Deferred event is earlier than the last event in the ordered list");
1093 }
1094#endif
1095
1096 /*
1097 * Can't use timer_move_head as entry positions
1098 * for the two lists are different.
1099 */
1100 while ((ev = timer_pop_head((&tl->deferred)))) {
1101 timer_insert_tail(&tl->ordered, ev);
1102 }
1103
1104 return 0;
1105}
1106
1107/** Move all deferred events into the shared list
1108 *
1109 * @param[in] tl to move events in.
1110 * @return
1111 * - 0 on success.
1112 * - -1 on failure.
1113 */
1115{
1116 void *uctx;
1117
1118 while((uctx = fr_rb_first(tl->shared.deferred)) != NULL) {
1120 (fr_rb_node_t *) (((uintptr_t) (uctx)) + tl->shared.node_offset));
1121
1122 fr_rb_insert(tl->shared.rb, uctx);
1123 }
1124
1125 return 0;
1126}
1127
1129{
1130 return fr_lst_num_elements(tl->lst);
1131}
1132
1134{
1135 return timer_num_elements(&tl->ordered);
1136}
1137
1139{
1140 return fr_rb_num_elements(tl->shared.rb);
1141}
1142
1143/** Disarm a timer list
1144 *
1145 * @param[in] tl Timer list to disarm
1146 * @return
1147 * - 0 on success.
1148 * - -1 on failure.
1149 */
1151{
1152 if (!tl->parent) {
1153 fr_strerror_const("Timer list does not have a parent");
1154 return -1;
1155 }
1156
1157 tl->disarmed = true;
1158
1160
1161 return 0;
1162}
1163
1164/** Arm (or re-arm) a timer list
1165 *
1166 * @param[in] tl Timer list to (re)-arm
1167 * @return
1168 * - 0 on success.
1169 * - -1 on failure.
1170 */
1172{
1173 if (!tl->parent) {
1174 fr_strerror_const("Timer list does not have a parent");
1175 return -1;
1176 }
1177
1178 if (!tl->disarmed) return 0;
1179
1180 tl->disarmed = false;
1181
1182 /*
1183 * Run any timer events which were missed during the time that the list was disarmed.
1184 */
1185 _parent_timer_cb(tl->parent, fr_time(), tl);
1186
1187 return timer_list_parent_update(tl);
1188}
1189
1190/** Return number of pending events
1191 *
1192 * @note This includes deferred events, i.e. those yet to be inserted into the main list
1193 *
1194 * @param[in] tl to get the number of events from.
1195 * @return
1196 * - The number of events in the list.
1197 */
1199{
1200 uint64_t num = timer_funcs[tl->type].num_events(tl);
1201
1202 return num + timer_num_elements(&tl->deferred);
1203}
1204
1205#ifndef NDEBUG
1206#define TIMER_EMPTY_MAX_PRINT (10)
1207
1208/** Print one timer event, and count the event as shown
1209 *
1210 * @param[in] ev to print.
1211 * @param[in,out] shown number of events printed so far.
1212 */
1213static void timer_print(fr_timer_t *ev, unsigned int *shown)
1214{
1215 FR_FAULT_LOG(" timer %p allocated at %s[%d]", ev, ev->file, ev->line);
1216 (*shown)++;
1217}
1218
1219/** Print the timer events which remain in a timer list
1220 *
1221 * At most TIMER_EMPTY_MAX_PRINT events are printed, followed by a count of the events which were not printed.
1222 *
1223 * @param[in] tl to print the events of.
1224 */
1226{
1227 uint64_t total = fr_timer_list_num_events(tl);
1228 unsigned int shown = 0;
1229 fr_timer_t *ev;
1230
1231 FR_FAULT_LOG("Timer list %p has %" PRIu64 " events remaining", tl, total);
1232
1233 switch (tl->type) {
1235 {
1236 fr_lst_iter_t iter;
1237
1238 for (ev = fr_lst_iter_init(tl->lst, &iter);
1239 ev && (shown < TIMER_EMPTY_MAX_PRINT);
1240 ev = fr_lst_iter_next(tl->lst, &iter)) {
1241 timer_print(ev, &shown);
1242 }
1243 }
1244 break;
1245
1247 for (ev = timer_head(&tl->ordered);
1248 ev && (shown < TIMER_EMPTY_MAX_PRINT);
1249 ev = timer_next(&tl->ordered, ev)) {
1250 timer_print(ev, &shown);
1251 }
1252 break;
1253
1255 /*
1256 * Shared lists hold raw pointers to the timers, so there is no file and line to print.
1257 */
1258 break;
1259 }
1260
1261 for (ev = timer_head(&tl->deferred);
1262 ev && (shown < TIMER_EMPTY_MAX_PRINT);
1263 ev = timer_next(&tl->deferred, ev)) {
1264 timer_print(ev, &shown);
1265 }
1266
1267 if (total > shown) FR_FAULT_LOG(" ... and %" PRIu64 " more events", total - shown);
1268}
1269
1270/** Assert that a timer list has no events
1271 *
1272 * If the list has events, print up to TIMER_EMPTY_MAX_PRINT of the events by file and line, and then assert.
1273 *
1274 * Only built when NDEBUG is not defined. Otherwise timer.h defines fr_timer_list_empty() as a macro which does
1275 * nothing.
1276 *
1277 * @param[in] tl to check.
1278 */
1280{
1281 if (fr_timer_list_num_events(tl) == 0) return;
1282
1284
1286}
1287#endif
1288
1290{
1291 fr_timer_t *ev;
1292
1293 switch (tl->type) {
1294 default:
1295 ev = timer_funcs[tl->type].head(tl);
1296 if (ev) return &ev->when;
1297 break;
1298
1300 void *uctx;
1301
1302 uctx = fr_rb_first(tl->shared.rb);
1303 if (!uctx) break;
1304
1305 return TIMER_UCTX_TO_TIME(tl, uctx);
1306 }
1307 }
1308
1309 return NULL;
1310}
1311
1312/** Return the time of the next event
1313 *
1314 * @param[in] tl to get the next event time from.
1315 * @return
1316 * - >0 the time of the next event.
1317 * - 0 if there are no more events.
1318 */
1320{
1321 fr_time_t const *when = timer_list_when(tl);
1322
1323 if (when) return *when;
1324
1325 return fr_time_wrap(0);
1326}
1327
1328/** Override event list time source
1329 *
1330 * @param[in] tl to set new time function for.
1331 * @param[in] func to set.
1332 */
1337
1338/** Cleanup all timers currently in the list
1339 *
1340 * If we're inside fr_timer_list_run(), then tell its on-stack variable that the list has been freed.
1341 *
1342 * @param[in] tl to cleanup.
1343 * @return
1344 * - 0 on success.
1345 * - -1 on failure.
1346 */
1348{
1349 fr_timer_t *ev;
1350 timer_run_t *run;
1351
1352 if (tl->parent_ev) if (unlikely(fr_timer_delete(&tl->parent_ev) < 0)) return -1;
1353
1354 /*
1355 * Freeing an event updates the event in the parent list. The event in the parent list was
1356 * freed above, and the list has no more use for the parent.
1357 */
1358 tl->parent = NULL;
1359
1360 if (tl->type != TIMER_LIST_TYPE_SHARED) {
1361 /*
1362 * Events which a callback inserted into the list are in the deferred list, and are not
1363 * in the main list.
1364 */
1365 while ((ev = timer_funcs[tl->type].head(tl))) {
1366 if (talloc_free(ev) < 0) return -1;
1367 }
1368
1369 while ((ev = timer_head(&tl->deferred))) {
1370 if (talloc_free(ev) < 0) return -1;
1371 }
1372 }
1373
1374 /*
1375 * Tell the parent caller(s) fr_timer_list_run() that a callback has freed the list.
1376 */
1377 for (run = tl->run; run != NULL; run = run->prev) run->freed = true;
1378
1379 return 0;
1380}
1381
1383{
1384 fr_timer_list_t *tl;
1385
1387
1388 tl = talloc_zero(ctx, fr_timer_list_t);
1389 if (unlikely(tl == NULL)) {
1390 fr_strerror_const("Out of memory");
1391 return NULL;
1392 }
1393
1394 timer_talloc_init(&tl->deferred);
1395 if (parent) {
1396 tl->parent = parent;
1397 tl->pub.time = parent->pub.time;
1398 } else {
1399 tl->pub.time = fr_time;
1400 }
1401 talloc_set_destructor(tl, _timer_list_free);
1402
1403 return tl;
1404}
1405
1406/** Allocate a new lst based timer list
1407 *
1408 * @note Entries may be inserted in any order.
1409 *
1410 * @param[in] ctx to insert head timer event into.
1411 * @param[in] parent to insert the head timer event into.
1412 */
1414{
1415 fr_timer_list_t *tl;
1416
1417 if (unlikely((tl = timer_list_alloc(ctx, parent)) == NULL)) return NULL;
1418
1419 tl->lst = fr_lst_talloc_alloc(tl, timer_cmp, fr_timer_t, lst_idx, 0);
1420 if (unlikely(tl->lst == NULL)) {
1421 fr_strerror_const("Failed allocating timer list");
1422 talloc_free(tl);
1423 return NULL;
1424 }
1426
1427#ifdef WITH_EVENT_REPORT
1428 fr_timer_in(tl, tl, &tl->report, fr_time_delta_from_sec(EVENT_REPORT_FREQ), false, fr_timer_report, NULL);
1429#endif
1430
1431 return tl;
1432}
1433
1434/** Allocate a new sorted event timer list
1435 *
1436 * @note Entries must be inserted in the order that they will fire.
1437 *
1438 * @param[in] ctx to allocate the event timer list from.
1439 * @param[in] parent to insert the head timer event into.
1440 */
1442{
1443 fr_timer_list_t *tl;
1444
1445 if (unlikely((tl = timer_list_alloc(ctx, parent)) == NULL)) return NULL;
1446
1447 fr_dlist_talloc_init((fr_dlist_head_t *)&tl->ordered, fr_timer_t, ordered_entry);
1449
1450 return tl;
1451}
1452
1453/** Allocate a new shared event timer list
1454 *
1455 * @param[in] ctx to allocate the event timer list from.
1456 * @param[in] parent to insert the head timer event into.
1457 * @param[in] cmp comparison routine (smaller times are earlier)
1458 * @param[in] callback to run on timer event
1459 * @param[in] node_offset offset from uctx to the fr_rb_node_t it contains
1460 * @param[in] time_offset offset from uctx to the fr_time_t it contains
1461 */
1463 fr_timer_cb_t callback, size_t node_offset, size_t time_offset)
1464{
1465 fr_timer_list_t *tl;
1466
1467 if (unlikely((tl = timer_list_alloc(ctx, parent)) == NULL)) return NULL;
1468
1470
1471 tl->shared.time_offset = time_offset;
1472 tl->shared.node_offset = node_offset;
1473 tl->shared.callback = callback;
1474
1475 tl->shared.rb = _fr_rb_alloc(tl, node_offset, NULL, cmp, NULL);
1476 if (!tl->shared.rb) {
1477 talloc_free(tl);
1478 return NULL;
1479 }
1480
1481 tl->shared.deferred = _fr_rb_alloc(tl, node_offset, NULL, cmp, NULL);
1482 if (!tl->shared.deferred) {
1483 talloc_free(tl);
1484 return NULL;
1485 }
1486
1487 return tl;
1488}
1489
1490/** Insert a uctx into a shared timer, and update the timer.
1491 *
1492 * @param[in] tl Timer list to insert into.
1493 * @param[in] uctx to insert
1494 * @return
1495 * - 0 on success.
1496 * - -1 on failure.
1497 */
1499{
1501
1502 if (tl->in_handler) {
1503 if (fr_rb_insert(tl->shared.deferred, uctx) != 0) return -1;
1504
1505 return 0;
1506 }
1507
1508 if (fr_rb_insert(tl->shared.rb, uctx) != 0) return -1;
1509
1510 return timer_list_parent_update(tl);
1511}
1512
1513/** Remove a uctx from a shared timer
1514 *
1515 * @param[in] tl Timer list to insert into.
1516 * @param[in] uctx to remove
1517 * @return
1518 * - 0 uctx was successfully removed.
1519 * - -1 uctx was removed, but the parent timer was not updated
1520 */
1522{
1524
1525 fr_rb_remove_by_inline_node(tl->shared.rb,
1526 (fr_rb_node_t *) (((uintptr_t) (uctx)) + tl->shared.node_offset));
1527
1528 return timer_list_parent_update(tl);
1529}
1530
1532{
1534
1535 return fr_rb_first(tl->shared.rb);
1536}
1537
1538
1539#if defined(WITH_EVENT_DEBUG) && !defined(NDEBUG)
1540static const fr_time_delta_t decades[18] = {
1541 { 1 }, { 10 }, { 100 },
1542 { 1000 }, { 10000 }, { 100000 },
1543 { 1000000 }, { 10000000 }, { 100000000 },
1544 { 1000000000 }, { 10000000000 }, { 100000000000 },
1545 { 1000000000000 }, { 10000000000000 }, { 100000000000000 },
1546 { 1000000000000000 }, { 10000000000000000 }, { 100000000000000000 },
1547};
1548
1549static const char *decade_names[18] = {
1550 "1ns", "10ns", "100ns",
1551 "1us", "10us", "100us",
1552 "1ms", "10ms", "100ms",
1553 "1s", "10s", "100s",
1554 "1Ks", "10Ks", "100Ks",
1555 "1Ms", "10Ms", "100Ms", /* 1 year is 300Ms */
1556};
1557
1558typedef struct {
1559 fr_rb_node_t node;
1560 char const *file;
1561 int line;
1563} fr_event_counter_t;
1564
1565static fr_cmp_ret_t timer_location_cmp(void const *one, void const *two)
1566{
1567 fr_event_counter_t const *a = one;
1568 fr_event_counter_t const *b = two;
1569
1570 CMP_RETURN(a, b, file);
1571
1572 return CMP(a->line, b->line);
1573}
1574
1575static int _event_report_process(fr_rb_tree_t **locations, size_t array[], fr_time_t now, fr_timer_t *ev)
1576{
1577 fr_time_delta_t diff = fr_time_sub(ev->when, now);
1578 size_t i;
1579
1580 for (i = 0; i < NUM_ELEMENTS(decades); i++) {
1581 if ((fr_time_delta_cmp(diff, decades[i]) <= 0) || (i == NUM_ELEMENTS(decades) - 1)) {
1582 fr_event_counter_t find = { .file = ev->file, .line = ev->line };
1583 fr_event_counter_t *counter;
1584
1585 fr_rb_find((void **)&counter, locations[i], &find);
1586 if (!counter) {
1587 counter = talloc(locations[i], fr_event_counter_t);
1588 if (!counter) {
1589 EVENT_DEBUG("Can't do report, out of memory");
1590 return -1;
1591 }
1592 counter->file = ev->file;
1593 counter->line = ev->line;
1594 counter->count = 1;
1595 fr_rb_insert(locations[i], counter);
1596 } else {
1597 counter->count++;
1598 }
1599
1600 array[i]++;
1601 break;
1602 }
1603 }
1604
1605 return 0;
1606}
1607
1608/** Print out information about timer events in the event loop
1609 *
1610 */
1611void fr_timer_report(fr_timer_list_t *tl, fr_time_t now, void *uctx)
1612{
1613 fr_lst_iter_t iter;
1614 fr_timer_t *ev;
1615 size_t i;
1616
1617 size_t array[NUM_ELEMENTS(decades)] = { 0 };
1618 fr_rb_tree_t *locations[NUM_ELEMENTS(decades)];
1619 TALLOC_CTX *tmp_ctx;
1620 static pthread_mutex_t print_lock = PTHREAD_MUTEX_INITIALIZER;
1621
1622 if (tl->type == TIMER_LIST_TYPE_SHARED) {
1623 EVENT_DEBUG("Cannot (yet) do timer report for TIMER_LIST_TYPE_SHARED");
1624 return;
1625 }
1626
1627 tmp_ctx = talloc_init_const("temporary stats");
1628 if (!tmp_ctx) {
1629 oom:
1630 EVENT_DEBUG("Can't do report, out of memory");
1631 talloc_free(tmp_ctx);
1632 return;
1633 }
1634
1635 for (i = 0; i < NUM_ELEMENTS(decades); i++) {
1636 locations[i] = fr_rb_inline_alloc(tmp_ctx, fr_event_counter_t, node, timer_location_cmp, NULL);
1637 if (!locations[i]) goto oom;
1638 }
1639
1640 switch (tl->type) {
1642 /*
1643 * Show which events are due, when they're due,
1644 * and where they were allocated
1645 */
1646 for (ev = fr_lst_iter_init(tl->lst, &iter);
1647 ev != NULL;
1648 ev = fr_lst_iter_next(tl->lst, &iter)) {
1649 if (_event_report_process(locations, array, now, ev) < 0) goto oom;
1650 }
1651 break;
1652
1654 /*
1655 * Show which events are due, when they're due,
1656 * and where they were allocated
1657 */
1658 for (ev = timer_head(&tl->ordered);
1659 ev != NULL;
1660 ev = timer_next(&tl->ordered, ev)) {
1661 if (_event_report_process(locations, array, now, ev) < 0) goto oom;
1662 }
1663 break;
1664
1666 fr_assert(0);
1667 return;
1668 }
1669
1670 pthread_mutex_lock(&print_lock);
1671 EVENT_DEBUG("num timer events: %"PRIu64, fr_timer_list_num_events(tl));
1672
1673 for (i = 0; i < NUM_ELEMENTS(decades); i++) {
1674 fr_rb_iter_inorder_t event_iter;
1675 void *node;
1676
1677 if (!array[i]) continue;
1678
1679 if (i == 0) {
1680 EVENT_DEBUG(" events <= %5s : %zu", decade_names[i], array[i]);
1681 } else if (i == (NUM_ELEMENTS(decades) - 1)) {
1682 EVENT_DEBUG(" events > %5s : %zu", decade_names[i - 1], array[i]);
1683 } else {
1684 EVENT_DEBUG(" events %5s - %5s : %zu", decade_names[i - 1], decade_names[i], array[i]);
1685 }
1686
1687 for (node = fr_rb_iter_init_inorder(locations[i], &event_iter);
1688 node;
1689 node = fr_rb_iter_next_inorder(locations[i], &event_iter)) {
1690 fr_event_counter_t *counter = talloc_get_type_abort(node, fr_event_counter_t);
1691
1692 EVENT_DEBUG(" : %u allocd at %s[%d]",
1693 counter->count, counter->file, counter->line);
1694 }
1695 }
1696 pthread_mutex_unlock(&print_lock);
1697
1698 fr_timer_in(tl, tl, &tl->report, fr_time_delta_from_sec(EVENT_REPORT_FREQ), false, fr_timer_report, uctx);
1699 talloc_free(tmp_ctx);
1700}
1701
1702void fr_timer_dump(fr_timer_list_t *tl)
1703{
1704 fr_lst_iter_t iter;
1705 fr_timer_t *ev;
1706 fr_time_t now = tl->pub.time(); /* Get the current time */
1707
1708#define TIMER_DUMP(_ev) \
1709 EVENT_DEBUG("%s[%d]: %p time=%" PRId64 " (%c), callback=%p", \
1710 (_ev)->file, (_ev)->line, _ev, fr_time_unwrap((_ev)->when), \
1711 fr_time_gt(now, (_ev)->when) ? '<' : '>', (_ev)->callback);
1712
1713 EVENT_DEBUG("Time is now %"PRId64"", fr_time_unwrap(now));
1714
1715 switch (tl->type) {
1717 EVENT_DEBUG("Dumping lst timer list");
1718
1719 for (ev = fr_lst_iter_init(tl->lst, &iter);
1720 ev;
1721 ev = fr_lst_iter_next(tl->lst, &iter)) {
1722 (void)talloc_get_type_abort(ev, fr_timer_t);
1723 TIMER_DUMP(ev);
1724 }
1725 break;
1726
1728 EVENT_DEBUG("Dumping ordered timer list");
1729
1730 for (ev = timer_head(&tl->ordered);
1731 ev;
1732 ev = timer_next(&tl->ordered, ev)) {
1733 (void)talloc_get_type_abort(ev, fr_timer_t);
1734 TIMER_DUMP(ev);
1735 }
1736 break;
1737
1739 EVENT_DEBUG("Dumping shared timer list");
1740
1741 fr_rb_inorder_foreach(tl->shared.rb, void, uctx) {
1742 EVENT_DEBUG("time %" PRIu64" uctx %p", fr_time_unwrap(*TIMER_UCTX_TO_TIME(tl, uctx)), uctx);
1743 }}
1744 break;
1745 }
1746}
1747#endif
int const char * file
Definition acutest.h:855
void int line
Definition acutest.h:810
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:203
#define NDEBUG_LOCATION_FMT
Definition build.h:363
#define CC_NO_UBSAN(_sanitize)
Definition build.h:540
#define CMP_RETURN(_a, _b, _field)
Return if the comparison is not 0 (is unequal)
Definition build.h:139
#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:130
#define unlikely(_x)
Definition build.h:492
#define NDEBUG_LOCATION_VALS
Definition build.h:362
#define NDEBUG_LOCATION_ARGS
Pass caller information to the function.
Definition build.h:361
#define UNUSED
Definition build.h:415
#define NUM_ELEMENTS(_t)
Definition build.h:443
fr_cmp_ret_t
Result of an ordering comparison.
Definition build.h:113
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:177
#define FR_FAULT_LOG(_fmt,...)
Definition debug.h:52
#define fr_cond_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:194
#define FR_DLIST_TYPES(_name)
Define type specific wrapper structs for dlists.
Definition dlist.h:1150
#define FR_DLIST_FUNCS(_name, _element_type, _element_entry)
Define type specific wrapper functions for dlists.
Definition dlist.h:1173
#define fr_dlist_talloc_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:257
#define FR_DLIST_TYPEDEFS(_name, _head, _entry)
Define friendly names for type specific dlist head and entry structures.
Definition dlist.h:1160
Head of a doubly linked list.
Definition dlist.h:51
Entry in a doubly linked list.
Definition dlist.h:41
#define EVENT_DEBUG(...)
Definition event.h:65
talloc_free(hp)
#define fr_time()
Definition event.c:61
void * fr_lst_iter_next(fr_lst_t *lst, fr_lst_iter_t *iter)
Get the next entry in an LST.
Definition lst.c:802
int fr_lst_extract(fr_lst_t *lst, void *data)
Remove an element from an LST.
Definition lst.c:734
void * fr_lst_iter_init(fr_lst_t *lst, fr_lst_iter_t *iter)
Iterate over entries in LST.
Definition lst.c:783
static void * item(fr_lst_t const *lst, fr_lst_index_t idx)
Definition lst.c:121
int fr_lst_insert(fr_lst_t *lst, void *data)
Definition lst.c:749
unsigned int fr_lst_num_elements(fr_lst_t *lst)
Definition lst.c:767
int fr_lst_peek(void **out, fr_lst_t *lst)
Definition lst.c:719
Definition lst.c:59
#define fr_lst_talloc_alloc(_ctx, _cmp, _talloc_type, _field, _init)
Creates an LST that verifies elements are of a specific talloc type.
Definition lst.h:75
fr_lst_index_t fr_lst_iter_t
Definition lst.h:46
unsigned int fr_lst_index_t
Definition lst.h:44
unsigned int uint32_t
unsigned char uint8_t
fr_cmp_ret_t(* fr_cmp_t)(void const *a, void const *b)
Definition misc.h:40
#define fr_assert(_expr)
Definition rad_assert.h:37
uint32_t fr_rb_num_elements(fr_rb_tree_t *tree)
Return how many nodes there are in a tree.
Definition rb.c:807
int fr_rb_remove(void **removed, fr_rb_tree_t *tree, void const *data)
Remove an entry from the tree, without freeing the data.
Definition rb.c:718
int fr_rb_find(void **found, fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
Definition rb.c:586
void * fr_rb_iter_init_inorder(fr_rb_tree_t *tree, fr_rb_iter_inorder_t *iter)
Initialise an in-order iterator.
Definition rb.c:850
fr_rb_tree_t * _fr_rb_alloc(TALLOC_CTX *ctx, ssize_t offset, char const *type, fr_cmp_t data_cmp, fr_free_t data_free)
Alloc a new RED-BLACK tree.
Definition rb.c:202
void * fr_rb_remove_by_inline_node(fr_rb_tree_t *tree, fr_rb_node_t *node)
Remove an entry from the tree, using the node structure, without freeing the data.
Definition rb.c:748
void * fr_rb_first(fr_rb_tree_t *tree)
Definition rb.c:812
int fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
Definition rb.c:637
void * fr_rb_iter_next_inorder(UNUSED fr_rb_tree_t *tree, fr_rb_iter_inorder_t *iter)
Return the next node.
Definition rb.c:876
#define fr_rb_inline_alloc(_ctx, _type, _field, _data_cmp, _data_free)
Allocs a red black tree.
Definition rb.h:269
#define fr_rb_inorder_foreach(_tree, _type, _iter)
Definition rb.h:330
Iterator structure for in-order traversal of an rbtree.
Definition rb.h:319
The main red black tree structure.
Definition rb.h:71
return count
Definition module.c:173
int talloc_link_ctx(TALLOC_CTX *parent, TALLOC_CTX *child)
Link two different parent and child contexts, so the child is freed before the parent.
Definition talloc.c:168
static TALLOC_CTX * talloc_init_const(char const *name)
Allocate a top level chunk with a constant name.
Definition talloc.h:127
#define fr_time_gteq(_a, _b)
Definition time.h:238
static int64_t fr_time_unwrap(fr_time_t time)
Definition time.h:146
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:599
#define fr_time_delta_wrap(_time)
Definition time.h:152
#define fr_time_wrap(_time)
Definition time.h:145
#define fr_time_eq(_a, _b)
Definition time.h:241
static fr_cmp_ret_t fr_time_cmp(fr_time_t a, fr_time_t b)
Compare two fr_time_t values.
Definition time.h:934
#define fr_time_add(_a, _b)
Add a time/time delta together.
Definition time.h:196
static fr_cmp_ret_t fr_time_delta_cmp(fr_time_delta_t a, fr_time_delta_t b)
Compare two fr_time_delta_t values.
Definition time.h:948
#define fr_time_sub(_a, _b)
Subtract one time from another.
Definition time.h:229
#define fr_time_lt(_a, _b)
Definition time.h:239
#define fr_time_max()
Definition time.h:143
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
void fr_timer_list_empty(fr_timer_list_t *tl)
Assert that a timer list has no events.
Definition timer.c:1279
TALLOC_CTX * linked_ctx
talloc ctx this event was bound to.
Definition timer.c:106
int fr_timer_list_run(fr_timer_list_t *tl, fr_time_t *when)
Execute any pending events in the event loop.
Definition timer.c:974
static uint64_t timer_list_ordered_num_events(fr_timer_list_t *tl)
Definition timer.c:1133
static fr_timer_list_t * timer_list_alloc(TALLOC_CTX *ctx, fr_timer_list_t *parent)
Definition timer.c:1382
int fr_timer_list_disarm(fr_timer_list_t *tl)
Disarm a timer list.
Definition timer.c:1150
int(* timer_disarm_t)(fr_timer_t *ev)
Specialisation function to delete a timer.
Definition timer.c:186
timer_head_t deferred
A list of timer events to be inserted, after the current batch has been processed.
Definition timer.c:84
fr_time_t fr_timer_when(fr_timer_t *ev)
Internal timestamp representing when the timer should fire.
Definition timer.c:771
uint64_t(* timer_list_num_elements_t)(fr_timer_list_t *tl)
Return the number of elements in the list.
Definition timer.c:224
timer_list_num_elements_t num_events
Function to get the number of elements in the list.
Definition timer.c:233
timer_list_type_t type
Definition timer.c:79
uint64_t fr_timer_list_num_events(fr_timer_list_t *tl)
Return number of pending events.
Definition timer.c:1198
fr_timer_t ** parent
A pointer to the parent structure containing the timer event.
Definition timer.c:108
static int timer_list_lst_deferred(fr_timer_list_t *tl)
Move all deferred events into the lst.
Definition timer.c:1053
static void _parent_timer_cb(UNUSED fr_timer_list_t *parent_tl, fr_time_t when, void *uctx)
This callback fires in the parent to execute events in this sublist.
Definition timer.c:317
int fr_timer_uctx_insert(fr_timer_list_t *tl, void *uctx)
Insert a uctx into a shared timer, and update the timer.
Definition timer.c:1498
static int _timer_free(fr_timer_t *ev)
Remove an event from the event loop.
Definition timer.c:439
static int timer_ordered_insert_at(fr_timer_list_t *tl, fr_timer_t *ev)
Insert an event into an ordered timer list.
Definition timer.c:414
static int timer_list_lst_run(fr_timer_list_t *tl, fr_time_t *when, timer_run_t *run)
Run all scheduled timer events in a lst.
Definition timer.c:810
bool freed
A callback freed the list.
Definition timer.c:59
fr_time_t fr_timer_list_when(fr_timer_list_t *tl)
Return the time of the next event.
Definition timer.c:1319
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:1441
static int timer_lst_insert_at(fr_timer_list_t *tl, fr_timer_t *ev)
Insert a timer event into a single event timer list.
Definition timer.c:394
#define TIMER_RETURN_IF_FREED
Definition timer.c:161
bool _fr_timer_armed(fr_timer_t *ev)
Check if a timer event is armed.
Definition timer.c:794
fr_timer_t *(* timer_list_head_t)(fr_timer_list_t *tl)
Return the soonest timer event.
Definition timer.c:207
fr_time_delta_t fr_timer_remaining(fr_timer_t *ev)
Return time delta between now and when the timer should fire.
Definition timer.c:781
timer_disarm_t disarm
Function to delete a timer event.
Definition timer.c:228
static void timer_print(fr_timer_t *ev, unsigned int *shown)
Print one timer event, and count the event as shown.
Definition timer.c:1213
#define TIMER_RETURN_DONE
Definition timer.c:150
timer_run_t * run
Track whether we freed the list in local variables.
Definition timer.c:81
static int _timer_list_free(fr_timer_list_t *tl)
Cleanup all timers currently in the list.
Definition timer.c:1347
void const * uctx
Context pointer to pass to the callback.
Definition timer.c:104
timer_list_head_t head
Function to get the head of the list.
Definition timer.c:231
int fr_timer_list_force_run(fr_timer_list_t *tl)
Forcibly run all events in an event loop.
Definition timer.c:953
static int timer_list_shared_run(fr_timer_list_t *tl, fr_time_t *when, timer_run_t *run)
Run all scheduled events in an ordered list.
Definition timer.c:916
fr_timer_cb_t callback
Callback to execute when the timer fires.
Definition timer.c:103
static int timer_lst_disarm(fr_timer_t *ev)
Definition timer.c:645
bool disarmed
the entire timer list is disarmed
Definition timer.c:82
int(* timer_list_deferred_t)(fr_timer_list_t *tl)
Process any deferred timer events.
Definition timer.c:216
fr_time_t when
When this timer should fire.
Definition timer.c:101
struct fr_timer_list_pub_s pub
Public interface to the event timer list.
Definition timer.c:66
int _fr_timer_in(NDEBUG_LOCATION_ARGS TALLOC_CTX *ctx, fr_timer_list_t *tl, fr_timer_t **ev_p, fr_time_delta_t delta, bool free_on_fire, fr_timer_cb_t callback, void const *uctx)
Insert a timer event into an event list.
Definition timer.c:635
static int timer_list_parent_update(fr_timer_list_t *tl)
Utility function to update parent timers.
Definition timer.c:337
static fr_cmp_ret_t timer_cmp(void const *a, void const *b)
Compare two timer events to see which one should occur first.
Definition timer.c:303
timer_insert_t insert
Function to insert a timer event.
Definition timer.c:227
fr_timer_t * parent_ev
Event in the parent's event loop.
Definition timer.c:90
bool free_on_fire
Whether to free the event when it fires.
Definition timer.c:116
fr_timer_entry_t entry
Entry in a list of timer events.
Definition timer.c:111
static int timer_list_shared_deferred(fr_timer_list_t *tl)
Move all deferred events into the shared list.
Definition timer.c:1114
fr_timer_list_t * fr_timer_list_shared_alloc(TALLOC_CTX *ctx, fr_timer_list_t *parent, fr_cmp_t cmp, fr_timer_cb_t callback, size_t node_offset, size_t time_offset)
Allocate a new shared event timer list.
Definition timer.c:1462
fr_timer_list_t * fr_timer_list_lst_alloc(TALLOC_CTX *ctx, fr_timer_list_t *parent)
Allocate a new lst based timer list.
Definition timer.c:1413
static timer_list_funcs_t const timer_funcs[]
Functions for performing operations on various types of timer list.
Definition timer.c:264
static int timer_ordered_disarm(fr_timer_t *ev)
Remove a timer from a timer list, but don't free it.
Definition timer.c:681
int fr_timer_disarm(fr_timer_t *ev)
Remove an event from the event list, but don't free the memory.
Definition timer.c:707
static void timer_list_print_remaining(fr_timer_list_t *tl)
Print the timer events which remain in a timer list.
Definition timer.c:1225
fr_timer_list_t * parent
Parent list to insert event into (if any).
Definition timer.c:89
#define CHECK_PARENT(_ev)
Definition timer.c:130
static fr_time_t * timer_list_when(fr_timer_list_t *tl)
Definition timer.c:1289
static fr_timer_t * timer_list_lst_head(fr_timer_list_t *tl)
Return the head of the lst.
Definition timer.c:1024
static uint64_t timer_list_lst_num_events(fr_timer_list_t *tl)
Definition timer.c:1128
int fr_timer_uctx_remove(fr_timer_list_t *tl, void *uctx)
Remove a uctx from a shared timer.
Definition timer.c:1521
int line
Line this event was last updated on.
Definition timer.c:124
void fr_timer_list_set_time_func(fr_timer_list_t *tl, fr_event_time_source_t func)
Override event list time source.
Definition timer.c:1333
#define TIMER_STOP_IF_DONE(_next)
Definition timer.c:142
timer_list_deferred_t deferred
Function to process deferred events.
Definition timer.c:232
void * fr_timer_uctx_peek(fr_timer_list_t *tl)
Definition timer.c:1531
static fr_timer_t * timer_list_ordered_head(fr_timer_list_t *tl)
Return the head of the ordered list.
Definition timer.c:1040
static int timer_list_ordered_run(fr_timer_list_t *tl, fr_time_t *when, timer_run_t *run)
Run all scheduled events in an ordered list.
Definition timer.c:864
#define TIMER_UCTX_TO_TIME(_tl, _x)
Definition timer.c:134
int fr_timer_list_arm(fr_timer_list_t *tl)
Arm (or re-arm) a timer list.
Definition timer.c:1171
bool in_handler
Whether we're currently in a callback.
Definition timer.c:80
timer_run_t * prev
The frame of the enclosing call, when a callback runs the same list again.
Definition timer.c:57
#define EVENT_ARMED(_ev)
Definition timer.c:236
int fr_timer_delete(fr_timer_t **ev_p)
Delete a timer event and free its memory.
Definition timer.c:744
timer_list_type_t
What type of event list the timer is inserted into.
Definition timer.c:40
@ TIMER_LIST_TYPE_SHARED
all events share one event callback
Definition timer.c:43
@ TIMER_LIST_TYPE_LST
Self-sorting timer list based on a left leaning skeleton tree.
Definition timer.c:41
@ TIMER_LIST_TYPE_ORDERED
Strictly ordered list of events in a dlist.
Definition timer.c:42
char const * file
Source file this event was last updated in.
Definition timer.c:123
#define TIMER_EMPTY_MAX_PRINT
Definition timer.c:1206
fr_timer_list_t * tl
The event list this timer is part of.
Definition timer.c:118
static int timer_list_ordered_deferred(fr_timer_list_t *tl)
Move all deferred events into the ordered event list.
Definition timer.c:1076
int(* timer_insert_t)(fr_timer_list_t *tl, fr_timer_t *ev)
Specialisation function to insert a timer.
Definition timer.c:177
timer_list_run_t run
Function to run a timer event.
Definition timer.c:230
static uint64_t timer_list_shared_num_events(fr_timer_list_t *tl)
Definition timer.c:1138
int _fr_timer_at(NDEBUG_LOCATION_ARGS TALLOC_CTX *ctx, fr_timer_list_t *tl, fr_timer_t **ev_p, fr_time_t when, bool free_on_fire, fr_timer_cb_t callback, void const *uctx)
Insert a timer event into an event list.
Definition timer.c:473
int(* timer_list_run_t)(fr_timer_list_t *tl, fr_time_t *when, timer_run_t *run)
Specialisation function to execute any pending timers.
Definition timer.c:198
An event timer list.
Definition timer.c:65
A timer event.
Definition timer.c:100
#define FR_TIMER_DISARM_RETURN(_ev)
Definition timer.h:98
#define fr_timer_in(...)
Definition timer.h:87
fr_time_t(* fr_event_time_source_t)(void)
Alternative time source, useful for testing.
Definition timer.h:52
void(* fr_timer_cb_t)(fr_timer_list_t *tl, fr_time_t now, void *uctx)
Called when a timer event fires.
Definition timer.h:75
static bool fr_timer_armed(fr_timer_t *ev)
Definition timer.h:124
#define fr_timer_at(...)
Definition timer.h:81
fr_event_time_source_t _CONST time
Time source this list uses to get the current time when calculating deltas (fr_timer_in).
Definition timer.h:61
Public event timer list structure.
Definition timer.h:60
static fr_time_delta_t time_offset
static fr_slen_t head
Definition xlat.h:410
static fr_slen_t parent
Definition pair.h:864
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:558
char const * fr_strerror_peek(void)
Get the last library error.
Definition strerror.c:631
#define fr_strerror_const_push(_msg)
Definition strerror.h:227
#define fr_strerror_const(_msg)
Definition strerror.h:223