blob: c576a0499c360267a71a7403b0e993d147c465fc [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001/*
2 * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +02003 * (C) 2011 by Harald Welte <laforge@gnumonks.org>
Harald Welteec8b4502010-02-20 20:34:29 +01004 * All Rights Reserved
5 *
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +02006 * Authors: Holger Hans Peter Freyther <zecke@selfish.org>
7 * Pablo Neira Ayuso <pablo@gnumonks.org>
8 *
Harald Welteec8b4502010-02-20 20:34:29 +01009 * 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 Ayuso9ebc5052011-09-26 11:46:32 +020026#include <stdlib.h>
Pablo Neira Ayuso72eb44c2011-11-13 17:40:09 +010027#include <signal.h>
Pablo Neira Ayusof1418372011-11-13 02:02:12 +010028#include <getopt.h>
Harald Welteb53717f2012-08-02 08:42:59 +020029#include <unistd.h>
Harald Welteec8b4502010-02-20 20:34:29 +010030
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020031#include <osmocom/core/talloc.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010032#include <osmocom/core/timer.h>
33#include <osmocom/core/select.h>
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020034#include <osmocom/core/linuxlist.h>
Harald Welteec8b4502010-02-20 20:34:29 +010035
Alex Badea695e5fb2013-01-05 20:59:00 +020036#include "../config.h"
Harald Welteec8b4502010-02-20 20:34:29 +010037
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020038static void main_timer_fired(void *data);
39static void secondary_timer_fired(void *data);
Harald Welteec8b4502010-02-20 20:34:29 +010040
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020041static unsigned int main_timer_step = 0;
42static struct osmo_timer_list main_timer = {
43 .cb = main_timer_fired,
44 .data = &main_timer_step,
Harald Welteec8b4502010-02-20 20:34:29 +010045};
46
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020047static LLIST_HEAD(timer_test_list);
48
49struct test_timer {
50 struct llist_head head;
51 struct osmo_timer_list timer;
52 struct timeval start;
53 struct timeval stop;
Harald Welteec8b4502010-02-20 20:34:29 +010054};
55
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020056/* number of test steps. We add fact(steps) timers in the whole test. */
57#define MAIN_TIMER_NSTEPS 16
Harald Welteec8b4502010-02-20 20:34:29 +010058
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020059/* 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 Freyther00a12fe2012-04-05 09:47:59 +020064#define TIMER_PRES_USECS 20000
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020065
Pablo Neira Ayusof1418372011-11-13 02:02:12 +010066static int timer_nsteps = MAIN_TIMER_NSTEPS;
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020067static unsigned int expired_timers = 0;
68static unsigned int total_timers = 0;
69static unsigned int too_late = 0;
Neels Hofmeyr9c9a0472016-09-22 04:23:34 +020070static unsigned int too_soon = 0;
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020071
72static void main_timer_fired(void *data)
Harald Welteec8b4502010-02-20 20:34:29 +010073{
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020074 unsigned int *step = data;
75 unsigned int add_in_this_step;
76 int i;
Harald Welteec8b4502010-02-20 20:34:29 +010077
Pablo Neira Ayusof1418372011-11-13 02:02:12 +010078 if (*step == timer_nsteps) {
Pablo Neira Ayuso7a0ca162011-11-13 17:22:33 +010079 fprintf(stderr, "Main timer has finished, please, "
80 "wait a bit for the final report.\n");
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020081 return;
82 }
83 /* add 2^step pair of timers per step. */
84 add_in_this_step = (1 << *step);
85
86 for (i=0; i<add_in_this_step; i++) {
87 struct test_timer *v;
88
89 v = talloc_zero(NULL, struct test_timer);
90 if (v == NULL) {
91 fprintf(stderr, "timer_test: OOM!\n");
92 return;
93 }
94 gettimeofday(&v->start, NULL);
95 v->timer.cb = secondary_timer_fired;
96 v->timer.data = v;
97 unsigned int seconds = (random() % 10) + 1;
98 v->stop.tv_sec = v->start.tv_sec + seconds;
Neels Hofmeyr633a0e72016-09-22 04:24:38 +020099 v->stop.tv_usec = v->start.tv_usec;
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200100 osmo_timer_schedule(&v->timer, seconds, 0);
101 llist_add(&v->head, &timer_test_list);
102 }
Pablo Neira Ayuso7a0ca162011-11-13 17:22:33 +0100103 fprintf(stderr, "added %d timers in step %u (expired=%u)\n",
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200104 add_in_this_step, *step, expired_timers);
105 total_timers += add_in_this_step;
106 osmo_timer_schedule(&main_timer, TIME_BETWEEN_STEPS, 0);
107 (*step)++;
108}
109
110static void secondary_timer_fired(void *data)
111{
112 struct test_timer *v = data, *this, *tmp;
113 struct timeval current, res, precision = { 1, 0 };
114
115 gettimeofday(&current, NULL);
116
117 timersub(&current, &v->stop, &res);
118 if (timercmp(&res, &precision, >)) {
Neels Hofmeyr9c9a0472016-09-22 04:23:34 +0200119 fprintf(stderr, "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
124 );
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200125 too_late++;
126 }
Neels Hofmeyr9c9a0472016-09-22 04:23:34 +0200127 else if (timercmp(&current, &v->stop, <)) {
128 fprintf(stderr, "ERROR: timer has expired too soon:"
129 " wanted %d.%06d now %d.%06d diff %d.%06d\n",
130 (int)v->stop.tv_sec, (int)v->stop.tv_usec,
131 (int)current.tv_sec, (int)current.tv_usec,
132 (int)res.tv_sec, (int)res.tv_usec
133 );
134 too_soon++;
135 }
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200136
137 llist_del(&v->head);
138 talloc_free(data);
139 expired_timers++;
140 if (expired_timers == total_timers) {
Neels Hofmeyr9c9a0472016-09-22 04:23:34 +0200141 fprintf(stdout, "test over: added=%u expired=%u too_soon=%u too_late=%u\n",
142 total_timers, expired_timers, too_soon, too_late);
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200143 exit(EXIT_SUCCESS);
144 }
145
146 /* randomly (10%) deletion of timers. */
147 llist_for_each_entry_safe(this, tmp, &timer_test_list, head) {
148 if ((random() % 100) < 10) {
149 osmo_timer_del(&this->timer);
150 llist_del(&this->head);
151 talloc_free(this);
152 expired_timers++;
153 }
154 }
Harald Welteec8b4502010-02-20 20:34:29 +0100155}
156
Pablo Neira Ayuso72eb44c2011-11-13 17:40:09 +0100157static void alarm_handler(int signum)
158{
159 fprintf(stderr, "ERROR: We took too long to run the timer test, "
160 "something seems broken, aborting.\n");
161 exit(EXIT_FAILURE);
162}
163
Pablo Neira Ayusof1418372011-11-13 02:02:12 +0100164int main(int argc, char *argv[])
Harald Welteec8b4502010-02-20 20:34:29 +0100165{
Pablo Neira Ayusof1418372011-11-13 02:02:12 +0100166 int c;
167
Pablo Neira Ayuso72eb44c2011-11-13 17:40:09 +0100168 if (signal(SIGALRM, alarm_handler) == SIG_ERR) {
169 perror("cannot register signal handler");
170 exit(EXIT_FAILURE);
171 }
172
Pablo Neira Ayusof1418372011-11-13 02:02:12 +0100173 while ((c = getopt_long(argc, argv, "s:", NULL, NULL)) != -1) {
174 switch(c) {
175 case 's':
176 timer_nsteps = atoi(optarg);
177 if (timer_nsteps <= 0) {
178 fprintf(stderr, "%s: steps must be > 0\n",
179 argv[0]);
180 exit(EXIT_FAILURE);
181 }
182 break;
183 default:
184 exit(EXIT_FAILURE);
185 }
186 }
187
Pablo Neira Ayuso7a0ca162011-11-13 17:22:33 +0100188 fprintf(stdout, "Running timer test for %u steps, accepting "
189 "imprecision of %u.%.6u seconds\n",
Pablo Neira Ayusof1418372011-11-13 02:02:12 +0100190 timer_nsteps, TIMER_PRES_SECS, TIMER_PRES_USECS);
Harald Welteec8b4502010-02-20 20:34:29 +0100191
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200192 osmo_timer_schedule(&main_timer, 1, 0);
Harald Welteec8b4502010-02-20 20:34:29 +0100193
Pablo Neira Ayuso72eb44c2011-11-13 17:40:09 +0100194 /* if the test takes too long, we may consider that the timer scheduler
195 * has hung. We set some maximum wait time which is the double of the
196 * maximum timeout randomly set (10 seconds, worst case) plus the
197 * number of steps (since some of them are reset each step). */
198 alarm(2 * (10 + timer_nsteps));
199
Harald Welteec8b4502010-02-20 20:34:29 +0100200#ifdef HAVE_SYS_SELECT_H
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200201 while (1) {
202 osmo_select_main(0);
203 }
Harald Welteec8b4502010-02-20 20:34:29 +0100204#else
Pablo Neira Ayuso7a0ca162011-11-13 17:22:33 +0100205 fprintf(stdout, "Select not supported on this platform!\n");
Harald Welteec8b4502010-02-20 20:34:29 +0100206#endif
207}