2017-03-15 21:13:17 +01:00
|
|
|
/* Copyright 2016-2017, The Tor Project, Inc. */
|
2016-04-13 21:30:59 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
#include "orconfig.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "compat.h"
|
|
|
|
#include "compat_libevent.h"
|
2018-04-06 23:23:29 +02:00
|
|
|
#include "crypto_rand.h"
|
2016-04-13 21:30:59 +02:00
|
|
|
#include "timers.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#define N_TIMERS 1000
|
|
|
|
#define MAX_DURATION 30
|
2016-04-15 15:31:18 +02:00
|
|
|
#define N_DISABLE 5
|
2016-04-13 21:30:59 +02:00
|
|
|
|
2016-06-09 13:50:25 +02:00
|
|
|
static struct timeval fire_at[N_TIMERS] = { {0,0} };
|
2016-04-15 15:31:18 +02:00
|
|
|
static int is_disabled[N_TIMERS] = {0};
|
2016-04-13 21:30:59 +02:00
|
|
|
static int fired[N_TIMERS] = {0};
|
2016-06-09 13:50:25 +02:00
|
|
|
static struct timeval difference[N_TIMERS] = { {0,0} };
|
2016-04-13 21:30:59 +02:00
|
|
|
static tor_timer_t *timers[N_TIMERS] = {NULL};
|
|
|
|
|
2016-04-15 15:31:18 +02:00
|
|
|
static int n_active_timers = 0;
|
2016-04-13 21:30:59 +02:00
|
|
|
static int n_fired = 0;
|
|
|
|
|
2016-07-08 20:38:02 +02:00
|
|
|
static monotime_t started_at;
|
|
|
|
static int64_t delay_usec[N_TIMERS];
|
|
|
|
static int64_t diffs_mono_usec[N_TIMERS];
|
|
|
|
|
2016-04-13 21:30:59 +02:00
|
|
|
static void
|
2016-07-08 20:38:02 +02:00
|
|
|
timer_cb(tor_timer_t *t, void *arg, const monotime_t *now_mono)
|
2016-04-13 21:30:59 +02:00
|
|
|
{
|
2016-07-08 20:38:02 +02:00
|
|
|
struct timeval now;
|
|
|
|
|
|
|
|
tor_gettimeofday(&now);
|
2016-04-13 21:30:59 +02:00
|
|
|
tor_timer_t **t_ptr = arg;
|
|
|
|
tor_assert(*t_ptr == t);
|
|
|
|
int idx = (int) (t_ptr - timers);
|
|
|
|
++fired[idx];
|
2016-07-08 20:38:02 +02:00
|
|
|
timersub(&now, &fire_at[idx], &difference[idx]);
|
|
|
|
diffs_mono_usec[idx] =
|
|
|
|
monotime_diff_usec(&started_at, now_mono) -
|
|
|
|
delay_usec[idx];
|
2016-04-13 21:30:59 +02:00
|
|
|
++n_fired;
|
2016-07-08 20:38:02 +02:00
|
|
|
|
2016-04-13 21:30:59 +02:00
|
|
|
// printf("%d / %d\n",n_fired, N_TIMERS);
|
2016-04-15 15:31:18 +02:00
|
|
|
if (n_fired == n_active_timers) {
|
2017-10-03 17:09:44 +02:00
|
|
|
tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
|
2016-04-13 21:30:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
|
|
|
tor_libevent_cfg cfg;
|
|
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
|
|
tor_libevent_initialize(&cfg);
|
|
|
|
timers_initialize();
|
|
|
|
|
|
|
|
int i;
|
2016-04-15 15:31:18 +02:00
|
|
|
int ret;
|
2016-04-13 21:30:59 +02:00
|
|
|
struct timeval now;
|
|
|
|
tor_gettimeofday(&now);
|
2016-07-08 20:38:02 +02:00
|
|
|
monotime_get(&started_at);
|
2016-04-13 21:30:59 +02:00
|
|
|
for (i = 0; i < N_TIMERS; ++i) {
|
|
|
|
struct timeval delay;
|
|
|
|
delay.tv_sec = crypto_rand_int_range(0,MAX_DURATION);
|
|
|
|
delay.tv_usec = crypto_rand_int_range(0,1000000);
|
2016-07-08 20:38:02 +02:00
|
|
|
delay_usec[i] = delay.tv_sec * 1000000 + delay.tv_usec;
|
2016-04-13 21:30:59 +02:00
|
|
|
timeradd(&now, &delay, &fire_at[i]);
|
2016-04-15 15:31:18 +02:00
|
|
|
timers[i] = timer_new(timer_cb, &timers[i]);
|
2016-04-13 21:30:59 +02:00
|
|
|
timer_schedule(timers[i], &delay);
|
2016-04-15 15:31:18 +02:00
|
|
|
++n_active_timers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disable some; we'll make sure they don't trigger. */
|
|
|
|
for (i = 0; i < N_DISABLE; ++i) {
|
|
|
|
int idx = crypto_rand_int_range(0, N_TIMERS);
|
|
|
|
if (is_disabled[idx])
|
|
|
|
continue;
|
|
|
|
is_disabled[idx] = 1;
|
|
|
|
timer_disable(timers[idx]);
|
|
|
|
--n_active_timers;
|
2016-04-13 21:30:59 +02:00
|
|
|
}
|
|
|
|
|
2017-10-03 17:20:59 +02:00
|
|
|
tor_libevent_run_event_loop(tor_libevent_get_base(), 0);
|
2016-04-13 21:30:59 +02:00
|
|
|
|
2016-04-25 20:34:02 +02:00
|
|
|
int64_t total_difference = 0;
|
2016-04-13 21:30:59 +02:00
|
|
|
uint64_t total_square_difference = 0;
|
2016-04-15 15:31:18 +02:00
|
|
|
tor_assert(n_fired == n_active_timers);
|
2016-04-13 21:30:59 +02:00
|
|
|
for (i = 0; i < N_TIMERS; ++i) {
|
2016-04-15 15:31:18 +02:00
|
|
|
if (is_disabled[i]) {
|
|
|
|
tor_assert(fired[i] == 0);
|
|
|
|
continue;
|
|
|
|
}
|
2016-04-13 21:30:59 +02:00
|
|
|
tor_assert(fired[i] == 1);
|
2016-07-08 20:38:02 +02:00
|
|
|
//int64_t diff = difference[i].tv_usec + difference[i].tv_sec * 1000000;
|
|
|
|
int64_t diff = diffs_mono_usec[i];
|
2016-04-13 21:30:59 +02:00
|
|
|
total_difference += diff;
|
|
|
|
total_square_difference += diff*diff;
|
|
|
|
}
|
2016-04-25 20:34:02 +02:00
|
|
|
const int64_t mean_diff = total_difference / n_active_timers;
|
2016-07-06 16:07:02 +02:00
|
|
|
printf("mean difference: "I64_FORMAT" usec\n",
|
|
|
|
I64_PRINTF_ARG(mean_diff));
|
2016-04-13 21:30:59 +02:00
|
|
|
|
2016-04-15 15:31:18 +02:00
|
|
|
const double mean_sq = ((double)total_square_difference)/ n_active_timers;
|
2016-04-13 21:30:59 +02:00
|
|
|
const double sq_mean = mean_diff * mean_diff;
|
|
|
|
const double stddev = sqrt(mean_sq - sq_mean);
|
|
|
|
printf("standard deviation: %lf usec\n", stddev);
|
|
|
|
|
2016-04-25 20:34:02 +02:00
|
|
|
#define MAX_DIFF_USEC (500*1000)
|
|
|
|
#define MAX_STDDEV_USEC (500*1000)
|
|
|
|
#define ODD_DIFF_USEC (2000)
|
|
|
|
#define ODD_STDDEV_USEC (2000)
|
|
|
|
|
|
|
|
if (mean_diff < 0 || mean_diff > MAX_DIFF_USEC || stddev > MAX_STDDEV_USEC) {
|
2016-04-13 21:30:59 +02:00
|
|
|
printf("Either your system is under ridiculous load, or the "
|
|
|
|
"timer backend is broken.\n");
|
2016-04-15 15:31:18 +02:00
|
|
|
ret = 1;
|
2016-04-25 20:34:02 +02:00
|
|
|
} else if (mean_diff > ODD_DIFF_USEC || stddev > ODD_STDDEV_USEC) {
|
2016-04-13 21:30:59 +02:00
|
|
|
printf("Either your system is a bit slow or the "
|
|
|
|
"timer backend is odd.\n");
|
2016-04-15 15:31:18 +02:00
|
|
|
ret = 0;
|
2016-04-13 21:30:59 +02:00
|
|
|
} else {
|
|
|
|
printf("Looks good enough.\n");
|
2016-04-15 15:31:18 +02:00
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
2017-11-17 18:27:25 +01:00
|
|
|
timer_free_(NULL);
|
2016-04-15 15:31:18 +02:00
|
|
|
|
|
|
|
for (i = 0; i < N_TIMERS; ++i) {
|
|
|
|
timer_free(timers[i]);
|
2016-04-13 21:30:59 +02:00
|
|
|
}
|
2016-04-15 15:31:18 +02:00
|
|
|
timers_shutdown();
|
|
|
|
return ret;
|
2016-04-13 21:30:59 +02:00
|
|
|
}
|
2016-06-09 13:50:25 +02:00
|
|
|
|