4 * Copyright(c) 2010-2014 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.
39 #include <sys/queue.h>
41 #include <rte_atomic.h>
42 #include <rte_common.h>
43 #include <rte_cycles.h>
44 #include <rte_per_lcore.h>
45 #include <rte_memory.h>
46 #include <rte_memzone.h>
47 #include <rte_launch.h>
49 #include <rte_per_lcore.h>
50 #include <rte_lcore.h>
51 #include <rte_branch_prediction.h>
52 #include <rte_spinlock.h>
53 #include <rte_random.h>
55 #include "rte_timer.h"
57 LIST_HEAD(rte_timer_list, rte_timer);
60 struct rte_timer pending_head; /**< dummy timer instance to head up list */
61 rte_spinlock_t list_lock; /**< lock to protect list access */
63 /** per-core variable that true if a timer was updated on this
64 * core since last reset of the variable */
67 /** track the current depth of the skiplist */
68 unsigned curr_skiplist_depth;
70 unsigned prev_lcore; /**< used for lcore round robin */
72 #ifdef RTE_LIBRTE_TIMER_DEBUG
73 /** per-lcore statistics */
74 struct rte_timer_debug_stats stats;
76 } __rte_cache_aligned;
78 /** per-lcore private info for timers */
79 static struct priv_timer priv_timer[RTE_MAX_LCORE];
81 /* when debug is enabled, store some statistics */
82 #ifdef RTE_LIBRTE_TIMER_DEBUG
83 #define __TIMER_STAT_ADD(name, n) do { \
84 unsigned __lcore_id = rte_lcore_id(); \
85 if (__lcore_id < RTE_MAX_LCORE) \
86 priv_timer[__lcore_id].stats.name += (n); \
89 #define __TIMER_STAT_ADD(name, n) do {} while(0)
92 /* Init the timer library. */
94 rte_timer_subsystem_init(void)
98 /* since priv_timer is static, it's zeroed by default, so only init some
101 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id ++) {
102 rte_spinlock_init(&priv_timer[lcore_id].list_lock);
103 priv_timer[lcore_id].prev_lcore = lcore_id;
107 /* Initialize the timer handle tim for use */
109 rte_timer_init(struct rte_timer *tim)
111 union rte_timer_status status;
113 status.state = RTE_TIMER_STOP;
114 status.owner = RTE_TIMER_NO_OWNER;
115 tim->status.u32 = status.u32;
119 * if timer is pending or stopped (or running on the same core than
120 * us), mark timer as configuring, and on success return the previous
121 * status of the timer
124 timer_set_config_state(struct rte_timer *tim,
125 union rte_timer_status *ret_prev_status)
127 union rte_timer_status prev_status, status;
131 lcore_id = rte_lcore_id();
133 /* wait that the timer is in correct status before update,
134 * and mark it as being configured */
135 while (success == 0) {
136 prev_status.u32 = tim->status.u32;
138 /* timer is running on another core, exit */
139 if (prev_status.state == RTE_TIMER_RUNNING &&
140 prev_status.owner != (uint16_t)lcore_id)
143 /* timer is being configured on another core */
144 if (prev_status.state == RTE_TIMER_CONFIG)
147 /* here, we know that timer is stopped or pending,
148 * mark it atomically as being configured */
149 status.state = RTE_TIMER_CONFIG;
150 status.owner = (int16_t)lcore_id;
151 success = rte_atomic32_cmpset(&tim->status.u32,
156 ret_prev_status->u32 = prev_status.u32;
161 * if timer is pending, mark timer as running
164 timer_set_running_state(struct rte_timer *tim)
166 union rte_timer_status prev_status, status;
167 unsigned lcore_id = rte_lcore_id();
170 /* wait that the timer is in correct status before update,
171 * and mark it as running */
172 while (success == 0) {
173 prev_status.u32 = tim->status.u32;
175 /* timer is not pending anymore */
176 if (prev_status.state != RTE_TIMER_PENDING)
179 /* here, we know that timer is stopped or pending,
180 * mark it atomically as beeing configured */
181 status.state = RTE_TIMER_RUNNING;
182 status.owner = (int16_t)lcore_id;
183 success = rte_atomic32_cmpset(&tim->status.u32,
192 * Return a skiplist level for a new entry.
193 * This probabalistically gives a level with p=1/4 that an entry at level n
194 * will also appear at level n+1.
197 timer_get_skiplist_level(unsigned curr_depth)
199 #ifdef RTE_LIBRTE_TIMER_DEBUG
200 static uint32_t i, count = 0;
201 static uint32_t levels[MAX_SKIPLIST_DEPTH] = {0};
204 /* probability value is 1/4, i.e. all at level 0, 1 in 4 is at level 1,
205 * 1 in 16 at level 2, 1 in 64 at level 3, etc. Calculated using lowest
206 * bit position of a (pseudo)random number.
208 uint32_t rand = rte_rand() & (UINT32_MAX - 1);
209 uint32_t level = rand == 0 ? MAX_SKIPLIST_DEPTH : (rte_bsf32(rand)-1) / 2;
211 /* limit the levels used to one above our current level, so we don't,
212 * for instance, have a level 0 and a level 7 without anything between
214 if (level > curr_depth)
216 if (level >= MAX_SKIPLIST_DEPTH)
217 level = MAX_SKIPLIST_DEPTH-1;
218 #ifdef RTE_LIBRTE_TIMER_DEBUG
221 if (count % 10000 == 0)
222 for (i = 0; i < MAX_SKIPLIST_DEPTH; i++)
223 printf("Level %u: %u\n", (unsigned)i, (unsigned)levels[i]);
229 * For a given time value, get the entries at each level which
230 * are <= that time value.
233 timer_get_prev_entries(uint64_t time_val, unsigned tim_lcore,
234 struct rte_timer **prev)
236 unsigned lvl = priv_timer[tim_lcore].curr_skiplist_depth;
237 prev[lvl] = &priv_timer[tim_lcore].pending_head;
240 prev[lvl] = prev[lvl+1];
241 while (prev[lvl]->sl_next[lvl] &&
242 prev[lvl]->sl_next[lvl]->expire <= time_val)
243 prev[lvl] = prev[lvl]->sl_next[lvl];
248 * Given a timer node in the skiplist, find the previous entries for it at
249 * all skiplist levels.
252 timer_get_prev_entries_for_node(struct rte_timer *tim, unsigned tim_lcore,
253 struct rte_timer **prev)
256 /* to get a specific entry in the list, look for just lower than the time
257 * values, and then increment on each level individually if necessary
259 timer_get_prev_entries(tim->expire - 1, tim_lcore, prev);
260 for (i = priv_timer[tim_lcore].curr_skiplist_depth - 1; i >= 0; i--) {
261 while (prev[i]->sl_next[i] != NULL &&
262 prev[i]->sl_next[i] != tim &&
263 prev[i]->sl_next[i]->expire <= tim->expire)
264 prev[i] = prev[i]->sl_next[i];
269 * add in list, lock if needed
270 * timer must be in config state
271 * timer must not be in a list
274 timer_add(struct rte_timer *tim, unsigned tim_lcore, int local_is_locked)
276 unsigned lcore_id = rte_lcore_id();
278 struct rte_timer *prev[MAX_SKIPLIST_DEPTH+1];
280 /* if timer needs to be scheduled on another core, we need to
281 * lock the list; if it is on local core, we need to lock if
282 * we are not called from rte_timer_manage() */
283 if (tim_lcore != lcore_id || !local_is_locked)
284 rte_spinlock_lock(&priv_timer[tim_lcore].list_lock);
286 /* find where exactly this element goes in the list of elements
288 timer_get_prev_entries(tim->expire, tim_lcore, prev);
290 /* now assign it a new level and add at that level */
291 const unsigned tim_level = timer_get_skiplist_level(
292 priv_timer[tim_lcore].curr_skiplist_depth);
293 if (tim_level == priv_timer[tim_lcore].curr_skiplist_depth)
294 priv_timer[tim_lcore].curr_skiplist_depth++;
298 tim->sl_next[lvl] = prev[lvl]->sl_next[lvl];
299 prev[lvl]->sl_next[lvl] = tim;
302 tim->sl_next[0] = prev[0]->sl_next[0];
303 prev[0]->sl_next[0] = tim;
305 /* save the lowest list entry into the expire field of the dummy hdr
306 * NOTE: this is not atomic on 32-bit*/
307 priv_timer[tim_lcore].pending_head.expire = priv_timer[tim_lcore].\
308 pending_head.sl_next[0]->expire;
310 if (tim_lcore != lcore_id || !local_is_locked)
311 rte_spinlock_unlock(&priv_timer[tim_lcore].list_lock);
315 * del from list, lock if needed
316 * timer must be in config state
317 * timer must be in a list
320 timer_del(struct rte_timer *tim, union rte_timer_status prev_status,
323 unsigned lcore_id = rte_lcore_id();
324 unsigned prev_owner = prev_status.owner;
326 struct rte_timer *prev[MAX_SKIPLIST_DEPTH+1];
328 /* if timer needs is pending another core, we need to lock the
329 * list; if it is on local core, we need to lock if we are not
330 * called from rte_timer_manage() */
331 if (prev_owner != lcore_id || !local_is_locked)
332 rte_spinlock_lock(&priv_timer[prev_owner].list_lock);
334 /* save the lowest list entry into the expire field of the dummy hdr.
335 * NOTE: this is not atomic on 32-bit */
336 if (tim == priv_timer[prev_owner].pending_head.sl_next[0])
337 priv_timer[prev_owner].pending_head.expire =
338 ((tim->sl_next[0] == NULL) ? 0 : tim->sl_next[0]->expire);
340 /* adjust pointers from previous entries to point past this */
341 timer_get_prev_entries_for_node(tim, prev_owner, prev);
342 for (i = priv_timer[prev_owner].curr_skiplist_depth - 1; i >= 0; i--) {
343 if (prev[i]->sl_next[i] == tim)
344 prev[i]->sl_next[i] = tim->sl_next[i];
347 /* in case we deleted last entry at a level, adjust down max level */
348 for (i = priv_timer[prev_owner].curr_skiplist_depth - 1; i >= 0; i--)
349 if (priv_timer[prev_owner].pending_head.sl_next[i] == NULL)
350 priv_timer[prev_owner].curr_skiplist_depth --;
354 if (prev_owner != lcore_id || !local_is_locked)
355 rte_spinlock_unlock(&priv_timer[prev_owner].list_lock);
358 /* Reset and start the timer associated with the timer handle (private func) */
360 __rte_timer_reset(struct rte_timer *tim, uint64_t expire,
361 uint64_t period, unsigned tim_lcore,
362 rte_timer_cb_t fct, void *arg,
365 union rte_timer_status prev_status, status;
367 unsigned lcore_id = rte_lcore_id();
369 /* round robin for tim_lcore */
370 if (tim_lcore == (unsigned)LCORE_ID_ANY) {
371 if (lcore_id < RTE_MAX_LCORE) {
372 /* EAL thread with valid lcore_id */
373 tim_lcore = rte_get_next_lcore(
374 priv_timer[lcore_id].prev_lcore,
376 priv_timer[lcore_id].prev_lcore = tim_lcore;
378 /* non-EAL thread do not run rte_timer_manage(),
379 * so schedule the timer on the first enabled lcore. */
380 tim_lcore = rte_get_next_lcore(LCORE_ID_ANY, 0, 1);
383 /* wait that the timer is in correct status before update,
384 * and mark it as being configured */
385 ret = timer_set_config_state(tim, &prev_status);
389 __TIMER_STAT_ADD(reset, 1);
390 if (prev_status.state == RTE_TIMER_RUNNING &&
391 lcore_id < RTE_MAX_LCORE) {
392 priv_timer[lcore_id].updated = 1;
395 /* remove it from list */
396 if (prev_status.state == RTE_TIMER_PENDING) {
397 timer_del(tim, prev_status, local_is_locked);
398 __TIMER_STAT_ADD(pending, -1);
401 tim->period = period;
402 tim->expire = expire;
406 __TIMER_STAT_ADD(pending, 1);
407 timer_add(tim, tim_lcore, local_is_locked);
409 /* update state: as we are in CONFIG state, only us can modify
410 * the state so we don't need to use cmpset() here */
412 status.state = RTE_TIMER_PENDING;
413 status.owner = (int16_t)tim_lcore;
414 tim->status.u32 = status.u32;
419 /* Reset and start the timer associated with the timer handle tim */
421 rte_timer_reset(struct rte_timer *tim, uint64_t ticks,
422 enum rte_timer_type type, unsigned tim_lcore,
423 rte_timer_cb_t fct, void *arg)
425 uint64_t cur_time = rte_get_timer_cycles();
428 if (unlikely((tim_lcore != (unsigned)LCORE_ID_ANY) &&
429 !rte_lcore_is_enabled(tim_lcore)))
432 if (type == PERIODICAL)
437 return __rte_timer_reset(tim, cur_time + ticks, period, tim_lcore,
441 /* loop until rte_timer_reset() succeed */
443 rte_timer_reset_sync(struct rte_timer *tim, uint64_t ticks,
444 enum rte_timer_type type, unsigned tim_lcore,
445 rte_timer_cb_t fct, void *arg)
447 while (rte_timer_reset(tim, ticks, type, tim_lcore,
452 /* Stop the timer associated with the timer handle tim */
454 rte_timer_stop(struct rte_timer *tim)
456 union rte_timer_status prev_status, status;
457 unsigned lcore_id = rte_lcore_id();
460 /* wait that the timer is in correct status before update,
461 * and mark it as being configured */
462 ret = timer_set_config_state(tim, &prev_status);
466 __TIMER_STAT_ADD(stop, 1);
467 if (prev_status.state == RTE_TIMER_RUNNING &&
468 lcore_id < RTE_MAX_LCORE) {
469 priv_timer[lcore_id].updated = 1;
472 /* remove it from list */
473 if (prev_status.state == RTE_TIMER_PENDING) {
474 timer_del(tim, prev_status, 0);
475 __TIMER_STAT_ADD(pending, -1);
478 /* mark timer as stopped */
480 status.state = RTE_TIMER_STOP;
481 status.owner = RTE_TIMER_NO_OWNER;
482 tim->status.u32 = status.u32;
487 /* loop until rte_timer_stop() succeed */
489 rte_timer_stop_sync(struct rte_timer *tim)
491 while (rte_timer_stop(tim) != 0)
495 /* Test the PENDING status of the timer handle tim */
497 rte_timer_pending(struct rte_timer *tim)
499 return tim->status.state == RTE_TIMER_PENDING;
502 /* must be called periodically, run all timer that expired */
503 void rte_timer_manage(void)
505 union rte_timer_status status;
506 struct rte_timer *tim, *next_tim;
507 struct rte_timer *run_first_tim, **pprev;
508 unsigned lcore_id = rte_lcore_id();
509 struct rte_timer *prev[MAX_SKIPLIST_DEPTH + 1];
513 /* timer manager only runs on EAL thread with valid lcore_id */
514 assert(lcore_id < RTE_MAX_LCORE);
516 __TIMER_STAT_ADD(manage, 1);
517 /* optimize for the case where per-cpu list is empty */
518 if (priv_timer[lcore_id].pending_head.sl_next[0] == NULL)
520 cur_time = rte_get_timer_cycles();
522 #ifdef RTE_ARCH_X86_64
523 /* on 64-bit the value cached in the pending_head.expired will be
524 * updated atomically, so we can consult that for a quick check here
525 * outside the lock */
526 if (likely(priv_timer[lcore_id].pending_head.expire > cur_time))
530 /* browse ordered list, add expired timers in 'expired' list */
531 rte_spinlock_lock(&priv_timer[lcore_id].list_lock);
533 /* if nothing to do just unlock and return */
534 if (priv_timer[lcore_id].pending_head.sl_next[0] == NULL ||
535 priv_timer[lcore_id].pending_head.sl_next[0]->expire > cur_time) {
536 rte_spinlock_unlock(&priv_timer[lcore_id].list_lock);
540 /* save start of list of expired timers */
541 tim = priv_timer[lcore_id].pending_head.sl_next[0];
543 /* break the existing list at current time point */
544 timer_get_prev_entries(cur_time, lcore_id, prev);
545 for (i = priv_timer[lcore_id].curr_skiplist_depth -1; i >= 0; i--) {
546 priv_timer[lcore_id].pending_head.sl_next[i] =
548 if (prev[i]->sl_next[i] == NULL)
549 priv_timer[lcore_id].curr_skiplist_depth--;
550 prev[i] ->sl_next[i] = NULL;
553 /* transition run-list from PENDING to RUNNING */
555 pprev = &run_first_tim;
557 for ( ; tim != NULL; tim = next_tim) {
558 next_tim = tim->sl_next[0];
560 ret = timer_set_running_state(tim);
561 if (likely(ret == 0)) {
562 pprev = &tim->sl_next[0];
564 /* another core is trying to re-config this one,
565 * remove it from local expired list and put it
566 * back on the priv_timer[] skip list */
568 timer_add(tim, lcore_id, 1);
572 /* update the next to expire timer value */
573 priv_timer[lcore_id].pending_head.expire =
574 (priv_timer[lcore_id].pending_head.sl_next[0] == NULL) ? 0 :
575 priv_timer[lcore_id].pending_head.sl_next[0]->expire;
577 rte_spinlock_unlock(&priv_timer[lcore_id].list_lock);
579 /* now scan expired list and call callbacks */
580 for (tim = run_first_tim; tim != NULL; tim = next_tim) {
581 next_tim = tim->sl_next[0];
582 priv_timer[lcore_id].updated = 0;
584 /* execute callback function with list unlocked */
585 tim->f(tim, tim->arg);
587 __TIMER_STAT_ADD(pending, -1);
588 /* the timer was stopped or reloaded by the callback
589 * function, we have nothing to do here */
590 if (priv_timer[lcore_id].updated == 1)
593 if (tim->period == 0) {
594 /* remove from done list and mark timer as stopped */
595 status.state = RTE_TIMER_STOP;
596 status.owner = RTE_TIMER_NO_OWNER;
598 tim->status.u32 = status.u32;
601 /* keep it in list and mark timer as pending */
602 rte_spinlock_lock(&priv_timer[lcore_id].list_lock);
603 status.state = RTE_TIMER_PENDING;
604 __TIMER_STAT_ADD(pending, 1);
605 status.owner = (int16_t)lcore_id;
607 tim->status.u32 = status.u32;
608 __rte_timer_reset(tim, cur_time + tim->period,
609 tim->period, lcore_id, tim->f, tim->arg, 1);
610 rte_spinlock_unlock(&priv_timer[lcore_id].list_lock);
615 /* dump statistics about timers */
616 void rte_timer_dump_stats(FILE *f)
618 #ifdef RTE_LIBRTE_TIMER_DEBUG
619 struct rte_timer_debug_stats sum;
622 memset(&sum, 0, sizeof(sum));
623 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
624 sum.reset += priv_timer[lcore_id].stats.reset;
625 sum.stop += priv_timer[lcore_id].stats.stop;
626 sum.manage += priv_timer[lcore_id].stats.manage;
627 sum.pending += priv_timer[lcore_id].stats.pending;
629 fprintf(f, "Timer statistics:\n");
630 fprintf(f, " reset = %"PRIu64"\n", sum.reset);
631 fprintf(f, " stop = %"PRIu64"\n", sum.stop);
632 fprintf(f, " manage = %"PRIu64"\n", sum.manage);
633 fprintf(f, " pending = %"PRIu64"\n", sum.pending);
635 fprintf(f, "No timer statistics, RTE_LIBRTE_TIMER_DEBUG is disabled\n");