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;
36 rte_eal_alarm_callback cb_fn;
38 volatile uint8_t executing;
39 volatile pthread_t executing_id;
42 static LIST_HEAD(alarm_list, alarm_entry) alarm_list = LIST_HEAD_INITIALIZER();
43 static rte_spinlock_t alarm_list_lk = RTE_SPINLOCK_INITIALIZER;
45 static struct rte_intr_handle *intr_handle;
46 static void eal_alarm_callback(void *arg);
49 rte_eal_alarm_cleanup(void)
51 rte_intr_instance_free(intr_handle);
55 rte_eal_alarm_init(void)
59 intr_handle = rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
60 if (intr_handle == NULL) {
61 RTE_LOG(ERR, EAL, "Fail to allocate intr_handle\n");
65 if (rte_intr_type_set(intr_handle, RTE_INTR_HANDLE_ALARM))
68 if (rte_intr_fd_set(intr_handle, -1))
71 /* on FreeBSD, timers don't use fd's, and their identifiers are stored
72 * in separate namespace from fd's, so using any value is OK. however,
73 * EAL interrupts handler expects fd's to be unique, so use an actual fd
74 * to guarantee unique timer identifier.
76 fd = open("/dev/zero", O_RDONLY);
78 if (rte_intr_fd_set(intr_handle, fd))
83 rte_intr_instance_free(intr_handle);
88 timespec_cmp(const struct timespec *now, const struct timespec *at)
90 if (now->tv_sec < at->tv_sec)
92 if (now->tv_sec > at->tv_sec)
94 if (now->tv_nsec < at->tv_nsec)
96 if (now->tv_nsec > at->tv_nsec)
101 static inline uint64_t
102 diff_ns(struct timespec *now, struct timespec *at)
104 uint64_t now_ns, at_ns;
106 if (timespec_cmp(now, at) >= 0)
109 now_ns = now->tv_sec * NS_PER_S + now->tv_nsec;
110 at_ns = at->tv_sec * NS_PER_S + at->tv_nsec;
112 return at_ns - now_ns;
116 eal_alarm_get_timeout_ns(uint64_t *val)
118 struct alarm_entry *ap;
121 if (clock_gettime(CLOCK_TYPE_ID, &now) < 0)
124 if (LIST_EMPTY(&alarm_list))
127 ap = LIST_FIRST(&alarm_list);
129 *val = diff_ns(&now, &ap->time);
135 unregister_current_callback(void)
137 struct alarm_entry *ap;
140 if (!LIST_EMPTY(&alarm_list)) {
141 ap = LIST_FIRST(&alarm_list);
144 ret = rte_intr_callback_unregister(intr_handle,
145 eal_alarm_callback, &ap->time);
146 } while (ret == -EAGAIN);
153 register_first_callback(void)
155 struct alarm_entry *ap;
158 if (!LIST_EMPTY(&alarm_list)) {
159 ap = LIST_FIRST(&alarm_list);
161 /* register a new callback */
162 ret = rte_intr_callback_register(intr_handle,
163 eal_alarm_callback, &ap->time);
169 eal_alarm_callback(void *arg __rte_unused)
172 struct alarm_entry *ap;
174 rte_spinlock_lock(&alarm_list_lk);
175 ap = LIST_FIRST(&alarm_list);
177 if (clock_gettime(CLOCK_TYPE_ID, &now) < 0)
180 while (ap != NULL && timespec_cmp(&now, &ap->time) >= 0) {
182 ap->executing_id = pthread_self();
183 rte_spinlock_unlock(&alarm_list_lk);
185 ap->cb_fn(ap->cb_arg);
187 rte_spinlock_lock(&alarm_list_lk);
189 LIST_REMOVE(ap, next);
192 ap = LIST_FIRST(&alarm_list);
195 /* timer has been deleted from the kqueue, so recreate it if needed */
196 register_first_callback();
198 rte_spinlock_unlock(&alarm_list_lk);
203 rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg)
205 struct alarm_entry *ap, *new_alarm;
210 /* check parameters, also ensure us won't cause a uint64_t overflow */
211 if (us < 1 || us > (UINT64_MAX - US_PER_S) || cb_fn == NULL)
214 new_alarm = calloc(1, sizeof(*new_alarm));
215 if (new_alarm == NULL)
218 /* use current time to calculate absolute time of alarm */
219 clock_gettime(CLOCK_TYPE_ID, &now);
223 new_alarm->cb_fn = cb_fn;
224 new_alarm->cb_arg = cb_arg;
225 new_alarm->time.tv_nsec = (now.tv_nsec + ns) % NS_PER_S;
226 new_alarm->time.tv_sec = now.tv_sec + ((now.tv_nsec + ns) / NS_PER_S);
228 rte_spinlock_lock(&alarm_list_lk);
230 if (LIST_EMPTY(&alarm_list))
231 LIST_INSERT_HEAD(&alarm_list, new_alarm, next);
233 LIST_FOREACH(ap, &alarm_list, next) {
234 if (timespec_cmp(&new_alarm->time, &ap->time) < 0) {
235 LIST_INSERT_BEFORE(ap, new_alarm, next);
238 if (LIST_NEXT(ap, next) == NULL) {
239 LIST_INSERT_AFTER(ap, new_alarm, next);
245 /* re-register first callback just in case */
246 register_first_callback();
248 rte_spinlock_unlock(&alarm_list_lk);
250 rte_eal_trace_alarm_set(us, cb_fn, cb_arg, ret);
255 rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
257 struct alarm_entry *ap, *ap_prev;
269 rte_spinlock_lock(&alarm_list_lk);
270 /* remove any matches at the start of the list */
272 ap = LIST_FIRST(&alarm_list);
275 if (cb_fn != ap->cb_fn)
277 if (cb_arg != ap->cb_arg && cb_arg != (void *) -1)
279 if (ap->executing == 0) {
280 LIST_REMOVE(ap, next);
284 /* If calling from other context, mark that
285 * alarm is executing so loop can spin till it
286 * finish. Otherwise we are trying to cancel
287 * ourselves - mark it by EINPROGRESS.
289 if (pthread_equal(ap->executing_id,
290 pthread_self()) == 0)
300 /* now go through list, removing entries not at start */
301 LIST_FOREACH(ap, &alarm_list, next) {
302 /* this won't be true first time through */
303 if (cb_fn == ap->cb_fn &&
304 (cb_arg == (void *)-1 ||
305 cb_arg == ap->cb_arg)) {
306 if (ap->executing == 0) {
307 LIST_REMOVE(ap, next);
311 } else if (pthread_equal(ap->executing_id,
312 pthread_self()) == 0) {
320 rte_spinlock_unlock(&alarm_list_lk);
321 } while (executing != 0);
323 if (count == 0 && err == 0)
328 rte_spinlock_lock(&alarm_list_lk);
330 /* unregister if no alarms left, otherwise re-register first */
331 if (LIST_EMPTY(&alarm_list))
332 unregister_current_callback();
334 register_first_callback();
336 rte_spinlock_unlock(&alarm_list_lk);
338 rte_eal_trace_alarm_cancel(cb_fn, cb_arg, count);