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; |
Pablo Neira Ayuso | 44f423f | 2017-05-08 18:00:28 +0200 | [diff] [blame] | 42 | static struct osmo_timer_list main_timer; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 43 | |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 44 | static LLIST_HEAD(timer_test_list); |
| 45 | |
| 46 | struct test_timer { |
| 47 | struct llist_head head; |
| 48 | struct osmo_timer_list timer; |
| 49 | struct timeval start; |
| 50 | struct timeval stop; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 51 | }; |
| 52 | |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 53 | /* number of test steps. We add fact(steps) timers in the whole test. */ |
Neels Hofmeyr | dbc6817 | 2016-09-22 04:21:50 +0200 | [diff] [blame] | 54 | #define MAIN_TIMER_NSTEPS 8 |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 55 | |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 56 | /* time between two steps, in secs. */ |
| 57 | #define TIME_BETWEEN_STEPS 1 |
| 58 | |
Neels Hofmeyr | 7b4d727 | 2016-09-22 04:47:04 +0200 | [diff] [blame] | 59 | /* how much time elapses between checks, in microsecs */ |
| 60 | #define TIME_BETWEEN_TIMER_CHECKS 423210 |
| 61 | |
Pablo Neira Ayuso | f141837 | 2011-11-13 02:02:12 +0100 | [diff] [blame] | 62 | static int timer_nsteps = MAIN_TIMER_NSTEPS; |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 63 | static unsigned int expired_timers = 0; |
| 64 | static unsigned int total_timers = 0; |
| 65 | static unsigned int too_late = 0; |
Neels Hofmeyr | 9c9a047 | 2016-09-22 04:23:34 +0200 | [diff] [blame] | 66 | static unsigned int too_soon = 0; |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 67 | |
| 68 | static void main_timer_fired(void *data) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 69 | { |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 70 | unsigned int *step = data; |
| 71 | unsigned int add_in_this_step; |
| 72 | int i; |
Neels Hofmeyr | d0858c2 | 2016-09-22 04:50:13 +0200 | [diff] [blame] | 73 | printf("main_timer_fired()\n"); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 74 | |
Pablo Neira Ayuso | f141837 | 2011-11-13 02:02:12 +0100 | [diff] [blame] | 75 | if (*step == timer_nsteps) { |
Neels Hofmeyr | 255dac1 | 2016-09-22 04:48:32 +0200 | [diff] [blame] | 76 | printf("Main timer has finished, please, " |
| 77 | "wait a bit for the final report.\n"); |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 78 | return; |
| 79 | } |
| 80 | /* add 2^step pair of timers per step. */ |
| 81 | add_in_this_step = (1 << *step); |
| 82 | |
| 83 | for (i=0; i<add_in_this_step; i++) { |
| 84 | struct test_timer *v; |
| 85 | |
| 86 | v = talloc_zero(NULL, struct test_timer); |
| 87 | if (v == NULL) { |
Neels Hofmeyr | 255dac1 | 2016-09-22 04:48:32 +0200 | [diff] [blame] | 88 | printf("timer_test: OOM!\n"); |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 89 | return; |
| 90 | } |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 91 | osmo_gettimeofday(&v->start, NULL); |
Pablo Neira Ayuso | 44f423f | 2017-05-08 18:00:28 +0200 | [diff] [blame] | 92 | osmo_timer_setup(&v->timer, secondary_timer_fired, v); |
Neels Hofmeyr | d73c1cc | 2016-09-22 05:20:53 +0200 | [diff] [blame] | 93 | unsigned int seconds = (i & 0x7) + 1; |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 94 | v->stop.tv_sec = v->start.tv_sec + seconds; |
Neels Hofmeyr | 633a0e7 | 2016-09-22 04:24:38 +0200 | [diff] [blame] | 95 | v->stop.tv_usec = v->start.tv_usec; |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 96 | osmo_timer_schedule(&v->timer, seconds, 0); |
| 97 | llist_add(&v->head, &timer_test_list); |
Neels Hofmeyr | d0858c2 | 2016-09-22 04:50:13 +0200 | [diff] [blame] | 98 | printf("scheduled timer at %d.%06d\n", |
| 99 | (int)v->stop.tv_sec, (int)v->stop.tv_usec); |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 100 | } |
Neels Hofmeyr | 255dac1 | 2016-09-22 04:48:32 +0200 | [diff] [blame] | 101 | printf("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; |
Neels Hofmeyr | 7b4d727 | 2016-09-22 04:47:04 +0200 | [diff] [blame] | 111 | struct timeval current, res; |
| 112 | struct timeval precision = { 0, TIME_BETWEEN_TIMER_CHECKS + 1}; |
Neels Hofmeyr | d0858c2 | 2016-09-22 04:50:13 +0200 | [diff] [blame] | 113 | int i, deleted; |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 114 | |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 115 | osmo_gettimeofday(¤t, NULL); |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 116 | |
| 117 | timersub(¤t, &v->stop, &res); |
| 118 | if (timercmp(&res, &precision, >)) { |
Neels Hofmeyr | 255dac1 | 2016-09-22 04:48:32 +0200 | [diff] [blame] | 119 | printf("ERROR: timer has expired too late:" |
| 120 | " wanted %d.%06d now %d.%06d diff %d.%06d\n", |
| 121 | (int)v->stop.tv_sec, (int)v->stop.tv_usec, |
| 122 | (int)current.tv_sec, (int)current.tv_usec, |
| 123 | (int)res.tv_sec, (int)res.tv_usec); |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 124 | too_late++; |
| 125 | } |
Neels Hofmeyr | 9c9a047 | 2016-09-22 04:23:34 +0200 | [diff] [blame] | 126 | else if (timercmp(¤t, &v->stop, <)) { |
Neels Hofmeyr | 255dac1 | 2016-09-22 04:48:32 +0200 | [diff] [blame] | 127 | printf("ERROR: timer has expired too soon:" |
| 128 | " wanted %d.%06d now %d.%06d diff %d.%06d\n", |
| 129 | (int)v->stop.tv_sec, (int)v->stop.tv_usec, |
| 130 | (int)current.tv_sec, (int)current.tv_usec, |
| 131 | (int)res.tv_sec, (int)res.tv_usec); |
Neels Hofmeyr | 9c9a047 | 2016-09-22 04:23:34 +0200 | [diff] [blame] | 132 | too_soon++; |
| 133 | } |
Neels Hofmeyr | d0858c2 | 2016-09-22 04:50:13 +0200 | [diff] [blame] | 134 | else |
| 135 | printf("timer fired on time: %d.%06d (+ %d.%06d)\n", |
| 136 | (int)v->stop.tv_sec, (int)v->stop.tv_usec, |
| 137 | (int)res.tv_sec, (int)res.tv_usec); |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 138 | |
| 139 | llist_del(&v->head); |
| 140 | talloc_free(data); |
| 141 | expired_timers++; |
| 142 | if (expired_timers == total_timers) { |
Neels Hofmeyr | 255dac1 | 2016-09-22 04:48:32 +0200 | [diff] [blame] | 143 | printf("test over: added=%u expired=%u too_soon=%u too_late=%u\n", |
| 144 | total_timers, expired_timers, too_soon, too_late); |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 145 | exit(EXIT_SUCCESS); |
| 146 | } |
| 147 | |
Neels Hofmeyr | d73c1cc | 2016-09-22 05:20:53 +0200 | [diff] [blame] | 148 | /* "random" deletion of timers. */ |
| 149 | i = 0; |
Neels Hofmeyr | d0858c2 | 2016-09-22 04:50:13 +0200 | [diff] [blame] | 150 | deleted = 0; |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 151 | llist_for_each_entry_safe(this, tmp, &timer_test_list, head) { |
Neels Hofmeyr | d73c1cc | 2016-09-22 05:20:53 +0200 | [diff] [blame] | 152 | i ++; |
| 153 | if (!(i & 0x3)) { |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 154 | osmo_timer_del(&this->timer); |
| 155 | llist_del(&this->head); |
| 156 | talloc_free(this); |
Neels Hofmeyr | d0858c2 | 2016-09-22 04:50:13 +0200 | [diff] [blame] | 157 | deleted++; |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 158 | } |
| 159 | } |
Neels Hofmeyr | d0858c2 | 2016-09-22 04:50:13 +0200 | [diff] [blame] | 160 | expired_timers += deleted; |
| 161 | printf("early deleted %d timers, %d still active\n", deleted, |
| 162 | total_timers - expired_timers); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Pablo Neira Ayuso | f141837 | 2011-11-13 02:02:12 +0100 | [diff] [blame] | 165 | int main(int argc, char *argv[]) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 166 | { |
Pablo Neira Ayuso | f141837 | 2011-11-13 02:02:12 +0100 | [diff] [blame] | 167 | int c; |
Neels Hofmeyr | 7b4d727 | 2016-09-22 04:47:04 +0200 | [diff] [blame] | 168 | int steps; |
Pablo Neira Ayuso | f141837 | 2011-11-13 02:02:12 +0100 | [diff] [blame] | 169 | |
Neels Hofmeyr | 7b4d727 | 2016-09-22 04:47:04 +0200 | [diff] [blame] | 170 | osmo_gettimeofday_override = true; |
Pablo Neira Ayuso | 72eb44c | 2011-11-13 17:40:09 +0100 | [diff] [blame] | 171 | |
Pablo Neira Ayuso | f141837 | 2011-11-13 02:02:12 +0100 | [diff] [blame] | 172 | while ((c = getopt_long(argc, argv, "s:", NULL, NULL)) != -1) { |
| 173 | switch(c) { |
| 174 | case 's': |
| 175 | timer_nsteps = atoi(optarg); |
| 176 | if (timer_nsteps <= 0) { |
| 177 | fprintf(stderr, "%s: steps must be > 0\n", |
| 178 | argv[0]); |
| 179 | exit(EXIT_FAILURE); |
| 180 | } |
| 181 | break; |
| 182 | default: |
| 183 | exit(EXIT_FAILURE); |
| 184 | } |
| 185 | } |
| 186 | |
Neels Hofmeyr | 7b4d727 | 2016-09-22 04:47:04 +0200 | [diff] [blame] | 187 | steps = ((MAIN_TIMER_NSTEPS * TIME_BETWEEN_STEPS + 20) * 1e6) |
| 188 | / TIME_BETWEEN_TIMER_CHECKS; |
| 189 | |
Neels Hofmeyr | d0858c2 | 2016-09-22 04:50:13 +0200 | [diff] [blame] | 190 | printf("Running timer test for %d iterations," |
| 191 | " %d steps of %d msecs each\n", |
| 192 | timer_nsteps, steps, TIME_BETWEEN_TIMER_CHECKS / 1000); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 193 | |
Pablo Neira Ayuso | 44f423f | 2017-05-08 18:00:28 +0200 | [diff] [blame] | 194 | osmo_timer_setup(&main_timer, main_timer_fired, &main_timer_step); |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 195 | osmo_timer_schedule(&main_timer, 1, 0); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 196 | |
| 197 | #ifdef HAVE_SYS_SELECT_H |
Neels Hofmeyr | 7b4d727 | 2016-09-22 04:47:04 +0200 | [diff] [blame] | 198 | while (steps--) { |
Neels Hofmeyr | d0858c2 | 2016-09-22 04:50:13 +0200 | [diff] [blame] | 199 | printf("%d.%06d\n", (int)osmo_gettimeofday_override_time.tv_sec, |
| 200 | (int)osmo_gettimeofday_override_time.tv_usec); |
Neels Hofmeyr | 7b4d727 | 2016-09-22 04:47:04 +0200 | [diff] [blame] | 201 | osmo_timers_prepare(); |
| 202 | osmo_timers_update(); |
| 203 | osmo_gettimeofday_override_add(0, TIME_BETWEEN_TIMER_CHECKS); |
Pablo Neira Ayuso | 9ebc505 | 2011-09-26 11:46:32 +0200 | [diff] [blame] | 204 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 205 | #else |
Neels Hofmeyr | 255dac1 | 2016-09-22 04:48:32 +0200 | [diff] [blame] | 206 | printf("Select not supported on this platform!\n"); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 207 | #endif |
Neels Hofmeyr | 7b4d727 | 2016-09-22 04:47:04 +0200 | [diff] [blame] | 208 | return 0; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 209 | } |