aboutsummaryrefslogtreecommitdiffstats
path: root/res/res_timing_timerfd.c
blob: 3e686f81054a4e6f8f73180e19aabdff0998b82e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2008, Digium, Inc.
 *
 * Mark Michelson <mmichelson@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*!
 * \file
 * \author Mark Michelson <mmichelson@digium.com>
 *
 * \brief timerfd timing interface
 */

/*** MODULEINFO
	<depend>timerfd</depend>
 ***/

#include "asterisk.h"

#include <sys/timerfd.h>

#include "asterisk/module.h"
#include "asterisk/astobj2.h"
#include "asterisk/timing.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
#include "asterisk/time.h"

static void *timing_funcs_handle;

static int timerfd_timer_open(void);
static void timerfd_timer_close(int handle);
static int timerfd_timer_set_rate(int handle, unsigned int rate);
static void timerfd_timer_ack(int handle, unsigned int quantity);
static int timerfd_timer_enable_continuous(int handle);
static int timerfd_timer_disable_continuous(int handle);
static enum ast_timer_event timerfd_timer_get_event(int handle);
static unsigned int timerfd_timer_get_max_rate(int handle);

static struct ast_timing_interface timerfd_timing = {
	.name = "timerfd",
	.priority = 200,
	.timer_open = timerfd_timer_open,
	.timer_close = timerfd_timer_close,
	.timer_set_rate = timerfd_timer_set_rate,
	.timer_ack = timerfd_timer_ack,
	.timer_enable_continuous = timerfd_timer_enable_continuous,
	.timer_disable_continuous = timerfd_timer_disable_continuous,
	.timer_get_event = timerfd_timer_get_event,
	.timer_get_max_rate = timerfd_timer_get_max_rate,
};

static struct ao2_container *timerfd_timers;

#define TIMERFD_TIMER_BUCKETS 563
#define TIMERFD_MAX_RATE 1000

struct timerfd_timer {
	int handle;
	struct itimerspec saved_timer;
	unsigned int is_continuous:1;
};

static int timerfd_timer_hash(const void *obj, const int flags)
{
	const struct timerfd_timer *timer = obj;

	return timer->handle;
}

static int timerfd_timer_cmp(void *obj, void *args, int flags)
{
	struct timerfd_timer *timer1 = obj, *timer2 = args;
	return timer1->handle == timer2->handle ? CMP_MATCH | CMP_STOP : 0;
}

static void timer_destroy(void *obj)
{
	struct timerfd_timer *timer = obj;
	close(timer->handle);
}

static int timerfd_timer_open(void)
{
	struct timerfd_timer *timer;
	int handle;

	if (!(timer = ao2_alloc(sizeof(*timer), timer_destroy))) {
		ast_log(LOG_ERROR, "Could not allocate memory for timerfd_timer structure\n");
		return -1;
	}
	if ((handle = timerfd_create(CLOCK_MONOTONIC, 0)) < 0) {
		ast_log(LOG_ERROR, "Failed to create timerfd timer: %s\n", strerror(errno));
		ao2_ref(timer, -1);
		return -1;
	}

	timer->handle = handle;
	ao2_link(timerfd_timers, timer);
	/* Get rid of the reference from the allocation */
	ao2_ref(timer, -1);
	return handle;
}

static void timerfd_timer_close(int handle)
{
	struct timerfd_timer *our_timer, find_helper = {
		.handle = handle,
	};

	if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
		ast_log(LOG_ERROR, "Couldn't find timer with handle %d\n", handle);
		return;
	}

	ao2_unlink(timerfd_timers, our_timer);
	ao2_ref(our_timer, -1);
}

static int timerfd_timer_set_rate(int handle, unsigned int rate)
{
	struct timerfd_timer *our_timer, find_helper = {
		.handle = handle,
	};
	int res = 0;

	if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
		ast_log(LOG_ERROR, "Couldn't find timer with handle %d\n", handle);
		return -1;
	}
	ao2_lock(our_timer);

	our_timer->saved_timer.it_value.tv_sec = 0;
	our_timer->saved_timer.it_value.tv_nsec = rate ? (long) (1000000000 / rate) : 0L;
	our_timer->saved_timer.it_interval.tv_sec = our_timer->saved_timer.it_value.tv_sec;
	our_timer->saved_timer.it_interval.tv_nsec = our_timer->saved_timer.it_value.tv_nsec;

	if (!our_timer->is_continuous) {
		res = timerfd_settime(handle, 0, &our_timer->saved_timer, NULL);
	}

	ao2_unlock(our_timer);
	ao2_ref(our_timer, -1);

	return res;
}

