4 * Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/queue.h>
40 #include <rte_atomic.h>
41 #include <rte_common.h>
42 #include <rte_cycles.h>
43 #include <rte_per_lcore.h>
44 #include <rte_memory.h>
45 #include <rte_memzone.h>
46 #include <rte_launch.h>
47 #include <rte_tailq.h>
49 #include <rte_per_lcore.h>
50 #include <rte_lcore.h>
51 #include <rte_branch_prediction.h>
52 #include <rte_spinlock.h>
54 #include "rte_timer.h"
56 LIST_HEAD(rte_timer_list, rte_timer);
59 struct rte_timer_list pending; /**< list of pending timers */
60 struct rte_timer_list expired; /**< list of expired timers */
61 struct rte_timer_list done; /**< list of done timers */
62 rte_spinlock_t list_lock; /**< lock to protect list access */
64 /** per-core variable that true if a timer was updated on this
65 * core since last reset of the variable */
68 unsigned prev_lcore; /**< used for lcore round robin */
70 #ifdef RTE_LIBRTE_TIMER_DEBUG
71 /** per-lcore statistics */
72 struct rte_timer_debug_stats stats;
74 } __rte_cache_aligned;
76 /** per-lcore private info for timers */
77 static struct priv_timer priv_timer[RTE_MAX_LCORE];
79 /* when debug is enabled, store some statistics */
80 #ifdef RTE_LIBRTE_TIMER_DEBUG
81 #define __TIMER_STAT_ADD(name, n) do { \
82 unsigned __lcore_id = rte_lcore_id(); \
83 priv_timer[__lcore_id].stats.name += (n); \
86 #define __TIMER_STAT_ADD(name, n) do {} while(0)
89 /* this macro allow to modify var while browsing the list */
90 #define LIST_FOREACH_SAFE(var, var2, head, field) \
91 for ((var) = ((head)->lh_first), \
92 (var2) = ((var) ? ((var)->field.le_next) : NULL); \
95 (var2) = ((var) ? ((var)->field.le_next) : NULL))
98 /* Init the timer library. */
100 rte_timer_subsystem_init(void)
104 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id ++) {
105 LIST_INIT(&priv_timer[lcore_id].pending);
106 LIST_INIT(&priv_timer[lcore_id].expired);
107 LIST_INIT(&priv_timer[lcore_id].done);
108 rte_spinlock_init(&priv_timer[lcore_id].list_lock);
109 priv_timer[lcore_id].prev_lcore = lcore_id;
113 /* Initialize the timer handle tim for use */
115 rte_timer_init(struct rte_timer *tim)
117 union rte_timer_status status;
119 status.state = RTE_TIMER_STOP;
120 status.owner = RTE_TIMER_NO_OWNER;
121 tim->status.u32 = status.u32;
125 * if timer is pending or stopped (or running on the same core than
126 * us), mark timer as configuring, and on success return the previous
127 * status of the timer
130 timer_set_config_state(struct rte_timer *tim,
131 union rte_timer_status *ret_prev_status)
133 union rte_timer_status prev_status, status;
137 lcore_id = rte_lcore_id();
139 /* wait that the timer is in correct status before update,
140 * and mark it as beeing configured */
141 while (success == 0) {
142 prev_status.u32 = tim->status.u32;
144 /* timer is running on another core, exit */
145 if (prev_status.state == RTE_TIMER_RUNNING &&
146 (unsigned)prev_status.owner != lcore_id)
149 /* timer is beeing configured on another core */
150 if (prev_status.state == RTE_TIMER_CONFIG)
153 /* here, we know that timer is stopped or pending,
154 * mark it atomically as beeing configured */
155 status.state = RTE_TIMER_CONFIG;
156 status.owner = (int16_t)lcore_id;
157 success = rte_atomic32_cmpset(&tim->status.u32,
162 ret_prev_status->u32 = prev_status.u32;
167 * if timer is pending, mark timer as running
170 timer_set_running_state(struct rte_timer *tim)
172 union rte_timer_status prev_status, status;
173 unsigned lcore_id = rte_lcore_id();
176 /* wait that the timer is in correct status before update,
177 * and mark it as running */
178 while (success == 0) {
179 prev_status.u32 = tim->status.u32;
181 /* timer is not pending anymore */
182 if (prev_status.state != RTE_TIMER_PENDING)
185 /* here, we know that timer is stopped or pending,
186 * mark it atomically as beeing configured */
187 status.state = RTE_TIMER_RUNNING;
188 status.owner = (int16_t)lcore_id;
189 success = rte_atomic32_cmpset(&tim->status.u32,
198 * add in list, lock if needed
199 * timer must be in config state
200 * timer must not be in a list
203 timer_add(struct rte_timer *tim, unsigned tim_lcore, int local_is_locked)
205 uint64_t cur_time = rte_get_timer_cycles();
206 unsigned lcore_id = rte_lcore_id();
207 struct rte_timer *t, *t_prev;
209 /* if timer needs to be scheduled on another core, we need to
210 * lock the list; if it is on local core, we need to lock if
211 * we are not called from rte_timer_manage() */
212 if (tim_lcore != lcore_id || !local_is_locked)
213 rte_spinlock_lock(&priv_timer[tim_lcore].list_lock);
215 t = LIST_FIRST(&priv_timer[tim_lcore].pending);
217 /* list is empty or 'tim' will expire before 't' */
218 if (t == NULL || ((int64_t)(tim->expire - cur_time) <
219 (int64_t)(t->expire - cur_time))) {
220 LIST_INSERT_HEAD(&priv_timer[tim_lcore].pending, tim, next);
225 /* find an element that will expire after 'tim' */
226 LIST_FOREACH(t, &priv_timer[tim_lcore].pending, next) {
227 if ((int64_t)(tim->expire - cur_time) <
228 (int64_t)(t->expire - cur_time)) {
229 LIST_INSERT_BEFORE(t, tim, next);
235 /* not found, insert at the end of the list */
237 LIST_INSERT_AFTER(t_prev, tim, next);
240 if (tim_lcore != lcore_id || !local_is_locked)
241 rte_spinlock_unlock(&priv_timer[tim_lcore].list_lock);
245 * del from list, lock if needed
246 * timer must be in config state
247 * timer must be in a list
250 timer_del(struct rte_timer *tim, unsigned prev_owner, int local_is_locked)
252 unsigned lcore_id = rte_lcore_id();
254 /* if timer needs is pending another core, we need to lock the
255 * list; if it is on local core, we need to lock if we are not
256 * called from rte_timer_manage() */
257 if (prev_owner != lcore_id || !local_is_locked)
258 rte_spinlock_lock(&priv_timer[prev_owner].list_lock);
260 LIST_REMOVE(tim, next);
262 if (prev_owner != lcore_id || !local_is_locked)
263 rte_spinlock_unlock(&priv_timer[prev_owner].list_lock);
266 /* Reset and start the timer associated with the timer handle (private func) */
268 __rte_timer_reset(struct rte_timer *tim, uint64_t expire,
269 uint64_t period, unsigned tim_lcore,
270 rte_timer_cb_t fct, void *arg,
273 union rte_timer_status prev_status, status;
275 unsigned lcore_id = rte_lcore_id();
277 /* round robin for tim_lcore */
278 if (tim_lcore == (unsigned)LCORE_ID_ANY) {
279 tim_lcore = rte_get_next_lcore(priv_timer[lcore_id].prev_lcore,
281 priv_timer[lcore_id].prev_lcore = tim_lcore;
284 /* wait that the timer is in correct status before update,
285 * and mark it as beeing configured */
286 ret = timer_set_config_state(tim, &prev_status);
290 __TIMER_STAT_ADD(reset, 1);
291 priv_timer[lcore_id].updated = 1;
293 /* remove it from list */
294 if (prev_status.state == RTE_TIMER_PENDING ||
295 prev_status.state == RTE_TIMER_RUNNING) {
296 timer_del(tim, prev_status.owner, local_is_locked);
297 __TIMER_STAT_ADD(pending, -1);
300 tim->period = period;
301 tim->expire = expire;
305 __TIMER_STAT_ADD(pending, 1);
306 timer_add(tim, tim_lcore, local_is_locked);
308 /* update state: as we are in CONFIG state, only us can modify
309 * the state so we don't need to use cmpset() here */
311 status.state = RTE_TIMER_PENDING;
312 status.owner = (int16_t)tim_lcore;
313 tim->status.u32 = status.u32;
318 /* Reset and start the timer associated with the timer handle tim */
320 rte_timer_reset(struct rte_timer *tim, uint64_t ticks,
321 enum rte_timer_type type, unsigned tim_lcore,
322 rte_timer_cb_t fct, void *arg)
324 uint64_t cur_time = rte_get_timer_cycles();
327 if (unlikely((tim_lcore != (unsigned)LCORE_ID_ANY) &&
328 !rte_lcore_is_enabled(tim_lcore)))
331 if (type == PERIODICAL)
336 __rte_timer_reset(tim, cur_time + ticks, period, tim_lcore,
342 /* loop until rte_timer_reset() succeed */
344 rte_timer_reset_sync(struct rte_timer *tim, uint64_t ticks,
345 enum rte_timer_type type, unsigned tim_lcore,
346 rte_timer_cb_t fct, void *arg)
348 while (rte_timer_reset(tim, ticks, type, tim_lcore,
352 /* Stop the timer associated with the timer handle tim */
354 rte_timer_stop(struct rte_timer *tim)
356 union rte_timer_status prev_status, status;
357 unsigned lcore_id = rte_lcore_id();
360 /* wait that the timer is in correct status before update,
361 * and mark it as beeing configured */
362 ret = timer_set_config_state(tim, &prev_status);
366 __TIMER_STAT_ADD(stop, 1);
367 priv_timer[lcore_id].updated = 1;
369 /* remove it from list */
370 if (prev_status.state == RTE_TIMER_PENDING ||
371 prev_status.state == RTE_TIMER_RUNNING) {
372 timer_del(tim, prev_status.owner, 0);
373 __TIMER_STAT_ADD(pending, -1);
376 /* mark timer as stopped */
378 status.state = RTE_TIMER_STOP;
379 status.owner = RTE_TIMER_NO_OWNER;
380 tim->status.u32 = status.u32;
385 /* loop until rte_timer_stop() succeed */
387 rte_timer_stop_sync(struct rte_timer *tim)
389 while (rte_timer_stop(tim) != 0)
393 /* Test the PENDING status of the timer handle tim */
395 rte_timer_pending(struct rte_timer *tim)
397 return tim->status.state == RTE_TIMER_PENDING;
400 /* must be called periodically, run all timer that expired */
401 void rte_timer_manage(void)
403 union rte_timer_status status;
404 struct rte_timer *tim, *tim2;
405 unsigned lcore_id = rte_lcore_id();
409 __TIMER_STAT_ADD(manage, 1);
410 /* optimize for the case where per-cpu list is empty */
411 if (LIST_EMPTY(&priv_timer[lcore_id].pending))
413 cur_time = rte_get_timer_cycles();
415 /* browse ordered list, add expired timers in 'expired' list */
416 rte_spinlock_lock(&priv_timer[lcore_id].list_lock);
418 LIST_FOREACH_SAFE(tim, tim2, &priv_timer[lcore_id].pending, next) {
419 if ((int64_t)(cur_time - tim->expire) < 0)
422 LIST_REMOVE(tim, next);
423 LIST_INSERT_HEAD(&priv_timer[lcore_id].expired, tim, next);
427 /* for each timer of 'expired' list, check state and execute callback */
428 while ((tim = LIST_FIRST(&priv_timer[lcore_id].expired)) != NULL) {
429 ret = timer_set_running_state(tim);
431 /* remove from expired list, and add it in done list */
432 LIST_REMOVE(tim, next);
433 LIST_INSERT_HEAD(&priv_timer[lcore_id].done, tim, next);
435 /* this timer was not pending, continue */
439 rte_spinlock_unlock(&priv_timer[lcore_id].list_lock);
441 priv_timer[lcore_id].updated = 0;
443 /* execute callback function with list unlocked */
444 tim->f(tim, tim->arg);
446 rte_spinlock_lock(&priv_timer[lcore_id].list_lock);
448 /* the timer was stopped or reloaded by the callback
449 * function, we have nothing to do here */
450 if (priv_timer[lcore_id].updated == 1)
453 if (tim->period == 0) {
454 /* remove from done list and mark timer as stopped */
455 LIST_REMOVE(tim, next);
456 __TIMER_STAT_ADD(pending, -1);
457 status.state = RTE_TIMER_STOP;
458 status.owner = RTE_TIMER_NO_OWNER;
460 tim->status.u32 = status.u32;
463 /* keep it in done list and mark timer as pending */
464 status.state = RTE_TIMER_PENDING;
465 status.owner = (int16_t)lcore_id;
467 tim->status.u32 = status.u32;
471 /* finally, browse done list, some timer may have to be
472 * rescheduled automatically */
473 LIST_FOREACH_SAFE(tim, tim2, &priv_timer[lcore_id].done, next) {
475 /* reset may fail if timer is beeing modified, in this
476 * case the timer will remain in 'done' list until the
477 * core that is modifying it remove it */
478 __rte_timer_reset(tim, cur_time + tim->period,
479 tim->period, lcore_id, tim->f,
483 /* job finished, unlock the list lock */
484 rte_spinlock_unlock(&priv_timer[lcore_id].list_lock);
487 /* dump statistics about timers */
488 void rte_timer_dump_stats(void)
490 #ifdef RTE_LIBRTE_TIMER_DEBUG
491 struct rte_timer_debug_stats sum;
494 memset(&sum, 0, sizeof(sum));
495 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
496 sum.reset += priv_timer[lcore_id].stats.reset;
497 sum.stop += priv_timer[lcore_id].stats.stop;
498 sum.manage += priv_timer[lcore_id].stats.manage;
499 sum.pending += priv_timer[lcore_id].stats.pending;
501 printf("Timer statistics:\n");
502 printf(" reset = %"PRIu64"\n", sum.reset);
503 printf(" stop = %"PRIu64"\n", sum.stop);
504 printf(" manage = %"PRIu64"\n", sum.manage);
505 printf(" pending = %"PRIu64"\n", sum.pending);
507 printf("No timer statistics, RTE_LIBRTE_TIMER_DEBUG is disabled\n");