1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <sys/timerfd.h>
13 #include <rte_memory.h>
14 #include <rte_interrupts.h>
15 #include <rte_alarm.h>
16 #include <rte_common.h>
17 #include <rte_per_lcore.h>
19 #include <rte_launch.h>
20 #include <rte_lcore.h>
21 #include <rte_errno.h>
22 #include <rte_spinlock.h>
23 #include <rte_eal_trace.h>
25 #include <eal_private.h>
29 #define TFD_NONBLOCK O_NONBLOCK
32 #define NS_PER_US 1000
33 #define US_PER_MS 1000
36 #define US_PER_S (US_PER_MS * MS_PER_S)
39 #ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */
40 #define CLOCK_TYPE_ID CLOCK_MONOTONIC_RAW
42 #define CLOCK_TYPE_ID CLOCK_MONOTONIC
46 LIST_ENTRY(alarm_entry) next;
48 rte_eal_alarm_callback cb_fn;
50 volatile uint8_t executing;
51 volatile pthread_t executing_id;
54 static LIST_HEAD(alarm_list, alarm_entry) alarm_list = LIST_HEAD_INITIALIZER();
55 static rte_spinlock_t alarm_list_lk = RTE_SPINLOCK_INITIALIZER;
57 static struct rte_intr_handle intr_handle = {.fd = -1 };
58 static int handler_registered = 0;
59 static void eal_alarm_callback(void *arg);
62 rte_eal_alarm_init(void)
64 intr_handle.type = RTE_INTR_HANDLE_ALARM;
65 /* create a timerfd file descriptor */
66 intr_handle.fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
67 if (intr_handle.fd == -1)
78 eal_alarm_callback(void *arg __rte_unused)
81 struct alarm_entry *ap;
83 rte_spinlock_lock(&alarm_list_lk);
84 while ((ap = LIST_FIRST(&alarm_list)) !=NULL &&
85 clock_gettime(CLOCK_TYPE_ID, &now) == 0 &&
86 (ap->time.tv_sec < now.tv_sec || (ap->time.tv_sec == now.tv_sec &&
87 (ap->time.tv_usec * NS_PER_US) <= now.tv_nsec))) {
89 ap->executing_id = pthread_self();
90 rte_spinlock_unlock(&alarm_list_lk);
92 ap->cb_fn(ap->cb_arg);
94 rte_spinlock_lock(&alarm_list_lk);
96 LIST_REMOVE(ap, next);
100 if (!LIST_EMPTY(&alarm_list)) {
101 struct itimerspec atime = { .it_interval = { 0, 0 } };
103 ap = LIST_FIRST(&alarm_list);
104 atime.it_value.tv_sec = ap->time.tv_sec;
105 atime.it_value.tv_nsec = ap->time.tv_usec * NS_PER_US;
106 /* perform borrow for subtraction if necessary */
107 if (now.tv_nsec > (ap->time.tv_usec * NS_PER_US))
108 atime.it_value.tv_sec--, atime.it_value.tv_nsec += US_PER_S * NS_PER_US;
110 atime.it_value.tv_sec -= now.tv_sec;
111 atime.it_value.tv_nsec -= now.tv_nsec;
112 timerfd_settime(intr_handle.fd, 0, &atime, NULL);
114 rte_spinlock_unlock(&alarm_list_lk);
118 rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg)
122 struct alarm_entry *ap, *new_alarm;
124 /* Check parameters, including that us won't cause a uint64_t overflow */
125 if (us < 1 || us > (UINT64_MAX - US_PER_S) || cb_fn == NULL)
128 new_alarm = calloc(1, sizeof(*new_alarm));
129 if (new_alarm == NULL)
132 /* use current time to calculate absolute time of alarm */
133 clock_gettime(CLOCK_TYPE_ID, &now);
135 new_alarm->cb_fn = cb_fn;
136 new_alarm->cb_arg = cb_arg;
137 new_alarm->time.tv_usec = ((now.tv_nsec / NS_PER_US) + us) % US_PER_S;
138 new_alarm->time.tv_sec = now.tv_sec + (((now.tv_nsec / NS_PER_US) + us) / US_PER_S);
140 rte_spinlock_lock(&alarm_list_lk);
141 if (!handler_registered) {
142 /* registration can fail, callback can be registered later */
143 if (rte_intr_callback_register(&intr_handle,
144 eal_alarm_callback, NULL) == 0)
145 handler_registered = 1;
148 if (LIST_EMPTY(&alarm_list))
149 LIST_INSERT_HEAD(&alarm_list, new_alarm, next);
151 LIST_FOREACH(ap, &alarm_list, next) {
152 if (ap->time.tv_sec > new_alarm->time.tv_sec ||
153 (ap->time.tv_sec == new_alarm->time.tv_sec &&
154 ap->time.tv_usec > new_alarm->time.tv_usec)){
155 LIST_INSERT_BEFORE(ap, new_alarm, next);
158 if (LIST_NEXT(ap, next) == NULL) {
159 LIST_INSERT_AFTER(ap, new_alarm, next);
165 if (LIST_FIRST(&alarm_list) == new_alarm) {
166 struct itimerspec alarm_time = {
167 .it_interval = {0, 0},
169 .tv_sec = us / US_PER_S,
170 .tv_nsec = (us % US_PER_S) * NS_PER_US,
173 ret |= timerfd_settime(intr_handle.fd, 0, &alarm_time, NULL);
175 rte_spinlock_unlock(&alarm_list_lk);
177 rte_eal_trace_alarm_set(us, cb_fn, cb_arg, ret);
182 rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
184 struct alarm_entry *ap, *ap_prev;
196 rte_spinlock_lock(&alarm_list_lk);
197 /* remove any matches at the start of the list */
198 while ((ap = LIST_FIRST(&alarm_list)) != NULL &&
199 cb_fn == ap->cb_fn &&
200 (cb_arg == (void *)-1 || cb_arg == ap->cb_arg)) {
202 if (ap->executing == 0) {
203 LIST_REMOVE(ap, next);
207 /* If calling from other context, mark that alarm is executing
208 * so loop can spin till it finish. Otherwise we are trying to
209 * cancel our self - mark it by EINPROGRESS */
210 if (pthread_equal(ap->executing_id, pthread_self()) == 0)
220 /* now go through list, removing entries not at start */
221 LIST_FOREACH(ap, &alarm_list, next) {
222 /* this won't be true first time through */
223 if (cb_fn == ap->cb_fn &&
224 (cb_arg == (void *)-1 || cb_arg == ap->cb_arg)) {
226 if (ap->executing == 0) {
227 LIST_REMOVE(ap, next);
231 } else if (pthread_equal(ap->executing_id, pthread_self()) == 0)
238 rte_spinlock_unlock(&alarm_list_lk);
239 } while (executing != 0);
241 if (count == 0 && err == 0)
246 rte_eal_trace_alarm_cancel(cb_fn, cb_arg, count);