1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
14 #include <rte_alarm.h>
15 #include <rte_cycles.h>
16 #include <rte_common.h>
17 #include <rte_errno.h>
18 #include <rte_interrupts.h>
19 #include <rte_spinlock.h>
20 #include <rte_eal_trace.h>
22 #include "eal_private.h"
23 #include "eal_alarm_private.h"
25 #define NS_PER_US 1000
27 #ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */
28 #define CLOCK_TYPE_ID CLOCK_MONOTONIC_RAW
30 #define CLOCK_TYPE_ID CLOCK_MONOTONIC
34 LIST_ENTRY(alarm_entry) next;
35 struct rte_intr_handle handle;
37 rte_eal_alarm_callback cb_fn;
39 volatile uint8_t executing;
40 volatile pthread_t executing_id;
43 static LIST_HEAD(alarm_list, alarm_entry) alarm_list = LIST_HEAD_INITIALIZER();
44 static rte_spinlock_t alarm_list_lk = RTE_SPINLOCK_INITIALIZER;
46 static struct rte_intr_handle intr_handle = {.fd = -1 };
47 static void eal_alarm_callback(void *arg);
50 rte_eal_alarm_init(void)
52 intr_handle.type = RTE_INTR_HANDLE_ALARM;
54 /* on FreeBSD, timers don't use fd's, and their identifiers are stored
55 * in separate namespace from fd's, so using any value is OK. however,
56 * EAL interrupts handler expects fd's to be unique, so use an actual fd
57 * to guarantee unique timer identifier.
59 intr_handle.fd = open("/dev/zero", O_RDONLY);
65 timespec_cmp(const struct timespec *now, const struct timespec *at)
67 if (now->tv_sec < at->tv_sec)
69 if (now->tv_sec > at->tv_sec)
71 if (now->tv_nsec < at->tv_nsec)
73 if (now->tv_nsec > at->tv_nsec)
78 static inline uint64_t
79 diff_ns(struct timespec *now, struct timespec *at)
81 uint64_t now_ns, at_ns;
83 if (timespec_cmp(now, at) >= 0)
86 now_ns = now->tv_sec * NS_PER_S + now->tv_nsec;
87 at_ns = at->tv_sec * NS_PER_S + at->tv_nsec;
89 return at_ns - now_ns;
93 eal_alarm_get_timeout_ns(uint64_t *val)
95 struct alarm_entry *ap;
98 if (clock_gettime(CLOCK_TYPE_ID, &now) < 0)
101 if (LIST_EMPTY(&alarm_list))
104 ap = LIST_FIRST(&alarm_list);
106 *val = diff_ns(&now, &ap->time);
112 unregister_current_callback(void)
114 struct alarm_entry *ap;
117 if (!LIST_EMPTY(&alarm_list)) {
118 ap = LIST_FIRST(&alarm_list);
121 ret = rte_intr_callback_unregister(&intr_handle,
122 eal_alarm_callback, &ap->time);
123 } while (ret == -EAGAIN);
130 register_first_callback(void)
132 struct alarm_entry *ap;
135 if (!LIST_EMPTY(&alarm_list)) {
136 ap = LIST_FIRST(&alarm_list);
138 /* register a new callback */
139 ret = rte_intr_callback_register(&intr_handle,
140 eal_alarm_callback, &ap->time);
146 eal_alarm_callback(void *arg __rte_unused)
149 struct alarm_entry *ap;
151 rte_spinlock_lock(&alarm_list_lk);
152 ap = LIST_FIRST(&alarm_list);
154 if (clock_gettime(CLOCK_TYPE_ID, &now) < 0)
157 while (ap != NULL && timespec_cmp(&now, &ap->time) >= 0) {
159 ap->executing_id = pthread_self();
160 rte_spinlock_unlock(&alarm_list_lk);
162 ap->cb_fn(ap->cb_arg);
164 rte_spinlock_lock(&alarm_list_lk);
166 LIST_REMOVE(ap, next);
169 ap = LIST_FIRST(&alarm_list);
172 /* timer has been deleted from the kqueue, so recreate it if needed */
173 register_first_callback();
175 rte_spinlock_unlock(&alarm_list_lk);
180 rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg)
182 struct alarm_entry *ap, *new_alarm;
187 /* check parameters, also ensure us won't cause a uint64_t overflow */
188 if (us < 1 || us > (UINT64_MAX - US_PER_S) || cb_fn == NULL)
191 new_alarm = calloc(1, sizeof(*new_alarm));
192 if (new_alarm == NULL)
195 /* use current time to calculate absolute time of alarm */
196 clock_gettime(CLOCK_TYPE_ID, &now);
200 new_alarm->cb_fn = cb_fn;
201 new_alarm->cb_arg = cb_arg;
202 new_alarm->time.tv_nsec = (now.tv_nsec + ns) % NS_PER_S;
203 new_alarm->time.tv_sec = now.tv_sec + ((now.tv_nsec + ns) / NS_PER_S);
205 rte_spinlock_lock(&alarm_list_lk);
207 if (LIST_EMPTY(&alarm_list))
208 LIST_INSERT_HEAD(&alarm_list, new_alarm, next);
210 LIST_FOREACH(ap, &alarm_list, next) {
211 if (timespec_cmp(&new_alarm->time, &ap->time) < 0) {
212 LIST_INSERT_BEFORE(ap, new_alarm, next);
215 if (LIST_NEXT(ap, next) == NULL) {
216 LIST_INSERT_AFTER(ap, new_alarm, next);
222 /* re-register first callback just in case */
223 register_first_callback();
225 rte_spinlock_unlock(&alarm_list_lk);
227 rte_eal_trace_alarm_set(us, cb_fn, cb_arg, ret);
232 rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
234 struct alarm_entry *ap, *ap_prev;
246 rte_spinlock_lock(&alarm_list_lk);
247 /* remove any matches at the start of the list */
249 ap = LIST_FIRST(&alarm_list);
252 if (cb_fn != ap->cb_fn)
254 if (cb_arg != ap->cb_arg && cb_arg != (void *) -1)
256 if (ap->executing == 0) {
257 LIST_REMOVE(ap, next);
261 /* If calling from other context, mark that
262 * alarm is executing so loop can spin till it
263 * finish. Otherwise we are trying to cancel
264 * ourselves - mark it by EINPROGRESS.
266 if (pthread_equal(ap->executing_id,
267 pthread_self()) == 0)
277 /* now go through list, removing entries not at start */
278 LIST_FOREACH(ap, &alarm_list, next) {
279 /* this won't be true first time through */
280 if (cb_fn == ap->cb_fn &&
281 (cb_arg == (void *)-1 ||
282 cb_arg == ap->cb_arg)) {
283 if (ap->executing == 0) {
284 LIST_REMOVE(ap, next);
288 } else if (pthread_equal(ap->executing_id,
289 pthread_self()) == 0) {
297 rte_spinlock_unlock(&alarm_list_lk);
298 } while (executing != 0);
300 if (count == 0 && err == 0)
305 rte_spinlock_lock(&alarm_list_lk);
307 /* unregister if no alarms left, otherwise re-register first */
308 if (LIST_EMPTY(&alarm_list))
309 unregister_current_callback();
311 register_first_callback();
313 rte_spinlock_unlock(&alarm_list_lk);
315 rte_eal_trace_alarm_cancel(cb_fn, cb_arg, count);