tor/src/test/test-timers.c
Nick Mathewson 4845ab53f0 Make all our struct names end with "_t".
This is an automated commit, generated by this command:

./scripts/maint/rename_c_identifier.py \
        address_ttl_s address_ttl_t \
        aes_cnt_cipher aes_cnt_cipher_t \
        authchallenge_data_s authchallenge_data_t \
        authenticate_data_s authenticate_data_t \
        cached_bw_event_s cached_bw_event_t \
        cbuf cbuf_t \
        cell_ewma_s cell_ewma_t \
        certs_data_s certs_data_t \
        channel_idmap_entry_s channel_idmap_entry_t \
        channel_listener_s channel_listener_t \
        channel_s channel_t \
        channel_tls_s channel_tls_t \
        circuit_build_times_s circuit_build_times_t \
        circuit_muxinfo_s circuit_muxinfo_t \
        circuitmux_policy_circ_data_s circuitmux_policy_circ_data_t \
        circuitmux_policy_data_s circuitmux_policy_data_t \
        circuitmux_policy_s circuitmux_policy_t \
        circuitmux_s circuitmux_t \
        coord coord_t \
        cpuworker_job_u cpuworker_job_u_t \
        cv_testinfo_s cv_testinfo_t \
        ddmap_entry_s ddmap_entry_t \
        dircollator_s dircollator_t \
        dist_ops dist_ops_t \
        ecdh_work_s ecdh_work_t \
        ewma_policy_circ_data_s ewma_policy_circ_data_t \
        ewma_policy_data_s ewma_policy_data_t \
        fp_pair_map_entry_s fp_pair_map_entry_t \
        fp_pair_map_s fp_pair_map_t \
        guard_selection_s guard_selection_t \
        mbw_cache_entry_s mbw_cache_entry_t \
        outbuf_table_ent_s outbuf_table_ent_t \
        queued_event_s queued_event_t \
        replyqueue_s replyqueue_t \
        rsa_work_s rsa_work_t \
        sandbox_cfg_elem sandbox_cfg_elem_t \
        scheduler_s scheduler_t \
        smp_param smp_param_t \
        socket_table_ent_s socket_table_ent_t \
        state_s state_t \
        threadpool_s threadpool_t \
        timeout_cb timeout_cb_t \
        tor_libevent_cfg tor_libevent_cfg_t \
        tor_threadlocal_s tor_threadlocal_t \
        url_table_ent_s url_table_ent_t \
        worker_state_s worker_state_t \
        workerthread_s workerthread_t \
        workqueue_entry_s workqueue_entry_t
2019-11-07 08:41:22 -05:00

147 lines
3.9 KiB
C

/* Copyright 2016-2019, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "lib/evloop/compat_libevent.h"
#include "lib/evloop/timers.h"
#include "lib/crypt_ops/crypto_init.h"
#include "lib/crypt_ops/crypto_rand.h"
#include "lib/log/util_bug.h"
#include "lib/time/compat_time.h"
#include "lib/wallclock/timeval.h"
#define N_TIMERS 1000
#define MAX_DURATION 30
#define N_DISABLE 5
static struct timeval fire_at[N_TIMERS] = { {0,0} };
static int is_disabled[N_TIMERS] = {0};
static int fired[N_TIMERS] = {0};
static struct timeval difference[N_TIMERS] = { {0,0} };
static tor_timer_t *timers[N_TIMERS] = {NULL};
static int n_active_timers = 0;
static int n_fired = 0;
static monotime_t started_at;
static int64_t delay_usec[N_TIMERS];
static int64_t diffs_mono_usec[N_TIMERS];
static void
timer_cb(tor_timer_t *t, void *arg, const monotime_t *now_mono)
{
struct timeval now;
tor_gettimeofday(&now);
tor_timer_t **t_ptr = arg;
tor_assert(*t_ptr == t);
int idx = (int) (t_ptr - timers);
++fired[idx];
timersub(&now, &fire_at[idx], &difference[idx]);
diffs_mono_usec[idx] =
monotime_diff_usec(&started_at, now_mono) -
delay_usec[idx];
++n_fired;
// printf("%d / %d\n",n_fired, N_TIMERS);
if (n_fired == n_active_timers) {
tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
}
}
int
main(int argc, char **argv)
{
(void)argc;
(void)argv;
tor_libevent_cfg_t cfg;
memset(&cfg, 0, sizeof(cfg));
tor_libevent_initialize(&cfg);
timers_initialize();
init_logging(1);
if (crypto_global_init(0, NULL, NULL) < 0)
return 1;
int i;
int ret;
struct timeval now;
tor_gettimeofday(&now);
monotime_get(&started_at);
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);
delay_usec[i] = delay.tv_sec * 1000000 + delay.tv_usec;
timeradd(&now, &delay, &fire_at[i]);
timers[i] = timer_new(timer_cb, &timers[i]);
timer_schedule(timers[i], &delay);
++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;
}
tor_libevent_run_event_loop(tor_libevent_get_base(), 0);
int64_t total_difference = 0;
uint64_t total_square_difference = 0;
tor_assert(n_fired == n_active_timers);
for (i = 0; i < N_TIMERS; ++i) {
if (is_disabled[i]) {
tor_assert(fired[i] == 0);
continue;
}
tor_assert(fired[i] == 1);
//int64_t diff = difference[i].tv_usec + difference[i].tv_sec * 1000000;
int64_t diff = diffs_mono_usec[i];
total_difference += diff;
total_square_difference += diff*diff;
}
const int64_t mean_diff = total_difference / n_active_timers;
printf("mean difference: %"PRId64" usec\n",
(mean_diff));
const double mean_sq = ((double)total_square_difference)/ n_active_timers;
const double sq_mean = mean_diff * mean_diff;
const double stddev = sqrt(mean_sq - sq_mean);
printf("standard deviation: %lf usec\n", stddev);
#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) {
printf("Either your system is under ridiculous load, or the "
"timer backend is broken.\n");
ret = 1;
} else if (mean_diff > ODD_DIFF_USEC || stddev > ODD_STDDEV_USEC) {
printf("Either your system is a bit slow or the "
"timer backend is odd.\n");
ret = 0;
} else {
printf("Looks good enough.\n");
ret = 0;
}
timer_free_(NULL);
for (i = 0; i < N_TIMERS; ++i) {
timer_free(timers[i]);
}
timers_shutdown();
return ret;
}