static void timerfd_timer_ack(int handle, unsigned int quantity)
{
	uint64_t expirations;
	int read_result = 0;

	struct timerfd_timer *our_timer, find_helper = {
		.handle = handle,
	};

	if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
		ast_log(LOG_ERROR, "Couldn't find timer with handle %d\n", handle);
		return;
	}

	if (our_timer->saved_timer.it_value.tv_nsec == 0L) {
		ast_log(LOG_DEBUG, "Reading attempt on idle timerfd.\n");
		return;
	}

	do {
		struct itimerspec timer_status;

		if (timerfd_gettime(handle, &timer_status)) {
			ast_log(LOG_ERROR, "Call to timerfd_gettime() error: %s\n", strerror(errno));
			expirations = 0;
			break;
		}

		if ((timer_status.it_value.tv_sec == 0) && (timer_status.it_value.tv_nsec == 0)) {
			ast_log(LOG_DEBUG, "Call to timerfd_timer_ack() with disarmed timer - break now.\n");
			expirations = 0;
			break;
		}

		read_result = read(handle, &expirations, sizeof(expirations));
		if (read_result == -1) {
			if (errno == EINTR || errno == EAGAIN) {
				continue;
			} else {
				ast_log(LOG_ERROR, "Read error: %s\n", strerror(errno));
				break;
			}
		}
	} while (read_result != sizeof(expirations));

	if (expirations != quantity) {
		ast_debug(2, "Expected to acknowledge %u ticks but got %llu instead\n", quantity, (unsigned long long) expirations);
	}
}

static int timerfd_timer_enable_continuous(int handle)
{
	int res;
	struct itimerspec continuous_timer = {
		.it_value.tv_nsec = 1L,
	};
	struct timerfd_timer *our_timer, find_helper = {
		.handle = handle,
	};

	if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
		ast_log(LOG_ERROR, "Couldn't find timer with handle %d\n", handle);
		return -1;
	}
	ao2_lock(our_timer);

	if (our_timer->is_continuous) {
		/*It's already in continous mode, no need to do
		 * anything further
		 */
		ao2_unlock(our_timer);
		ao2_ref(our_timer, -1);
		return 0;
	}

	res = timerfd_settime(handle, 0, &continuous_timer, &our_timer->saved_timer);
	our_timer->is_continuous = 1;
	ao2_unlock(our_timer);
	ao2_ref(our_timer, -1);
	return res;
}

static int timerfd_timer_disable_continuous(int handle)
{
	int res;
	struct timerfd_timer *our_timer, find_helper = {
		.handle = handle,
	};

	if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
		ast_log(LOG_ERROR, "Couldn't find timer with handle %d\n", handle);
		return -1;
	}
	ao2_lock(our_timer);

	if(!our_timer->is_continuous) {
		/* No reason to do anything if we're not
		 * in continuous mode
		 */
		ao2_unlock(our_timer);
		ao2_ref(our_timer, -1);
		return 0;
	}

	res = timerfd_settime(handle, 0, &our_timer->saved_timer, NULL);
	our_timer->is_continuous = 0;
	memset(&our_timer->saved_timer, 0, sizeof(our_timer->saved_timer));
	ao2_unlock(our_timer);
	ao2_ref(our_timer, -1);
	return res;
}

static enum ast_timer_event timerfd_timer_get_event(int handle)
{
	enum ast_timer_event res;
	struct timerfd_timer *our_timer, find_helper = {
		.handle = handle,
	};

	if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
		ast_log(LOG_ERROR, "Couldn't find timer with handle %d\n", handle);
		return -1;
	}
	ao2_lock(our_timer);

	if (our_timer->is_continuous) {
		res = AST_TIMING_EVENT_CONTINUOUS;
	} else {
		res = AST_TIMING_EVENT_EXPIRED;
	}

	ao2_unlock(our_timer);
	ao2_ref(our_timer, -1);
	return res;
}

static unsigned int timerfd_timer_get_max_rate(int handle)
{
	return TIMERFD_MAX_RATE;
}

static int load_module(void)
{
	int fd;

	/* Make sure we support the necessary clock type */
	if ((fd = timerfd_create(CLOCK_MONOTONIC, 0)) < 0) {
		ast_log(LOG_ERROR, "timerfd_create() not supported by the kernel.  Not loading.\n");
		return AST_MODULE_LOAD_DECLINE;
	}

	close(fd);

	if (!(timerfd_timers = ao2_container_alloc(TIMERFD_TIMER_BUCKETS, timerfd_timer_hash, timerfd_timer_cmp))) {
		return AST_MODULE_LOAD_DECLINE;
	}

	if (!(timing_funcs_handle = ast_register_timing_interface(&timerfd_timing))) {
		ao2_ref(timerfd_timers, -1);
		return AST_MODULE_LOAD_DECLINE;
	}

	return AST_MODULE_LOAD_SUCCESS;
}

static int unload_module(void)
{
	int res;

	if (!(res = ast_unregister_timing_interface(timing_funcs_handle))) {
		ao2_ref(timerfd_timers, -1);
		timerfd_timers = NULL;
	}

	return res;
}

AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Timerfd Timing Interface",
		.load = load_module,
		.unload = unload_module,
		.load_pri = AST_MODPRI_CHANNEL_DEPEND,
		);