blob: 12caecfdfae2dfd6526b5c43e521752300dd9ce6 [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
Neels Hofmeyr7b4d7272016-09-22 04:47:04 +020062/* how much time elapses between checks, in microsecs */
63#define TIME_BETWEEN_TIMER_CHECKS 423210
64
Pablo Neira Ayusof1418372011-11-13 02:02:12 +010065static int timer_nsteps = MAIN_TIMER_NSTEPS;
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020066static unsigned int expired_timers = 0;
67static unsigned int total_timers = 0;
68static unsigned int too_late = 0;
Neels Hofmeyr9c9a0472016-09-22 04:23:34 +020069static unsigned int too_soon = 0;
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020070
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) {
Neels Hofmeyr255dac12016-09-22 04:48:32 +020078 printf("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) {
Neels Hofmeyr255dac12016-09-22 04:48:32 +020090 printf("timer_test: OOM!\n");
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020091 return;
92 }
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020093 osmo_gettimeofday(&v->start, NULL);
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020094 v->timer.cb = secondary_timer_fired;
95 v->timer.data = v;
Neels Hofmeyrd73c1cc2016-09-22 05:20:53 +020096 unsigned int seconds = (i & 0x7) + 1;
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +020097 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 }
Neels Hofmeyr255dac12016-09-22 04:48:32 +0200102 printf("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;
Neels Hofmeyr7b4d7272016-09-22 04:47:04 +0200112 struct timeval current, res;
113 struct timeval precision = { 0, TIME_BETWEEN_TIMER_CHECKS + 1};
Neels Hofmeyrd73c1cc2016-09-22 05:20:53 +0200114 int i;
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200115
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200116 osmo_gettimeofday(&current, NULL);
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200117
118 timersub(&current, &v->stop, &res);
119 if (timercmp(&res, &precision, >)) {
Neels Hofmeyr255dac12016-09-22 04:48:32 +0200120 printf("ERROR: timer has expired too late:"
121 " wanted %d.%06d now %d.%06d diff %d.%06d\n",
122 (int)v->stop.tv_sec, (int)v->stop.tv_usec,
123 (int)current.tv_sec, (int)current.tv_usec,
124 (int)res.tv_sec, (int)res.tv_usec);
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, <)) {
Neels Hofmeyr255dac12016-09-22 04:48:32 +0200128 printf("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);
Neels Hofmeyr9c9a0472016-09-22 04:23:34 +0200133 too_soon++;
134 }
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200135
136 llist_del(&v->head);
137 talloc_free(data);
138 expired_timers++;
139 if (expired_timers == total_timers) {
Neels Hofmeyr255dac12016-09-22 04:48:32 +0200140 printf("test over: added=%u expired=%u too_soon=%u too_late=%u\n",
141 total_timers, expired_timers, too_soon, too_late);
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200142 exit(EXIT_SUCCESS);
143 }
144
Neels Hofmeyrd73c1cc2016-09-22 05:20:53 +0200145 /* "random" deletion of timers. */
146 i = 0;
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200147 llist_for_each_entry_safe(this, tmp, &timer_test_list, head) {
Neels Hofmeyrd73c1cc2016-09-22 05:20:53 +0200148 i ++;
149 if (!(i & 0x3)) {
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200150 osmo_timer_del(&this->timer);
151 llist_del(&this->head);
152 talloc_free(this);
153 expired_timers++;
154 }
155 }
Harald Welteec8b4502010-02-20 20:34:29 +0100156}
157
Pablo Neira Ayusof1418372011-11-13 02:02:12 +0100158int main(int argc, char *argv[])
Harald Welteec8b4502010-02-20 20:34:29 +0100159{
Pablo Neira Ayusof1418372011-11-13 02:02:12 +0100160 int c;
Neels Hofmeyr7b4d7272016-09-22 04:47:04 +0200161 int steps;
Pablo Neira Ayusof1418372011-11-13 02:02:12 +0100162
Neels Hofmeyr7b4d7272016-09-22 04:47:04 +0200163 osmo_gettimeofday_override = true;
Pablo Neira Ayuso72eb44c2011-11-13 17:40:09 +0100164
Pablo Neira Ayusof1418372011-11-13 02:02:12 +0100165 while ((c = getopt_long(argc, argv, "s:", NULL, NULL)) != -1) {
166 switch(c) {
167 case 's':
168 timer_nsteps = atoi(optarg);
169 if (timer_nsteps <= 0) {
170 fprintf(stderr, "%s: steps must be > 0\n",
171 argv[0]);
172 exit(EXIT_FAILURE);
173 }
174 break;
175 default:
176 exit(EXIT_FAILURE);
177 }
178 }
179
Neels Hofmeyr7b4d7272016-09-22 04:47:04 +0200180 steps = ((MAIN_TIMER_NSTEPS * TIME_BETWEEN_STEPS + 20) * 1e6)
181 / TIME_BETWEEN_TIMER_CHECKS;
182
Neels Hofmeyr255dac12016-09-22 04:48:32 +0200183 printf("Running timer test for %u steps\n", timer_nsteps);
Harald Welteec8b4502010-02-20 20:34:29 +0100184
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200185 osmo_timer_schedule(&main_timer, 1, 0);
Harald Welteec8b4502010-02-20 20:34:29 +0100186
187#ifdef HAVE_SYS_SELECT_H
Neels Hofmeyr7b4d7272016-09-22 04:47:04 +0200188 while (steps--) {
189 osmo_timers_prepare();
190 osmo_timers_update();
191 osmo_gettimeofday_override_add(0, TIME_BETWEEN_TIMER_CHECKS);
Pablo Neira Ayuso9ebc5052011-09-26 11:46:32 +0200192 }
Harald Welteec8b4502010-02-20 20:34:29 +0100193#else
Neels Hofmeyr255dac12016-09-22 04:48:32 +0200194 printf("Select not supported on this platform!\n");
Harald Welteec8b4502010-02-20 20:34:29 +0100195#endif
Neels Hofmeyr7b4d7272016-09-22 04:47:04 +0200196 return 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100197}