test-timers.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Copyright 2016-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "common/compat_libevent.h"
  8. #include "lib/crypt_ops/crypto_rand.h"
  9. #include "common/timers.h"
  10. #include "common/util.h"
  11. #define N_TIMERS 1000
  12. #define MAX_DURATION 30
  13. #define N_DISABLE 5
  14. static struct timeval fire_at[N_TIMERS] = { {0,0} };
  15. static int is_disabled[N_TIMERS] = {0};
  16. static int fired[N_TIMERS] = {0};
  17. static struct timeval difference[N_TIMERS] = { {0,0} };
  18. static tor_timer_t *timers[N_TIMERS] = {NULL};
  19. static int n_active_timers = 0;
  20. static int n_fired = 0;
  21. static monotime_t started_at;
  22. static int64_t delay_usec[N_TIMERS];
  23. static int64_t diffs_mono_usec[N_TIMERS];
  24. static void
  25. timer_cb(tor_timer_t *t, void *arg, const monotime_t *now_mono)
  26. {
  27. struct timeval now;
  28. tor_gettimeofday(&now);
  29. tor_timer_t **t_ptr = arg;
  30. tor_assert(*t_ptr == t);
  31. int idx = (int) (t_ptr - timers);
  32. ++fired[idx];
  33. timersub(&now, &fire_at[idx], &difference[idx]);
  34. diffs_mono_usec[idx] =
  35. monotime_diff_usec(&started_at, now_mono) -
  36. delay_usec[idx];
  37. ++n_fired;
  38. // printf("%d / %d\n",n_fired, N_TIMERS);
  39. if (n_fired == n_active_timers) {
  40. tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
  41. }
  42. }
  43. int
  44. main(int argc, char **argv)
  45. {
  46. (void)argc;
  47. (void)argv;
  48. tor_libevent_cfg cfg;
  49. memset(&cfg, 0, sizeof(cfg));
  50. tor_libevent_initialize(&cfg);
  51. timers_initialize();
  52. int i;
  53. int ret;
  54. struct timeval now;
  55. tor_gettimeofday(&now);
  56. monotime_get(&started_at);
  57. for (i = 0; i < N_TIMERS; ++i) {
  58. struct timeval delay;
  59. delay.tv_sec = crypto_rand_int_range(0,MAX_DURATION);
  60. delay.tv_usec = crypto_rand_int_range(0,1000000);
  61. delay_usec[i] = delay.tv_sec * 1000000 + delay.tv_usec;
  62. timeradd(&now, &delay, &fire_at[i]);
  63. timers[i] = timer_new(timer_cb, &timers[i]);
  64. timer_schedule(timers[i], &delay);
  65. ++n_active_timers;
  66. }
  67. /* Disable some; we'll make sure they don't trigger. */
  68. for (i = 0; i < N_DISABLE; ++i) {
  69. int idx = crypto_rand_int_range(0, N_TIMERS);
  70. if (is_disabled[idx])
  71. continue;
  72. is_disabled[idx] = 1;
  73. timer_disable(timers[idx]);
  74. --n_active_timers;
  75. }
  76. tor_libevent_run_event_loop(tor_libevent_get_base(), 0);
  77. int64_t total_difference = 0;
  78. uint64_t total_square_difference = 0;
  79. tor_assert(n_fired == n_active_timers);
  80. for (i = 0; i < N_TIMERS; ++i) {
  81. if (is_disabled[i]) {
  82. tor_assert(fired[i] == 0);
  83. continue;
  84. }
  85. tor_assert(fired[i] == 1);
  86. //int64_t diff = difference[i].tv_usec + difference[i].tv_sec * 1000000;
  87. int64_t diff = diffs_mono_usec[i];
  88. total_difference += diff;
  89. total_square_difference += diff*diff;
  90. }
  91. const int64_t mean_diff = total_difference / n_active_timers;
  92. printf("mean difference: %"PRId64" usec\n",
  93. (mean_diff));
  94. const double mean_sq = ((double)total_square_difference)/ n_active_timers;
  95. const double sq_mean = mean_diff * mean_diff;
  96. const double stddev = sqrt(mean_sq - sq_mean);
  97. printf("standard deviation: %lf usec\n", stddev);
  98. #define MAX_DIFF_USEC (500*1000)
  99. #define MAX_STDDEV_USEC (500*1000)
  100. #define ODD_DIFF_USEC (2000)
  101. #define ODD_STDDEV_USEC (2000)
  102. if (mean_diff < 0 || mean_diff > MAX_DIFF_USEC || stddev > MAX_STDDEV_USEC) {
  103. printf("Either your system is under ridiculous load, or the "
  104. "timer backend is broken.\n");
  105. ret = 1;
  106. } else if (mean_diff > ODD_DIFF_USEC || stddev > ODD_STDDEV_USEC) {
  107. printf("Either your system is a bit slow or the "
  108. "timer backend is odd.\n");
  109. ret = 0;
  110. } else {
  111. printf("Looks good enough.\n");
  112. ret = 0;
  113. }
  114. timer_free_(NULL);
  115. for (i = 0; i < N_TIMERS; ++i) {
  116. timer_free(timers[i]);
  117. }
  118. timers_shutdown();
  119. return ret;
  120. }