2017-05-30 17:12:35 +00:00
|
|
|
/*
|
|
|
|
* BIRD -- Timers
|
|
|
|
*
|
|
|
|
* (c) 2013--2017 Ondrej Zajicek <santiago@crfreenet.org>
|
|
|
|
* (c) 2013--2017 CZ.NIC z.s.p.o.
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2017-06-21 13:38:11 +00:00
|
|
|
/**
|
|
|
|
* DOC: Timers
|
|
|
|
*
|
|
|
|
* Timers are resources which represent a wish of a module to call a function at
|
|
|
|
* the specified time. The timer code does not guarantee exact timing, only that
|
|
|
|
* a timer function will not be called before the requested time.
|
|
|
|
*
|
|
|
|
* In BIRD, time is represented by values of the &btime type which is signed
|
|
|
|
* 64-bit integer interpreted as a relative number of microseconds since some
|
|
|
|
* fixed time point in past. The current time can be obtained by current_time()
|
|
|
|
* function with reasonable accuracy and is monotonic. There is also a current
|
|
|
|
* 'wall-clock' real time obtainable by current_real_time() reported by OS.
|
|
|
|
*
|
|
|
|
* Each timer is described by a &timer structure containing a pointer to the
|
|
|
|
* handler function (@hook), data private to this function (@data), time the
|
|
|
|
* function should be called at (@expires, 0 for inactive timers), for the other
|
|
|
|
* fields see |timer.h|.
|
|
|
|
*/
|
2017-05-30 17:12:35 +00:00
|
|
|
|
2017-06-06 14:47:30 +00:00
|
|
|
#include <stdio.h>
|
2017-05-30 17:12:35 +00:00
|
|
|
#include <stdlib.h>
|
2017-12-14 20:52:07 +00:00
|
|
|
#include <time.h>
|
2017-05-30 17:12:35 +00:00
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
|
|
|
|
#include "lib/heap.h"
|
|
|
|
#include "lib/resource.h"
|
|
|
|
#include "lib/timer.h"
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2021-08-04 20:48:51 +00:00
|
|
|
_Atomic btime last_time;
|
|
|
|
_Atomic btime real_time;
|
2017-05-30 17:12:35 +00:00
|
|
|
|
|
|
|
void wakeup_kick_current(void);
|
|
|
|
|
|
|
|
|
|
|
|
#define TIMER_LESS(a,b) ((a)->expires < (b)->expires)
|
|
|
|
#define TIMER_SWAP(heap,a,b,t) (t = heap[a], heap[a] = heap[b], heap[b] = t, \
|
|
|
|
heap[a]->index = (a), heap[b]->index = (b))
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2017-11-28 16:43:20 +00:00
|
|
|
tm_free(resource *r)
|
2017-05-30 17:12:35 +00:00
|
|
|
{
|
2017-11-28 16:43:20 +00:00
|
|
|
timer *t = (void *) r;
|
2017-05-30 17:12:35 +00:00
|
|
|
|
2017-11-28 16:43:20 +00:00
|
|
|
tm_stop(t);
|
2017-05-30 17:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-02-28 09:42:47 +00:00
|
|
|
tm_dump(resource *r, unsigned indent UNUSED)
|
2017-05-30 17:12:35 +00:00
|
|
|
{
|
2017-11-28 16:43:20 +00:00
|
|
|
timer *t = (void *) r;
|
2017-05-30 17:12:35 +00:00
|
|
|
|
|
|
|
debug("(code %p, data %p, ", t->hook, t->data);
|
|
|
|
if (t->randomize)
|
|
|
|
debug("rand %d, ", t->randomize);
|
|
|
|
if (t->recurrent)
|
|
|
|
debug("recur %d, ", t->recurrent);
|
|
|
|
if (t->expires)
|
2021-06-19 18:50:18 +00:00
|
|
|
debug("in loop %p expires in %d ms)\n", t->loop, (t->expires - current_time()) TO_MS);
|
2017-05-30 17:12:35 +00:00
|
|
|
else
|
|
|
|
debug("inactive)\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-28 16:43:20 +00:00
|
|
|
static struct resclass tm_class = {
|
2017-05-30 17:12:35 +00:00
|
|
|
"Timer",
|
2017-11-28 16:43:20 +00:00
|
|
|
sizeof(timer),
|
|
|
|
tm_free,
|
|
|
|
tm_dump,
|
2017-05-30 17:12:35 +00:00
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2017-11-28 16:43:20 +00:00
|
|
|
timer *
|
|
|
|
tm_new(pool *p)
|
2017-05-30 17:12:35 +00:00
|
|
|
{
|
2017-11-28 16:43:20 +00:00
|
|
|
timer *t = ralloc(p, &tm_class);
|
2017-05-30 17:12:35 +00:00
|
|
|
t->index = -1;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2021-06-19 18:50:18 +00:00
|
|
|
static void
|
|
|
|
tm_set_in_tl(timer *t, btime when, struct timeloop *local_timeloop)
|
2017-05-30 17:12:35 +00:00
|
|
|
{
|
2021-06-18 16:10:42 +00:00
|
|
|
uint tc = timers_count(local_timeloop);
|
2017-05-30 17:12:35 +00:00
|
|
|
|
|
|
|
if (!t->expires)
|
|
|
|
{
|
|
|
|
t->index = ++tc;
|
|
|
|
t->expires = when;
|
2021-06-18 16:10:42 +00:00
|
|
|
BUFFER_PUSH(local_timeloop->timers) = t;
|
|
|
|
HEAP_INSERT(local_timeloop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP);
|
2017-05-30 17:12:35 +00:00
|
|
|
}
|
|
|
|
else if (t->expires < when)
|
|
|
|
{
|
|
|
|
t->expires = when;
|
2021-06-18 16:10:42 +00:00
|
|
|
HEAP_INCREASE(local_timeloop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP, t->index);
|
2017-05-30 17:12:35 +00:00
|
|
|
}
|
|
|
|
else if (t->expires > when)
|
|
|
|
{
|
|
|
|
t->expires = when;
|
2021-06-18 16:10:42 +00:00
|
|
|
HEAP_DECREASE(local_timeloop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP, t->index);
|
2017-05-30 17:12:35 +00:00
|
|
|
}
|
|
|
|
|
2021-06-19 18:50:18 +00:00
|
|
|
t->loop = local_timeloop;
|
|
|
|
|
2022-07-28 17:49:03 +00:00
|
|
|
if (t->index == 1)
|
2021-06-19 18:50:18 +00:00
|
|
|
birdloop_ping(local_timeloop->loop);
|
2017-05-30 17:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-06-19 18:50:18 +00:00
|
|
|
tm_set_in(timer *t, btime when, struct birdloop *loop)
|
2017-05-30 17:12:35 +00:00
|
|
|
{
|
2021-06-19 18:50:18 +00:00
|
|
|
ASSERT_DIE(birdloop_inside(loop));
|
|
|
|
tm_set_in_tl(t, when, birdloop_time_loop(loop));
|
2017-05-30 17:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-11-28 16:43:20 +00:00
|
|
|
tm_stop(timer *t)
|
2017-05-30 17:12:35 +00:00
|
|
|
{
|
|
|
|
if (!t->expires)
|
|
|
|
return;
|
|
|
|
|
2021-06-19 18:50:18 +00:00
|
|
|
TLOCK_TIMER_ASSERT(t->loop);
|
2017-05-30 17:12:35 +00:00
|
|
|
|
2021-06-19 18:50:18 +00:00
|
|
|
uint tc = timers_count(t->loop);
|
|
|
|
|
|
|
|
HEAP_DELETE(t->loop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP, t->index);
|
|
|
|
BUFFER_POP(t->loop->timers);
|
2017-05-30 17:12:35 +00:00
|
|
|
|
|
|
|
t->index = -1;
|
|
|
|
t->expires = 0;
|
2021-06-19 18:50:18 +00:00
|
|
|
t->loop = NULL;
|
2017-05-30 17:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
timers_init(struct timeloop *loop, pool *p)
|
|
|
|
{
|
2021-06-19 18:50:18 +00:00
|
|
|
TLOCK_TIMER_ASSERT(loop);
|
|
|
|
|
2017-05-30 17:12:35 +00:00
|
|
|
BUFFER_INIT(loop->timers, p, 4);
|
|
|
|
BUFFER_PUSH(loop->timers) = NULL;
|
|
|
|
}
|
|
|
|
|
2017-05-31 13:46:04 +00:00
|
|
|
void io_log_event(void *hook, void *data);
|
|
|
|
|
2017-05-30 17:12:35 +00:00
|
|
|
void
|
2021-06-19 18:50:18 +00:00
|
|
|
timers_fire(struct timeloop *loop, int io_log)
|
2017-05-30 17:12:35 +00:00
|
|
|
{
|
2021-06-19 18:50:18 +00:00
|
|
|
TLOCK_TIMER_ASSERT(loop);
|
|
|
|
|
2017-05-30 17:12:35 +00:00
|
|
|
btime base_time;
|
2017-11-28 16:43:20 +00:00
|
|
|
timer *t;
|
2017-05-30 17:12:35 +00:00
|
|
|
|
2021-08-04 20:48:51 +00:00
|
|
|
times_update();
|
|
|
|
base_time = current_time();
|
2017-05-30 17:12:35 +00:00
|
|
|
|
|
|
|
while (t = timers_first(loop))
|
|
|
|
{
|
|
|
|
if (t->expires > base_time)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (t->recurrent)
|
|
|
|
{
|
|
|
|
btime when = t->expires + t->recurrent;
|
|
|
|
|
2021-08-04 20:48:51 +00:00
|
|
|
if (when <= base_time)
|
|
|
|
when = base_time + t->recurrent;
|
2017-05-30 17:12:35 +00:00
|
|
|
|
|
|
|
if (t->randomize)
|
|
|
|
when += random() % (t->randomize + 1);
|
|
|
|
|
2021-06-19 18:50:18 +00:00
|
|
|
tm_set_in_tl(t, when, loop);
|
2017-05-30 17:12:35 +00:00
|
|
|
}
|
|
|
|
else
|
2017-11-28 16:43:20 +00:00
|
|
|
tm_stop(t);
|
2017-05-30 17:12:35 +00:00
|
|
|
|
2017-05-31 13:46:04 +00:00
|
|
|
/* This is ugly hack, we want to log just timers executed from the main I/O loop */
|
2021-06-19 18:50:18 +00:00
|
|
|
if (io_log)
|
2017-05-31 13:46:04 +00:00
|
|
|
io_log_event(t->hook, t->data);
|
|
|
|
|
2017-05-30 17:12:35 +00:00
|
|
|
t->hook(t);
|
2022-03-02 09:35:21 +00:00
|
|
|
tmp_flush();
|
2017-05-30 17:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 14:47:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* tm_parse_time - parse a date and time
|
|
|
|
* @x: time string
|
|
|
|
*
|
|
|
|
* tm_parse_time() takes a textual representation of a date and time
|
|
|
|
* (yyyy-mm-dd[ hh:mm:ss[.sss]]) and converts it to the corresponding value of
|
|
|
|
* type &btime.
|
|
|
|
*/
|
|
|
|
btime
|
2020-04-08 20:25:15 +00:00
|
|
|
tm_parse_time(const char *x)
|
2017-06-06 14:47:30 +00:00
|
|
|
{
|
2019-08-17 14:09:29 +00:00
|
|
|
struct tm tm = {};
|
2017-06-06 14:47:30 +00:00
|
|
|
int usec, n1, n2, n3, r;
|
|
|
|
|
|
|
|
r = sscanf(x, "%d-%d-%d%n %d:%d:%d%n.%d%n",
|
|
|
|
&tm.tm_year, &tm.tm_mon, &tm.tm_mday, &n1,
|
|
|
|
&tm.tm_hour, &tm.tm_min, &tm.tm_sec, &n2,
|
|
|
|
&usec, &n3);
|
|
|
|
|
|
|
|
if ((r == 3) && !x[n1])
|
|
|
|
tm.tm_hour = tm.tm_min = tm.tm_sec = usec = 0;
|
|
|
|
else if ((r == 6) && !x[n2])
|
|
|
|
usec = 0;
|
|
|
|
else if ((r == 7) && !x[n3])
|
|
|
|
{
|
|
|
|
/* Convert subsecond digits to proper precision */
|
|
|
|
int digits = n3 - n2 - 1;
|
|
|
|
if ((usec < 0) || (usec > 999999) || (digits < 1) || (digits > 6))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while (digits++ < 6)
|
|
|
|
usec *= 10;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
tm.tm_mon--;
|
|
|
|
tm.tm_year -= 1900;
|
|
|
|
s64 ts = mktime(&tm);
|
|
|
|
if ((ts == (s64) (time_t) -1) || (ts < 0) || (ts > ((s64) 1 << 40)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return ts S + usec;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tm_format_time - convert date and time to textual representation
|
|
|
|
* @x: destination buffer of size %TM_DATETIME_BUFFER_SIZE
|
|
|
|
* @fmt: specification of resulting textual representation of the time
|
|
|
|
* @t: time
|
|
|
|
*
|
|
|
|
* This function formats the given relative time value @t to a textual
|
|
|
|
* date/time representation (dd-mm-yyyy hh:mm:ss) in real time.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
tm_format_time(char *x, struct timeformat *fmt, btime t)
|
|
|
|
{
|
|
|
|
btime dt = current_time() - t;
|
|
|
|
btime rt = current_real_time() - dt;
|
|
|
|
int v1 = !fmt->limit || (dt < fmt->limit);
|
|
|
|
|
2018-11-20 16:38:19 +00:00
|
|
|
if (!tm_format_real_time(x, TM_DATETIME_BUFFER_SIZE, v1 ? fmt->fmt1 : fmt->fmt2, rt))
|
|
|
|
strcpy(x, "<error>");
|
2017-06-06 14:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Replace %f in format string with usec scaled to requested precision */
|
|
|
|
static int
|
|
|
|
strfusec(char *buf, int size, const char *fmt, uint usec)
|
|
|
|
{
|
|
|
|
char *str = buf;
|
|
|
|
int parity = 0;
|
|
|
|
|
|
|
|
while (*fmt)
|
|
|
|
{
|
|
|
|
if (!size)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((fmt[0] == '%') && (!parity) &&
|
|
|
|
((fmt[1] == 'f') || (fmt[1] >= '1') && (fmt[1] <= '6') && (fmt[2] == 'f')))
|
|
|
|
{
|
|
|
|
int digits = (fmt[1] == 'f') ? 6 : (fmt[1] - '0');
|
|
|
|
uint d = digits, u = usec;
|
|
|
|
|
|
|
|
/* Convert microseconds to requested precision */
|
|
|
|
while (d++ < 6)
|
|
|
|
u /= 10;
|
|
|
|
|
|
|
|
int num = bsnprintf(str, size, "%0*u", digits, u);
|
|
|
|
if (num < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fmt += (fmt[1] == 'f') ? 2 : 3;
|
|
|
|
ADVANCE(str, size, num);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Handle '%%' expression */
|
|
|
|
parity = (*fmt == '%') ? !parity : 0;
|
|
|
|
*str++ = *fmt++;
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!size)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*str = 0;
|
|
|
|
return str - buf;
|
|
|
|
}
|
|
|
|
|
2018-11-20 16:38:19 +00:00
|
|
|
int
|
|
|
|
tm_format_real_time(char *x, size_t max, const char *fmt, btime t)
|
2017-06-06 14:47:30 +00:00
|
|
|
{
|
|
|
|
s64 t1 = t TO_S;
|
|
|
|
s64 t2 = t - t1 S;
|
|
|
|
|
|
|
|
time_t ts = t1;
|
|
|
|
struct tm tm;
|
|
|
|
if (!localtime_r(&ts, &tm))
|
2018-11-20 16:38:19 +00:00
|
|
|
return 0;
|
2017-06-06 14:47:30 +00:00
|
|
|
|
2021-04-12 15:01:31 +00:00
|
|
|
size_t tbuf_size = MIN(max, 4096);
|
|
|
|
byte *tbuf = alloca(tbuf_size);
|
|
|
|
if (!strfusec(tbuf, tbuf_size, fmt, t2))
|
2018-11-20 16:38:19 +00:00
|
|
|
return 0;
|
2017-06-06 14:47:30 +00:00
|
|
|
|
2023-08-16 13:05:36 +00:00
|
|
|
return strftime(x, max, tbuf, &tm);
|
2017-06-06 14:47:30 +00:00
|
|
|
}
|