blob: 4981fd528c38b43e6d566b8676928dc6a09c0f77 [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;
70
71static void main_timer_fired(void *data)
Harald Welteec8b4502010-02-20 20:34:29 +010072{
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020073 unsigned int *step = data;
74 unsigned int add_in_this_step;
75 int i;
Harald Welteec8b4502010-02-20 20:34:29 +010076
Pablo Neira Ayusof1418372011-11-13 02:02:12 +010077 if (*step == timer_nsteps) {
Pablo Neira Ayuso7a0ca162011-11-13 17:22:33 +010078 fprintf(stderr, "Main timer has finished, please, "
79 "wait a bit for the final report.\n");
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020080 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;
Neels Hofmeyr633a0e72016-09-22 04:24:38 +020098 v->stop.tv_usec = v->start.tv_usec;
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020099 osmo_timer_schedule(&v->timer, seconds, 0);
100 llist_add(&v->head, &timer_test_list);
101 }
Pablo Neira Ayuso7a0ca162011-11-13 17:22:33 +0100102 fprintf(stderr, "added %d timers in step %u (expired=%u)\n",
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200103 add_in_this_step, *step, expired_timers);
104 total_timers += add_in_this_step;
105 osmo_timer_schedule(&main_timer, TIME_BETWEEN_STEPS, 0);
106 (*step)++;
107}
108
109static void secondary_timer_fired(void *data)
110{
111 struct test_timer *v = data, *this, *tmp;
112 struct timeval current, res, precision = { 1, 0 };
113
114 gettimeofday(&current, NULL);
115
116 timersub(&current, &v->stop, &res);
117 if (timercmp(&res, &precision, >)) {
Pablo Neira Ayuso7a0ca162011-11-13 17:22:33 +0100118 fprintf(stderr, "ERROR: timer %p has expired too late!\n",
Jacob Erlbeck73ae7a92013-10-08 12:04:42 +0200119 &v->timer);
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200120 too_late++;
121 }
122
123 llist_del(&v->head);
124 talloc_free(data);
125 expired_timers++;
126 if (expired_timers == total_timers) {
Pablo Neira Ayuso7a0ca162011-11-13 17:22:33 +0100127 fprintf(stdout, "test over: added=%u expired=%u too_late=%u \n",
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200128 total_timers, expired_timers, too_late);
129 exit(EXIT_SUCCESS);
130 }
131
132 /* randomly (10%) deletion of timers. */
133 llist_for_each_entry_safe(this, tmp, &timer_test_list, head) {
134 if ((random() % 100) < 10) {
135 osmo_timer_del(&this->timer);
136 llist_del(&this->head);
137 talloc_free(this);
138 expired_timers++;
139 }
140 }
Harald Welteec8b4502010-02-20 20:34:29 +0100141}
142
Pablo Neira Ayuso72eb44c2011-11-13 17:40:09 +0100143static void alarm_handler(int signum)
144{
145 fprintf(stderr, "ERROR: We took too long to run the timer test, "
146 "something seems broken, aborting.\n");
147 exit(EXIT_FAILURE);
148}
149
Pablo Neira Ayusof1418372011-11-13 02:02:12 +0100150int main(int argc, char *argv[])
Harald Welteec8b4502010-02-20 20:34:29 +0100151{
Pablo Neira Ayusof1418372011-11-13 02:02:12 +0100152 int c;
153
Pablo Neira Ayuso72eb44c2011-11-13 17:40:09 +0100154 if (signal(SIGALRM, alarm_handler) == SIG_ERR) {
155 perror("cannot register signal handler");
156 exit(EXIT_FAILURE);
157 }
158
Pablo Neira Ayusof1418372011-11-13 02:02:12 +0100159 while ((c = getopt_long(argc, argv, "s:", NULL, NULL)) != -1) {
160 switch(c) {
161 case 's':
162 timer_nsteps = atoi(optarg);
163 if (timer_nsteps <= 0) {
164 fprintf(stderr, "%s: steps must be > 0\n",
165 argv[0]);
166 exit(EXIT_FAILURE);
167 }
168 break;
169 default:
170 exit(EXIT_FAILURE);
171 }
172 }
173
Pablo Neira Ayuso7a0ca162011-11-13 17:22:33 +0100174 fprintf(stdout, "Running timer test for %u steps, accepting "
175 "imprecision of %u.%.6u seconds\n",
Pablo Neira Ayusof1418372011-11-13 02:02:12 +0100176 timer_nsteps, TIMER_PRES_SECS, TIMER_PRES_USECS);
Harald Welteec8b4502010-02-20 20:34:29 +0100177
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200178 osmo_timer_schedule(&main_timer, 1, 0);
Harald Welteec8b4502010-02-20 20:34:29 +0100179
Pablo Neira Ayuso72eb44c2011-11-13 17:40:09 +0100180 /* if the test takes too long, we may consider that the timer scheduler
181 * has hung. We set some maximum wait time which is the double of the
182 * maximum timeout randomly set (10 seconds, worst case) plus the
183 * number of steps (since some of them are reset each step). */
184 alarm(2 * (10 + timer_nsteps));
185
Harald Welteec8b4502010-02-20 20:34:29 +0100186#ifdef HAVE_SYS_SELECT_H
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200187 while (1) {
188 osmo_select_main(0);
189 }
Harald Welteec8b4502010-02-20 20:34:29 +0100190#else
Pablo Neira Ayuso7a0ca162011-11-13 17:22:33 +0100191 fprintf(stdout, "Select not supported on this platform!\n");
Harald Welteec8b4502010-02-20 20:34:29 +0100192#endif
193}