41 #include <sys/param.h> 63 tv->tv_usec = 1e6 * (t - tv->tv_sec);
64 while (tv->tv_usec >= 1000000)
66 tv->tv_usec -= 1000000;
78 return dt < 0 ? -1 : (dt > 0 ? +1 : 0);
87 #define TIMER_SIGNAL SIGALRM 114 ASSERT (now >= 0 && finite (now));
123 _vec_len (timers) -= 1;
129 struct itimerval itv;
130 memset (&itv, 0,
sizeof (itv));
132 if (setitimer (ITIMER_REAL, &itv, 0) < 0)
142 sigset_t block_timer;
144 memset (&block_timer, 0,
sizeof (block_timer));
146 sigprocmask (SIG_BLOCK, &block_timer, save);
152 sigprocmask (SIG_SETMASK, save, 0);
164 static word signal_installed = 0;
166 if (!signal_installed)
173 memset (&sa, 0,
sizeof (sa));
179 signal_installed = 1;
194 if (_vec_len (timers) > 1)
196 reset_timer += t->
time < (t - 1)->time;
215 static f64 ave_delay = 0;
216 static word ave_delay_count = 0;
222 ave_delay_count += 1;
227 f64 time_requested, time_called;
230 static f64 foo_base_time = 0;
231 static foo_t *foos = 0;
249 bar_t *b = (bar_t *) arg;
251 fformat (stdout,
"bar %d delay %g\n", b->count++, delay);
254 if (b->count < b->limit)
259 main (
int argc,
char *argv[])
261 word i, n = atoi (argv[1]);
262 word run_foo = argc > 2;
263 bar_t b = { limit:10 };
269 time_limit = atof (argv[2]);
272 for (i = 0; i < n; i++)
274 foos[
i].time_requested = time_limit *
random_f64 ();
275 foos[
i].time_called = 1e100;
279 for (i = 0; i < n; i++)
290 f64 min = 1e100, max = -min;
291 f64 ave = 0, rms = 0;
293 for (i = 0; i < n; i++)
295 f64 dt = foos[
i].time_requested - foos[
i].time_called;
304 rms =
sqrt (rms / n - ave * ave);
305 fformat (stdout,
"error min %g max %g ave %g +- %g\n", min, max, ave,
309 fformat (stdout,
"%d function calls, ave. timer delay %g secs\n",
310 ave_delay_count, ave_delay / ave_delay_count);
sll srl srl sll sra u16x4 i
void timer_unblock(sigset_t *save)
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
static f64 time_resolution
static timer_callback_t * timers
static f64 unix_time_now(void)
#define vec_resize(V, N)
Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V...
#define vec_end(v)
End (last data address) of vector.
void timer_call(timer_func_t *func, any arg, f64 dt)
static int timer_compare(const void *_a, const void *_b)
static void timer_interrupt(int signum)
static void f64_to_tv(f64 t, struct timeval *tv)
static f64 random_f64(u32 *seed)
Generate f64 random number in the interval [0,1].
#define clib_unix_error(format, args...)
void( timer_func_t)(any arg, f64 delay)
void timer_block(sigset_t *save)
void qsort(void *base, uword n, uword size, int(*compar)(const void *, const void *))
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Linear Congruential Random Number Generator.
#define clib_panic(format, args...)
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
static void sort_timers(timer_callback_t *timers)