static int test_time_main (unformat_input_t * input) { f64 wait, error; f64 t, tu[3], ave, rms; clib_time_t c; int i, n, j; clib_time_init (&c); wait = 1e-3; n = 1000; unformat (input, "%f %d", &wait, &n); ave = rms = 0; tu[0] = unix_time_now (); tu[1] = unix_time_now (); for (i = 0; i < n; i++) { j = 0; t = clib_time_now (&c); while (clib_time_now (&c) < t + wait) j++; t = j; ave += t; rms += t*t; } tu[2] = unix_time_now (); ave /= n; rms = sqrt (rms/n - ave*ave); error = ((tu[2] - tu[1]) - 2 * (tu[1] - tu[0]) - n*wait) / n; if_verbose ("tested %d x %.6e sec waits, error %.6e loops %.6e +- %.6e\n", n, wait, error, ave, rms); return 0; }
void clib_timebase_init (clib_timebase_t * tb, i32 timezone_offset_in_hours, clib_timebase_daylight_time_t daylight_type) { clib_memset (tb, 0, sizeof (*tb)); clib_time_init (&tb->clib_time); tb->time_zero = unix_time_now (); tb->timezone_offset = ((f64) (timezone_offset_in_hours)) * 3600.0; tb->daylight_time_type = daylight_type; switch (tb->daylight_time_type) { case CLIB_TIMEBASE_DAYLIGHT_NONE: tb->summer_offset = 0.0; break; case CLIB_TIMEBASE_DAYLIGHT_USA: tb->summer_offset = 3600.0; break; default: clib_warning ("unknown daylight type %d", tb->daylight_time_type); tb->daylight_time_type = CLIB_TIMEBASE_DAYLIGHT_NONE; tb->summer_offset = 0.0; } }
int main (int argc, char *argv[]) { word i, n = atoi (argv[1]); word run_foo = argc > 2; bar_t b = { limit:10 }; if (run_foo) { f64 time_limit; time_limit = atof (argv[2]); vec_resize (foos, n); for (i = 0; i < n; i++) { foos[i].time_requested = time_limit * random_f64 (); foos[i].time_called = 1e100; } foo_base_time = unix_time_now (); for (i = 0; i < n; i++) timer_call (foo, i, foos[i].time_requested); } else timer_call (bar, (any) & b, random_f64 ()); while (vec_len (timers) > 0) os_sched_yield (); if (vec_len (foos) > 0) { f64 min = 1e100, max = -min; f64 ave = 0, rms = 0; for (i = 0; i < n; i++) { f64 dt = foos[i].time_requested - foos[i].time_called; if (dt < min) min = dt; if (dt > max) max = dt; ave += dt; rms += dt * dt; } ave /= n; rms = sqrt (rms / n - ave * ave); fformat (stdout, "error min %g max %g ave %g +- %g\n", min, max, ave, rms); } fformat (stdout, "%d function calls, ave. timer delay %g secs\n", ave_delay_count, ave_delay / ave_delay_count); return 0; }
/* Arrange for function to be called some time, roughly equal to dt seconds, in the future. */ void timer_call (timer_func_t * func, any arg, f64 dt) { timer_callback_t *t; sigset_t save; /* Install signal handler on first call. */ static word signal_installed = 0; if (!signal_installed) { struct sigaction sa; /* Initialize time_resolution before first call to timer_interrupt */ time_resolution = 0.75 / (f64) HZ; clib_memset (&sa, 0, sizeof (sa)); sa.sa_handler = timer_interrupt; if (sigaction (TIMER_SIGNAL, &sa, 0) < 0) clib_panic ("sigaction"); signal_installed = 1; } timer_block (&save); /* Add new timer. */ vec_add2 (timers, t, 1); t->time = unix_time_now () + dt; t->func = func; t->arg = arg; { word reset_timer = vec_len (timers) == 1; if (_vec_len (timers) > 1) { reset_timer += t->time < (t - 1)->time; sort_timers (timers); } if (reset_timer) timer_interrupt (TIMER_SIGNAL); } timer_unblock (&save); }
/* Interrupt handler. Call functions for all expired timers. Set time for next timer interrupt. */ static void timer_interrupt (int signum) { f64 now = unix_time_now (); f64 dt; timer_callback_t *t; while (1) { if (vec_len (timers) <= 0) return; /* Consider last (earliest) timer in reverse sorted vector of pending timers. */ t = vec_end (timers) - 1; ASSERT (now >= 0 && isfinite (now)); /* Time difference between when timer goes off and now. */ dt = t->time - now; /* If timer is within threshold of going off call user's callback. */ if (dt <= time_resolution && isfinite (dt)) { _vec_len (timers) -= 1; (*t->func) (t->arg, -dt); } else { /* Set timer for to go off in future. */ struct itimerval itv; clib_memset (&itv, 0, sizeof (itv)); f64_to_tv (dt, &itv.it_value); if (setitimer (ITIMER_REAL, &itv, 0) < 0) clib_unix_error ("sititmer"); return; } } }
void foo (any arg, f64 delay) { foos[arg].time_called = unix_time_now () - foo_base_time; update (delay); }
int test_elog_main (unformat_input_t * input) { clib_error_t * error = 0; u32 i, n_iter, seed, max_events; elog_main_t _em, * em = &_em; u32 verbose; f64 min_sample_time; char * dump_file, * load_file, * merge_file, ** merge_files; u8 * tag, ** tags; n_iter = 100; max_events = 100000; seed = 1; verbose = 0; dump_file = 0; load_file = 0; merge_files = 0; tags = 0; min_sample_time = 2; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "iter %d", &n_iter)) ; else if (unformat (input, "seed %d", &seed)) ; else if (unformat (input, "dump %s", &dump_file)) ; else if (unformat (input, "load %s", &load_file)) ; else if (unformat (input, "tag %s", &tag)) vec_add1 (tags, tag); else if (unformat (input, "merge %s", &merge_file)) vec_add1 (merge_files, merge_file); else if (unformat (input, "verbose %=", &verbose, 1)) ; else if (unformat (input, "max-events %d", &max_events)) ; else if (unformat (input, "sample-time %f", &min_sample_time)) ; else { error = clib_error_create ("unknown input `%U'\n", format_unformat_error, input); goto done; } } #ifdef CLIB_UNIX if (load_file) { if ((error = elog_read_file (em, load_file))) goto done; } else if (merge_files) { uword i; elog_main_t * ems; vec_clone (ems, merge_files); elog_init (em, max_events); for (i = 0; i < vec_len (ems); i++) { if ((error = elog_read_file (i == 0 ? em : &ems[i], merge_files[i]))) goto done; if (i > 0) { elog_merge (em, tags[0], &ems[i], tags[i]); tags[0] = 0; } } } else #endif /* CLIB_UNIX */ { f64 t[2]; elog_init (em, max_events); elog_enable_disable (em, 1); t[0] = unix_time_now (); for (i = 0; i < n_iter; i++) { u32 j, n, sum; n = 1 + (random_u32 (&seed) % 128); sum = 0; for (j = 0; j < n; j++) sum += random_u32 (&seed); { ELOG_TYPE_XF (e); ELOG (em, e, sum); } { ELOG_TYPE_XF (e); ELOG (em, e, sum + 1); } { struct { u32 string_index; f32 f; } * d; ELOG_TYPE_DECLARE (e) = { .format = "fumble %s %.9f", .format_args = "t4f4", .n_enum_strings = 4, .enum_strings = { "string0", "string1", "string2", "string3", }, }; d = ELOG_DATA (em, e); d->string_index = sum & 3; d->f = (sum & 0xff) / 128.; } { ELOG_TYPE_DECLARE (e) = { .format = "bar %d.%d.%d.%d", .format_args = "i1i1i1i1", }; ELOG_TRACK (my_track); u8 * d = ELOG_TRACK_DATA (em, e, my_track); d[0] = i + 0; d[1] = i + 1; d[2] = i + 2; d[3] = i + 3; } { ELOG_TYPE_DECLARE (e) = { .format = "bar `%s'", .format_args = "s20", }; struct { char s[20]; } * d; u8 * v; d = ELOG_DATA (em, e); v = format (0, "foo %d%c", i, 0); memcpy (d->s, v, clib_min (vec_len (v), sizeof (d->s))); } { ELOG_TYPE_DECLARE (e) = { .format = "bar `%s'", .format_args = "T4", }; struct { u32 offset; } * d; d = ELOG_DATA (em, e); d->offset = elog_string (em, "string table %d", i); } } do { t[1] = unix_time_now (); } while (t[1] - t[0] < min_sample_time); } #ifdef CLIB_UNIX if (dump_file) { if ((error = elog_write_file (em, dump_file))) goto done; } #endif if (verbose) { elog_event_t * e, * es; es = elog_get_events (em); vec_foreach (e, es) { clib_warning ("%18.9f: %12U %U\n", e->time, format_elog_track, em, e, format_elog_event, em, e); } } done: if (error) clib_error_report (error); return 0; } #ifdef CLIB_UNIX int main (int argc, char * argv []) { unformat_input_t i; int r; unformat_init_command_line (&i, argv); r = test_elog_main (&i); unformat_free (&i); return r; }