Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org> |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 3 | * (C) 2011 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 4 | * All Rights Reserved |
| 5 | * |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 6 | * Authors: Holger Hans Peter Freyther <zecke@selfish.org> |
| 7 | * Pablo Neira Ayuso <pablo@gnumonks.org> |
| 8 | * |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <stdio.h> |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 26 | #include <stdlib.h> |
Pablo Neira Ayuso | 72eb44c | 2011-11-13 17:40:09 +0100 | [diff] [blame] | 27 | #include <signal.h> |
Pablo Neira Ayuso | f141837 | 2011-11-13 02:02:12 +0100 | [diff] [blame] | 28 | #include <getopt.h> |
Harald Welte | b53717f | 2012-08-02 08:42:59 +0200 | [diff] [blame] | 29 | #include <unistd.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 30 | |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 31 | #include <osmocom/core/talloc.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 32 | #include <osmocom/core/timer.h> |
| 33 | #include <osmocom/core/select.h> |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 34 | #include <osmocom/core/linuxlist.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 35 | |
Alex Badea | 695e5fb | 2013-01-05 20:59:00 +0200 | [diff] [blame] | 36 | #include "../config.h" |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 37 | |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 38 | static void main_timer_fired(void *data); |
| 39 | static void secondary_timer_fired(void *data); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 40 | |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 41 | static unsigned int main_timer_step = 0; |
| 42 | static struct osmo_timer_list main_timer = { |
| 43 | .cb = main_timer_fired, |
| 44 | .data = &main_timer_step, |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 47 | static LLIST_HEAD(timer_test_list); |
| 48 | |
| 49 | struct test_timer { |
| 50 | struct llist_head head; |
| 51 | struct osmo_timer_list timer; |
| 52 | struct timeval start; |
| 53 | struct timeval stop; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 54 | }; |
| 55 | |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 56 | /* number of test steps. We add fact(steps) timers in the whole test. */ |
| 57 | #define MAIN_TIMER_NSTEPS 16 |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 58 | |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 59 | /* time between two steps, in secs. */ |
| 60 | #define TIME_BETWEEN_STEPS 1 |
| 61 | |
| 62 | /* timer imprecision that we accept for this test: 10 milliseconds. */ |
| 63 | #define TIMER_PRES_SECS 0 |
Holger Hans Peter Freyther | 00a12fe | 2012-04-05 09:47:59 +0200 | [diff] [blame] | 64 | #define TIMER_PRES_USECS 20000 |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 65 | |
Pablo Neira Ayuso | f141837 | 2011-11-13 02:02:12 +0100 | [diff] [blame] | 66 | static int timer_nsteps = MAIN_TIMER_NSTEPS; |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 67 | static unsigned int expired_timers = 0; |
| 68 | static unsigned int total_timers = 0; |
| 69 | static unsigned int too_late = 0; |
| 70 | |
| 71 | static void main_timer_fired(void *data) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 72 | { |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 73 | unsigned int *step = data; |
| 74 | unsigned int add_in_this_step; |
| 75 | int i; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 76 | |
Pablo Neira Ayuso | f141837 | 2011-11-13 02:02:12 +0100 | [diff] [blame] | 77 | if (*step == timer_nsteps) { |
Pablo Neira Ayuso | 7a0ca16 | 2011-11-13 17:22:33 +0100 | [diff] [blame] | 78 | fprintf(stderr, "Main timer has finished, please, " |
| 79 | "wait a bit for the final report.\n"); |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 80 | return; |
| 81 | } |
| 82 | /* add 2^step pair of timers per step. */ |
| 83 | add_in_this_step = (1 << *step); |
| 84 | |
| 85 | for (i=0; i<add_in_this_step; i++) { |
| 86 | struct test_timer *v; |
| 87 | |
| 88 | v = talloc_zero(NULL, struct test_timer); |
| 89 | if (v == NULL) { |
| 90 | fprintf(stderr, "timer_test: OOM!\n"); |
| 91 | return; |
| 92 | } |
| 93 | gettimeofday(&v->start, NULL); |
| 94 | v->timer.cb = secondary_timer_fired; |
| 95 | v->timer.data = v; |
| 96 | unsigned int seconds = (random() % 10) + 1; |
| 97 | v->stop.tv_sec = v->start.tv_sec + seconds; |
| 98 | osmo_timer_schedule(&v->timer, seconds, 0); |
| 99 | llist_add(&v->head, &timer_test_list); |
| 100 | } |
Pablo Neira Ayuso | 7a0ca16 | 2011-11-13 17:22:33 +0100 | [diff] [blame] | 101 | fprintf(stderr, "added %d timers in step %u (expired=%u)\n", |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 102 | add_in_this_step, *step, expired_timers); |
| 103 | total_timers += add_in_this_step; |
| 104 | osmo_timer_schedule(&main_timer, TIME_BETWEEN_STEPS, 0); |
| 105 | (*step)++; |
| 106 | } |
| 107 | |
| 108 | static void secondary_timer_fired(void *data) |
| 109 | { |
| 110 | struct test_timer *v = data, *this, *tmp; |
| 111 | struct timeval current, res, precision = { 1, 0 }; |
| 112 | |
| 113 | gettimeofday(¤t, NULL); |
| 114 | |
| 115 | timersub(¤t, &v->stop, &res); |
| 116 | if (timercmp(&res, &precision, >)) { |
Pablo Neira Ayuso | 7a0ca16 | 2011-11-13 17:22:33 +0100 | [diff] [blame] | 117 | fprintf(stderr, "ERROR: timer %p has expired too late!\n", |
Jacob Erlbeck | 73ae7a9 | 2013-10-08 12:04:42 +0200 | [diff] [blame] | 118 | &v->timer); |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 119 | too_late++; |
| 120 | } |
| 121 | |
| 122 | llist_del(&v->head); |
| 123 | talloc_free(data); |
| 124 | expired_timers++; |
| 125 | if (expired_timers == total_timers) { |
Pablo Neira Ayuso | 7a0ca16 | 2011-11-13 17:22:33 +0100 | [diff] [blame] | 126 | fprintf(stdout, "test over: added=%u expired=%u too_late=%u \n", |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 127 | total_timers, expired_timers, too_late); |
| 128 | exit(EXIT_SUCCESS); |
| 129 | } |
| 130 | |
| 131 | /* randomly (10%) deletion of timers. */ |
| 132 | llist_for_each_entry_safe(this, tmp, &timer_test_list, head) { |
| 133 | if ((random() % 100) < 10) { |
| 134 | osmo_timer_del(&this->timer); |
| 135 | llist_del(&this->head); |
| 136 | talloc_free(this); |
| 137 | expired_timers++; |
| 138 | } |
| 139 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Pablo Neira Ayuso | 72eb44c | 2011-11-13 17:40:09 +0100 | [diff] [blame] | 142 | static void alarm_handler(int signum) |
| 143 | { |
| 144 | fprintf(stderr, "ERROR: We took too long to run the timer test, " |
| 145 | "something seems broken, aborting.\n"); |
| 146 | exit(EXIT_FAILURE); |
| 147 | } |
| 148 | |
Pablo Neira Ayuso | f141837 | 2011-11-13 02:02:12 +0100 | [diff] [blame] | 149 | int main(int argc, char *argv[]) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 150 | { |
Pablo Neira Ayuso | f141837 | 2011-11-13 02:02:12 +0100 | [diff] [blame] | 151 | int c; |
| 152 | |
Pablo Neira Ayuso | 72eb44c | 2011-11-13 17:40:09 +0100 | [diff] [blame] | 153 | if (signal(SIGALRM, alarm_handler) == SIG_ERR) { |
| 154 | perror("cannot register signal handler"); |
| 155 | exit(EXIT_FAILURE); |
| 156 | } |
| 157 | |
Pablo Neira Ayuso | f141837 | 2011-11-13 02:02:12 +0100 | [diff] [blame] | 158 | while ((c = getopt_long(argc, argv, "s:", NULL, NULL)) != -1) { |
| 159 | switch(c) { |
| 160 | case 's': |
| 161 | timer_nsteps = atoi(optarg); |
| 162 | if (timer_nsteps <= 0) { |
| 163 | fprintf(stderr, "%s: steps must be > 0\n", |
| 164 | argv[0]); |
| 165 | exit(EXIT_FAILURE); |
| 166 | } |
| 167 | break; |
| 168 | default: |
| 169 | exit(EXIT_FAILURE); |
| 170 | } |
| 171 | } |
| 172 | |
Pablo Neira Ayuso | 7a0ca16 | 2011-11-13 17:22:33 +0100 | [diff] [blame] | 173 | fprintf(stdout, "Running timer test for %u steps, accepting " |
| 174 | "imprecision of %u.%.6u seconds\n", |
Pablo Neira Ayuso | f141837 | 2011-11-13 02:02:12 +0100 | [diff] [blame] | 175 | timer_nsteps, TIMER_PRES_SECS, TIMER_PRES_USECS); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 176 | |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 177 | osmo_timer_schedule(&main_timer, 1, 0); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 178 | |
Pablo Neira Ayuso | 72eb44c | 2011-11-13 17:40:09 +0100 | [diff] [blame] | 179 | /* if the test takes too long, we may consider that the timer scheduler |
| 180 | * has hung. We set some maximum wait time which is the double of the |
| 181 | * maximum timeout randomly set (10 seconds, worst case) plus the |
| 182 | * number of steps (since some of them are reset each step). */ |
| 183 | alarm(2 * (10 + timer_nsteps)); |
| 184 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 185 | #ifdef HAVE_SYS_SELECT_H |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 186 | while (1) { |
| 187 | osmo_select_main(0); |
| 188 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 189 | #else |
Pablo Neira Ayuso | 7a0ca16 | 2011-11-13 17:22:33 +0100 | [diff] [blame] | 190 | fprintf(stdout, "Select not supported on this platform!\n"); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 191 | #endif |
| 192 | } |